# bzoj 2839 集合计数

http://www.cnblogs.com/hzoi-wangxh/p/7738616.html

2839: 集合计数

Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 500 Solved: 274
[Submit][Status][Discuss]
Description

Input

Output

Sample Input

3 2
Sample Output

6
HINT

【样例说明】

N<=1000000,k<=N;

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define int long long
int jie[2001000],ny[2001000],mod=1000000007;
int n,k,ans;
int ksm(int,int,int);
int getc(int,int);
signed main()
{
//  freopen("in.txt","r",stdin);
scanf("%lld%lld",&n,&k);
int f=1;
ny[0]=jie[0]=1;
for(int i=1;i<=n;i++)
jie[i]=jie[i-1]*i%mod;
ny[n]=ksm(jie[n],mod-2,mod);
for(int i=n-1;i>=1;i--)
ny[i]=ny[i+1]*(i+1)%mod;
for(int i=k;i<=n;i++)
{
int l=getc(n-k,i-k);
int x1=ksm(2,n-i,mod-1);
int x2=ksm(2,x1+mod-1,mod);
ans=(ans+f*l*(x2-1)%mod+mod)%mod;
f=-f;
}
ans*=getc(n,k);
printf("%lld",ans%mod);
return 0;
}
int ksm(int x,int y,int p)
{
int z=x,sum=1;
while(y)
{
if((y&1)==1)
sum=sum*z%p;
y=y>>1;
z=z*z%p;
}
return sum;
}
int getc(int x,int y)
{
return jie[x]*ny[y]%mod*ny[x-y]%mod;
}
posted @ 2017-10-18 21:14  hzoi_wangxh  阅读(153)  评论(0编辑  收藏  举报