Given a positive integer N, you should output the most right digit of N^N.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
2 3 4
7
6
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.
题解:同余定理的应用
#include<cstdio>
typedef __int64 LL;
LL f(LL a,LL b,LL mod)
{
LL res=1;
while(b){
if(b&1)
res=res*a%mod;
a=a*a%mod;
b>>=1;
}
return res;
}
int main()
{
int t;
LL n;
while(scanf("%d",&t)!=EOF)
while(t--)
{
scanf("%I64d",&n);
printf("%I64d\n",f(n,n,(LL)10));
}
return 0;
}