模板题
def ex_gcd(a, b):
if b == 0:
return 1, 0, a
else:
x, y, gcd = ex_gcd(b, a % b)
return y, x - a // b * y, gcd
def cal(r1, m1, r2, m2):
_, _, d = ex_gcd(m1, m2)
if (r2 - r1) % d != 0:
return -1, -1
k1, k2, _ = ex_gcd(m1 // d, m2 // d)
k1 = (r2 - r1) // d * k1
mm = m1 * m2 // d
rr = (r1 + k1 * m1) % mm
return rr, mm
n = int(input())
r, m = [], []
for _ in range(n):
mm, rr = map(int, input().split())
r.append(rr)
m.append(mm)
rr, mm = r[0], m[0]
for i in range(1, n):
rr, mm = cal(rr, mm, r[i], m[i])
if rr == -1:
break
print(rr)
小伙子有潜力