一、 直接公式求
二、基于杨辉三角的递推
三、基于逆元的求法
需要进行取模的时候,我们可以使用逆元法加速运算。
//逆元法快速求组合数#define LL long longconst LL mod = 1e9 + 7;LL fact[N << 1], inv[N << 1];//快速幂LL quickpow(LL a, LL b) {LL res = 1;while (b) {if (b & 1) res = res * a % mod;b >>= 1;a = a * a % mod;}return res;}//处理n!的逆元void init(int n) {fact[0] = 1;for (int i = 1; i <= n; ++i)fact[i] = fact[i - 1] * i % mod;inv[n] = quickpow(fact[n], mod - 2);for (int i = n - 1; i >= 0; --i)inv[i] = inv[i + 1] * (i + 1) % mod;}LL C(LL n, LL m) {if (m > n) return 0;return fact[n] * inv[m] % mod * inv[n - m] % mod;}
