# bzoj2839 集合计数

## 2839: 集合计数

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 854  Solved: 470

3 2

6

## HINT

【样例说明】

【数据说明】

对于100%的数据，1≤N≤1000000；0≤K≤N；

## Source

 1 #include <cstdio>
2
3 const int MO = 1000000007, phi = 1000000006;
4
5 const int N = 1000010;
6
7 int f[N], pw[N], pww[N], fac[N], inv[N], invn[N];
8
9 inline int C(int n, int m) {
10     if(n < m || n < 0 || m < 0) return 0;
11     return 1ll * fac[n] * invn[m] % MO * invn[n - m] % MO;
12 }
13
14 inline int qpow(int a, int b) {
15     int ans = 1;
16     while(b) {
17         if(b & 1) ans = 1ll * ans * a % MO;
18         a = 1ll * a * a % MO;
19         b = b >> 1;
20     }
21     return ans;
22 }
23
24 int main() {
25
26     int n, k;
27     scanf("%d%d", &n, &k);
28     pww[0] = pw[0] = fac[0] = inv[0] = invn[0] = 1;
29     fac[1] = inv[1] = invn[1] = 1; pw[1] = pww[1] = 2;
30     for(int i = 2; i <= n; i++) {
31         fac[i] = 1ll * fac[i - 1] * i % MO;
32         inv[i] = 1ll * inv[MO % i] * (MO - MO / i) % MO;
33         invn[i] = 1ll * invn[i - 1] * inv[i] % MO;
34         pw[i] = pw[i - 1] * 2 % MO;
35         pww[i] = pww[i - 1] * 2 % (phi);
36     }
37
38     int ans = 0;
39     for(int i = k; i <= n; i++) {
40         int temp = 1ll * (qpow(2, pww[n - i]) - 1) * C(n, i) % MO * C(i, k) % MO;
41         if((i - k) & 1) ans = (ans - temp) % MO;
42         else ans = (ans + temp) % MO;
43     }
44     printf("%d\n", (ans + MO) % MO);
45     return 0;
46 }
AC代码

 1 #include <cstdio>
2
3 const int MO = 1000000007, phi = 1000000006;
4
5 const int N = 1000010;
6
7 int f[N], pw[N], pww[N], fac[N], inv[N], invn[N];
8
9 inline int C(int n, int m) {
10     if(n < m || n < 0 || m < 0) return 0;
11     return 1ll * fac[n] * invn[m] % MO * invn[n - m] % MO;
12 }
13
14 inline int qpow(int a, int b) {
15     int ans = 1;
16     while(b) {
17         if(b & 1) ans = 1ll * ans * a % MO;
18         a = 1ll * a * a % MO;
19         b = b >> 1;
20     }
21     return ans;
22 }
23
24 int main() {
25
26
27
28     int n, k;
29     scanf("%d%d", &n, &k);
30     pww[0] = pw[0] = fac[0] = inv[0] = invn[0] = 1;
31     fac[1] = inv[1] = invn[1] = 1; pw[1] = pww[1] = 2;
32     for(int i = 2; i <= n; i++) {
33         fac[i] = 1ll * fac[i - 1] * i % MO;
34         inv[i] = 1ll * inv[MO % i] * (MO - MO / i) % MO;
35         invn[i] = 1ll * invn[i - 1] * inv[i] % MO;
36         pw[i] = pw[i - 1] * 2 % MO;
37         pww[i] = pww[i - 1] * 2 % (phi);
38     }
39
40     int ans = 0;
41
42     /*for(int i = k; i <= n; i++) {
43         int temp = 1ll * (qpow(2, pww[n - i]) - 1) * C(n, i) % MO * C(i, k) % MO;
44         if((i - k) & 1) ans = (ans - temp) % MO;
45         else ans = (ans + temp) % MO;
46     }*/
47     for(int i = n; i >= k; i--) {
48         f[i] = 1ll * (qpow(2, pww[n - i]) - 1) * C(n, i) % MO;
49         for(int j = i + 1; j <= n; j++) {
50             f[i] -= 1ll * f[j] * C(j, i) % MO;
51             if(f[i] < 0) f[i] += MO;
52         }
53     }
54
55     printf("%d\n", (f[k] + MO) % MO);
56     return 0;
57 }
70分TLE代码

posted @ 2019-03-05 15:47  huyufeifei  阅读(155)  评论(0编辑  收藏  举报