hdu 1061
阿新 • • 發佈:2017-11-26
while sam 遞歸 turn return several 快速冪 ould line Each test case contains a single positive integer N(1<=N<=1,000,000,000).
1、鏈接
http://acm.hdu.edu.cn/showproblem.php?pid=1061
2、題目
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 is a single integer T which is the number of test cases. T test cases follow.
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.
3、解題分析
快速冪取模(否則會超時)
4、Code
#include<bits/stdc++.h> using namespace std; /*遞歸*/ //int quick_pow(int base, int n, int mod) //{ // if(n == 0) // return 1; // if(n == 1) // return base%mod; // int res = quick_pow(base,n>>1,mod); // res = res * res % mod; // if(n&1)// { // res = res*base%mod; // } // return res; //} /*非遞歸*/ int quick_pow(int base, int n, int mod) { int res = 1; while(n) { if(n&1) { res = res * base%mod; } base = base * base % mod; n>>=1; } return res; } int main() { int t,n; scanf("%d",&t); while(t--) { scanf("%d",&n); printf("%d\n",quick_pow(n%10,n,10)); } return 0; }
hdu 1061