bzoj权限题

# 思路

$S(i)=\sum_{j=1}ndis(i,j)m=\sum_{j=1}n\sum_{k=1}m \begin{Bmatrix} m \\ k \end{Bmatrix} \binom{dis(i,j)}{k}k!$

$S(i)=\sum_{k=1}^m \begin{Bmatrix} m \\ k \end{Bmatrix} k! \sum_{j=1}^n\binom{dis(i,j)}{k}$

# Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cassert>
#include<cmath>
#define ll long long
#define MOD 10007
using namespace std;
int re=0,flag=1;char ch=getchar();
while(ch>'9'||ch<'0'){
if(ch=='-') flag=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') re=(re<<1)+(re<<3)+ch-'0',ch=getchar();
return re*flag;
}
int n,m,first[50010],cnte,s[200][200],fac[200];
struct edge{
int to,next;
}a[100010];
a[++cnte]=(edge){v,first[u]};first[u]=cnte;
a[++cnte]=(edge){u,first[v]};first[v]=cnte;
}
void init(){//预处理阶乘和斯特林数
int i,j;fac[0]=fac[1]=1;
for(i=2;i<=m;i++) fac[i]=fac[i-1]*i%MOD;
s[0][0]=1;
for(i=1;i<=m;i++){
for(j=0;j<=i;j++){
s[i][j]=s[i-1][j-1]+s[i-1][j]*j;s[i][j]%=MOD;
}
}
}
int down[50010][210],up[50010][210],ans[50010];
void dfs1(int u,int f){
int i,v,k;
down[u][0]=1;
for(i=first[u];~i;i=a[i].next){
v=a[i].to;if(v==f) continue;
dfs1(v,u);
down[u][0]+=down[v][0];//down[u][0]相当于siz[u]，可以自己证证
for(k=1;k<=m;k++) down[u][k]+=down[v][k]+down[v][k-1],down[u][k]%=MOD;
}
}
void dfs2(int u,int f){
int i,v,k;
if(!f) goto skip;//没有fa，哪来的up?
up[u][0]=n-down[u][0];
for(k=1;k<=m;k++){
up[u][k]=up[f][k]+up[f][k-1]+down[f][k]+down[f][k-1]-down[u][k]-down[u][k-1]-down[u][k-1];
if(k>1) up[u][k]-=down[u][k-2];
up[u][k]=(up[u][k]%MOD+MOD)%MOD;
}
skip:
for(i=first[u];~i;i=a[i].next){
v=a[i].to;if(v==f) continue;
dfs2(v,u);
}
}
int main(){
memset(first,-1,sizeof(first));