-
Notifications
You must be signed in to change notification settings - Fork 12
/
formal_power_series.cpp
708 lines (648 loc) · 20.9 KB
/
formal_power_series.cpp
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
//
// Formal Power Series
//
// verified:
// Yosupo Library Checker - Inv of Formal Power Series
// https://judge.yosupo.jp/problem/inv_of_formal_power_series
//
// Yosupo Library Checker - Exp of Formal Power Series
// https://judge.yosupo.jp/problem/exp_of_formal_power_series
//
// Yosupo Library Checker - Log of Formal Power Series
// https://judge.yosupo.jp/problem/log_of_formal_power_series
//
// Yosupo Library Checker - Pow of Formal Power Series
// https://judge.yosupo.jp/problem/pow_of_formal_power_series
//
// Yosupo Library Checker - Sqrt of Formal Power Series
// https://judge.yosupo.jp/problem/sqrt_of_formal_power_series
//
// HackerRank Array Restoring
// https://www.hackerrank.com/contests/happy-query-contest/challenges/array-restoring/problem
//
// Codeforces 205 Div1 E. The Child and Binary TreeE
// https://codeforces.com/contest/438/problem/E
//
// TDPC T - フィボナッチ (mod. 1000000007)
// https://atcoder.jp/contests/tdpc/tasks/tdpc_fibonacci
//
#include <bits/stdc++.h>
using namespace std;
// modint
template<int MOD> struct Fp {
// inner value
long long val;
// constructor
constexpr Fp() : val(0) { }
constexpr Fp(long long v) : val(v % MOD) {
if (val < 0) val += MOD;
}
constexpr long long get() const { return val; }
constexpr int get_mod() const { return MOD; }
// arithmetic operators
constexpr Fp operator + () const { return Fp(*this); }
constexpr Fp operator - () const { return Fp(0) - Fp(*this); }
constexpr Fp operator + (const Fp &r) const { return Fp(*this) += r; }
constexpr Fp operator - (const Fp &r) const { return Fp(*this) -= r; }
constexpr Fp operator * (const Fp &r) const { return Fp(*this) *= r; }
constexpr Fp operator / (const Fp &r) const { return Fp(*this) /= r; }
constexpr Fp& operator += (const Fp &r) {
val += r.val;
if (val >= MOD) val -= MOD;
return *this;
}
constexpr Fp& operator -= (const Fp &r) {
val -= r.val;
if (val < 0) val += MOD;
return *this;
}
constexpr Fp& operator *= (const Fp &r) {
val = val * r.val % MOD;
return *this;
}
constexpr Fp& operator /= (const Fp &r) {
long long a = r.val, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b, swap(a, b);
u -= t * v, swap(u, v);
}
val = val * u % MOD;
if (val < 0) val += MOD;
return *this;
}
constexpr Fp pow(long long n) const {
Fp res(1), mul(*this);
while (n > 0) {
if (n & 1) res *= mul;
mul *= mul;
n >>= 1;
}
return res;
}
constexpr Fp inv() const {
Fp res(1), div(*this);
return res / div;
}
// other operators
constexpr bool operator == (const Fp &r) const {
return this->val == r.val;
}
constexpr bool operator != (const Fp &r) const {
return this->val != r.val;
}
constexpr Fp& operator ++ () {
++val;
if (val >= MOD) val -= MOD;
return *this;
}
constexpr Fp& operator -- () {
if (val == 0) val += MOD;
--val;
return *this;
}
constexpr Fp operator ++ (int) const {
Fp res = *this;
++*this;
return res;
}
constexpr Fp operator -- (int) const {
Fp res = *this;
--*this;
return res;
}
friend constexpr istream& operator >> (istream &is, Fp<MOD> &x) {
is >> x.val;
x.val %= MOD;
if (x.val < 0) x.val += MOD;
return is;
}
friend constexpr ostream& operator << (ostream &os, const Fp<MOD> &x) {
return os << x.val;
}
friend constexpr Fp<MOD> pow(const Fp<MOD> &r, long long n) {
return r.pow(n);
}
friend constexpr Fp<MOD> inv(const Fp<MOD> &r) {
return r.inv();
}
};
namespace NTT {
long long modpow(long long a, long long n, int mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
long long modinv(long long a, int mod) {
long long b = mod, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b, swap(a, b);
u -= t * v, swap(u, v);
}
u %= mod;
if (u < 0) u += mod;
return u;
}
int calc_primitive_root(int mod) {
if (mod == 2) return 1;
if (mod == 167772161) return 3;
if (mod == 469762049) return 3;
if (mod == 754974721) return 11;
if (mod == 998244353) return 3;
int divs[20] = {};
divs[0] = 2;
int cnt = 1;
long long x = (mod - 1) / 2;
while (x % 2 == 0) x /= 2;
for (long long i = 3; i * i <= x; i += 2) {
if (x % i == 0) {
divs[cnt++] = i;
while (x % i == 0) x /= i;
}
}
if (x > 1) divs[cnt++] = x;
for (int g = 2;; g++) {
bool ok = true;
for (int i = 0; i < cnt; i++) {
if (modpow(g, (mod - 1) / divs[i], mod) == 1) {
ok = false;
break;
}
}
if (ok) return g;
}
}
int get_fft_size(int N, int M) {
int size_a = 1, size_b = 1;
while (size_a < N) size_a <<= 1;
while (size_b < M) size_b <<= 1;
return max(size_a, size_b) << 1;
}
// number-theoretic transform
template<class mint> void trans(vector<mint> &v, bool inv = false) {
if (v.empty()) return;
int N = (int)v.size();
int MOD = v[0].get_mod();
int PR = calc_primitive_root(MOD);
static bool first = true;
static vector<long long> vbw(30), vibw(30);
if (first) {
first = false;
for (int k = 0; k < 30; ++k) {
vbw[k] = modpow(PR, (MOD - 1) >> (k + 1), MOD);
vibw[k] = modinv(vbw[k], MOD);
}
}
for (int i = 0, j = 1; j < N - 1; j++) {
for (int k = N >> 1; k > (i ^= k); k >>= 1);
if (i > j) swap(v[i], v[j]);
}
for (int k = 0, t = 2; t <= N; ++k, t <<= 1) {
long long bw = vbw[k];
if (inv) bw = vibw[k];
for (int i = 0; i < N; i += t) {
mint w = 1;
for (int j = 0; j < t/2; ++j) {
int j1 = i + j, j2 = i + j + t/2;
mint c1 = v[j1], c2 = v[j2] * w;
v[j1] = c1 + c2;
v[j2] = c1 - c2;
w *= bw;
}
}
}
if (inv) {
long long invN = modinv(N, MOD);
for (int i = 0; i < N; ++i) v[i] = v[i] * invN;
}
}
// for garner
static constexpr int MOD0 = 754974721;
static constexpr int MOD1 = 167772161;
static constexpr int MOD2 = 469762049;
using mint0 = Fp<MOD0>;
using mint1 = Fp<MOD1>;
using mint2 = Fp<MOD2>;
static const mint1 imod0 = 95869806; // modinv(MOD0, MOD1);
static const mint2 imod1 = 104391568; // modinv(MOD1, MOD2);
static const mint2 imod01 = 187290749; // imod1 / MOD0;
// small case (T = mint, long long)
template<class T> vector<T> naive_mul(const vector<T> &A, const vector<T> &B) {
if (A.empty() || B.empty()) return {};
int N = (int)A.size(), M = (int)B.size();
vector<T> res(N + M - 1);
for (int i = 0; i < N; ++i)
for (int j = 0; j < M; ++j)
res[i + j] += A[i] * B[j];
return res;
}
// mul by convolution
template<class mint> vector<mint> mul(const vector<mint> &A, const vector<mint> &B) {
if (A.empty() || B.empty()) return {};
int N = (int)A.size(), M = (int)B.size();
if (min(N, M) < 30) return naive_mul(A, B);
int MOD = A[0].get_mod();
int size_fft = get_fft_size(N, M);
if (MOD == 998244353) {
vector<mint> a(size_fft), b(size_fft), c(size_fft);
for (int i = 0; i < N; ++i) a[i] = A[i];
for (int i = 0; i < M; ++i) b[i] = B[i];
trans(a), trans(b);
vector<mint> res(size_fft);
for (int i = 0; i < size_fft; ++i) res[i] = a[i] * b[i];
trans(res, true);
res.resize(N + M - 1);
return res;
}
vector<mint0> a0(size_fft, 0), b0(size_fft, 0), c0(size_fft, 0);
vector<mint1> a1(size_fft, 0), b1(size_fft, 0), c1(size_fft, 0);
vector<mint2> a2(size_fft, 0), b2(size_fft, 0), c2(size_fft, 0);
for (int i = 0; i < N; ++i)
a0[i] = A[i].val, a1[i] = A[i].val, a2[i] = A[i].val;
for (int i = 0; i < M; ++i)
b0[i] = B[i].val, b1[i] = B[i].val, b2[i] = B[i].val;
trans(a0), trans(a1), trans(a2), trans(b0), trans(b1), trans(b2);
for (int i = 0; i < size_fft; ++i) {
c0[i] = a0[i] * b0[i];
c1[i] = a1[i] * b1[i];
c2[i] = a2[i] * b2[i];
}
trans(c0, true), trans(c1, true), trans(c2, true);
mint mod0 = MOD0, mod01 = mod0 * MOD1;
vector<mint> res(N + M - 1);
for (int i = 0; i < N + M - 1; ++i) {
int y0 = c0[i].val;
int y1 = (imod0 * (c1[i] - y0)).val;
int y2 = (imod01 * (c2[i] - y0) - imod1 * y1).val;
res[i] = mod01 * y2 + mod0 * y1 + y0;
}
return res;
}
};
// Formal Power Series
template<typename mint> struct FPS : vector<mint> {
using vector<mint>::vector;
// constructor
constexpr FPS(const vector<mint> &r) : vector<mint>(r) {}
// core operator
constexpr FPS pre(int siz) const {
return FPS(begin(*this), begin(*this) + min((int)this->size(), siz));
}
constexpr FPS rev() const {
FPS res = *this;
reverse(begin(res), end(res));
return res;
}
constexpr FPS& normalize() {
while (!this->empty() && this->back() == 0) this->pop_back();
return *this;
}
// basic operator
constexpr FPS operator - () const noexcept {
FPS res = (*this);
for (int i = 0; i < (int)res.size(); ++i) res[i] = -res[i];
return res;
}
constexpr FPS operator + (const mint &v) const { return FPS(*this) += v; }
constexpr FPS operator + (const FPS &r) const { return FPS(*this) += r; }
constexpr FPS operator - (const mint &v) const { return FPS(*this) -= v; }
constexpr FPS operator - (const FPS &r) const { return FPS(*this) -= r; }
constexpr FPS operator * (const mint &v) const { return FPS(*this) *= v; }
constexpr FPS operator * (const FPS &r) const { return FPS(*this) *= r; }
constexpr FPS operator / (const mint &v) const { return FPS(*this) /= v; }
constexpr FPS operator / (const FPS &r) const { return FPS(*this) /= r; }
constexpr FPS operator % (const FPS &r) const { return FPS(*this) %= r; }
constexpr FPS operator << (int x) const { return FPS(*this) <<= x; }
constexpr FPS operator >> (int x) const { return FPS(*this) >>= x; }
constexpr FPS& operator += (const mint &v) {
if (this->empty()) this->resize(1);
(*this)[0] += v;
return *this;
}
constexpr FPS& operator += (const FPS &r) {
if (r.size() > this->size()) this->resize(r.size());
for (int i = 0; i < (int)r.size(); ++i) (*this)[i] += r[i];
return this->normalize();
}
constexpr FPS& operator -= (const mint &v) {
if (this->empty()) this->resize(1);
(*this)[0] -= v;
return *this;
}
constexpr FPS& operator -= (const FPS &r) {
if (r.size() > this->size()) this->resize(r.size());
for (int i = 0; i < (int)r.size(); ++i) (*this)[i] -= r[i];
return this->normalize();
}
constexpr FPS& operator *= (const mint &v) {
for (int i = 0; i < (int)this->size(); ++i) (*this)[i] *= v;
return *this;
}
constexpr FPS& operator *= (const FPS &r) {
return *this = NTT::mul((*this), r);
}
constexpr FPS& operator /= (const mint &v) {
assert(v != 0);
mint iv = modinv(v);
for (int i = 0; i < (int)this->size(); ++i) (*this)[i] *= iv;
return *this;
}
// division, r must be normalized (r.back() must not be 0)
constexpr FPS& operator /= (const FPS &r) {
assert(!r.empty());
assert(r.back() != 0);
this->normalize();
if (this->size() < r.size()) {
this->clear();
return *this;
}
int need = (int)this->size() - (int)r.size() + 1;
*this = (rev().pre(need) * r.rev().inv(need)).pre(need).rev();
return *this;
}
constexpr FPS& operator %= (const FPS &r) {
assert(!r.empty());
assert(r.back() != 0);
this->normalize();
FPS q = (*this) / r;
return *this -= q * r;
}
constexpr FPS& operator <<= (int x) {
FPS res(x, 0);
res.insert(res.end(), begin(*this), end(*this));
return *this = res;
}
constexpr FPS& operator >>= (int x) {
FPS res;
res.insert(res.end(), begin(*this) + x, end(*this));
return *this = res;
}
constexpr mint eval(const mint &v) {
mint res = 0;
for (int i = (int)this->size()-1; i >= 0; --i) {
res *= v;
res += (*this)[i];
}
return res;
}
// advanced operation
// df/dx
constexpr FPS diff() const {
int n = (int)this->size();
FPS res(n-1);
for (int i = 1; i < n; ++i) res[i-1] = (*this)[i] * i;
return res;
}
// \int f dx
constexpr FPS integral() const {
int n = (int)this->size();
FPS res(n+1, 0);
for (int i = 0; i < n; ++i) res[i+1] = (*this)[i] / (i+1);
return res;
}
// inv(f), f[0] must not be 0
constexpr FPS inv(int deg) const {
assert((*this)[0] != 0);
if (deg < 0) deg = (int)this->size();
FPS res({mint(1) / (*this)[0]});
for (int i = 1; i < deg; i <<= 1) {
res = (res + res - res * res * pre(i << 1)).pre(i << 1);
}
res.resize(deg);
return res;
}
constexpr FPS inv() const {
return inv((int)this->size());
}
// log(f) = \int f'/f dx, f[0] must be 1
constexpr FPS log(int deg) const {
assert((*this)[0] == 1);
FPS res = (diff() * inv(deg)).integral();
res.resize(deg);
return res;
}
constexpr FPS log() const {
return log((int)this->size());
}
// exp(f), f[0] must be 0
constexpr FPS exp(int deg) const {
assert((*this)[0] == 0);
FPS res(1, 1);
for (int i = 1; i < deg; i <<= 1) {
res = res * (pre(i << 1) - res.log(i << 1) + 1).pre(i << 1);
}
res.resize(deg);
return res;
}
constexpr FPS exp() const {
return exp((int)this->size());
}
// pow(f) = exp(e * log f)
constexpr FPS pow(long long e, int deg) const {
if (e == 0) {
FPS res(deg, 0);
res[0] = 1;
return res;
}
long long i = 0;
while (i < (int)this->size() && (*this)[i] == 0) ++i;
if (i == (int)this->size() || i > (deg - 1) / e) return FPS(deg, 0);
mint k = (*this)[i];
FPS res = ((((*this) >> i) / k).log(deg) * e).exp(deg) * mint(k).pow(e) << (e * i);
res.resize(deg);
return res;
}
constexpr FPS pow(long long e) const {
return pow(e, (int)this->size());
}
// sqrt(f), f[0] must be 1
constexpr FPS sqrt_base(int deg) const {
assert((*this)[0] == 1);
mint inv2 = mint(1) / 2;
FPS res(1, 1);
for (int i = 1; i < deg; i <<= 1) {
res = (res + pre(i << 1) * res.inv(i << 1)).pre(i << 1);
for (mint &x : res) x *= inv2;
}
res.resize(deg);
return res;
}
constexpr FPS sqrt_base() const {
return sqrt_base((int)this->size());
}
// friend operators
friend constexpr FPS diff(const FPS &f) { return f.diff(); }
friend constexpr FPS integral(const FPS &f) { return f.integral(); }
friend constexpr FPS inv(const FPS &f, int deg) { return f.inv(deg); }
friend constexpr FPS inv(const FPS &f) { return f.inv((int)f.size()); }
friend constexpr FPS log(const FPS &f, int deg) { return f.log(deg); }
friend constexpr FPS log(const FPS &f) { return f.log((int)f.size()); }
friend constexpr FPS exp(const FPS &f, int deg) { return f.exp(deg); }
friend constexpr FPS exp(const FPS &f) { return f.exp((int)f.size()); }
friend constexpr FPS pow(const FPS &f, long long e, int deg) { return f.pow(e, deg); }
friend constexpr FPS pow(const FPS &f, long long e) { return f.pow(e, (int)f.size()); }
friend constexpr FPS sqrt_base(const FPS &f, int deg) { return f.sqrt_base(deg); }
friend constexpr FPS sqrt_base(const FPS &f) { return f.sqrt_base((int)f.size()); }
};
//------------------------------//
// Polynomial, FPS algorithms
//------------------------------//
// Bostan-Mori
// find [x^N] P(x)/Q(x), O(K log K log N)
// deg(Q(x)) = K, deg(P(x)) < K
template<typename mint> mint BostanMori(const FPS<mint> &P, const FPS<mint> &Q, long long N) {
assert(!P.empty() && !Q.empty());
if (N == 0 || Q.size() == 1) return P[0] / Q[0];
int qdeg = (int)Q.size();
FPS<mint> P2{P}, minusQ{Q};
P2.resize(qdeg - 1);
for (int i = 1; i < (int)Q.size(); i += 2) minusQ[i] = -minusQ[i];
P2 *= minusQ;
FPS<mint> Q2 = Q * minusQ;
FPS<mint> S(qdeg - 1), T(qdeg);
for (int i = 0; i < (int)S.size(); ++i) {
S[i] = (N % 2 == 0 ? P2[i * 2] : P2[i * 2 + 1]);
}
for (int i = 0; i < (int)T.size(); ++i) {
T[i] = Q2[i * 2];
}
return BostanMori(S, T, N >> 1);
}
//------------------------------//
// Examples
//------------------------------//
void Yosupo_Inv_of_FPS() {
const int MOD = 998244353;
using mint = Fp<MOD>;
int N;
cin >> N;
FPS<mint> a(N);
for (int i = 0; i < N; ++i) cin >> a[i];
auto res = inv(a);
for (int i = 0; i < res.size(); ++i) {
if (i) cout << " ";
cout << res[i];
}
cout << endl;
}
void Yosupo_Exp_of_FPS() {
const int MOD = 998244353;
using mint = Fp<MOD>;
int N;
cin >> N;
FPS<mint> a(N);
for (int i = 0; i < N; ++i) cin >> a[i];
auto res = exp(a);
for (int i = 0; i < res.size(); ++i) {
if (i) cout << " ";
cout << res[i];
}
cout << endl;
}
void Yosupo_Log_of_FPS() {
const int MOD = 998244353;
using mint = Fp<MOD>;
int N;
cin >> N;
FPS<mint> a(N);
for (int i = 0; i < N; ++i) cin >> a[i];
auto res = log(a);
for (int i = 0; i < res.size(); ++i) {
if (i) cout << " ";
cout << res[i];
}
cout << endl;
}
void Yosupo_Pow_of_FPS() {
const int MOD = 998244353;
using mint = Fp<MOD>;
long long N, M;
cin >> N >> M;
FPS<mint> a(N);
for (int i = 0; i < N; ++i) cin >> a[i];
auto res = pow(a, M);
for (int i = 0; i < res.size(); ++i) {
if (i) cout << " ";
cout << res[i];
}
cout << endl;
}
void Yosupo_Sqrt_of_FPS() {
const int MOD = 998244353;
using mint = Fp<MOD>;
int N;
cin >> N;
FPS<mint> a(N);
for (int i = 0; i < N; ++i) cin >> a[i];
auto res = sqrt_base(a);
for (int i = 0; i < res.size(); ++i) {
if (i) cout << " ";
cout << res[i];
}
cout << endl;
}
void HackerRankArrayRestoring() {
const int MOD = 998244353;
using mint = Fp<MOD>;
int N, M, Q;
cin >> N >> M >> Q;
FPS<mint> f(N-M+1, 0);
for (int i = 0; i < Q; ++i) {
int l, r;
cin >> l >> r;
--l;
f[l] += 1;
}
f.normalize();
FPS<mint> A(N);
for (int i = 0; i < N; ++i) cin >> A[i];
auto B = A / f;
B.resize(M);
for (int i = 0; i < M; ++i) {
if (i) cout << " ";
cout << B[i];
}
cout << endl;
}
void Codeforces205Div1E() {
const int MOD = 998244353;
using mint = Fp<MOD>;
int N, M;
cin >> N >> M;
FPS<mint> C(M+1, 0);
for (int i = 0; i < N; ++i) {
int c;
cin >> c;
if (c > M) continue;
C[c] += 1;
}
FPS<mint> F = inv(sqrt_base(C * mint(-4) + 1) + 1) * 2;
for (int w = 1; w <= M; ++w) cout << F[w] << endl;
}
void TDPC_T() {
const int MOD = 1000000007;
using mint = Fp<MOD>;
long long K, N;
cin >> K >> N;
--N;
FPS<mint> P(K), Q(K + 1);
Q[0] = 1;
for (int i = 0; i < P.size(); ++i) P[i] = mint(1 - i);
for (int i = 1; i < Q.size(); ++i) Q[i] = mint(-1);
cout << BostanMori(P, Q, N) << endl;
}
int main() {
//Yosupo_Inv_of_FPS();
//Yosupo_Exp_of_FPS();
//Yosupo_Log_of_FPS();
Yosupo_Pow_of_FPS();
//Yosupo_Sqrt_of_FPS();
//HackerRankArrayRestoring();
//Codeforces205Div1E();
//TDPC_T();
}