bzoj2839 集合计数

[没有嘟嘟嘟,权限题]


我们dp,令\(dp[i]\)表示选若干个集合,交集为\(i\)的方案数,则\(dp[i] = C_{n} ^ {i} * (2 ^ {2 ^ {n - i}} - 1)\)。就是说我们先强制选\(i\)个,有\(C_{n} ^ {i}\)个选法,然后剩下的随便选,于是就产生了\(2 ^ {n - i}\)个集合,从这些集合中又可以随便选,那么就有\(2 ^ {2 ^ {n - i}}\)种选法。


然后令\(f[i]\)表示恰好有\(i\)个交集的方案数,于是就有\(dp[i] = \sum _ {j =i} ^ {n} C_{j} ^ {i} f[j]\)。这个上二项式反演,得到\(f[i] = \sum _ {j = i} ^ {n} (-1) ^ {j - i} C_{j} ^ {i} dp[j]\)
这样就完事了。


\(dp[i]\)的时候可以从后往前推,就能处理\(2 ^ {2 ^ {n - i}}\)这东西了。
(别忘了有\(k = 0\)的情况)

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<assert.h>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e6 + 5;
const ll mod = 1e9 + 7;
In ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  if(last == '-') ans = -ans;
  return ans;
}
In void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
  freopen("ha.in", "r", stdin);
  freopen("ha.out", "w", stdout);
#endif
}

int n, K;
ll fac[maxn], inv[maxn];

In ll inc(ll a, ll b) {return a + b < mod ? a + b : a + b - mod;}
In ll C(int n, int m)
{
  if(n < m) return 0;
  return fac[n] * inv[m] % mod * inv[n - m] % mod;
}
In ll quickpow(ll a, ll b)
{
  ll ret = 1;
  for(; b; b >>= 1, a = a * a % mod)
    if(b & 1) ret = ret * a % mod;
  return ret;
}

In void init()
{
  fac[0] = inv[0] = 1;
  for(int i = 1; i < maxn; ++i) fac[i] = fac[i - 1] * i % mod;
  inv[maxn - 1] = quickpow(fac[maxn - 1], mod - 2);
  for(int i = maxn - 2; i; --i) inv[i] = inv[i + 1] * (i + 1) % mod;
}

ll dp[maxn];

int main()
{
  MYFILE();
  n = read(), K = read();
  init();
  ll tp = 2;
  for(int i = n; i >= 0; --i)
    {
      dp[i] = C(n, i) * (tp - 1) % mod;
      tp = tp * tp % mod;
    }
  ll ans = 0;
  for(int i = K, t = 1; i <= n; ++i, t *= (-1))
    {
      ll tp = C(i, K) * dp[i] % mod * t;
      if(tp < 0) tp += mod;
      ans = inc(ans, tp); 
    }
  write(ans), enter;
  return 0;
}
posted @ 2019-06-06 20:30  mrclr  阅读(169)  评论(0编辑  收藏  举报