P5488 差分和前缀和

洛谷P5488差分和前缀和

P5488 差分和前缀和

题目描述

给定一个长为 nn 的序列aa,求出其 kk 阶差分或前缀和。
结果的每一项都需要对 10045358091004535809 取模。

输入格式

第一行三个整数 n,k,tn,k,t,若 t=0t=0 表示求前缀和,t=1t=1 表示求差分。
第二行 nn 个整数,表示序列 aa

输出格式

输出一行 nn 个整数,表示 aakk 阶差分或前缀和。

题目分析

求前缀和相当于求原本多项式与下面式子的卷积

f(x)(1+x+x2+x3...xn)f(x)*(1+x+x^2+x^3...x^{n})

求差分相当于

f(x)(1x+x2x3...(x)n)f(x)*(1-x+x^2-x^3...(-x)^{n})

k阶前缀和和差分相当原多项式乘以这两个的k次方,通过一点简单的幂级数和函数以及展开知识可以得到其k解差分对应转移多项式,然后就可以O(nlogn)O(nlogn)算出其k阶前缀和数组和差分。

代码

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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//
// Created by mrx on 2022/8/5.
//
#include <bits/stdc++.h>

using ll = long long;

template<typename T>
T inverse(T a, T b) {
T u = 0, v = 1;
while (a != 0) {
T t = b / a;
b -= t * a;
std::swap(a, b);
u -= t * v;
std::swap(u, v);
}
assert(b == 1);
return u;
}

template<typename T>
T power(T a, int b) {
T ans = 1;
for (; b; a *= a, b >>= 1) {
if (b & 1)ans *= a;
}
return ans;
}

template<int Mod>
class Modular {
public:
using Type = int;

template<typename U>
static Type norm(U &x) {
Type v;
if (-Mod <= x && x < Mod) v = static_cast<Type>(x);
else v = static_cast<Type>(x % Mod);
if (v < 0) v += Mod;
return v;
}

constexpr Modular() : value() {}

int val() const { return value; }

Modular inv() const {
return Modular(inverse(value, Mod));
}

template<typename U>
Modular(const U &x) {
value = norm(x);
}

const Type &operator()() const {
return value;
}

template<typename U>
explicit operator U() const {
return static_cast<U>(value);
}

Modular &operator+=(const Modular &other) {
if ((value += other.value) >= Mod) value -= Mod;
return *this;
}

Modular &operator-=(
const Modular &other) {
if ((value -= other.value) < 0) value += Mod;
return *this;
}

template<typename U>
Modular &operator+=(const U &other) { return *this += Modular(other); }

template<typename U>
Modular &operator-=(const U &other) { return *this -= Modular(other); }

Modular &operator++() { return *this += 1; }

Modular &operator--() { return *this -= 1; }

Modular operator++(int) {
Modular result(*this);
*this += 1;
return result;
}

Modular operator--(int) {
Modular result(*this);
*this -= 1;
return result;
}

Modular operator-() const { return Modular(-value); }

template<class ISTREAM_TYPE>
friend ISTREAM_TYPE &operator>>(ISTREAM_TYPE &is, Modular &rhs) {
ll v;
is >> v;
rhs = Modular(v);
return is;
}

template<class OSTREAM_TYPE>
friend OSTREAM_TYPE &operator<<(OSTREAM_TYPE &os, const Modular &rhs) {
return os << rhs.val();
}

Modular &operator*=(const Modular &rhs) {
value = ll(value) * rhs.value % Mod;
return *this;
}

Modular &operator/=(const Modular &other) { return *this *= Modular(inverse(other.value, Mod)); }

friend const Type &abs(const Modular &x) { return x.value; }

friend bool operator==(const Modular &lhs, const Modular &rhs) { return lhs.x == rhs.x; }

friend bool operator<(const Modular &lhs, const Modular &rhs) { return lhs.x < rhs.x; }


bool operator==(const Modular &rhs) { return *this == rhs.value; }

template<typename U>
bool operator==(U rhs) { return *this == Modular(rhs); }

template<typename U>
friend bool operator==(U lhs, const Modular &rhs) { return Modular(lhs) == rhs; }

bool operator!=(const Modular &rhs) { return *this != rhs; }

template<typename U>
bool operator!=(U rhs) { return *this != rhs; }

template<typename U>
friend bool operator!=(U lhs, const Modular &rhs) { return lhs != rhs; }

bool operator<(const Modular &rhs) { return this->value < rhs.value; }

Modular operator+(const Modular &rhs) { return Modular(*this) += rhs; }

template<typename U>
Modular operator+(U rhs) { return Modular(*this) += rhs; }

template<typename U>
friend Modular operator+(U lhs, const Modular &rhs) { return Modular(lhs) += rhs; }

Modular operator-(const Modular &rhs) { return Modular(*this) -= rhs; }

template<typename U>
Modular operator-(U rhs) { return Modular(*this) -= rhs; }

template<typename U>
friend Modular operator-(U lhs, const Modular &rhs) { return Modular(lhs) -= rhs; }

Modular operator*(const Modular &rhs) { return Modular(*this) *= rhs; }

template<typename U>
Modular operator*(U rhs) { return Modular(*this) *= rhs; }

template<typename U>
friend Modular operator*(U lhs, const Modular &rhs) { return Modular(lhs) *= rhs; }

Modular operator/(const Modular &rhs) { return Modular(*this) /= rhs; }

template<typename U>
Modular operator/(U rhs) { return Modular(*this) /= rhs; }

template<typename U>
friend Modular operator/(U lhs, const Modular &rhs) { return Modular(lhs) /= rhs; }

private:
Type value;
};

