「THUPC 2017」sum

Problem

Description

给定一个长为 n 的数列 \{a_{n}\} ,定义数列 f

f_{k} = \sum_{i = 1}^{n} a_{i}^{k} \pmod{998244353}

现要求计算 f_{1}, f_{2}, \dots, f_{n} 的值。

Constraints

1 \le n \le 2 \times 10^{5}, 0 \le a_{i} \le 10^{9}

Solution

Analysis

设数列 \{f_{n}\} 的 OGF 为 F(x) ,那么有:

\begin{aligned} F(x) &= \sum_{i \ge 0} f_{i} x^{i} \\ &= \sum_{i \ge 0} x^{i} \sum_{j = 1}^{n} a_{j}^{i} \\ &= \sum_{i = 1}^{n} \sum_{j \ge 0} (a_{i} x)^{j} \\ &= \sum_{i = 1}^{n} \frac{1}{1 - a_{i} x} \end{aligned}

但是到这一步还是不能快速求出 F(x) 。考虑通过取对数来将求和变为求积,然后就可以分治 FFT 做了。

\begin{aligned} F(x) &= \sum_{i = 1}^{n} \frac{1}{1 - a_{i} x} \\ &= n - x \sum_{i = 1}^{n} \frac{-a_{i}}{1 - a_{i} x} \\ &= n - x \sum_{i = 1}^{n} \left(\ln{(1 - a_{i} x)}\right)' \\ &= n - x \left(\ln{\left(\prod_{i = 1}^{n} (1 - a_{i} x)\right)}\right)' \end{aligned}

这样我们就得到了一个可以通过分治 FFT 快速求出的形式,直接做就行了。

时间复杂度 O\left(n \log^{2}{n}\right)

Code

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
#include <cstdio>
#include <algorithm>

using i64 = long long;

namespace IOManager {
constexpr int FILESZ = 131072;
char buf[FILESZ];
const char *ibuf = buf, *tbuf = buf;

struct IOManager {
inline char gc() {
return (ibuf == tbuf) && (tbuf = (ibuf = buf) + fread(buf, 1, FILESZ, stdin), ibuf == tbuf) ? EOF : *ibuf++;
}

template <typename _Tp>
inline operator _Tp() {
_Tp s = 0u; char c = gc();
for (; c < 48; c = gc());
for (; c > 47; c = gc())
s = (_Tp)(s * 10u + c - 48u);
return s;
}
};
} IOManager::IOManager io;

namespace math {

using i64 = long long;
using u32 = unsigned int;
using u64 = unsigned long long;

constexpr int mod = 998244353;
constexpr int primitive_root = 3;

inline int sum(const int &x, const int &y) {
return x + y < mod ? x + y : (x + y - mod);
}
inline int sub(const int &x, const int &y) {
return x < y ? x - y + mod : (x - y);
}

inline void inc(int &x, const int &y) {
x += y; (mod <= x) and (x -= mod);
}
inline void dec(int &x, const int &y) {
x -= y; (x < 0) and (x += mod);
}

inline int fpow(int x, int n) {
int pw = 1;
for (; n; n >>= 1, x = (i64)x * x % mod)
if (n & 1) pw = (i64)pw * x % mod;
return pw;
}

template <class _Tp>
inline void swap(_Tp &x, _Tp &y) {
_Tp z = x; x = y; y = z;
}

}

