rightmost Digit
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) total submission (s): 33161 Accepted Submission (s): 12696
Problem 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 was a single integer T which is the number of test cases. T test Cases follow.
Each test case is 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 = rightmost, so the-the-digit is 7.
In the second case, 4 * 4 * 4 * 4 = at the very rightmost digit is 6.
Author
Ignatius.l
To give you a n, calculate the number of digits on the n^n digit is how much
Idea: The common method time-out, the use of fast power to take the remainder calculation n^n%10, here paste a binary
Code for fast power-over redundancy
#include <stdio.h> #include <string.h>__int64 quickpow (__int64 A,__int64 p) { __int64 r = 1,base = A; __int64 m = ten; while (p!=0) { if (P & 1) R = r * Base% m; Base = base * Base% m; P >>= 1; } return r;} int main () { __int64 N; int T; scanf ("%d", &t); while (t--) { scanf ("%i64d", &n); __int64 ans = quickpow (n,n); printf ("%i64d\n", ans); } return 0;}
Hdu1061_rightmost Digit "Fast power take-over surplus"