Skip to content

Commit d21f30b

Browse files
committed
Enhanced efficiency of ModInt::pow
1 parent 5116390 commit d21f30b

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

src/alfred/math/mod-int.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ class ModInt {
4444
constexpr ModInt pow(int64_t n) const noexcept {
4545
ModInt res = 1, x = *this;
4646
if (n < 0) {
47-
x = x.inv();
48-
n = -n;
47+
n = (-n) % (M - 1) * (M - 2);
4948
}
49+
n %= (M - 1);
5050
while (n) {
5151
if (n & 1) res *= x;
5252
x *= x;

0 commit comments

Comments
 (0)