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.
快速幂模板题,对10取余
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<cstdio>
#include<vector>
#include<set>
#include<cstring>
#include<cstdlib>
#include<time.h>
#include<stack>
#define INF 0x3f3f3f3f
using namespace std;
#define maxn 200907
typedef long long ll;
ll mode(ll a,ll b,ll mode){
ll sum=1;
a=a%mode;
while(b>0){
if(b%2==1)
sum=(sum*a)%mode;
b/=2;
a=(a*a)%mode;
}
return sum;
}
int main()
{
int n;
scanf("%d",&n);
while(n--){
ll m;
scanf("%lld",&m);
ll ans;
ans=mode(m,m,10);
printf("%lld\n",ans);
}
return 0;
}