参考
程序
快速幂指数取模函数和模逆函数:
def fast_power(base, power, MOD):
result = 1
while power > 0:
# If power is odd
if power % 2 == 1:
result = (result * base) % MOD
# Divide the power by 2
power = power // 2
# Multiply base to itself
base = (base * base) % MOD
return result
def gcd(a, b):
while a != 0:
a, b = b%a, a
return b
# calc : b^(-1) mod m
def findModeInverse(b, m, show = True):
if gcd(m,b) != 1:
return None
A1, A2, A3 = 1, 0, m
B1, B2, B3 = 0, 1, b
if show:
print('-'*54)
print("|{:^5}\t{:^5}\t{:^5}\t{:^5}\t{:^5}\t{:^5}\t{:^5}|".format("Q","A1","A2","A3","B1","B2","B3"))
print("|{:^5}\t{