1. 程式人生 > >BZOJ 2839: 集合計數

BZOJ 2839: 集合計數

const pan std ret scan n-1 return mic microsoft

容斥

#include<cstdio>
using namespace std;
const int mod=1e9+7;
int mi[1000005],ni[1000005],tmp[1000005];
int C(int n,int m){
	return 1ll*mi[n]*ni[m]%mod*ni[n-m]%mod;
}
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	tmp[n]=1;
	for (int i=n-1; i>=k; i--) tmp[i]=1ll*tmp[i+1]*(tmp[i+1]+2)%mod;
	mi[0]=1;
	for (int i=1; i<=n; i++) mi[i]=1ll*mi[i-1]*i%mod;
	ni[0]=1;
	ni[1]=1;
	for (int i=2; i<=n; i++) ni[i]=1ll*ni[mod%i]*(mod-mod/i)%mod;
	for (int i=1; i<=n; i++) ni[i]=1ll*ni[i-1]*ni[i]%mod;
	int ans=0,f=1;
	for (int i=k; i<=n; i++){
		(ans+=1ll*f*C(i,k)%mod*C(n,i)%mod*tmp[i]%mod)%=mod;
		(ans+=mod)%=mod;
		f=-f;
	}
	printf("%d\n",ans);
	return 0;
}

  

BZOJ 2839: 集合計數