Skip to content

Instantly share code, notes, and snippets.

@iseki-masaya
Last active December 23, 2015 04:19
Show Gist options
  • Save iseki-masaya/6579331 to your computer and use it in GitHub Desktop.
Save iseki-masaya/6579331 to your computer and use it in GitHub Desktop.
MOD乗算のコード ・if文で分岐させることでモジュロ演算をしない(PGOが備わってれば分岐数の少ないif文は低コスト) ・bit毎に確認するので計算量はO(log(N))
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment