Solve problem 381

This commit is contained in:
2026-06-18 15:42:03 -04:00
parent 5c9e9d775f
commit 908cdb9a7e
2 changed files with 57 additions and 0 deletions
+15
View File
@@ -230,3 +230,18 @@ def cache(f):
return r
return func_cached
def ext_gcd(a: int, b: int) -> tuple[int, int, int]:
if a == 0:
return (b, 0, 1)
else:
gcd, x, y = ext_gcd(b % a, a)
return (gcd, y - (b // a) * x, x)
def modinv(a: int, b: int) -> int:
gcd, x, _ = ext_gcd(a, b)
if gcd != 1:
raise Exception('Modular inverse does not exist')
else:
return x % b