Skip to content

Instantly share code, notes, and snippets.

@iseki-masaya
Created April 6, 2015 11:45
Show Gist options
  • Save iseki-masaya/5badbd85eacea1420c3b to your computer and use it in GitHub Desktop.
Save iseki-masaya/5badbd85eacea1420c3b to your computer and use it in GitHub Desktop.
const int MOD = 1000000007;
int exp_mod(long long a, long long b, int m) {
long long res = 1;
while (b > 0) {
if (b&1)
res = (res * a)%m;
a = (a * a)%m;
b >>= 1;
}
return (int)res;
}
int fact_mod(int n, int m) {
long long res = 1;
for (int i=n; i>0; --i)
res = (res * i)%m;
return (int)res;
}
int comb_mod(int n, int r) {
long long res = 1;
for (int i=0; i<r; ++i)
res = (res * (n - i) )%MOD;
return ( res * exp_mod(fact_mod(r, MOD), MOD-2, MOD) ) % MOD;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment