#include <iostream>
#include <cstdio>
using namespace std;
int line;
typedef long long LL;
LL exgcd(LL a, LL b, LL & x, LL & y)
{
if(b == 0)
{
x = 1, y = 0;
return a;
}
LL d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
inline LL mod(LL a, LL b)
{
return (a % b + b) % b;
}
int main()
{
cin >> line;
LL a1, m1, k1;
scanf("%lld%lld", &a1, &m1);
bool flag = true;
LL a2, m2, k2;
for (int i = 1; i < line; i ++)
{
scanf("%lld%lld", &a2, &m2);
LL d = exgcd(a1, -a2, k1, k2);
if((m2 - m1) % d)
{
flag = false;
continue;
}
k1 *= (m2 - m1) / d;
m1 = mod(k1, a2 / d) * a1 + m1;
a1 = abs(a1 / d * a2);
}
if(flag) printf("%lld\n", mod(m1, a1));
else puts("-1");
return 0;
}
来源:CSDN
作者:Victayria
链接:https://blog.csdn.net/Victayria/article/details/104526025