码迷,mamicode.com
首页 > 其他好文 > 详细

组合数 + 快速幂 + 逆元打表 + 阶乘打表

时间:2018-07-29 17:54:38      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:closed   div   c++   long   sizeof   sign   exp   code   const   

技术分享图片
 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 }
View Code

 

组合数 + 快速幂 + 逆元打表 + 阶乘打表

标签:closed   div   c++   long   sizeof   sign   exp   code   const   

原文地址:https://www.cnblogs.com/chunibyo/p/9385854.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!