HDU 1061
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.
这道题的大致意思就是给你一个数n,让你去求n的n次方的个位数上的数。
这道题是标椎的快速幂取模题。(想看 快速幂简单讲解请看快速幂简单讲解
具体代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int mode = 10;
LL fas(LL n,LL d){
LL r=1;
while(d){
if(d&1)
r = (r % mode) * (n % mode) % mode;
d = d / 2;
n = (n % mode) * (n % mode) % mode;
}
return r;
}
int main(){
LL t,n;
cin >> t;
while(t--){
cin >> n;
cout << fas(n, n) << endl;
}
}
道阻且长
自己走的路 跪着也要走完