You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
629 B

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
const int N = 10;
int n;
int a[N], m[N];
void exgcd(int a, int b, int &x, int &y) {
if (!b)
x = 1, y = 0;
else {
exgcd(b, a % b, y, x);
y -= a / b * x;
}
}
signed main() {
cin >> n;
int M = 1;
for (int i = 1; i <= n; i++) {
cin >> m[i] >> a[i];
M *= m[i];
}
int res = 0, x, y;
for (int i = 1; i <= n; i++) {
exgcd(M / m[i], m[i], x, y);
res += a[i] * M / m[i] * x;
}
cout << (res % M + M) % M << endl;
}