# bzoj2839: 集合计数 容斥+组合

#### 2839: 集合计数

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 523  Solved: 287
[Submit][Status][Discuss]

3 2

6

#### HINT

【样例说明】

【数据说明】
对于100%的数据，1≤N≤1000000；0≤K≤N；

ans=C(n,k)*sum(C(n-k,i)*(2^(2^(n-i-k))-1))  0<=i<=n-k
i=0是任意选的方案数

https://www.cnblogs.com/candy99/p/6613808.html

/*

ans=C(n,k)*sum(C(n-k,i)*(2^(2^(n-i-k))-1))  0<=i<=n-k
i=0是任意选的方案数

https://www.cnblogs.com/candy99/p/6613808.html
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define ll long long
#define N 1000100
#define mod 1000000007
using namespace std;
int fac[N],n,k,now=2;

ll quick(int a,int b){
ll c=1;
while(b){
if(b&1)c=(c*a)%mod;
a=(1ll*a*a)%mod;b>>=1;
}
return c;
}

int C(int n,int m){
int ans=fac[n];
ll div1=quick(fac[m],mod-2);
ll div2=quick(fac[n-m],mod-2);
ans=(ans*div1)%mod;
ans=(ans*div2)%mod;
return ans;
}
int main(){
scanf("%d%d",&n,&k);
fac[0]=1;
for(int i=1;i<=n;i++)
fac[i]=(1ll*fac[i-1]*i)%mod;
n-=k;ll ans=0;
for(int i=n;~i;i--){
(ans+=1ll*(i&1?-1:1)*C(n,i)*(now-1))%=mod;
now=(1ll*now*now)%mod;
}
ans=(ans*C(n+k,k))%mod;
ans<0?ans+=mod:1;
cout<<ans;
return 0;
}

If you live in the echo,
your heart never beats as loud.

posted @ 2017-12-10 22:12  _wsy  阅读(131)  评论(0编辑  收藏  举报