幂塔个位数的计算
题目描述
题解思路
**作者:神崎兰子
链接:https://ac.nowcoder.com/discuss/593200
来源:牛客网
对标cf难度:2100
知识点:找规律
计算a↑↑n
当n为1时,直接输出a的个位数即可。
当n=2时,如果a的个位数是2、3、7、8,那么需要观察a的十位数是奇数还是偶数。因为a↑↑2=a^aa↑↑2=a
a
,而2、3、7、8的幂的个位数是4个一循环,所以只用观察a模4是2还是0就行。
如果a的个位数是其他的,那么可以直接输出答案:a的个位数是0 1 5 6 9输出本身即可。a个位数是4则输出6。
当n大于2时,由于a↑↑n=a^{a↑↑(n-1)}a↑↑n=a
a↑↑(n−1)
,而如果a是偶数,a↑↑(n-1)a↑↑(n−1)模4的值是确定的,所以a↑↑na↑↑n的个位数也是确定的。而如果a是奇数(特指个位数是3和7),那么a↑↑(n-1)a↑↑(n−1)模4取决于aa模4的值。如果aa模4为1,那么a↑↑(n-1)a↑↑(n−1)模4也是1,此时a的个位数是3和7的话,a↑↑na↑↑n的个位数也是3和7,否则应为7和3。
a的个位数是{0,1,2,3,4,5,6,7,8,9}时,对应的a↑↑na↑↑n的个位数为{0,1,6,7,6,5,6,3,6,9}。
不过这道题也可以欧拉降幂直接秒掉,但由于我欧拉降幂不是很熟,所以这里就不讲解了,想学习的同学可以自行百度。**
在此附上我的AC代码
#include<iostream>
#include<string>
using namespace std;
typedef long long ll;
string a, n;
int out[10] = {0,1,6,7,6,5,6,3,6,9};
int main()
{
cin >> a;
cin >> n;
int x = (a[a.size()-1]-'0')%10;
if(n.size()==1 && n[0]=='1'){
cout << x;
}else if(n.size()==1 && n[0]=='2'){
if(x==0 || x==1 || x==5 || x==6 || x==9) cout << x;
else if(x==4) cout << 6;
else if(x==2){
if(a.size()==1) cout << 4;
else{
if((a[a.size()-2]-'0')%2==0) cout << 4;
else cout << 6;
}
}else if(x==8){
if(a.size()==1) cout << 6;
else{
if((a[a.size()-2]-'0')%2==0) cout << 6;
else cout << 4;
}
}else if(x==3){
if(a.size()==1) cout << 7;
else{
if((a[a.size()-2]-'0')%2==0) cout << 7;
else cout << 3;
}
}else if(x==7){
if(a.size()==1) cout << 3;
else{
if((a[a.size()-2]-'0')%2==0) cout << 3;
else cout << 7;
}
}
}else {
if(x!=3 && x!=7) cout << out[x];
else{
if(a.size()==1){
cout << out[x];
}else {
if((a[a.size()-2]-'0')%2==0) cout << out[x];
else cout << x;
}
}
}
return 0;
}