Codeforces 932 E Team Work
Discription
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.
Output the sum of costs over all non-empty subsets of people.
Input
Only line of input contains two integers N (1?≤?N?≤?109) representing total number of people and k
Output
Output the sum of costs for all non empty subsets modulo 109?+?7.
Example
Input1 1Output
1Input
3 2Output
24
Note
In the first example, there is only one non-empty subset {1} with cost 11?=?1.
In the second example, there are seven non-empty subsets.
- {1} with cost 12
- {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遞推斯特林數了。
#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; }
Codeforces 932 E Team Work