You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
998 B
C++
36 lines
998 B
C++
#define _CRT_SECURE_NO_WARNINGS
|
|
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
|
|
#include <cstdio>
|
|
using namespace std;
|
|
typedef long long ll;
|
|
void exgcd(ll a, ll b, ll &d, ll &x, ll &y) { b == 0 ? (x = 1, y = 0, d = a) : (exgcd(b, a % b, d, y, x), y -= x * (a / b)); }
|
|
int main()
|
|
{
|
|
for (int n; ~scanf("%d", &n);)
|
|
{
|
|
ll a1, r1, a2, r2;
|
|
scanf("%lld%lld", &a1, &r1);
|
|
bool flag = true;
|
|
for (int i = 1; i < n; i++)
|
|
{
|
|
ll d, x, y;
|
|
scanf("%lld%lld", &a2, &r2);
|
|
ll ans = 0;
|
|
exgcd(a1, a2, d, x, y);
|
|
if ((r2 - r1) % d)
|
|
flag = false;
|
|
else
|
|
{
|
|
x *= ((r2 - r1) / d);
|
|
ll n1 = a2 / d;
|
|
x = (x % n1 + n1) % n1;
|
|
r1 = a1 * x + r1;
|
|
a1 = (a1 * a2) / d;
|
|
}
|
|
}
|
|
if (!flag) r1 = -1;
|
|
printf("%lld\n", r1);
|
|
}
|
|
return 0;
|
|
}
|