2021牛客寒假算法基础集训营1(H-幂塔个位数的计算)

幂塔个位数的计算

题目描述

在这里插入图片描述
在这里插入图片描述

题目链接

题解思路

**作者:神崎兰子
链接: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;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值