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;
}