Codeforces 932 E Team Work

Discriptionhtml

You have a team of N people. For a particular task, you can pick any non-empty subset of people. The cost of having x people for the task is xk.ios

Output the sum of costs over all non-empty subsets of people.spa

Inputcode

Only line of input contains two integers N (1 ≤ N ≤ 109) representing total number of people and k (1 ≤ k ≤ 5000).htm

Outputblog

Output the sum of costs for all non empty subsets modulo 109 + 7.ip

Exampleci

Input
1 1
Output
1
Input
3 2
Output
24

Noteinput

In the first example, there is only one non-empty subset {1} with cost 11 = 1.string

In the second example, there are seven non-empty subsets.

{1} with cost 12 = 1

{2} with cost 12 = 1

{1, 2} with cost 22 = 4

{3} with cost 12 = 1

{1, 3} with cost 22 = 4

{2, 3} with cost 22 = 4

{1, 2, 3} with cost 32 = 9

The total cost is 1 + 1 + 4 + 1 + 4 + 4 + 9 = 24.

 

題目大意就是要你求一下ΣC(n,i)*i^k。

而後直接上我推的式子了(就是用第二類斯特林數代換一下)

 

(怎麼這個圖這麼大。。。。無論了)

而後就開開心心A了。

不過好像k再大一點也能夠作,,,就是要用FFT 在N log N的時間求出某一行的斯特林數了(反正我也不會hhhh),不能再N^2遞推斯特林數了。

 

(後記:如今會NTT 在 N log N求某一行斯特林數了,請見 : http://www.cnblogs.com/JYYHH/p/8641094.html)

 

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define ll long long
#define maxn 5005
using namespace std;
const int ha=1000000007;
const int inv=ha/2+1;
int S[maxn],n,m;
int ans=0,tmp,ci;

inline void init(){
	S[1]=1;
	for(int i=2;i<=m;i++)
	    for(int j=i;j;j--){
	    	S[j]=(S[j]*(ll)j+(ll)S[j-1])%ha;
		}
}

inline int ksm(int x,int y){
	int an=1;
	for(;y;y>>=1,x=x*(ll)x%ha) if(y&1) an=an*(ll)x%ha;
	return an;
}

inline void solve(){
	tmp=1,ci=ksm(2,n);
	for(int i=1;i<=m;i++){
		tmp=tmp*(ll)(n-i+1)%ha;
		ci=ci*(ll)inv%ha;
		ans=((ll)ans+S[i]*(ll)tmp%ha*(ll)ci)%ha;
	}
}

int main(){
	scanf("%d%d",&n,&m);
	init();
	solve();
	printf("%d\n",ans);
	return 0;
}
相關文章
相關標籤/搜索