lyre's Blog

不敢幻想的明天和明天
交于夏的延长线

0%

多项式板子

随便扔个多项式板子,没写 DIF-DIT,跑的很慢(

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
# include <iostream>
# include <algorithm>
# include <vector>
# include <cassert>

using namespace std;

namespace lyre {
constexpr int P(998'244'353);
constexpr int pow(int a, int n) {
auto r(1);
while (n) {
r = 1l * r * (n & 1 ? a : 1) % P;
n >>= 1, a = 1l * a * a % P;
}
return r;
}
constexpr int inv(int a)
{ return pow(a, P - 2); }
class poly : public vector<int> {
public:
using vector<int>::vector;
vector<int> trans;
inline void expand(size_t n = 0) {
if (!n) n = size();
if (n != (n & -n)) n = 1 << (__lg(n) + 1);
if (n > size()) resize(n);
}
inline void shrink() {
while (!empty() && !back()) pop_back();
}
inline int& operator[](size_t n) {
if (n >= size()) resize(n + 1);
return at(n);
}
inline int operator[](size_t n) const {
if (n >= size()) return 0;
return at(n);
}
poly operator+(poly const &g) const {
auto f(*this);
f.resize(max(size(), g.size()));
for (auto i(0u); i < f.size(); ++i)
f[i] = (f[i] + g[i]) % P;
return f;
}
poly operator-(poly const &g) const {
auto f(*this);
f.resize(max(size(), g.size()));
for (auto i(0u); i < f.size(); ++i)
f[i] = (f[i] + P - g[i]) % P;
return f;
}
poly operator*(int const x) const {
auto f(*this);
for (auto i(0u); i < f.size(); ++i)
f[i] = 1l * f[i] * x % P;
return f;
}
friend poly operator*(int const x, poly const &o) {
auto f(o);
for (auto i(0u); i < f.size(); ++i)
f[i] = 1l * f[i] * x % P;
return f;
}
void init(size_t n) {
assert(n == (n & -n));
if (trans.size() == n) return;
trans = vector<int>(n);
for (auto i(0u); i < n; ++i)
trans[i] = (trans[i >> 1] >> 1) + (i & 1) * (n >> 1);
}
void dif() {
int const n(size());
assert(n == (n & -n));
init(n);
for (int i(0); i < n; ++i)
if (i < trans[i]) std::swap(at(i), at(trans[i]));
for (int mid(1); mid < n; mid *= 2) {
auto w1(lyre::pow(3, (P - 1) / (mid * 2)));
for (int i(0); i < n; i += mid * 2) {
auto wk(1);
for (int j(0), k(mid); j < mid; ++j, ++k) {
auto x(at(i + j));
auto y(1l * wk * at(i + k) % P);
at(i + j) = (x + y) % P;
at(i + k) = (x + P - y) % P;
wk = 1l * wk * w1 % P;
}
}
}
}
void dit() {
int const n(size());
assert(n == (n & -n));
init(n);
for (int i(0); i < n; ++i)
if (i < trans[i]) std::swap(at(i), at(trans[i]));
for (int mid(1); mid < n; mid *= 2) {
auto w1(lyre::pow(lyre::inv(3), (P - 1) / (mid * 2)));
for (int i(0); i < n; i += mid * 2) {
auto wk(1);
for (int j(0), k(mid); j < mid; ++j, ++k) {
auto x(at(i + j));
auto y(1l * wk * at(i + k) % P);
at(i + j) = (x + y) % P;
at(i + k) = (x + P - y) % P;
wk = 1l * wk * w1 % P;
}
}
}
for (int i(0), v(lyre::inv(n)); i < n; ++i)
at(i) = 1l * at(i) * v % P;
}
poly operator*(poly const &o) const {
auto f(*this), g(o);
poly h(f.size() + g.size() - 1);
h.expand();
f.resize(h.size()), f.dif();
g.resize(h.size()), g.dif();
for (auto i(0u); i < h.size(); ++i)
h[i] = 1l * f[i] * g[i] % P;
h.dit();
return h;
}
poly inv(size_t n) const {
poly f(1), g(1);
g[0] = lyre::inv(f[0] = at(0));
for (size_t i(1); i < n; i *= 2) {
f.resize(i * 2);
for (auto j(i); j < min(i * 2, n); ++j)
f[j] = operator[](j);
f.expand(i * 3), f.dif();
g.expand(i * 3), g.dif();
for (size_t j(0); j < g.size(); ++j)
g[j] = (2 + P - 1l * f[j] * g[j] % P) * g[j] % P;
f.dit(), f.resize(i * 2);
g.dit(), g.resize(i * 2);
}
g.resize(n);
return g;
}
poly derivative() const {
poly f;
f.reserve(size());
for (auto i(1u); i < size(); ++i)
f.emplace_back(1l * i * at(i) % P);
return f;
}
poly integral() const {
poly f(1);
f.reserve(size());
for (auto i(0u); i < size(); ++i)
f.emplace_back(1l * at(i) * lyre::inv(i + 1) % P);
return f;
}
poly ln(size_t n) const {
auto f(derivative()), g(inv(n));
auto h((f * g).integral());
h.resize(n);
return h;
}
poly exp(size_t n) const {
poly f(1), g(1), I(1);
I[0] = g[0] = 1;
for (size_t i(1); i < n; i *= 2) {
f.resize(i * 2);
for (auto j(i); j < min(i * 2, n); ++j)
f[j] = operator[](j);
g = g * (I - g.ln(i * 2) + f);
g.resize(i * 2);
}
g.resize(n);
return g;
}
poly pow(size_t n, int k) const {
auto f(*this);
auto ord(0), p(0);
reverse(f.begin(), f.end());
while (!f.empty() && !f.back())
++ord, f.pop_back();
reverse(f.begin(), f.end());
if (f.empty() || 1ul * ord * k > n)
return poly(n);
p = f[0], f = f * lyre::inv(p);
p = lyre::pow(p, k);
auto g((f.ln(n) * k).exp(n) * p);
reverse(g.begin(), g.end());
g.insert(g.end(), ord * k, 0);
reverse(g.begin(), g.end());
g.resize(n);
return g;
}
void print(size_t n = 0) const {
if (!n) n = size();
cerr << "[ ";
for (auto i(0u); i < n; ++i)
cerr << operator[](i) << (i + 1 < n ? ", " : " ]");
cerr << endl;
}
};
}