Skip to content

Commit 55d62aa

Browse files
authored
Merge pull request NASU41#45 from NASU41/bugfix/issue-33
NASU41#33 pow_modを原典準拠にする
2 parents df25ae4 + 1321793 commit 55d62aa

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

‎Math/MathLib.java‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ private static long[] inv_gcd(long a, long b){
2222
return new long[]{s,m0};
2323
}
2424

25-
public static long pow_mod(long x, long n, long m){
26-
assert(n >= 0 && m >= 1);
27-
long ans = 1;
25+
public static long pow_mod(long x, long n, int m){
26+
assert n >= 0;
27+
assert m >= 1;
28+
if(m == 1)return 0L;
29+
x = safe_mod(x, m);
30+
long ans = 1L;
2831
while(n > 0){
29-
if(n%2==1) ans = (ans * x) % m;
32+
if((n&1) == 1) ans = (ans * x) % m;
3033
x = (x*x) % m;
31-
n /= 2;
34+
n >>>= 1;
3235
}
3336
return ans;
3437
}

0 commit comments

Comments
 (0)