Skip to content

Instantly share code, notes, and snippets.

@Sicalxy
Created August 20, 2019 15:52
Show Gist options
  • Save Sicalxy/7477fb6541bedd4b3d92b95ab3cbdc66 to your computer and use it in GitHub Desktop.
Save Sicalxy/7477fb6541bedd4b3d92b95ab3cbdc66 to your computer and use it in GitHub Desktop.
Modular Multiplicative Inverse 模逆元
def ext_euclid(a, b):
if b == 0:
return 1, 0, a
else:
x, y, q = ext_euclid(b, a % b)
x, y = y, (x - (a // b) * y)
return x, y, q
def inverse(a, n):
x, y, q = ext_euclid(a, n)
return x % n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment