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> #include<cstring> #include<algorithm> using namespace std; int spow(int a,int b, int c){ int ans=1; a=a%c; while(b){ if(b&1) ans=(ans*a)%c; b >>= 1; a=(a*a)%c; } return ans; } int main(){ int t; cin >> t; while(t--){ int s; cin >> s; cout << spow(s,s,10) << endl; //最后一位相当于就是对10取模 } return 0; }