Crash的文明世界
作者:互联网
description
给出一个\(n\) 个点的树,对于每个点\(u\) 求
\[S(u)=\sum_{i=1}^ndis(i,u)^k \]data range
\(n\le 50000,k\le 150\)
solution
先来推式子
\[S(u)=\sum_{i=1}^ndis(i,u)^k\\ =\sum_{i=1}^n\sum_{j=0}^k\left\{\begin{matrix}k\\j\end{matrix}\right\}dis(i,u)^{\underline j}\\ =\sum_{j=0}^kj!\left\{\begin{matrix}k\\j\end{matrix}\right\}\sum_{i=1}^n\binom {dis(i,u)}{j} \]因此只需对每个点\(u\) 和次数\(j\) 求出\(\sum_{i=1}^n\binom {dis(i,u)}j\) 就可以了
记\(f_{u,j}=\sum_{i=1}^n\binom {dis(i,u)}j\) ,\(up_{u,j}=\sum_{i\notin u}\binom {dis(i,u)}j\) ,\(down_{u,j}=\sum_{i\in u}\binom {dis(i,u)}j\) 显然\(f_{u,j}=up_{u,j}+down_{u,j}\)
注意组合数有很好的性质
\[\binom nm=\binom{n-1}m+\binom{n-1}{m-1} \]先考虑\(down_{u,j}\) (简单些)
\[down_{u,j}=\sum_{i\in u}\binom{dis(i,u)}j\\ =[j=0]+\sum_{i\in u}(\binom{dis(i,u)-1}{j}+\binom{dis(i,u)-1}{j-1})\\ =[j=0]+\sum_{i\in u}(\binom{dis(i,v)}{j}+\binom{dis(i,v)}{j-1})\\ =[j=0]+\sum_{i\in u}(down_{v,j}+down_{v,j-1}) \]再考虑\(up_{u,j}\)
\[up_{u,j}=\sum_{i\notin u}\binom{dis(i,u)}j\\ =\sum_{i\notin fa_u}\binom{dis(i,fa_u)+1}{j}+\sum_{i\in fa_u}\binom{dis(i,fa_u)+1}j-\sum_{i\in u}\binom{dis(i,u)+2}j\\ =up_{fa_u,j}+up_{fa_u,j-1}+down_{fa_u,j}+down_{fa_u,j-1}-down_{u,j}-2down_{u,j-1}-down_{u,j-2} \]注意边界上需要特殊处理
这类题目只是外面套了个斯特林数的外壳,本质还是\(dp\) 之类的传统东西
普通幂的差分十分困难(即便预处理组合数也需要\(\mathcal O(k)\)),而斯特林数将普通幂转为下降幂使得其差分变得简单(\(\mathcal O(1)\))
time complexity
\(\mathcal O(nk)\)
code
#include<bits/stdc++.h>
using namespace std;
const int N=5e4+5,K=155,mod=1e4+7;
int f[N][K],s2[K][K],n,k;
int tot,fi[N],ne[N<<1],to[N<<1];
inline int read()
{
int s=0,w=1; char ch=getchar();
for(;!isdigit(ch);ch=getchar())if(ch=='-')w=-1;
for(;isdigit(ch);ch=getchar())s=(s<<1)+(s<<3)+(ch^48);
return s*w;
}
inline void adde(int x,int y){ne[++tot]=fi[x],fi[x]=tot,to[tot]=y;}
inline int add(int x,int y){return x+y>=mod?x+y-mod:x+y;}
inline int dec(int x,int y){return x-y<0?x-y+mod:x-y;}
inline void pre()
{
s2[0][0]=1;
for(int i=1;i<=k;++i)
for(int j=1;j<=k;++j)
s2[i][j]=add(s2[i-1][j-1],s2[i-1][j]*j%mod);
}
int down[N][K],up[N][K];
void dfs1(int u,int f)
{
down[u][0]=1;
for(int i=fi[u];i;i=ne[i])
{
int v=to[i];
if(v==f)continue;
dfs1(v,u);
down[u][0]=add(down[u][0],down[v][0]);
for(int j=1;j<=k;++j)
down[u][j]=add(down[u][j],add(down[v][j],down[v][j-1]));
}
}
void dfs2(int u,int f)
{
int now[K];
now[0]=add(up[u][0],down[u][0]);
for(int j=1;j<=k;++j)
now[j]=(up[u][j]+up[u][j-1]+down[u][j]+down[u][j-1])%mod;
for(int i=fi[u];i;i=ne[i])
{
int v=to[i];
if(v==f)continue;
up[v][0]=dec(now[0],down[v][0]);
up[v][1]=dec(now[1],add(down[v][1],add(down[v][0],down[v][0])));
for(int j=2;j<=k;++j)
up[v][j]=dec(now[j],add(down[v][j],add(2*down[v][j-1]%mod,down[v][j-2])));
dfs2(v,u);
}
}
int main()
{
n=read(),k=read();
for(int i=1,u,v;i<n;++i)
{
u=read(),v=read();
adde(u,v),adde(v,u);
}
pre();dfs1(1,0);dfs2(1,0);
for(int i=1;i<=n;++i)
{
int ans=0,fac=1;
for(int j=0;j<=k;++j,fac=fac*j%mod)
ans=add(ans,fac*s2[k][j]%mod*add(up[i][j],down[i][j])%mod);
printf("%d\n",ans);
}
return 0;
}
标签:Crash,sum,世界,up,down,fa,文明,binom,dis 来源: https://www.cnblogs.com/zmyzmy/p/14403519.html