His majesty chatted with Han Xin about the capabilities of the generals. Each had their shortcomings. His majesty asked, ``How many troops could I lead?" Han Xin replied, ``Your highness should not lead more than 100000." His majesty said, ``And what about you?" ``For your humble servant, the more the merrier!" said Han Xin.
---Records of the Grand Historian
Han Xin was a military general who served Liu Bang during the Chu-Han contention and contributed greatly to the founding of the Han dynasty. Your friend, in a strange coincidence, also named Han Xin, is a military general as well.
One day you asked him how many troops he led. He was reluctant to tell you the exact number, but he told you some clues in certain form, for example:
- if we count the troops by threes, we have two left over;
- if we count by fives, we have three left over;
- if we count by sevens, two are left over;
- ...
You wonder the number of his troops, or he was simply lying. More specifically, you would like to know the minimum possible number of troops he leads, and if the minimum number is too large, then you suspect he was lying.
中国剩余定理模板题, 要用高精度
M = 0
m = [0] * 110
a = [0] * 110
x = 0
y = 0
def exgcd(a, b):
global x, y
if b == 0:
x = 1
y = 0
return a
else:
d = exgcd(b, a % b)
t = x
x = y
y = t - (a // b) * y
return d
def CRT(n):
global x, y, M
flag = 0
for i in range(1, n):
d = exgcd(m[0], m[i])
#print(d)
u = a[i] - a[0]
if u % d != 0:
flag = 1
break
x = (x * u % m[i]) // d
m[i] //= d
x = (x % m[i] + m[i]) % m[i]
a[0] += m[0] * x
m[0] *= m[i]
# print(flag)
if flag == 1:
print("he was definitely lying")
elif a[0] > M:
print("he was probably lying")
else:
print(a[0])
n, M = map(int, input().split())
for i in range(0, n):
m[i], a[i] = map(int, input().split())
CRT(n)