Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/louisabraham/algnuth
Browse files Browse the repository at this point in the history
  • Loading branch information
louisabraham committed Jun 23, 2021
2 parents 0217257 + 4b47e05 commit 47d2cd8
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions algnuth/polynom.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,24 @@ def __pow__(P, k):
break
A *= A
return V


def inv(self):
if self.v == 0:
raise ZeroDivisionError
return ModInt(ModInt._inv(self.v, self.n), self.n)

@staticmethod
def _inv(k, n):
k %= n
if k == 1:
return k
return (n - n // k) * ModInt._inv(n % k, n) % n

def __truediv__(a, b):
assert isinstance(b, ModInt)
assert a.n == b.n
return ModInt(
a.v * ModInt.extended_euclid(b.v, b.n)[0], a.n)
return a * b.inv()

def __rtruediv__(a, k):
assert isinstance(k, int)
Expand Down

0 comments on commit 47d2cd8

Please sign in to comment.