HDU 1061 Rightmost Digit(快速幂)
2015-09-04 08:32
405 查看
Description
给出一正整数n,输出n^n的个位
Input
第一行为用例组数T,每组用例占一行为一整数n
Output
对每组用例,输出n^n的个位
Sample Input
2
3
4
Sample Output
7
6
Solution
相当于求n^n(mod 10),用快速幂即可
Code
给出一正整数n,输出n^n的个位
Input
第一行为用例组数T,每组用例占一行为一整数n
Output
对每组用例,输出n^n的个位
Sample Input
2
3
4
Sample Output
7
6
Solution
相当于求n^n(mod 10),用快速幂即可
Code
#include<cstdio> #include<iostream> using namespace std; typedef long long ll; ll mod_pow(ll a,ll b,ll p)//快速幂,求a^b(mod p) { ll ans=1; a%=p; while(b) { if(b&1) ans=(ans*a)%p; a=a*a%p; b>>=1; } return ans; } int main() { int T; scanf("%d",&T); while(T--) { ll n; scanf("%lld",&n); printf("%lld\n",mod_pow(n,n,10)); } return 0; }
相关文章推荐
- HDU 4497 GCD and LCM(素因子分解+组合数学)
- HDU 1466 计算直线的交点数(dp)
- HDU 1536 S-Nim(博弈论)
- PHP基础----HTML表单的创建与提交----18表单与 PHP
- Threat Intellgence
- POJ 1284 Primitive Roots(欧拉函数)
- POJ 2034 Anti-prime Sequences(数论+dfs)
- POJ 1365 Prime Land(分解质因数)
- POJ 2115 C Looooops(一元线性同余方程)
- POJ 3090 Visible Lattice Points(欧拉函数)
- POJ 2478 Farey Sequence(欧拉函数)
- POJ 2773 Happy 2006(数论)
- PHP基础----常用库函数----17图片打水印实例
- Fragment+RadioButton实现点击切换页面效果
- PHP基础----常用库函数----16生成图片
- C++之随笔(二)
- 关于存储过程可空条件查询
- PHP基础----常用库函数----15文件操作
- PHP基础----常用库函数----14JSON格式数据的操作
- PHP基础----常用库函数----13时间和日期