defegcd(a, b, f1, f2):while b !=0:
f1, f2= f2, f1 - a // b * f2
a, b = b, a%b
return f1, a
defget_mutiply_reverse(a, b):
r1, gcf = egcd(n1, n2,1,0)if gcf !=1:return-1else:return r1 % b
n1, n2 =map(int,input().split(" "))
reverse = get_mutiply_reverse(n1, n2)if reverse !=-1:print("{}在{}下的逆元为{}".format(n1, n2, reverse))else:print("{}在{}条件下不存在逆元".format(n1, n2))
模指数运算,这个题好像书里有代码了,这
defrec_mod_exp(x, y, p):if y ==0:return1
z = rec_mod_exp(x, y //2, p)if(y &1)==0:# y is an even numberreturn z * z % p
else:# y is an odd numberreturn x * z * z % p
a =eval(input())
b =eval(input())
m =eval(input())print(rec_mod_exp(a, b, m))
利用费马小定理计算
defget_mod(a, m, p):
i =0whileTrue:
r =(m - i)%(p -1)if r ==0:
result = a ** i
result = result % p
return result
else:
i = i +1
base =eval(input()