Skip to content

Instantly share code, notes, and snippets.

@iseki-masaya
Last active December 23, 2015 04:19
Show Gist options
  • Save iseki-masaya/6579929 to your computer and use it in GitHub Desktop.
Save iseki-masaya/6579929 to your computer and use it in GitHub Desktop.
MOD指数演算
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