Gym 102091H. As Rich as Crassus (中国剩余定理)

在这里插入图片描述
Examples
Input
2
6 11 19
5 4 11
25 36 7
16 0 6

output
5
6

Solution
中国剩余定理模板
题目保证了模数互质,所以不用拓展CRT

Hint
1.统计答案时数据会溢出long long,可以用快速乘避免
2.题目保证了模数互质,所以不用拓展CRT
3.此时求逆元不能用费马小定理(快速幂), 因为模数不一定为质数
另:因为x^3会在long long 范围内,所以其实 x 最大不会超过 2 21 2^{21} 221
故暴力枚举x也可

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll exgcd(ll a,ll b,ll &x,ll &y) {
	if(b == 0) {
		x = 1,y = 0;return a;
	}
	ll d = exgcd(b,a % b,x,y);
	ll t = x; x = y, y = t - (a / b) * y;
	return d;
}

ll ksc(ll a,ll b,ll m){
	ll res = 0;
	while(b) {
		if(b & 1) res = (res + a) % m;
		a = (a + a) % m;
		b /= 2;
	}
	return (res%m+m)%m;
}
ll n[4],a[4];

int main() {
	int T;scanf("%d",&T);
	while(T--) {
		scanf("%lld%lld%lld",&n[0],&n[1],&n[2]);
		scanf("%lld%lld%lld",&a[0],&a[1],&a[2]);
		ull N = (ull)1 * n[0] * n[1] * n[2];
		ull res = 0;
		for(int i = 0;i < 3;++i) {
			ll m = N / n[i], x = 0,y = 0;
			exgcd(m,n[i],x,y);
			x = (x%n[i]+n[i])%n[i];
			res = (res + ksc(ksc(a[i],m,N),x,N)) % N;
		}
		res = (res % N + N) % N; 
		ll tmp = cbrt(res);
		if(tmp * tmp * tmp != res) tmp++;
		printf("%lld\n", tmp);
	}
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值