hdu1061 Rightmost Digit 标准快速幂
2016-05-01 15:41
351 查看
Problem Description
Given a positive integer N, you should output the most right digit of N^N.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
Output
For each test case, you should output the rightmost digit of N^N.
Sample Input
2 3 4
Sample Output
7 6
Hint
In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7. In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.
题目的意思呢, 就是给出一个数 N, 然后让你算N的N次幂, 看一下规模, 在10^9这个数量级, 慢慢乘肯定乘不出来了. 快速幂套模板直接AC
Given a positive integer N, you should output the most right digit of N^N.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
Output
For each test case, you should output the rightmost digit of N^N.
Sample Input
2 3 4
Sample Output
7 6
Hint
In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7. In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.
题目的意思呢, 就是给出一个数 N, 然后让你算N的N次幂, 看一下规模, 在10^9这个数量级, 慢慢乘肯定乘不出来了. 快速幂套模板直接AC
#include<iostream> #include<cstdio> using namespace std; int xcpow(int x,int n){ long long ans=1,tmp=x%10; while(n){ if(n&1)ans=ans*tmp%10; tmp = tmp*tmp%10; n>>=1; } return ans; } int main(){ int n,x; cin>>n; while(n--){ cin>>x; cout<<xcpow(x,x)<<endl; } return 0; }
相关文章推荐
- RFC1321 MD5算法信息介绍
- 基于全注解方式实现MyBatis链接数据库的demo
- 大道至简阅读笔记02
- Tricks(四十五)—— 可视化 min 和 max(表示一种截断)
- POJ 1275 Cashier Employment (差分约束 + spfa)
- Ubuntu16.04 Sublime Text3 安装激活和设置
- 2016.5.1
- LeetCode 4. Median of Two Sorted Arrays(两个有序数组的中位数)
- 第 29 章 CSS3 弹性伸缩布局[上]
- 我的大学规划
- 读《Spring-技术内幕》-第二章:IoC容器的实现-1
- C语言编程程序的内存如何布局
- Genymotion 从注册到安装到运行到调试
- C++随笔
- PAT 1006. 换个格式输出整数 (15)
- 20145327 《Java程序设计》第九周学习总结
- day56-Spark SQL和DataFrame的本质
- 欢迎使用CSDN-markdown编辑器
- C 的布尔值
- PAT(Basic Level) 完美数列(25)