思路分析:
应用中国剩余定理求解模数两两互质的线性同余方程组即可, 下面给出AC代码:
//CH3B04_Xiao 9*大战朱最学
#include <iostream>
using namespace std;
typedef long long ll;
const ll MAX = 20;
//M[i]表示a1*...*an / ai
ll a[MAX], b[MAX], M[MAX], t[MAX], n;
//返回a, b的最大公约数d, 且满足a * x + b * y = d
ll exgcd(ll a, ll b, ll &x, ll &y){
if(b == 0){x = 1, y = 0; return a;}
ll res = exgcd(b, a % b, x, y);
ll z = x; x = y, y = z - a / b * y;
return res;
}
int main(){
cin >> n; for(ll i = 1; i <= n; ++i) cin >> a[i] >> b[i];
ll m = 1; for(ll i = 1; i <= n; ++i) m *= a[i];
for(ll i = 1; i <= n; ++i) M[i] = m / a[i];
for(ll i = 1, x, y; i <= n; ++i)
exgcd(M[i], a[i], x, y), t[i] = (x + a[i]) % a[i];
ll res = 0; for(ll i = 1; i <= n; ++i) res += (((M[i] % m) * b[i]) % m) * t[i] % m;
cout << res % m << endl;
}