Skip to content

Instantly share code, notes, and snippets.

@catupper
Last active December 20, 2020 15:03
Show Gist options
  • Save catupper/cb9a146dd21a9916e75deef90991169d to your computer and use it in GitHub Desktop.
Save catupper/cb9a146dd21a9916e75deef90991169d to your computer and use it in GitHub Desktop.
def gcd(a, b):
if a == 0:return b
else:return gcd(b % a, a)
#returns (x, y, gcd(a,b)) s.t. ax + by = gcd(a,b)
def ext_gcd(a, b):
if a == 0:return (0, 1, b)
else:
(X, Y, g) = ext_gcd(b % a, a)
return (Y-b//a*X, X, g)
MOD = 10**9 + 7
inv = [0] * 10000
inv[1] = 1
for i in range(2, 10000):
inv[i] = MOD // i * -inv[MOD % i] % MOD
#check
print(all(x * ext_gcd(x, MOD)[0] % MOD == 1 for x in range(1, 10000)))
print(all(x * inv[x] % MOD == 1 for x in range(1, 10000)))
@catupper
Copy link
Author

The code was written in https://youtu.be/eiJyDb9c3Js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment