@(XSY)[中国剩余定理]
中国剩余定理.
注意求逆元要用exGCD.
#include <cstdio>
#include <cctype>
const int N = 8;
void exGCD(long long a, long long b, long long &x, long long &y)
{
if(! b)
{
x = 1, y = 0;
return;
}
exGCD(b, a % b, y, x);
y -= a / b * x;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("CRT.in", "r", stdin);
freopen("CRT.out", "w", stdout);
#endif
int n;
scanf("%d", &n);
static int b[N], m[N];
long long prd = 1;
for(int i = 0; i < n; ++ i)
scanf("%d%d", b + i, m + i), prd *= m[i];
long long ans = 0;
for(int i = 0; i < n; ++ i)
{
long long x, y;
exGCD(prd / m[i], m[i], x, y);
ans += b[i] * (prd / m[i]) * ((x + m[i]) % m[i]);
}
printf("%lld", ans % prd);
}