题目中已经给出了互质的条件,直接相乘模数即可。我们可以用一个比较巧妙的方法求解,对于所有未满足上面 x 的通解的形式的,我们给最终答案累加上我们要的 b_iM_iN_i 这一项,若满足通解的形式,直接跳过就行。这种情况下一定是最小解。
#include<bits/stdc++.h>
using namespace std;
int a[11], b[11];
int main() {
long long tot, ans;
int n;
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
}
ans = b[1];//答案
tot = a[1];//目前已经满足的条件中模数的乘积
for(int i = 2; i <= n; i++) {
while(ans % a[i] != b[i]) {
ans += tot;//枚举即可
}
tot *= a[i];
}
cout << ans;
}