constexpr int mod = 1004535809;
using Z = Modular<mod>;
std::vector<int> rev;
std::vector<Z> roots{0, 1};

void dft(std::vector<Z> &a) {
int n = a.size();

if (int(rev.size()) != n) {
rev.resize(n);
for (int i = 0; i < n; ++i) {
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) ? n >> 1 : 0);
}
}

for (int i = 0; i < n; ++i) {
if (rev[i] < i)std::swap(a[i], a[rev[i]]);
}
if (int(roots.size() < n)) {
int k = __builtin_ctz(roots.size());
roots.resize(n);
while ((1 << k) < n) {
Z e = power(Z(3), (mod - 1) >> (k + 1));
for (int i = 1 << (k - 1); i < (1 << k); i++) {
roots[i << 1] = roots[i];
roots[i << 1 | 1] = roots[i] * e;
}
k++;
}
}

for (int k = 1; k < n; k *= 2) {
for (int i = 0; i < n; i += 2 * k) {
for (int j = 0; j < k; j++) {
Z u = a[i + j];
Z v = a[i + j + k] * roots[k + j];
a[i + j] = u + v;
a[i + j + k] = u - v;
}
}
}
}

void idft(std::vector<Z> &a) {
int n = a.size();
std::reverse(a.begin() + 1, a.end());
dft(a);
}

struct Poly {
std::vector<Z> a;

Poly() {}

Poly(const std::vector<Z> &a) : a(a) {}

Poly(const std::initializer_list<Z> &a) : a(a) {}

int size() const {
return a.size();
}

void resize(int n) {
a.resize(n);
}

Z operator[](int idx) const {
if (idx < size()) {
return a[idx];
} else {
return 0;
}
}

Z &operator[](int idx) {
return a[idx];
}

Poly mulxk(int k) const {
auto b = a;
b.insert(b.begin(), k, 0);
return Poly(b);
}

Poly modxk(int k) const {
k = std::min(k, size());
return Poly(std::vector<Z>(a.begin(), a.begin() + k));
}

Poly divxk(int k) const {
if (size() <= k) {
return Poly();
}
return Poly(std::vector<Z>(a.begin() + k, a.end()));
}

friend Poly operator+(const Poly &a, const Poly &b) {
std::vector<Z> res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); i++) {
res[i] = a[i] + b[i];
}
return Poly(res);
}

friend Poly operator-(const Poly &a, const Poly &b) {
std::vector<Z> res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); i++) {
res[i] = a[i] - b[i];
}
return Poly(res);
}

friend Poly operator*(Poly a, Poly b) {
if (a.size() == 0 || b.size() == 0) {
return Poly();
}
int sz = 1, tot = a.size() + b.size() - 1;
while (sz < tot) {
sz *= 2;
}
a.a.resize(sz);
b.a.resize(sz);
dft(a.a);
dft(b.a);
Z inv = Z(sz).inv();
for (int i = 0; i < sz; ++i) {
a.a[i] = a[i] * b[i] * inv;
}
idft(a.a);
a.resize(tot);
return a;
}

friend Poly operator*(Z a, Poly b) {
for (int i = 0; i < int(b.size()); i++) {
b[i] *= a;
}
return b;
}

friend Poly operator*(Poly a, Z b) {
for (int i = 0; i < int(a.size()); i++) {
a[i] *= b;
}
return a;
}

Poly &operator+=(Poly b) {
return (*this) = (*this) + b;
}

