[BZOJ3684] 大朋友和多叉树

做法

设点权为 \(k\) 的树有 \(a_k\) 棵,设 \(A(x) = \sum_{i\ge 1} a_i x^i\)

\(D(x) = \sum x^{d_i}\)

\(A(x) = D(A(x)) + x\)。设 \(C(x) = x - D(x)\),则 \(C(A(x)) = x\)

其中 \(C\)\(A\) 都是没有常数项且一次项为 \(1\) 的多项式。我们要求的是 \([x^s] A(x)\),作拉格朗日反演:\([x^s] A(x) = \frac 1 s [x^{-1}] \frac 1 {C^s(x)}\)

\(C(x) = xP(x)\),则 \([x^s]A(x) = \frac 1 s [x^{s-1}] \frac 1 {P^s(x)}\)

求个逆然后多项式快速幂一下即可,也可以先 \(\ln\)\(\exp\)

BZOJ 上快速幂 T 了,但是贴了个板子改成 \(\ln\) + \(\exp\) 就过了,看了确实是比快速幂快的。

(BZOJ 确实太慢了)

代码

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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <bits/stdc++.h>

using namespace std;

const int mod = 950009857;
const int maxn = 100010;

int g = 0;
int s, m;

int wa[maxn * 8], wb[maxn * 8], wc[maxn * 8], rev[maxn * 8];
int wl[21];

vector<int> vp;

int qpow(int x, int y) {
int ret = 1;
while (y) {
if (y & 1) {
ret = 1LL * ret * x % mod;
}
x = 1LL * x * x % mod;
y >>= 1;
}
return ret;
}

int checkg(int g) {
int ok = 1;
for (int i = 0; i < vp.size(); i++) {
int p = vp[i];
if (qpow(g, (mod - 1) / p) == 1) {
ok = 0;
}
}
return ok;
}

struct poly {
int *a, len;
poly (int len_=0) {
len = len_;
a = new int [len];
for (int i = 0; i < len; i++) {
a[i] = 0;
}
}
};

inline int mo(int x) {
if (x >= mod) {
return x - mod;
} else {
return x;
}
}

void ntt(int *a, int _l, int ty) {
int l = (1 << _l);
for (int i = 1; i < l; i++) {
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (_l - 1));
}
for (int i = 0; i < l; i++) {
if (i < rev[i]) {
swap(a[i], a[rev[i]]);
}
}
int _ = 0;
for (int len = 2; len <= l; len <<= 1) {
int _wl = wl[++ _];
for (int s = 0; s < l; s += len) {
int w = 1;
for (int i = 0; i < (len >> 1); i++) {
int v1 = a[s + i], v2 = 1LL * a[s + i + (len >> 1)] * w % mod;
a[s + i] = mo(v1 + v2);
a[s + i + (len >> 1)] = mo(v1 + mod - v2);
w = 1LL * w * _wl % mod;
}
}
}
if (ty == -1) {
int inv = qpow(l, mod-2);
for (int i = 0; i < l; i++) {
a[i] = 1LL * inv * a[i] % mod;
}
for (int i = 1; i < l / 2; i++) {
swap(a[i], a[l - i]);
}
}
}

poly operator*(const poly &p1, const poly &p2) {
poly ret(p1.len + p2.len - 1);
int _l = 0;
while ((1 << _l) < ret.len) {
++ _l;
}
int l = (1 << _l);
for (int i = 0; i < l; i++) {
wa[i] = wb[i] = wc[i] = 0;
}
for (int i = 0; i < p1.len; i++) {
wa[i] = p1.a[i];
}
for (int i = 0; i < p2.len; i++) {
wb[i] = p2.a[i];
}
ntt(wa, _l, 1), ntt(wb, _l, 1);
for (int i = 0; i < l; i++) {
wc[i] = 1LL * wa[i] * wb[i] % mod;
}
ntt(wc, _l, -1);
for (int i = 0; i < ret.len; i++) {
ret.a[i] = wc[i];
}
return ret;
}

poly qpow(poly p, int x) {
int l = p.len;
poly ret(1); ret.a[0] = 1;
while (x) {
if (x & 1) {
ret = ret * p;
ret.len = l;
}
x >>= 1;
p = p * p;
p.len = l;
}
return ret;
}

poly polyInv(const poly &p) {
if (p.len == 1) {
poly ret(1);
ret.a[0] = qpow(p.a[0], mod-2);
return ret;
}
int tl = (p.len + 1) >> 1;
poly p0 (tl);
for (int i = 0; i < tl; i++) {
p0.a[i] = p.a[i];
}
poly r0 = polyInv(p0);
poly v0 = r0 * p; v0.a[0] = (v0.a[0] + mod - 2) % mod;
for (int i = 0; i < p.len; i++) {
v0.a[i] = (mod - v0.a[i]) % mod;
}
v0.len = p.len;
poly v1 = v0 * r0;
v1.len = p.len;
return v1;
}

poly polyLn(const poly &p) {
if (p.len == 1) return poly(1);
poly inv = polyInv(p);
poly d(p.len);
for (int i = 0; i+1 < p.len; i++) {
d.a[i] = 1LL*p.a[i+1]*(i+1)%mod;
}
inv = inv * d;
poly ret(p.len);
for (int i = 1; i < ret.len; i++) {
ret.a[i] = 1LL*qpow(i, mod-2)*inv.a[i-1]%mod;
}
return ret;
}

poly polyExp(const poly &p) {
if (p.len == 1) {
poly ret(1);
ret.a[0] = 1;
return ret;
}
int nlen = (p.len+1)/2;
poly tmp(nlen); for (int i = 0; i < nlen; i++) tmp.a[i] = p.a[i];
poly G = polyExp(tmp);
poly t(p.len), v(p.len);
for (int i = 0; i < nlen; i++) v.a[i] = G.a[i];
v = polyLn(v);
for (int i = 0; i < p.len; i++) {
t.a[i] = (mod - v.a[i] + p.a[i]) % mod;
}
t.a[0] = (t.a[0] + 1) % mod;
t = t*G;
poly ret(p.len);
for (int i = 0; i < ret.len; i++) ret.a[i] = t.a[i];
return ret;
}

int main() {
{
int _ = mod - 1;
for (int i = 2; i * i <= _; i++) {
if (_ % i == 0) {
vp.push_back(i);
while (_ % i == 0) {
_ /= i;
}
}
}
if (_ > 1) {
vp.push_back(_);
}
for (int i = 2; i <= mod; i++) {
if (checkg(i)) {
g = i;
break;
}
}
}
for (int i = 0; i <= 20; i++) {
wl[i] = qpow(g, (mod - 1) / (1 << i));
}
scanf("%d%d", &s, &m);
poly P(s);
for (int i = 1; i <= m; i++) {
int x = 0; scanf("%d", &x);
P.a[x - 1] = mod - 1;
}
P.a[0] = 1;
poly lnP = polyLn(P);
int t = (mod - s) % mod;
for (int i = 0; i < lnP.len; i++) {
lnP.a[i] = 1LL * lnP.a[i] * t % mod;
}
poly res = polyExp(lnP);
int ans = 1LL * qpow(s, mod-2) * res.a[s-1] % mod;
printf("%d\n", ans);
return 0;
}