Hdoj Address: http://acm.hdu.edu.cn/showproblem.php?pid=1061
Fast Power algorithm Explanation: http://blog.csdn.net/qq_26891045/article/details/51334101
Rightmost DigitTime Limit: -/1 theMS (java/others) Memory Limit:65536 /32768K (Java/others)Total Submission (s):45746 Accepted Submission (s): 17221
problem DescriptionGiven A positive integer N, you should output the most right digit of n^n.
InputThe 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).
Outputfor each test case, you should output the rightmost digit of n^n.
Sample Input
234
Sample Output
the Hint in the first case, 3 * 3 * 3 = digit, so the rightmost is 7.In the second case, 4 * 4 * 4 * 4 =, so the rightm OST digit is 6.
================================ born proud of the split line =============================
The meaning of the topic has been very clear, that is, to seek the single digit of n^n, it is equivalent to a single digit of the MoD 10 operation, here directly with the fast power algorithm, you can get the result, the topic given n may be relatively large, so a long long is more insurance.
#include <iostream> #include <stdio.h>using namespace std;/** fast power algorithm */long long calculation (long long A, Long long B,long long c) { int ans=1; a=a%c; while (b>0) { if (b%2==1) ans= (ans*a)%c; B=B/2; A= (a*a)%c; } return ans;} int main () { int n,n; cin>>n; while (n--) { cin>>n; printf ("%d\n", Calculation (n,n,10)); } return 0;}
Reference Blog: http://blog.csdn.net/whjkm/article/details/42803805
acm--Fast Power--hdoj 1061--rightmost Digit