# 【CF932E】Team Work-二项式反演+第二类斯特林数

$f\left(n\right)=\sum _{i=0}^{n}\left(-1{\right)}^{i}\cdot {C}_{n}^{i}\cdot g\left(i\right)$$g\left(n\right)=\sum _{i=0}^{n}\left(-1{\right)}^{i}\cdot {C}_{n}^{i}\cdot f\left(i\right)$等价，或：
$f\left(n\right)=\sum _{i=0}^{n}{C}_{n}^{i}\cdot g\left(i\right)$$g\left(n\right)=\sum _{i=0}^{n}\left(-1{\right)}^{n-i}\cdot {C}_{n}^{i}\cdot f\left(i\right)$等价。

$S\left(n,m\right)=\frac{1}{m!}\sum _{i=0}^{m}\left(-1{\right)}^{i}\cdot {C}_{m}^{i}\cdot \left(m-i{\right)}^{n}$

$m!S\left(n,m\right)=\sum _{i=0}^{m}\left(-1{\right)}^{i}\cdot {C}_{m}^{i}\cdot \left(m-i{\right)}^{n}$
$m-i$替换$i$，得：
$m!S\left(n,m\right)=\sum _{i=0}^{m}\left(-1{\right)}^{m-i}\cdot {C}_{m}^{i}\cdot {i}^{n}$

${m}^{n}=\sum _{i=0}^{n}{C}_{m}^{i}\cdot i!\cdot S\left(n,i\right)$

$ans=\sum _{i=1}^{n}{C}_{n}^{i}\cdot {i}^{k}$
$=\sum _{i=0}^{n}{C}_{n}^{i}\sum _{j=0}^{k}{C}_{i}^{j}\cdot j!\cdot S\left(k,j\right)$（这一步在$k=0$时会多出一个$1$，最后特判减去即可）

$ans=\sum _{j=0}^{k}S\left(k,j\right)\cdot j!\sum _{i=0}^{n}{C}_{n}^{i}{C}_{i}^{j}$

$ans=\sum _{j=0}^{k}S\left(k,j\right)\cdot j!\cdot {C}_{n}^{j}\cdot {2}^{n-j}$
$=\sum _{j=0}^{k}S\left(k,j\right)\cdot \frac{n!}{\left(n-j\right)!}\cdot {2}^{n-j}$

$S\left(k,j\right)=\frac{1}{j!}\sum _{i=0}^{j}\left(-1{\right)}^{i}\cdot {C}_{j}^{i}\cdot \left(j-i{\right)}^{k}$
$=\sum _{i=0}^{j}\frac{\left(-1{\right)}^{i}}{i!}\cdot \frac{\left(j-i{\right)}^{k}}{\left(j-i\right)!}$

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1000000007;
ll n,k,S[5010][5010]={0};

ll power(ll a,ll b)
{
ll s=1,ss=a;
while(b)
{
if (b&1) s=s*ss%mod;
b>>=1;ss=ss*ss%mod;
}
return s;
}

int main()
{
scanf("%lld%lld",&n,&k);

S[0][0]=1;
for(ll i=1;i<=k;i++)
for(ll j=1;j<=i;j++)
S[i][j]=(S[i-1][j-1]+j*S[i-1][j])%mod;

ll s1=1,s2=power(2,n),inv=500000004,ans=0;
for(ll i=0;i<=k;i++)
{
ans=(ans+S[k][i]*s1%mod*s2)%mod;
s1=s1*(n-i)%mod;
s2=s2*inv%mod;
}
if (k) printf("%lld",ans);
else printf("%lld",(ans-1+mod)%mod);

return 0;
}
posted @ 2018-06-16 23:10  Maxwei_wzj  阅读(132)  评论(0编辑  收藏  举报