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;
}