E - Rightmost Digit
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
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.
题意分析:
这道题本身的意思就是输入T的样例,对于每个样例N,做N^N,然后求N^N的最后一位的数值。
解题思路:
这道题本质上非常简单,而且以前也做过。只不过看起来比较吓人,因为N^N的数量级太大。不过也很明显,肯定不能直接暴力。来说说我的解题思路,对于0-9每一个数与自己相乘都是有规律的,只需要判断N的最后一位数,然后分类输出结果就好了,下面是编码。
编码:
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; while (n--) { int m; cin >> m; int a = m % 10; if (a == 1 || a == 5 || a == 6 || a == 0) cout << a << endl; else if (a == 2) { int num[4] = {6, 2, 4, 8}; cout << num[m % 4] << endl; } else if (a == 3) { int num[4] = {1, 3, 9, 7}; cout << num[m % 4] << endl; } else if (a == 4) { int num[2] = {6, 4}; cout << num[m % 2] << endl; } else if (a == 7) { int num[4] = {1, 7, 9, 3}; cout << num[m % 4] << endl; } else if (a == 8) { int num[4] = {6, 8, 4, 2}; cout << num[m % 4] << endl; } else if (a == 9) { int num[2] = {1, 9}; cout << num[m % 2] << endl; } } return 0; }