标签:clu cond 非递归 ble space hdu 1061 using pos htm
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.
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.
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; }
标签:clu cond 非递归 ble space hdu 1061 using pos htm
原文地址:http://www.cnblogs.com/hhkobeww/p/7898988.html