Skip to content

Commit 8e040e7

Browse files
authored
Update miller-rabin.cpp
1 parent a55ccbe commit 8e040e7

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

math/miller-rabin.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
// Miller-Rabin - Primarily Test O(k*logn*logn*logn)
2+
ll addmod(ll a, ll b, ll m) {
3+
if(a >= m - b) return a + b - m;
4+
return a + b;
5+
}
6+
27
ll mulmod(ll a, ll b, ll m) {
38
ll ans = 0;
49
while (b) {
5-
if (b&1) ans=(ans+a)%m;
6-
a = (2*a)%m;
10+
if (b&1) ans=addmod(ans, a, m);
11+
a=addmod(a, a, m);
712
b>>=1;
813
}
914
return ans;

0 commit comments

Comments
 (0)