P1495 【模板】中国剩余定理(CRT)/曹冲养猪
链接: link.
纯板子题,太久没写,以至于今天才发现自己存的板子是有bug的,又去看了一遍。
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
int n;
void ex_gcd(LL a, LL b, LL &d, LL &x, LL &y) {
if (!b) {
d = a, x = 1, y = 0;
} else {
ex_gcd(b, a % b, d, y, x);
y -= x * (a / b);
}
}
LL ex_crt(LL *m, LL *r, int n) {
LL M = m[1], R = r[1], x, y, d;
for (int i = 2; i <= n; ++i) {
ex_gcd(M, m[i], d, x, y);
if ((r[i] - R) % d)
return -1;
x = (r[i] - R) / d * x % (m[i] / d);
R += x * M;
M = M / d * m[i];
R %= M;
}
return R > 0 ? R : R + M;
}
LL m[maxn], r[maxn];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>m[i]>>r[i];
}
cout<<ex_crt(m,r,n);
return 0;
}