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.
39 lines
756 B
39 lines
756 B
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
#define int long long
|
|
#define endl "\n"
|
|
|
|
int exgcd(int a, int b, int &x, int &y) {
|
|
if (!b) {
|
|
x = 1, y = 0;
|
|
return a;
|
|
}
|
|
int d = exgcd(b, a % b, y, x);
|
|
y -= a / b * x;
|
|
return d;
|
|
}
|
|
|
|
int n, a1, m1, a2, m2, k1, k2;
|
|
signed main() {
|
|
cin >> n >> a1 >> m1;
|
|
n--;
|
|
while (n--) {
|
|
cin >> a2 >> m2;
|
|
|
|
int d = exgcd(a1, -a2, k1, k2);
|
|
if ((m2 - m1) % d) {
|
|
cout << -1 << endl;
|
|
exit(0);
|
|
}
|
|
|
|
k1 *= (m2 - m1) / d;
|
|
|
|
int t = abs(a2 / d);
|
|
k1 = (k1 % t + t) % t;
|
|
|
|
m1 = k1 * a1 + m1;
|
|
a1 = abs(a1 / d * a2);
|
|
}
|
|
|
|
cout << (m1 % a1 + a1) % a1 << endl;
|
|
} |