ModPow
8 lines tested
content/number-theory/ModPow.h — Noam527, source: folklore
const ll mod = 1000000007; // faster if const
ll modpow(ll b, ll e) {
ll ans = 1;
for (; e; b = b * b % mod, e /= 2)
if (e & 1) ans = ans * b % mod;
return ans;
}8 lines tested
content/number-theory/ModPow.h — Noam527, source: folklore
const ll mod = 1000000007; // faster if const
ll modpow(ll b, ll e) {
ll ans = 1;
for (; e; b = b * b % mod, e /= 2)
if (e & 1) ans = ans * b % mod;
return ans;
}