def modinv(a, b):
"""Returns the modular inverse of a mod b.
Pre: a < b and gcd(a, b) = 1
Adapted from https://en.wikibooks.org/wiki/Algorithm_Implementation/
Mathematics/Extended_Euclidean_algorithm#Python
"""
saved = b
x, y, u, v = 0, 1, 1, 0
while a:
q, r = b // a, b % a
m, n = x - u*q, y - v*q
b, a, x, y, u, v = a, r, u, v, m, n
return x % saved
评论列表
文章目录