题目链接:https://ac.nowcoder.com/acm/contest/890/D
思路:这个题就是中国剩余定理的模板题,只不过会爆,所以用写的非递归的版本。
AC代码:
def ex_gcd(m,n):
if n==0:
x=1
y=0
return (m,x,y)
else :
a1=1
b=1
a=0
b1=0
c=m
d=n
q=c//d
r=c%d
while r > 0:
c=d
d=r
t=a1
a1=a
a=t-a*q
t=b1
b1=b
b=t-q*b
q=c//d
r=c%d
x=a
y=b
return (d, x, y)
def excrt(r,m,n):
mo=m[0]
re=r[0]
for i in range(1,n):
(d,x,y)=ex_gcd(mo,m[i])
if (r[i]-re)%d!=0:
return (0,re)
x=(r[i]-re)//d*x%(m[i]//d)
re=re+x*mo
mo=mo//d*m[i]
re=re%mo
re=(re+mo)%mo
return (1,re)
r = []
m = []
n, up = map(int, input().split())
for i in range(0, n):
t1,t2=map(int, input().split())
m.append(t1)
r.append(t2)
(tmp,ans)=excrt(r,m,n)
if tmp==0:
print("he was definitely lying")
elif ans>up:
print("he was probably lying")
else:
print(ans)