代码:
#include<iostream>
using namespace std;
typedef long long LL;
const int N = 23;
int n;
LL a[N], b[N], m, y[N], x;
LL exgcd(LL a, LL b, LL &x, LL &y){
if(b == 0){
x = 1, y = 0;
return a;
}
LL tmp = exgcd(b, a % b, x, y);
LL t = x;
x = y;
y = t - a / b * y;
return tmp;
}
int main(){
cin >> n;
m = 1;
for(int i = 1; i <= n; i ++){
cin >> a[i] >> b[i];
m *= a[i];
}
for(int i = 1; i <= n; i ++){
LL p = 0, q = 0;//p是逆元
LL now = m / a[i];
exgcd(abs(now), abs(a[i]), p, q);
p = (p % m + m) % m;
x = ((x + now * p * b[i]) % m + m) % m;
}
x = (x + m) % m;
cout << x;
return 0;
}