ʕ·͡ˑ·ཻ ʕ•̫͡• ʔ•̫͡•ཻʕ•̫͡•ʔ•͓͡•ʔ

题目 - Rightmost Digit

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

1
2
3
2
3
4

Sample Output

1
2
7
6

Hint

1
2
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.

题目大意:

求n的平方最右边的数字。

思路:

需要用到快速幂以及取模。

AC代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
using namespace std;

int fastPow(int a, int n) {
int base = a % 10;
int res = 1;
while(n) {
if(n & 1)
res = res * base % 10;
base = base * base % 10;
n >>= 1;
}
return res;
}

int main() {
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
cout << fastPow(n, n) << endl;
}
return 0;
}