您的位置:首页 > 其它

hdu 1061 Rightmost Digit

2014-10-22 11:20 405 查看


Rightmost Digit

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 33017 Accepted Submission(s): 12648



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次方这个数的个位数是几
关键点:题目给的数值很大,所以找规律
解题时间:2014,10,22
思路:先输出前30个数来找规律
体会:这种题数值很大不是有公式,就是有规律,我这么觉得
#include<stdio.h>
int main(){
int T,n;
scanf("%d",&T);
int s[21]={0,1,4,7,6,5,6,3,6,9,0,1,6,3,6,5,6,7,4,9,0};
while(T--){
scanf("%d",&n);
printf("%d\n",s[n%20]);
}
return 0;
}
还有一种方法,快速幂算法:
#include<stdio.h>
__int64 pow(__int64 a,__int64 b){
__int64 k=1;
while(b){
if(b&1)
k=(k*a)%10;
b>>=1;
a=(a*a)%10;
}
return k;
}
int main(){
__int64 a;
int t;
scanf("%d",&t);
while(t--){
scanf("%I64d",&a);
printf("%I64d\n",pow(a,a));
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: