您的位置:首页 > 其它

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

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: