原来源: dian巨
阶乘逆元求组合数
在做D - Madoka and The Corruption Scheme时, 一个满二叉树的走法就是C(n,i), 在n轮中赢几场, 最终就是杨辉三角前缀和
template<typename T = long long, int P = 1000000007>
class Combination{
public:// 初始化组合数, mul[i]=i!, div[i]=1/(i!)
Combination(int n): div(n + 1, 1), mul(n + 1, 1){
for(int i = 1; i <= n; ++i) mul[i] = mul[i - 1] * i % P;
div[n] = qPow(mul[n], P - 2);
for(int i = n - 1; i > 0; --i) div[i] = div[i + 1] * (i + 1) % P;
}
T operator () (int n, int m){// c(2,1)=2
if(m < 0) return 0;
if(m > n) return 0;
return mul[n] * div[m] % P * div[n - m] % P;
}
private:
T qPow(T b, T n){
T ret = 1;
while(n){
if(n & 1) ret = ret * b % P;
b = b * b % P;
n >>= 1;
}
return ret;
}
vector<T> div, mul;
};
标签:return,Combination,数论,ret,int,mul,div,模板
From: https://www.cnblogs.com/LiamEvander/p/16652223.html