BZOJ2159

# 题解

$n^k = \sum\limits_{i = 0} \begin{Bmatrix} k \\ i \end{Bmatrix} {n \choose i}i!$

$f[i][j] = \sum\limits_{x = 1}^{N}{dis(i,x) \choose j}$

${n \choose m} = {n - 1 \choose m} + {n - 1 \choose m - 1}$

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 50005,maxm = 155,INF = 1000000000,P = 10007;
int f[maxn][maxm],g[maxm],n,K,fa[maxn];
int h[maxn],ne = 1;
struct EDGE{int to,nxt;}ed[maxn << 1];
inline void build(int u,int v){
ed[++ne] = (EDGE){v,h[u]}; h[u] = ne;
ed[++ne] = (EDGE){u,h[v]}; h[v] = ne;
}
void dfs1(int u){
f[u][0] = 1;
Redge(u) if ((to = ed[k].to) != fa[u]){
fa[to] = u; dfs1(to);
f[u][0] = f[u][0] + f[to][0];
for (int k = 1; k <= K; k++)
f[u][k] = (f[u][k] + f[to][k - 1] + f[to][k]) % P;
}
}
void dfs2(int u){
if (fa[u]){
int v = fa[u];
g[0] = f[v][0] - f[u][0];
for (int k = 1; k <= K; k++)
g[k] = ((f[v][k] - f[u][k] - f[u][k - 1]) % P + P) % P;
f[u][0] = n;
for (int k = 1; k <= K; k++)
f[u][k] = (f[u][k] + g[k] + g[k - 1]) % P;
}
Redge(u) if ((to = ed[k].to) != fa[u])
dfs2(to);
}
int S[maxm][maxm],ans,fac[maxm];
void work(){
S[0][0] = 1; fac[0] = 1;
for (register int i = 1; i <= K; i++) fac[i] = fac[i - 1] * i % P;
for (register int i = 1; i <= K; i++)
for (register int j = 1; j <= i; j++)
S[i][j] = (S[i - 1][j - 1] + j * S[i - 1][j]) % P;
for (register int i = 1; i <= n; i++){
ans = 0;
for (register int k = 0; k <= K; k++)
ans = (ans + f[i][k] * fac[k] % P * S[K][k] % P) % P;
printf("%d\n",ans);
}
}
int L,now,A,B,Q,tmp;
scanf("%d%d%d",&n,&K,&L);
scanf("%d%d%d%d",&now,&A, &B, &Q);
for (int i = 1; i < n; i++){
now = (now * A + B) % Q; tmp = (i < L) ? i : L;
build(i - now % tmp,i + 1);
}
}
int main(){