codeforces932E Team Work

时间:2023-03-08 23:10:58
codeforces932E Team Work

题目链接:CF932E

由第二类斯特林数知

\[n^m=\sum_{i=0}^nS(m,i)*i!*\dbinom{n}{i}
\]

\[\begin{aligned}
\sum_{i=1}^n \dbinom{n}{i}i^k&=\sum_{i=1}^n\dbinom{n}{i}\sum_{j=0}^iS(k,j)*j!*\dbinom{i}{j}\\
&=\sum_{i=1}^n\frac{n!}{i!(n-i)!}\sum_{j=0}^iS(k,j)*j!*\frac{i!}{(i-j)!j!}\\
&=\sum_{i=1}^n\frac{n!}{(n-i)!}\sum_{j=0}^iS(k,j)*\frac{1}{(i-j)!}\\
&=\sum_{j=0}^nS(k,j)\sum_{i=j}^n\frac{n!}{(n-i)!(i-j)!}\\
&=\sum_{j=0}^nS(k,j)\sum_{i=j}^nn^{\underline{j}}\dbinom{n-j}{n-i}\\
&=\sum_{j=0}^kS(k,j)n^{\underline{j}}\sum_{i=j}^n\dbinom{n-j}{n-i}\\
&=\sum_{j=0}^kS(k,j)n^{\underline{j}}2^{n-j}
\end{aligned}
\]

\(O(k^2)\)预处理第二类斯特林数,\(O(k)\)计算答案,沿途维护\(n^\underline{j}\)和\(2^{n-j}\)即可

#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define lowbit(x) (x)&(-x)
#define rep(i,a,b) for (int i=a;i<=b;i++)
#define per(i,a,b) for (int i=a;i>=b;i--)
#define maxd 1000000007
#define inv2 500000004
typedef long long ll;
const double pi=acos(-1.0);
int n,k,N;
ll s[5050][5050]; int read()
{
int x=0,f=1;char ch=getchar();
while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
return x*f;
} ll qpow(ll x,int y)
{
ll ans=1;
while (y)
{
if (y&1) ans=(ans*x)%maxd;
x=(x*x)%maxd;
y>>=1;
}
return ans;
} int main()
{
n=read();k=read();
N=min(n,k);s[0][0]=1;
rep(i,1,k)
{
rep(j,1,k) s[i][j]=(s[i-1][j-1]+s[i-1][j]*j)%maxd;
}
ll bin=qpow(2,n);
ll ans=0,now=1;
rep(i,0,N)
{
ans=(ans+s[k][i]*now%maxd*bin%maxd)%maxd;
now=(now*(n-i))%maxd;bin=(bin*inv2)%maxd;
}
printf("%lld",ans);
return 0;
}