Skip to content

Commit 1e8df9f

Browse files
Update binary-exp.md
Distributive law of modulo over multiplication to handle overflow
1 parent 0da3b79 commit 1e8df9f

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/algebra/binary-exp.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ long long binpow(long long a, long long b, long long m) {
104104
long long res = 1;
105105
while (b > 0) {
106106
if (b & 1)
107-
res = res * a % m;
108-
a = a * a % m;
107+
res = (res%m * a%m) % m;
108+
a = (a%m * a%m) % m;
109109
b >>= 1;
110110
}
111111
return res;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy