HDU-1061-Rightmost Digit,快速幂水过!~~
2016-04-20 21:30
369 查看
Rightmost Digit
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
http://acm.hdu.edu.cn/showproblem.php?pid=1061Problem 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次方的最后一位,随便举几个数不难发现其实就是((N%10)^N )%10;所以关键就在这个快速幂算法了;
直接上代码:
#include<bits/stdc++.h> using namespace std; int fast(int x) { int s=1,xx=x; x%=10; while(xx)//快速幂取余核心问题; { if(xx&1) s=(s*x)%10; x=x*x%10; xx=xx>>1; } return s; } int main() { int t,n; scanf("%d",&t); while(t--) { scanf("%d",&n); if(n%10==0) printf("0\n"); else printf("%d\n",fast(n)); } return 0; }
相关文章推荐
- 站立会议(2)
- json
- 字符串长度的三种求取方法
- 专题二 · 1017
- jabc数据库的增删改查
- html学习笔记——html发展历程
- 练习二 1004 射箭问题
- wampserver配置域名
- Lightroom CC 2015 (6.0) for MAC【中文安装包及超简单5秒破解】
- 服务治理--服务灰度发布
- JAVA中的NIO(一)
- Tomact、Servlet与浏览器之间的关系
- C++学习笔记联合/共用体(union)用法
- jQuery Ajax 方法调用 Asp.Net WebService 以及调用aspx.cs中方法的详细例子
- Error running app: Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled
- poj1180(斜率优化dp)
- BZOJ4540 HNOI2016 序列
- zepto.fullpage.js学习总结
- 常用查找算法的Java实现
- SGU239(搜索?)