Created
August 20, 2019 15:52
-
-
Save Sicalxy/7477fb6541bedd4b3d92b95ab3cbdc66 to your computer and use it in GitHub Desktop.
Modular Multiplicative Inverse 模逆元
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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