题目链接:https://ac.nowcoder.com/acm/contest/890/D
思路:这个题就是中国剩余定理的模板题,只不过会爆long longlong \ longlong long,所以用pythonpythonpython写的非递归的exgcd+excrtexgcd+excrtexgcd+excrt版本。
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)

本文介绍了一道编程竞赛题目,通过使用中国剩余定理解决。文章提供了Python实现的非递归exgcd和excrt算法代码,用于处理超过long long范围的问题。通过实例演示了如何应用这些算法来求解特定数学问题。
790

被折叠的 条评论
为什么被折叠?



