Last active
December 23, 2015 04:19
-
-
Save iseki-masaya/6579929 to your computer and use it in GitHub Desktop.
MOD指数演算
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int MOD = 1000000007; | |
long long | |
mod_mult(long long a,long long b) | |
{ | |
long long res = 0; | |
long long mir = a%MOD; | |
while (b) { | |
if (b&1) { | |
res += mir; | |
if (res > MOD) { | |
res -= MOD; | |
} | |
} | |
mir <<= 1; | |
if (mir > MOD) { | |
mir -= MOD; | |
} | |
b >>= 1; | |
} | |
return res; | |
} | |
long long | |
mod_exp(long long a,long long b) | |
{ | |
long long res = 1; | |
long long mir = a%MOD; | |
while (b) { | |
if (b&1) { | |
res = mod_mult(res, mir); | |
} | |
mir = mod_mult(mir, mir); | |
b >>= 1; | |
} | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment