Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P4827 [国家集训队] Crash 的文明世界

P4827 [国家集训队] Crash 的文明世界

Algorithm I

考虑点分治,每一次考虑跨越当前点的答案,对于一棵子树,对于子树外记录 $cnt _ i$ 表示深度为 $i$ 的点的个数有多少个。那么子树内深度为 $d$ 的答案为 $ans _ d = \sum _ {i} cnt _ i (i + d) ^ k$ ,发现两项差为点值,可以卷积。于是可以在 $O(n \log ^ 2 n)$ 内完成。

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <cstdio>
#include <vector>
#define pb push_back
using namespace std;
template <class Type>
void read (Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0';
flag && (x = ~x + 1);
}
template <class Type, class ...rest>
void read (Type &x, rest &...y) { read(x), read(y...); }
template <class Type>
void write (Type x)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar('0' + x % 10);
}
typedef long long LL;
const int N = 5e4 + 10, mod = 1e4 + 7;
namespace Mul
{
const int N = 2e5 + 10, mod = 998244353;
int rev[N];
int binpow (int b, int k = mod - 2)
{
int res = 1;
for (; k; k >>= 1, b = (LL)b * b % mod)
if (k & 1) res = (LL)res * b % mod;
return res;
}
void ntt (int *x, int bit, int op)
{
int tot = 1 << bit;
for (int i = 0; i < tot; ++i)
if ((rev[i] = rev[i >> 1] >> 1 | (i & 1) << bit - 1) > i)
swap(x[i], x[rev[i]]);
for (int mid = 1; mid < tot; mid <<= 1)
{
int w1 = binpow(3, (mod - 1) / (mid << 1));
if (!~op) w1 = binpow(w1);
for (int i = 0; i < tot; i += mid << 1)
for (int j = 0, k = 1; j < mid; ++j, k = (LL)k * w1 % mod)
{
int p = x[i | j], q = (LL)k * x[i | mid | j] % mod;
x[i | j] = (p + q) % mod, x[i | mid | j] = (p - q) % mod;
}
}
if (~op) return;
int itot = binpow(tot);
for (int i = 0; i < tot; ++i)
x[i] = (LL)x[i] * itot % mod;
}
void PolyMerge (int n, int *f, int *g, int *w)
{
int bit = 0;
while (1 << bit < n + n - 1) ++bit;
int tot = 1 << bit;
static int A[N], B[N], C[N];
for (int i = 0; i < n; ++i) A[i] = f[n - i - 1];
for (int i = n; i < tot; ++i) A[i] = 0;
for (int i = 0; i < n; ++i) B[i] = g[i];
for (int i = n; i < tot; ++i) B[i] = 0;
ntt(A, bit, 1), ntt(B, bit, 1);
for (int i = 0; i < tot; ++i)
C[i] = (LL)A[i] * B[i] % mod;
ntt(C, bit, -1);
for (int i = 0; i < n; ++i)
w[i] = (C[n - i - 1] + mod) % mod;
}
}
bool vis[N];
vector <int> g[N];
int n, m, ans[N], v[N], h[N], cnt[N];
int binpow (int b, int k)
{
int res = 1;
for (; k; k >>= 1, b = (LL)b * b % mod)
if (k & 1) res = (LL)res * b % mod;
return res;
}
int WeightCentre (int x, int fa, int tot, int &wc)
{
if (vis[x]) return 0;
int sz = 1, mx = 0;
for (int i : g[x]) if (i ^ fa)
{
int t = WeightCentre(i, x, tot, wc);
mx = max(mx, t), sz += t;
}
mx = max(mx, tot - sz);
if (mx <= tot / 2) wc = x;
return sz;
}
int Size (int x, int fa)
{
if (vis[x]) return 0;
int sz = 1;
for (int i : g[x]) if (i ^ fa)
sz += Size(i, x);
return sz;
}
void add (int x, int fa, int d, int &mx)
{
if (vis[x]) return;
mx = max(mx, d);
++cnt[d++];
for (int i : g[x]) if (i ^ fa)
add(i, x, d, mx);
}
void del (int x, int fa, int d)
{
if (vis[x]) return;
--cnt[d++];
for (int i : g[x]) if (i ^ fa)
del(i, x, d);
}
void get (int x, int fa, int d)
{
if (vis[x]) return;
ans[x] = (ans[x] + h[d]) % mod;
++cnt[d++];
for (int i : g[x]) if (i ^ fa)
get(i, x, d);
}
void calc (int x)
{
if (vis[x]) return;
WeightCentre(x, 0, Size(x, 0), x);
int d = 0;
add(x, 0, 0, d);
vis[x] = true;
for (int i = 1; i <= d; ++i)
ans[x] = (ans[x] + cnt[i] * v[i]) % mod;
for (int i : g[x])
{
del(i, x, 1);
Mul::PolyMerge(d * 2 + 1, v, cnt, h);
get(i, x, 1);
}
for (int i = 0; i <= d; ++i) cnt[i] = 0;
for (int i : g[x]) calc(i);
}
int main ()
{
read(n, m);
for (int i = 1; i <= n; ++i)
v[i] = binpow(i, m);
for (int i = 1, a, b; i < n; ++i)
read(a, b), g[a].pb(b), g[b].pb(a);
calc(1);
for (int i = 1; i <= n; ++i)
write(ans[i]), puts("");
return 0;
}

Algorithm II

注意到幂的形式以及 $k$ 并不大,考虑将其拆为斯特林数和下降幂。

$$
\begin {aligned}
S(x) & = \sum _ {j = 1} ^ n d(x, j) ^ k \\
& = \sum _ {j = 1} ^ n \sum _ {i = 0} ^ k {k \brace i} d(x, j) ^ {\underline i} \\
& = \sum _ {i = 0} ^ k i! {k \brace i} \sum _ {j = 1} ^ n \binom {d(x, j)} i
\end {aligned}
$$

考虑 $\binom n m $ 的递推式,$\binom n m = \binom {n - 1} {m - 1} + \binom {n - 1} m$ 。考虑每个点为根的情况,结合 $d(x, j)$ 的意义,记 $f _ {i, j}$ 表示 $i$ 子树内,组合数下标为 $j$ 的和,那么有 $f _ {i, j} = \sum _ {k \in son _ i} f _ {i, j} + f _ {i, j - 1} $ 。得到 $1$ 为根的答案后,换根DP即可。

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <cstdio>
#include <vector>
#define pb push_back
using namespace std;
template <class Type>
void read (Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0';
flag && (x = ~x + 1);
}
template <class Type, class ...rest>
void read (Type &x, rest &...y) { read(x), read(y...); }
template <class Type>
void write (Type x)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar('0' + x % 10);
}
const int N = 5e4 + 10, M = 160, mod = 1e4 + 7;
int n, m, f[N][M], g[N][M], s[M][M], fact[M];
vector <int> e[N];
void init ()
{
s[0][0] = 1;
for (int i = 1; i <= m; ++i)
for (int j = 1; j <= m; ++j)
s[i][j] = (s[i - 1][j - 1] + j * s[i - 1][j]) % mod;
fact[0] = 1;
for (int i = 1; i <= m; ++i)
fact[i] = fact[i - 1] * i % mod;
}
void dfs1 (int u = 1, int fa = 0)
{
f[u][0] = 1;
for (int v : e[u]) if (v ^ fa)
{
dfs1(v, u);
(f[u][0] += f[v][0]) %= mod;
for (int i = 1; i <= m; ++i)
(f[u][i] += f[v][i] + f[v][i - 1]) %= mod;
}
}
void dfs2 (int u = 1, int fa = 0)
{
for (int v : e[u]) if (v ^ fa)
{
static int t[N];
t[0] = (g[u][0] - f[v][0]);
for (int i = 1; i <= m; ++i)
t[i] = (g[u][i] - f[v][i] - f[v][i - 1]) % mod;
g[v][0] = (f[v][0] + t[0]);
for (int i = 1; i <= m; ++i)
g[v][i] = (f[v][i] + t[i] + t[i - 1]) % mod;
dfs2(v, u);
}
}
int main ()
{
read(n, m);
init();
for (int i = 1, a, b; i < n; ++i)
read(a, b), e[a].pb(b), e[b].pb(a);
dfs1();
for (int i = 0; i <= m; ++i) g[1][i] = f[1][i];
dfs2();
for (int i = 1; i <= n; ++i)
{
int res = 0;
for (int j = 0; j <= m; ++j)
res = (res + s[m][j] * fact[j] % mod * g[i][j]) % mod;
write((res + mod) % mod), puts("");
}
return 0;
}