多项式板子

多项式板子

多项式模板

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
namespace Poly
{
#define ll long long
#define poly vector <ll>
const int N = 4e5 + 10;
const int mod = 998244353;
const int g = 3;
const int gi = 332748118;
const int inv2 = (mod + 1) >> 1;

namespace MOD
{
ll add(ll x, ll y){return x + y >= mod ? x + y - mod : x + y;}
ll dec(ll x, ll y){return x - y < 0 ? x - y + mod : x - y;}
}
using namespace MOD;

namespace IO
{
inline ll read()
{
ll x = 0, f = 1;char ch = getchar();
while(!isdigit(ch)){if(ch == '-')f = -1;ch = getchar();}
while(isdigit(ch)){x =((x << 3ll) + (x << 1ll)) + (ch ^ 48ll);ch = getchar();}
return x * f;
}
inline void write(ll x)
{
char ch[100];int len = 0;
if(x == 0)ch[++len] = '0';
if(x < 0)putchar('-'), x = -x;
while(x != 0){ch[++len] = x % 10ll + '0'; x /= 10ll;}
while(len)putchar(ch[len--]);
}
}

using namespace IO;

int rev[N];

ll p[3][50], fv[N];

inline void read(poly &A, int n)
{
A.resize(n + 1);
for(int i = 0; i <= n; i++)
A[i] = read();
}

inline void print(poly B)
{
for(auto x : B)write(x), putchar(' ');
}

inline ll qpow(ll a, ll b)
{
ll t = 1;
while(b != 0)
{
if(b & 1)t = t * a % mod;
a = a * a % mod; b >>= 1;
}
return t;
}

inline ll inv(ll x)
{
return qpow(x, mod - 2);
}

inline void initfv()
{
fv[1] = 1;
for(ll i = 2; i <=(N - 10) / 4; i++)
fv[i] = fv[mod % i] * (mod - mod / i) % mod;
}

inline void prework()
{
for(int k = 1, i = 1; k < N; k <<= 1, i++)
{
p[0][i] = qpow(g, (mod - 1) / (k << 1));
p[1][i] = qpow(gi, (mod - 1) / (k << 1));
p[2][i] = inv(k << 1);
}
}

inline void init(int n, int m, int &len)
{
len = 1; int cnt = 0;
while(len <= (n + m))len <<= 1, cnt++;
for(int i = 0; i < len; i++)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (cnt - 1));
}

inline void NTT(poly &a, int len, int type)
{
for(int i = 0; i < len; i++)
if(i < rev[i])swap(a[i], a[rev[i]]);
for(int k = 1, o = 1; k < len; k <<= 1, o++)
{
ll x = type == 1 ? p[0][o] : p[1][o];
for(int i = 0; i < len; i += k << 1)
{
ll w = 1;
for(int j = 0; j < k; j++)
{
ll y = a[i + j];
ll z = w * a[i + j + k] % mod;
a[i + j] = add(y, z);
a[i + j + k] = dec(y, z);
w = w * x % mod;
}
}
}
if(type == -1)
{
ll iv = p[2][(int)log2(len)];
for(int i = 0; i < len; i++)
a[i] = a[i] * iv % mod;
}
}

inline poly operator + (poly A, poly B)
{
int n = max(A.size(), B.size());
A.resize(n); B.resize(n);
for(int i = 0; i < n; i++)
A[i] = add(A[i], B[i]);
return A;
}
inline poly operator - (poly A, poly B)
{
int n = max(A.size(), B.size());
A.resize(n); B.resize(n);
for(int i = 0; i < n; i++)
A[i] = dec(A[i], B[i]);
return A;
}
inline poly operator * (poly A, poly B)
{
int len, n = A.size() - 1, m = B.size() - 1;
init(n, m, len);
A.resize(len); B.resize(len);
NTT(A, len, 1); NTT(B, len, 1);
for(int i = 0; i < len; i++)
A[i] = A[i] * B[i] % mod;
NTT(A, len, -1); A.resize(n + m + 1);
return A;
}

inline void Divide(int l, int r, poly &F, poly &G)
{
if(l == r)return;
int mid = (l + r) >> 1;
Divide(l, mid, F, G);
int n = mid - l + 1;
int m = r - l + 1;
poly A; A.resize(n);
poly B; B.resize(m);
for(int i = l; i <= mid; i++)
A[i - l] = F[i];
for(int i = 1; i <= r - l; i++)
B[i] = G[i];
A = A * B;
for(int i = mid + 1; i <= r; i++)
F[i] = add(F[i], A[i - l]);
Divide(mid + 1, r, F, G);
}

inline void Inverse(poly &A, poly &B, int n)
{
if(n == 1)return void(B[0] = inv(A[0]));
Inverse(A, B, (n + 1) >> 1);
int len; init(n, n, len);
poly C; C.resize(len); B.resize(len);
for(int i = 0; i < n; i++)C[i] = A[i];
NTT(C, len, 1); NTT(B, len, 1);
for(int i = 0; i < len; i++)
B[i] = dec(2, B[i] * C[i] % mod) * B[i] % mod;
NTT(B, len, -1); B.resize(n);
}

inline void Diff(poly &A, poly &B)
{
int n = A.size() - 1;
B.resize(n);
for(ll i = 1; i <= n; i++)
B[i - 1] = i * A[i] % mod;
}

inline void Integ(poly &A, poly &B)
{
int n = A.size();
B.resize(n + 1);
for(int i = 1; i <= n; i++)
B[i] = A[i - 1] * fv[i] % mod;
B[0] = 0;
}

inline void Ln(poly &A, poly &B)
{
int n = A.size() - 1;
poly F; poly G; G.resize(1);
Diff(A, F); Inverse(A, G, n + 1);
F = F * G;
Integ(F, B); B.resize(n + 1);
}

inline void Exp(poly &A, poly &B, int n)
{
if(n == 1)return void(B[0] = 1);
Exp(A, B, (n + 1) >> 1);
poly G; B.resize(n);
Ln(B, G); G[0] -= 1; B = (A - G) * B;
B.resize(n);
}

inline void Sqrt(poly &A, poly &B, int n)
{
if(n == 1)return void(B[0] = 1);
Sqrt(A, B, (n + 1) >> 1);
int len; init(n, n, len); B.resize(n);
poly G, H; H.resize(1); Inverse(B, H, n);
G.resize(len); H.resize(len); B.resize(len);
for(int i = 0; i < n; i++)G[i] = A[i];
NTT(H, len, 1); NTT(B, len, 1); NTT(G, len, 1);
for(int i = 0; i < len; i++)
B[i] = add(B[i], G[i] * H[i] % mod) * inv2 % mod;
NTT(B, len, -1); B.resize(n);
}

inline void Pow(poly &A, poly &B, ll k)
{
int n = A.size() - 1;
poly C; Ln(A, C);
for(int i = 0; i <= n; i++)
C[i] = C[i] * k % mod;
Exp(C, B, n + 1);
}

inline void Mod(poly &F, poly &G, poly &A, poly &B)
{
poly f = F, g = G, h;
int n = F.size() - 1, m = G.size() - 1;
reverse(f.begin(), f.end());
reverse(g.begin(), g.end());
g.resize(n - m + 1);
h.resize(1); Inverse(g, h, n - m + 1);
A = f * h; A.resize(n - m + 1);
reverse(A.begin(), A.end());
B = F - (A * G); B.resize(m);
}
}

using namespace Poly;
作者

Jekyll_Y

发布于

2022-09-30

更新于

2023-03-02

许可协议

评论