思路
其实就是中国剩余定理的板子,有兴趣可以去看一下我的算法笔记: CRT
代码
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);cin.tie(nullptr)
#define int long long
#define endl "\n"
#define xx first
#define yy second
using namespace std;
typedef long long LL;
const int N = 3e6 + 10, mod = 998244353;
int n, m, k, _;
int a[N], m[N];
void exgcd(LL a, LL b, LL &x, LL &y) {
if (!b) x = 1, y = 0;
else {
exgcd(b, a % b, y, x);
y -= a / b * x;
}
}
int main() {
cin >> n;
LL M = 1;
for (int i = 0; i < n; ++ i) {
cin >> m[i] >> a[i];
M *= m[i];
}
LL res = 0;
for (int i = 0; i < n; ++ i) {
LL Mi = M / m[i];
LL ti, y;
exgcd(Mi, m[i], ti, y);
res += a[i] * Mi * ti;
}
cout << (res % M + M) % M << endl;
return 0;
}