组合数 + 快速幂 + 逆元打表 + 阶乘打表
Posted chunibyo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了组合数 + 快速幂 + 逆元打表 + 阶乘打表相关的知识,希望对你有一定的参考价值。
1 /** 2 * Fuck you. 3 * I love you too. 4 */ 5 6 #include<bits/stdc++.h> 7 #define lson i<<2 8 #define rson i<<2|1 9 #define LS l,mid,lson 10 #define RS mid+1,r,rson 11 #define mem(a,x) memset(a,x,sizeof(a)) 12 #define gcd(a,b) __gcd(a,b) 13 #define ll long long 14 #define ull unsigned long long 15 #define lowbit(x) (x&-x) 16 #define enld endl 17 #define mian main 18 #define itn int 19 #define prinft printf 20 21 const int INF = 0x3f3f3f3f; 22 const int EXP = 1e-8; 23 const int N = 1e5 + 5; 24 const int M = 1e9 + 7; 25 const int MAXN = 1e5 + 5; 26 27 using namespace std; 28 29 // n不太大 1e5 30 ll fac[N] = {1, 1}, inv[N] = {1, 1}, f[N] = {1, 1}; 31 32 ll c (ll a, ll b) { 33 if (b > a) 34 return 0; 35 return fac[a] * inv[b] % M * inv[a - b] % M; 36 } 37 38 void init() { 39 for (int i = 2; i < N; i++) { 40 fac[i] = fac[i - 1] * i % M; 41 f[i] = (M - M / i) * f[M % i] % M; 42 inv[i] = inv[i - 1] * f[i] % M; 43 } 44 } 45 46 // n较大 1e9 47 ll qpow (ll a, ll b) { 48 ll ans = 1; 49 while (b) { 50 if (b & 1) 51 ans = ans * a % M; 52 a = a * a % M; 53 b >>= 1; 54 } 55 return ans; 56 } 57 58 ll C (ll n, ll m) { 59 if (m > n) 60 return 0; 61 ll ans = 1; 62 for (int i = 1; i <= m; i++) 63 ans = ans * (n - i + 1) % M * qpow (i, M - 2) % M; 64 return ans; 65 } 66 67 int main() { 68 init(); 69 cout << c (4, 2) << endl; 70 cout << C (4, 2) << endl; 71 return 0; 72 }
以上是关于组合数 + 快速幂 + 逆元打表 + 阶乘打表的主要内容,如果未能解决你的问题,请参考以下文章