Rightmost Digit |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission(s): 8142 Accepted Submission(s): 2078 |
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 |
乍一看,好像很简单的样子,循环后取余就好了。
下面是我最开始的错误代码:
#include<stdio.h>
int main()
{
int num,a,b,zu,i;
scanf("%d",&zu);
while(zu--){
scanf("%d",&a);
b=a;
for(i=1;i<b;i++){
a=(a*b)%10;
}
printf("%d\n",a);
}
return 0;
}
看起来好像挺好的,运算的结果也都是对的。
然而提交的时候会显示超时。
据说要用快速幂取模运算法。这个方法我还没来得及去百度研究下(懒。。
这题还有简便的方法,就是他们相乘是循环的,4次一循环。
观察题目后易知,结果的最右边数其实只和个位有关。于是我们单独拉出个位来看看。
—————————————————————————————————————————————————————————————————————————————
下面是各个数字的循环情况
0 0 000
1 1 11 1
2 4 86 2
3 9 71 3
4 6 46 6
5 5 5 55
6 6 6 66
7 9 31 7
8 4 26 8
9 1 91 9
—————————————————————————————————————————————————————————————————————————————
我们发现,大部分都是4次一循环,红色标注的都是不变的。有了这个规律后,经过调试,最后我写了这个代码。
#include<stdio.h>
int main()
{
int a,b,num,zu,b2,c,i,b3;
scanf("%d",&zu);
while(zu--){
scanf("%d",&a);
b2=a;
b=a%10;
b3=b;
if(b==0||b==1||b==5||b==6) {
printf("%d\n",b);
continue;
}
if(a<10){
for(i=1;i<b2;i++){
a=(a*(b2))%10;
}
printf("%d\n",a);
continue;
}
c=a%4;
c=c+3;
for(i=1;i<=c;i++)
b=b3*b;
printf("%d\n",b%10);
}
}
一些具体细节就暂时不写了。。等我自学完快速幂取模再回来填坑吧
感谢HSS一直以来对我提供的帮助
昨天晚上开完ACM的会议,发现任重道远,回到寝室后一直在刷题。第一次用杭电的OJ系统来刷题(之前一直用的是南洋理工学院的,队里有一个人也和我一样用这个2333)
刷的steps 在第二章遇到这样一题,原先很快的就编好了,但是提交的时候一直都在提示超时。后来问了基友(以后不特别说明的话,基友就是HSS。。)这题他大一上的时候也遇到过,当时也是被卡住过。
————————————写于2016-3-24
寝室·夜