Created
January 28, 2020 10:13
-
-
Save oskimura/37ed9f52b403acac4dcf9b26a067d8da to your computer and use it in GitHub Desktop.
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
// テーブルを作る前処理 | |
void COMinit() { | |
fac[0] = fac[1] = 1; | |
finv[0] = finv[1] = 1; | |
inv[1] = 1; | |
for (int i = 2; i < MAX; i++){ | |
fac[i] = fac[i - 1] * i % MOD; | |
inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD; | |
finv[i] = finv[i - 1] * inv[i] % MOD; | |
} | |
} | |
// 二項係数計算 | |
long long COM(int n, int k){ | |
if (n < k) return 0; | |
if (n < 0 || k < 0) return 0; | |
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; | |
} | |
// 法mでのaの逆元を計算 | |
long long modinv(long long a, long long m) { | |
long long b = m, u = 1, v = 0; | |
while (b) { | |
long long t = a / b; | |
a -= t * b; swap(a, b); | |
u -= t * v; swap(u, v); | |
} | |
u %= m; | |
if (u < 0) u += m; | |
return u; | |
} | |
//最大公約数 | |
int gcd(int x, int y) { return (x % y)? gcd(y, x % y): y; } | |
//最小公倍数 | |
int lcm(int x, int y) { return x / gcd(x, y) * y; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment