the most right digit of N^N.
Rightmost Digit
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 13 Accepted Submission(s) : 5
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Given a positive integer N, you should output the most right digit of N^N.
Input
The document comprises multiple test scenarios. The opening line introduces an integer T signifying the total number of these scenarios. Following this, there are T individual test scenarios. Each scenario encompasses a unique positive integer N (where 1 ≤ N ≤ 1 billion).
Output
For each test case, you should output the rightmost digit of N^N.
Sample Input
Sample Output
Hint
For the first instance, multiplying three together yields a product of twenty-seven; thus, its units digit is seven. For the second instance, multiplying four four times results in a product of two hundred fifty-six; therefore, its units digit is six.
本人觉得很有意思的题目~
题意在最后解释得很清楚,但是想要不超时地做出来,我动了一番脑筋。
立刻想到:"其实很简单嘛!就是让这个数字自乘若干次后进行模运算来获取最后一位数字啦!"
所以我写了如下代码:
#include<iostream>
using namespace std;
int main()
{
int N;
while(N--)
{
int a,i,t=1;
cin>>a;
for(i=0;i<n;i++)
t=t*a;
t=t%10;
cout<<t<<endl;
}
return 0;
}
遗憾,在VC++6.0环境下无法运行成功。经过进一步查阅资料后发现有相关指导性文字让我得到了启发。如果希望避免超时现象发生,则可以选择另一种方法进行处理。肖明教授经常说‘计算机是非常笨的,需要人类教给它该干什么它才能做什么’。
遗憾,在VC++6.0环境下无法运行成功。经过进一步查阅资料后发现有相关指导性文字让我得到了启发。如果希望避免超时现象发生,则可以选择另一种方法进行处理。肖明教授经常说‘计算机是非常笨的,需要人类教给它该干什么它才能做什么’
就是这段话:

根据这段话,我又进行了自己的总结:

0到9,分为3组:
1.根据九九乘法表,0,1,5,6这四个数字自乘后还是得到个位为自身的数
2.4和9自乘多次,在两个数之间循环
3.2、3、7、8这四个数自乘多次,在四个数之间循环
进一步分析(也是为了节省运算时间):
个位为4的数字属于偶数集合,在经过奇数次乘法运算后会最终停留在数值6的位置;同样地,则会停留在数值9的位置。
二:深入观察第二类对应的数字吧,在其中可能会发现一些微小的模式并提高效率。不妨深入查看代码中的相关内容这样可能会发现一些有趣的细节和潜在的问题
#include<iostream>
using namespace std;
int main()
{
int n;//测试数据组数
cin>>n;
while(n--)
{
int m,t;
cin>>m;//求m的m次方最右边的数
t=m%10;//得出尾数
if(t==0 || t==1 || t==5 || t==6)
cout<<t<<endl;
else
if(t==4) cout<<"6"<<endl;
else
if(t==9) cout<<"9"<<endl;
else
{
int x=(m-1)%4;
if(x==0)
cout<<t<<endl;
else if(x==1)
{
if(t==2 || t==8) cout<<"4"<<endl;
else cout<<"9"<<endl;
}
else if(x==3)
{
if(t==2 ||t==8) cout<<"6"<<endl;
else cout<<"1"<<endl;
}
else
cout<<10-t<<endl;
}
}
return 0;
}
