-
Notifications
You must be signed in to change notification settings - Fork 0
/
AcWing204.cpp
46 lines (46 loc) · 849 Bytes
/
AcWing204.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL exgcd(LL a, LL b, LL &x, LL &y)
{
if(!b)
{
x = 1, y = 0;
return a;
}
LL d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
int main()
{
int n;
cin >> n;
bool has_answer = true;
LL a1, m1;
cin >> a1 >> m1;
for (int i = 0; i < n - 1; i++)
{
LL a2, m2;
cin >> a2 >> m2;
LL k1, k2;
LL d = exgcd(a1, a2, k1, k2);
if((m2 - m1) % d)
{
has_answer = false;
break;
}
k1 *= (m2 - m1) / d;
LL t = a2 / d;
k1 = (k1 % t + t) % t;
m1 = a1 * k1 + m1;
a1 = abs(a1 / d * a2);
}
if (has_answer)
{
cout << (m1 % a1 + a1) % a1 << endl;
}
else{
cout << -1 << endl;
}
}