# [BZOJ 2839]集合计数

## Description

$2^n$ 个集合，每个集合只包含 $[1,n]$ ，且这些集合两两不同。问有多少种选择方法（至少选一个），使得这些集合交集大小为 $k$

$0\leq k\leq n\leq 1000000$

## Solution

$f(n)$ 为交集元素大于 $k$ 的方案数，设 $g(n)$ 为交集元素等于 $k$ 的方案数。

$f(k)=\sum_{i=k}^n{i\choose k}g(i)\Rightarrow g(k)=\sum_{i=k}^n(-1)^{i-k}{i\choose k}f(i)$

## Code

#include <bits/stdc++.h>
using namespace std;
const int N = 1000000+5, yzh = 1000000007;

int n, k, ifac[N], fac[N], ans;

int quick_pow(int a, int b, int p) {
int ans = 1;
while (b) {
if (b&1) ans = 1ll*ans*a%p;
b >>= 1, a = 1ll*a*a%p;
}
return ans;
}
int C(int n, int m) {return 1ll*fac[n]*ifac[m]%yzh*ifac[n-m]%yzh; }
void work() {
scanf("%d%d", &n, &k);
fac[0] = fac[1] = ifac[0] = ifac[1] = 1;
for (int i = 2; i <= n; i++) ifac[i] = -1ll*yzh/i*ifac[yzh%i]%yzh;
for (int i = 2; i <= n; i++)
fac[i] = 1ll*fac[i-1]*i%yzh, ifac[i] = 1ll*ifac[i]*ifac[i-1]%yzh;
for (int i = k; i <= n; i++)
if ((i-k)&1) (ans -= 1ll*C(i, k)*C(n, i)%yzh*quick_pow(2, quick_pow(2, n-i, yzh-1), yzh)%yzh) %= yzh;
else (ans += 1ll*C(i, k)*C(n, i)%yzh*quick_pow(2, quick_pow(2, n-i, yzh-1), yzh)%yzh) %= yzh;
printf("%d\n", (ans+yzh)%yzh);
}
int main() {work(); return 0; }

posted @ 2018-06-29 21:58  NaVi_Awson  阅读(236)  评论(0编辑  收藏  举报