I - Rightmost Digit
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 <iostream>
#include <cstdio>
int ksm(int a,int b)
{
int ans=1;
a=a%10;
while(b)
{
if(b%2==1)
ans=(ans*a)%10;
b=b/2;
a=(a*a)%10;
}
return ans;
}
int main()
{
int t,a,b;
scanf("%d",&t);
while(t--)
{
scanf("%d",&a);
b=a;
printf("%d\n",ksm(a,b));
}
return 0;
}