namespace poly {

using namespace math;

constexpr int maxn = 524288;

using poly_t = int[maxn];
using poly = int *const;

inline int calcpw2(const int &n) {
for (int pw = 1; ; pw <<= 1)
if (n <= pw) return pw;
}

poly_t wn, iwn, inv;

inline void init() {
const int unit_root = fpow(primitive_root, (mod - 1) / maxn);
wn[0] = iwn[0] = inv[1] = 1;
for (int i = 1; i != maxn; ++i)
wn[i] = (i64)wn[i - 1] * unit_root % mod;
std::reverse_copy(wn + 1, wn + maxn, iwn + 1);
for (int i = 2; i != maxn; ++i)
inv[i] = (i64)inv[mod % i] * (mod - mod / i) % mod;
}

void DFT(poly &a, const int &n, const poly &omega = wn) {
static poly_t curw; curw[0] = 1;
for (int i = 0, j = 0; i != n; ++i) {
if (i < j) swap(a[i], a[j]);
for (int k = n >> 1; (j xor_eq k) < k; k >>= 1);
}

poly endpos = a + n, wendpos = omega + maxn;
for (int l = 1, tp = maxn >> 1; l != n; l <<= 1, tp >>= 1) {
for (int *i = curw, *w = omega + tp; w != wendpos; w += tp)
*++i = *w;
for (int *i = a, z; i != endpos; i += l + l)
for (int j = 0; j != l; ++j)
z = (i64)i[j + l] * curw[j] % mod,
i[j + l] = sub(i[j], z),
inc(i[j], z);
}
}

void IDFT(poly &a, const int &n) {
DFT(a, n, iwn);
const int invn = fpow(n, mod - 2);
for (int i = 0; i != n; ++i)
a[i] = (i64)a[i] * invn % mod;
}

inline void derivative(const poly &h, const int &n, poly &f) {
for (int i = 1; i != n; ++i)
f[i - 1] = (i64)h[i] * i % mod;
f[n - 1] = 0;
}

inline void integral(const poly &h, const int &n, poly &f) {
for (int i = n - 1; i; --i)
f[i] = (i64)h[i - 1] * inv[i] % mod;
f[0] = 0;
}

void polyinv(const poly &h, const int &n, poly &f) {
static poly_t inv_t;
std::fill(f, f + n + n, 0);
std::fill(inv_t, inv_t + n + n, 0);
f[0] = fpow(h[0], mod - 2);
for (int t = 2; t <= n; t <<= 1) {
const int t2 = t << 1;
std::copy(h, h + t, inv_t);

DFT(f, t2); DFT(inv_t, t2);
for (int i = 0; i != t2; ++i)
f[i] = (i64)f[i] * sub(2, (i64)f[i] * inv_t[i] % mod) % mod;
IDFT(f, t2);

std::fill(f + t, f + t2, 0);
}
}

void polyln(const poly &h, const int &n, poly &f) {
if (h[0] != 1) throw "in function polyln(): the constant term not equal to 1";
static poly_t ln_t;
const int t = n << 1;

derivative(h, n, ln_t);
std::fill(ln_t + n, ln_t + t, 0);
polyinv(h, n, f);

DFT(ln_t, t); DFT(f, t);
for (int i = 0; i != t; ++i)
ln_t[i] = (i64)ln_t[i] * f[i] % mod;
IDFT(ln_t, t);

integral(ln_t, n, f);
}

inline void mul(poly &a, const int &an, poly &b, const int &bn, poly &c) {
static poly_t f, g;
const int n = calcpw2(an + bn + 1);
f[0] = 1; std::copy(a, a + an, f + 1); std::fill(f + an + 1, f + n, 0);
g[0] = 1; std::copy(b, b + bn, g + 1); std::fill(g + bn + 1, g + n, 0);

DFT(f, n); DFT(g, n);
for (int i = 0; i != n; ++i)
f[i] = (i64)f[i] * g[i] % mod;
IDFT(f, n);

std::copy(f + 1, f + an + bn + 1, c);
}

}

using math::mod;

poly::poly_t a, f;

void calc(const int &l, const int &r) {
if (l == r) return void(f[l] = mod - a[l]);
const int m = (l + r) >> 1;
calc(l, m); calc(m + 1, r);
poly::mul(f + l, m - l + 1, f + m + 1, r - m, f + l);
}

int main() {
poly::init();
for (int T = io; T; --T) {
const int n = io;
for (int i = 1; i <= n; ++i)
a[i] = io, (mod <= a[i]) and (a[i] -= mod);

f[0] = 1; calc(1, n);
poly::polyln(f, poly::calcpw2(n + 1), a);

int ans = 0;
for (int i = 1; i <= n; ++i)
ans xor_eq (mod - (i64)a[i] * i % mod);
printf("%d\n", ans);
}

return 0;
}