NOWCODER 最大乘积(枚举和暴力+__int128的快速幂)

链接:https://ac.nowcoder.com/acm/problem/15954
来源:牛客网

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
题意:
  给你一个正整数S,若有若干个正整数的和为S,则这若干的数的乘积最大是多少,结果对2000000000000000003(共有17 个零) 取模
思路:
  一看到对2e18+3取模就想到了快速幂,但实际上稍微乘一下即使用 ull ( 2 64 = 18446744073709551615 2^{64}=18446744073709551615 264=18446744073709551615)也会爆,没办法,只能拿出__int128来算了。。。。。。
但实际上循环乘3取模就行。。。。毕竟最大只有 3 2000 3 3^{\frac{2000}{3}} 332000,也不会超时

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
typedef __int128 int128;
const int maxn=1e3+10;
const ll mod=2000000000000000003;
ll t,s,ans;
//ll qpow(ll a,ll b) { //a的b次方不断对mod取模 
//    ll res = 1;
//    for(int i=0;i<b;i++) res=res*a%mod;
//    return res;
//}
ll qpow(ll a,ll b) { //快速幂模板
	int128 res = 1,base = a;
	while(b) {
		if(b&1) res = res*base%mod;
		base = base*base%mod;
		b >>= 1;
	}
	return res;
}
int main(){
	cin>>t;
	while(t--){
		cin>>s;
		if(s<=4) ans=s;
		else{
			if(s%3==0) ans=qpow(3,s/3);
			else if(s%3==1) ans=qpow(3,s/3-1)*4%mod;
			else ans=qpow(3,s/3)*2%mod;
		}
		cout<<ans<<endl;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值