「CF932E」Team Work
「CF932E」Team Work
Solution
题意:求\(\sum_{i=1}^n\begin{pmatrix}n\\i\end{pmatrix}\times i^k\)
大力颓柿子,根据常幂转下降幂公式,有
\[\sum_{i=1}^n\begin{pmatrix}n\\i\end{pmatrix}\times i^k
\]
\[=\sum_{i=1}^n\begin{pmatrix}n\\i\end{pmatrix}\sum_{j=0}^i\begin{Bmatrix}k\\j\end{Bmatrix}\times i^{\underline j}
\]
\[=\sum_{i=1}^n\begin{pmatrix}n\\i\end{pmatrix}\sum_{j=0}^i\begin{Bmatrix}k\\j\end{Bmatrix}\times j!\begin{pmatrix}i\\j\end{pmatrix}
\]
\[=\sum_{j=0}^{min(n,k)}\begin{Bmatrix}k\\j\end{Bmatrix}\sum_{i=j}^n\frac{n!}{i!(n-i)!}\times j! \times \frac{i!}{j!(i-j)!}
\]
\[=\sum_{j=0}^{min(n,k)}\begin{Bmatrix}k\\j\end{Bmatrix}\sum_{i=j}^n\frac{n!}{(n-i)!}\times \frac{1}{(i-j)!}
\]
后面那个求和上下同乘\((n-j)!\),有
\[\sum_{j=0}^{min(n,k)}\begin{Bmatrix}k\\j\end{Bmatrix}\sum_{i=j}^n\frac{n!}{(n-i)!}\times \frac{1}{(i-j)}\times\frac{(n-j)!}{(n-j)!}
\]
\[=\sum_{j=0}^{min(n,k)}\begin{Bmatrix}k\\j\end{Bmatrix}\frac{n!}{(n-j)!}\sum_{i=j}^n\begin{pmatrix}n-j\\n-i\end{pmatrix}
\]
\(i<j\)的时候组合数为\(0\),不妨把\(i\)的下界变为\(0\),于是有
\[\sum_{j=0}^{min(n,k)}\begin{Bmatrix}k\\j\end{Bmatrix}\frac{n!}{(n-j)!}\sum_{i=0}^n\begin{pmatrix}n-j\\n-i\end{pmatrix}
\]
\[=\sum_{j=0}^{min(n,k)}\begin{Bmatrix}k\\j\end{Bmatrix}\frac{n!}{(n-j)!}\times 2^{n-j}
\]
于是我们可以\(O(k^2)\)递推求出第二类斯特林数,枚举\(j\)时迭代计算\(\frac{n!}{(n-j)!}\),快速幂计算\(2^{n-j}\)即可
Code
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#define inv(x) (fastpow((x),mod-2))
using namespace std;
typedef long long ll;
template <typename T> void read (T &t)
{
t=0;int f=0;char c=getchar();
while(!isdigit(c)){f|=c=='-';c=getchar();}
while(isdigit(c)){t=t*10+c-'0';c=getchar();}
if(f)t=-t;
}
const int maxk=5000+5;
const ll mod=1e9+7;
ll n,k;
ll s[maxk][maxk];
ll fastpow(ll a,ll b)
{
ll re=1,base=a;
while(b)
{
if(b&1)
re=re*base%mod;
base=base*base%mod;
b>>=1;
}
return re;
}
int main()
{
read(n),read(k);
s[0][0]=1;
for(register ll i=1;i<=k;++i)
for(register ll j=1;j<=i;++j)
s[i][j]=(s[i-1][j-1]+j*s[i-1][j]%mod)%mod;
ll kkk=1,ans=0;
for(register ll j=0;j<=min(n,k);++j)
{
ans=(ans+s[k][j]*kkk%mod*fastpow(2,n-j)%mod)%mod;
kkk=kkk*(n-j)%mod;
}
printf("%lld",ans);
return 0;
}