Poly &operator-=(Poly b) {
return (*this) = (*this) - b;
}

Poly &operator*=(Poly b) {
return (*this) = (*this) * b;
}

Poly deriv() const {
if (a.empty()) {
return Poly();
}
std::vector<Z> res(size() - 1);
for (int i = 0; i < size() - 1; ++i) {
res[i] = (i + 1) * a[i + 1];
}
return Poly(res);
}

Poly integr() const {
std::vector<Z> res(size() + 1);
for (int i = 0; i < size(); ++i) {
res[i + 1] = a[i] / (i + 1);
}
return Poly(res);
}

Poly inv(int m) const {
Poly x{a[0].inv()};
int k = 1;
while (k < m) {
k *= 2;
x = (x * (Poly{2} - modxk(k) * x)).modxk(k);
}
return x.modxk(m);
}

Poly log(int m) const {
return (deriv() * inv(m)).integr().modxk(m);
}

Poly exp(int m) const {
Poly x{1};
int k = 1;
while (k < m) {
k *= 2;
x = (x * (Poly{1} - x.log(k) + modxk(k))).modxk(k);
}
return x.modxk(m);
}

Poly pow(int k, int m) const {
int i = 0;
while (i < size() && a[i].val() == 0) {
i++;
}
if (i == size() || 1LL * i * k >= m) {
return Poly(std::vector<Z>(m));
}
Z v = a[i];
auto f = divxk(i) * v.inv();
return (f.log(m - i * k) * k).exp(m - i * k).mulxk(i * k) * power(v, k);
}

Poly sqrt(int m) const {
Poly x{1};
int k = 1;
while (k < m) {
k *= 2;
x = (x + (modxk(k) * x.inv(k)).modxk(k)) * ((mod + 1) / 2);
}
return x.modxk(m);
}

Poly mulT(Poly b) const {
if (b.size() == 0) {
return Poly();
}
int n = b.size();
std::reverse(b.a.begin(), b.a.end());
return ((*this) * b).divxk(n - 1);
}

std::vector<Z> eval(std::vector<Z> x) const {
if (size() == 0) {
return std::vector<Z>(x.size(), 0);
}
const int n = std::max(int(x.size()), size());
std::vector<Poly> q(4 * n);
std::vector<Z> ans(x.size());
x.resize(n);
std::function<void(int, int, int)> build = [&](int p, int l, int r) {
if (r - l == 1) {
q[p] = Poly{1, -x[l]};
} else {
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
q[p] = q[2 * p] * q[2 * p + 1];
}
};
build(1, 0, n);
std::function<void(int, int, int, const Poly &)> work = [&](int p, int l, int r, const Poly &num) {
if (r - l == 1) {
if (l < int(ans.size())) {
ans[l] = num[0];
}
} else {
int m = (l + r) / 2;
work(2 * p, l, m, num.mulT(q[2 * p + 1]).modxk(m - l));
work(2 * p + 1, m, r, num.mulT(q[2 * p]).modxk(r - m));
}
};
work(1, 0, n, mulT(q[1].inv(n)));
return ans;
}
};

int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("o.txt", "w", stderr);
#endif
#ifdef ONLINE_JUDGE
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
#endif
int n, opt;
Z k;
std::cin >> n;
char cc = std::cin.get();
ll cn = 0, flus = 1;
while (cc < '0' || cc > '9') {
if (cc == '-') flus = -flus;
cc = std::cin.get();
}
while (cc >= '0' && cc <= '9') {
cn = (cn * 10 + cc - '0') % (mod), cc = std::cin.get();
}
k = cn * flus;

std::cin >> opt;
std::vector<Z> a(n);
for (int i = 0; i < n; ++i)std::cin >> a[i];
Poly A(a);
std::vector<Z> B(n);
B[0] = 1;
if (opt == 0) {
for (int i = 1; i < n; ++i)B[i] = B[i - 1] * Z(k + i - 1) * power(Z(i), mod - 2);
} else {
for (int i = 1; i < n; ++i)B[i] = Z(-B[i - 1] * Z(k - i + 1) * power(Z(i), mod - 2));
}
Poly rhs(B);
for (int i = 0; i < n; ++i)std::cerr << B[i] << ' ';
Poly ans = A * rhs;
for (int i = 0; i < n; ++i) std::cout << ans[i] << ' ';
return 0;
}

P5488 差分和前缀和
https://mrxyan6.github.io/2022/09/02/p5488/
作者
mrx
发布于
2022年9月2日
许可协议