- 两个数组(Two Arrays)
组合数模板,线性逆元,ksm求逆元
- @ 2026-8-6 9:22:53
const int mod = 998244353;
const int N = 5e5 + 10;
ll fac[N], ifac[N], inv[N];
void init() {
fac[0] = ifac[0] = ifac[1] = inv[1] = 1;
for(int i = 1; i < N; i++) {
fac[i] = fac[i - 1] * i % mod;
if(i > 1) ifac[i] = (mod - mod / i) * ifac[mod % i] % mod, inv[i] = ifac[i];
}
for(int i = 1; i < N; i++) ifac[i] = ifac[i - 1] * ifac[i] % mod;
}
ll C(int n, int m) {
return fac[n] * ifac[m] % mod * ifac[n - m] % mod;
}
ll ksm(ll a, ll b = mod - 2) {
ll res = 1;
while(b) {
if(b & 1) res = res * a % mod;
a = a * a % mod, b /= 2;
}
return res;
}
使用组合数必须init,预处理O(N) ksm(a)表示求a的倒数(逆元)在mod意义下的值,要求mod为质数
信息
- ID
- 562
- 时间
- ms
- 内存
- MiB
- 难度
- 4
- 标签
- 递交数
- 44
- 已通过
- 25
- 上传者