Dyd's Blog

He who has a strong enough why can bear almost any how.

多项式

多项式没家桶

多项式

前置

  • NTT
  • FFT
  • 一定的数学

多项式乘法

NTT/FFT即可

毒瘤卡常之预处理原根(然鹅不开O2有可能反向优化,慎用):

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
#define VIT vector<int>::iterator
vector<int> g[N], _g[N];
void init(int len)
{
int tot = 1, gn, mid, i, _gn, t, _t;
while (tot < (len << 1))
tot <<= 1;
for (mid = 1; mid < tot; mid <<= 1)
{
gn = qpow(3, (P - 1) / (mid << 1));
_gn = qpow(gn, P - 2);
t = _t = 1;
for (i = 0; i < mid; ++i)
{
g[mid].push_back(t);
_g[mid].push_back(_t);
t = (LL)t * gn % P;
_t = (LL)_t * _gn % P;
}
}
}
void ntt(int x[], int tot, int op)
{
int i, j, mid, len, a, b;
VIT gn;
for (i = 0; i < tot; ++i)
if (i < r[i])
swap(x[i], x[r[i]]);
for (mid = 1; mid < tot; mid <<= 1)
{
len = mid << 1;
gn = (op == 1 ? g[mid].begin() : _g[mid].begin());
for (i = 0; i < tot; i += len)
for (j = 0; j < mid; ++j)
{
a = x[i + j], b = (LL)gn[j] * x[i + j + mid] % P;
x[i + j] = (a + b) % P;
x[i + j + mid] = (a - b + P) % P;
}
}
if (op == 1)
return ;
a = qpow(tot, P - 2);
for (i = 0; i < tot; ++i)
x[i] = (LL)x[i] * a % P;
}

多项式求逆

问题:

板子

对于给定 $F (x)$ ,求出 $G(x)$ 使得 $ G(x)F(x) \equiv 1 \pmod {x^n}$ ,其中 $A(x) \equiv 1 \pmod {x^n}$ 指 $A(x)$ 次数低于 $n$ 的项只有常数为1,其余都为0,即 $A(x) = 1 + 0x + 0x^2 + 0x^3 + … + 0x^{n - 1} + a_nx^n + a_{n + 1}x^{n + 1} + …$

做法:

考虑倍增,变形:
$$
\begin{aligned}
设G_0(x)满足:\\
& G_0(x)F(x) \equiv 1 \pmod {x^{\lceil \frac{n}{2} \rceil}}\\
故:\\
& G(x) - G_0(x) \equiv 0 \pmod {x^{\lceil \frac{n}{2} \rceil}}\\
& (G(x) - G_0(x))^2 \equiv 0 \pmod {x^n}\\
& G^2(x) - 2G(x)G_0(x) + G_0^2(x) \equiv 0 \pmod {x^n}\\
\because & G(x)F(x) \equiv 1 \pmod {x^n}\\
\therefore & G(x) - 2G_0(x) + G_0^2(x)F(x) \equiv 0 \pmod {x^n}\\
& G(x) \equiv (2 - G_0(x)F(x))G_0(x) \pmod {x^n}
\end{aligned}
$$
注意特判常数项要有逆,然后倍增即可,时间 $O(n \log n)$

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
#include <bits/stdc++.h>
#define LL long long
#define STC static
using namespace std;
const int N = 3e5 + 5, P = 998244353;
int r[N];
int qpow(int x, int y)
{
int res = 1;
while (y)
{
if (y & 1)
res = (LL)res * x % P;
x = (LL)x * x % P;
y >>= 1;
}
return res;
}
void ntt(int x[], int tot, int op)
{
for (int i = 0; i < tot; ++i)
if (i < r[i])
swap(x[i], x[r[i]]);
int mid, i, j, len, gn, g, a, b;
for (mid = 1; mid < tot; mid <<= 1)
{
len = mid << 1;
gn = qpow(3, (P - 1) / len);
if (op == -1)
gn = qpow(gn, P - 2);
for (i = 0; i < tot; i += len)
{
g = 1;
for (j = 0; j < mid; ++j, g = (LL)g * gn % P)
{
a = x[i + j], b = (LL)g * x[i + j + mid] % P;
x[i + j] = (a + b) % P;
x[i + j + mid] = (a - b + P) % P;
}
}
}
if (op == 1)
return ;
//逆变换记得除
gn = qpow(tot, P - 2);
for (int i = 0; i < tot; ++i)
x[i] = (LL)x[i] * gn % P;
}
void polyinv(int len, int x[], int y[]) //将x求逆,答案为y
{
if (len == 1)
{
y[0] = qpow(x[0], P - 2);
return ;
}
polyinv((len + 1) >> 1, x, y);
int bit = 0, tot = 1;
while (tot < (len << 1))
tot <<= 1, ++bit;
STC int t[N];
for (int i = ((len + 1) >> 1); i < tot; i++)
y[i] = 0;
for (int i = 0; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
for (int i = 0; i < len; ++i)
t[i] = x[i];
for (int i = len; i < tot; ++i)
t[i] = 0;
ntt(t, tot, 1), ntt(y, tot, 1);
for (int i = 0; i < tot; ++i)
y[i] = (2 - (LL)t[i] * y[i] % P + P) % P * y[i] % P;
ntt(y, tot, -1);
for (int i = len; i < tot; ++i)
y[i] = 0;
}
int main()
{
STC int a[N], b[N];
int n;
scanf("%d", &n);
--n;
for (int i = 0; i <= n; ++i)
scanf("%d", &a[i]), a[i] = (a[i] + P) % P;
polyinv(n + 1, a, b); //是长度,要加1
for (int i = 0; i <= n; ++i)
printf("%d ", b[i]);
return 0;
}

多项式除法和取模

问题:

板子

对于给定 $F(x), G(x)$ ,其中 $deg(F) \ge deg(G)$ ,不妨设 $deg(F) = n, deg(G) = m$ ,求 $Q(x), R(x)$ 使得 $F(x) = Q(x)G(x) + R(x)$ ,且 $deg(Q) = n - m, deg(R) = m - 1$ ,其中 $R(x)$ 最高位可以为0

做法:

对于多项式 $A(x)$ ,记 $A_r(x) = x^{deg(A)}A(\frac{1}{x})$ (其实就是把 $A$ 的系数翻转)

变形:
$$
\begin{aligned}
& F(\frac{1}{x}) = Q(\frac{1}{x}) G(\frac{1}{x}) + R(\frac{1}{x})\\
& x^n F(\frac{1}{x}) = x^{n -m} Q(\frac{1}{x}) x^m G(\frac{1}{x}) + x^{n - m + 1} x^{m - 1} R(\frac{1}{x})\\
& F_r(x) = Q_r(x) G_r(x) + x^{n - m + 1} R_r(x)\\
& F_r(x) \equiv Q_r(x) G_r(x) \pmod {x^{n - m + 1}}\\
& Q_r(x) \equiv F_r(x) G_r^{-1}(x) \pmod {x^{n - m + 1}}
\end{aligned}
$$
多项式求逆得 $G_r^{-1}(x)$ ,多项式乘法得 $Q_r(x)$ ,计算即可得 $Q(x), R(x)$ ,时间还是 $O(n \log n)$

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
#include <bits/stdc++.h>
#define LL long long
#define STC static
using namespace std;
const int N = 3e5 + 5, P = 998244353;
int r[N];
int qpow(int x, int y)
{
int res = 1;
while (y)
{
if (y & 1)
res = (LL)res * x % P;
x = (LL)x * x % P;
y >>= 1;
}
return res;
}
void ntt(int x[], int tot, int op)
{
for (int i = 0; i < tot; ++i)
if (i < r[i])
swap(x[i], x[r[i]]);
int mid, i, j, len, gn, g, a, b;
for (mid = 1; mid < tot; mid <<= 1)
{
len = mid << 1;
gn = qpow(3, (P - 1) / len);
if (op == -1)
gn = qpow(gn, P - 2);
for (i = 0; i < tot; i += len)
{
g = 1;
for (j = 0; j < mid; ++j, g = (LL)g * gn % P)
{
a = x[i + j], b = (LL)g * x[i + j + mid] % P;
x[i + j] = (a + b) % P;
x[i + j + mid] = (a - b + P) % P;
}
}
}
if (op == 1)
return ;
gn = qpow(tot, P - 2);
for (int i = 0; i < tot; ++i)
x[i] = (LL)x[i] * gn % P;
}
void polyinv(int x[], int y[], int len)
{
if (len == 1)
{
y[0] = qpow(x[0], P - 2);
return ;
}
polyinv(x, y, (len + 1) >> 1);
int bit = 0, tot = 1;
while (tot < (len << 1))
tot <<= 1, ++bit;
STC int t[N];
for (int i = ((len + 1) >> 1); i < tot; i++)
y[i] = 0;
for (int i = 0; i < len; ++i)
t[i] = x[i];
for (int i = len; i < tot; ++i)
t[i] = 0;
for (int i = 0; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
ntt(t, tot, 1), ntt(y, tot, 1);
for (int i = 0; i < tot; ++i)
y[i] = (2 - (LL)y[i] * t[i] % P + P) * y[i] % P;
ntt(y, tot, -1);
for (int i = len; i < tot; ++i)
y[i] = 0;
}
void polydiv(int x[], int n, int y[], int m, int a[], int b[]) //x除以y,商a余b
{
STC int ta[N], tb[N];
int dt = n - m + 1, tot = 1, bit = 0;
while (tot < (n << 1))
tot <<= 1, ++bit;
for (int i = 0; i < tot; ++i)
ta[i] = tb[i] = 0;
//求逆
for (int i = 0; i < m; ++i)
ta[i] = y[m - i - 1];
polyinv(ta, tb, dt); //这里就不加1了
//求乘积
for (int i = 0; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
for (int i = 0; i < n; ++i)
ta[i] = x[n - i - 1];
ntt(ta, tot, 1), ntt(tb, tot, 1);
for (int i = 0; i < tot; ++i)
ta[i] = (LL)ta[i] * tb[i] %P;
ntt(ta, tot, -1);
for (int i = 0; i < dt; ++i)
a[i] = ta[dt - i - 1];
//求余式
for (int i = 0; i < tot; ++i)
ta[i] = tb[i] = 0;
for (int i = 0; i < dt; ++i)
ta[i] = a[i];
for (int i = 0; i < m; ++i)
tb[i] = y[i];
ntt(ta, tot, 1), ntt(tb, tot, 1);
for (int i = 0; i < tot; ++i)
ta[i] = (LL)ta[i] * tb[i] % P;
ntt(ta, tot, -1);
for (int i = 0; i < m - 1; ++i)
b[i] = (x[i] - ta[i] + P) % P;
}
int main()
{
STC int a[N], b[N], as1[N], as2[N];
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i <= n; ++i)
scanf("%d", &a[i]), a[i] = (a[i] + P) % P;
for (int i = 0; i <= m; ++i)
scanf("%d", &b[i]), b[i] = (b[i] + P) % P;
polydiv(a, n + 1, b, m + 1, as1, as2); //多项式注意+1
for (int i = 0; i <= n - m; ++i)
printf("%d ", as1[i]);
puts("");
for (int i = 0; i <= m - 1; ++i)
printf("%d ", as2[i]);
return 0;
}

多项式开根

问题:

板子

给定 $F(x)$ ,求 $G(x)$ 满足 $G^2(x) \equiv F(x) \pmod {x^n}$

做法:

当然可以多项式快速幂求 $F^{\frac{1}{2}}(x)$

也可以考虑倍增,变形:
$$
\begin{aligned}
设G_0(x)满足:\\
& G_0^2(x) \equiv F(x) \pmod {x^{\lceil \frac{n}{2} \rceil}}\\
故:\\
& G^2(x) - G_0^2(x) \equiv 0 \pmod {x^{\lceil \frac{n}{2} \rceil}}\\
& (G(x) + G_0(x)) (G(x) - G_0(x)) \equiv 0 \pmod {x^{\lceil \frac{n}{2} \rceil}}\\
& G(x) - G_0(x) \equiv 0 \pmod {x^{\lceil \frac{n}{2} \rceil}}\\
& G^2(x) - 2G(x)G_0(x) + G_0^2(x) \equiv 0 \pmod {x^n}\\
& F(x) - 2G(x) G_0(x) + G_0^2(x) \equiv 0 \pmod {x^n}\\
& G(x) \equiv \frac{G_0^2(x) + F(x)}{2G_0(x)} \pmod {x^n}
\end{aligned}
$$
边界条件是常数项必须是模 $P$ 的二次剩余,时间 $O(n \log n)$

代码懒得打了,反正次幂也行

牛顿迭代

牛顿是一种思考的方法,多项式的题很多都用倍增,但每一道题都手推式子太过痛苦,于是,我们可以(更痛苦的)推出式子

问题:

对于给定 $F(x)$ ,求 $G(x)$ ,使 $F(G(x)) \equiv 0 \pmod {x^n}$

做法:

推式子(啊啊啊我要疯了):

呃……突然发现康托展开我不会,推个寂寞,直接给结论:
$$
G(x) \equiv G_0(x) - \frac{F(G_0(x))}{F’(G_0(x))} \pmod {x^n}
$$

多项式求导和积分

来两个简单的,直接算就行了

求导:

1
2
3
for (int i = 1; i < len; ++i)
res[i - 1] = (LL)x[i] * i % P;
res[len - 1] = 0;

积分:

1
2
3
4
//逆元可以预处理
for (int i = len - 1; i >= 1; --i)
res[i] = (LL)x[i - 1] * inv(i) % P;
res[0] = 0;

多项式对数

问题:

板子

对于给定 $F(x)$ ,求出 $G(x)$ 使得 $G(x) \equiv \ln(F(x)) \pmod {x^n}$

做法:

求导,有 $G’(x) \equiv \frac{F’(x)}{F(x)} \pmod {x^n}$

多项式求导得 $F’(x)$ ,求逆得 $\frac{1}{F(x)}$ ,乘法得 $G’(x)$ ,积分出 $G(x)$ ,时间 $O(n \log n)$ ,注意要保证常数项 $f_0 = 1$

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
#include <bits/stdc++.h>
#define LL long long
#define STC static
using namespace std;
const int N = 4e5 + 5, P = 998244353;
int r[N];
int qpow(int x, int y)
{
int res = 1;
while (y)
{
if (y & 1)
res = (LL)res * x % P;
x = (LL)x * x % P;
y >>= 1;
}
return res;
}
void ntt(int x[], int tot, int op)
{
int i, j, mid, len, gn, g, a, b;
for (i = 0; i < tot; ++i)
if (i < r[i])
swap(x[i], x[r[i]]);
for (mid = 1; mid < tot; mid <<= 1)
{
len = mid << 1;
gn = qpow(3, (P - 1) / len);
if (op == -1)
gn = qpow(gn, P - 2);
for (i = 0; i < tot; i += len)
{
g = 1;
for (j = 0; j < mid; ++j, g = (LL)g * gn % P)
{
a = x[i + j], b = (LL)g * x[i + j + mid] % P;
x[i + j] = (a + b) % P;
x[i + j + mid] = (a - b + P) % P;
}
}
}
if (op == 1)
return ;
gn = qpow(tot, P - 2);
for (i = 0; i < tot; ++i)
x[i] = (LL)x[i] * gn % P;
}
void polyinv(int x[], int y[], int len)
{
if (len == 1)
{
y[0] = qpow(x[0], P - 2);
return ;
}
polyinv(x, y, (len + 1) >> 1);
int bit = 0, tot = 1;
while (tot < (len << 1))
tot <<= 1, ++bit;
STC int t[N];
for (int i = ((len + 1) >> 1); i < tot; i++)
y[i] = 0;
for (int i = 0; i < len; ++i)
t[i] = x[i];
for (int i = len; i < tot; ++i)
t[i] = 0;
for (int i = 0; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
ntt(t, tot, 1), ntt(y, tot, 1);
for (int i = 0; i < tot; ++i)
y[i] = (2 - (LL)y[i] * t[i] % P + P) * y[i] % P;
ntt(y, tot, -1);
for (int i = len; i < tot; ++i)
y[i] = 0;
}
void polydif(int x[], int y[], int len) //differentiate
{
for (int i = 1; i < len; ++i)
y[i - 1] = (LL)x[i] * i % P;
y[len - 1] = 0;
}
void polyint(int x[], int y[], int len) //integral
{
for (int i = 1; i < len; ++i)
y[i] = (LL)x[i - 1] * qpow(i, P - 2) % P;
y[0] = 0;
}
void polyln(int x[], int y[], int len)
{
STC int a[N], b[N];
polydif(x, a, len);
polyinv(x, b, len);
int bit = 0, tot = 1;
while (tot < (len << 1))
tot <<= 1, ++bit;
for (int i = 1; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
ntt(a, tot, 1), ntt(b, tot, 1);
for (int i = 0; i < tot; ++i)
a[i] = (LL)a[i] * b[i] % P;
ntt(a, tot, -1);
polyint(a, y, len);
}
int main()
{
STC int n, a[N], b[N];
scanf("%d", &n);
for (int i = 0; i < n; ++i)
scanf("%d", &a[i]), a[i] = (a[i] + P) % P;
polyln(a, b, n);
for (int i = 0; i < n; ++i)
printf("%d ", b[i]);
return 0;
}

多项指数

问题:

板子

对于给定 $F(x)$ ,求 $G(x)$ 使得 $G(x) \equiv e^{F(x)} \pmod {x^n}$

做法:

两边取对数得, $\ln(G(x)) \equiv F(x) \pmod {x^n}$

由牛顿迭代:
$$
\begin{aligned}
G(x) \equiv G_0(x) - \frac{\ln(G_0(x)) - F(x)}{\frac{1}{G_0(x)}} \pmod {x^n}\\
G(x) \equiv (1 - \ln(G_0(x)) + F(x)) G_0(x) \pmod {x^n}\\
\end{aligned}
$$

时间 $O(n \log n)$ ,注意一定要有 $f_0 = 0$

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
#include <bits/stdc++.h>
#define LL long long
#define STC static

using namespace std;
const int N = 4e5 + 5, P = 998244353;
int r[N];
int qpow(int x, int y)
{
int res = 1;
while (y)
{
if (y & 1)
res = (LL)res * x % P;
x = (LL)x * x % P;
y >>= 1;
}
return res;
}
void ntt(int x[], int tot, int op)
{
int i, j, mid, len, gn, g, a, b;
for (i = 0; i < tot; ++i)
if (i < r[i])
swap(x[i], x[r[i]]);
for (mid = 1; mid < tot; mid <<= 1)
{
len = mid << 1;
gn = qpow(3, (P - 1) / len);
if (op == -1)
gn = qpow(gn, P - 2);
for (i = 0; i < tot; i += len)
{
g = 1;
for (j = 0; j < mid; ++j, g = (LL)g * gn % P)
{
a = x[i + j], b = (LL)g * x[i + j + mid] % P;
x[i + j] = (a + b) % P;
x[i + j + mid] = (a - b + P) % P;
}
}
}
if (op == 1)
return ;
gn = qpow(tot, P - 2);
for (i = 0; i < tot; ++i)
x[i] = (LL)x[i] * gn % P;
}
void polyinv(int x[], int y[], int len)
{
if (len == 1)
{
y[0] = qpow(x[0], P - 2);
return ;
}
polyinv(x, y, (len + 1) >> 1);
int bit = 0, tot = 1;
while (tot < (len << 1))
tot <<= 1, ++bit;
STC int t[N];
for (int i = ((len + 1) >> 1); i < tot; i++)
y[i] = 0;
for (int i = 0; i < len; ++i)
t[i] = x[i];
for (int i = len; i < tot; ++i)
t[i] = 0;
for (int i = 0; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
ntt(t, tot, 1), ntt(y, tot, 1);
for (int i = 0; i < tot; ++i)
y[i] = (2 - (LL)y[i] * t[i] % P + P) * y[i] % P;
ntt(y, tot, -1);
for (int i = len; i < tot; ++i)
y[i] = 0;
}
void polydif(int x[], int y[], int len)
{
for (int i = 1; i < len; ++i)
y[i - 1] = (LL)x[i] * i % P;
y[len - 1] = 0;
}
void polyint(int x[], int y[], int len)
{
for (int i = 1; i < len; ++i)
y[i] = (LL)x[i - 1] * qpow(i, P - 2) % P;
y[0] = 0;
}
void polyln(int x[], int y[], int len)
{
STC int a[N], b[N];
polydif(x, a, len);
polyinv(x, b, len);
int bit = 0, tot = 1;
while (tot < (len << 1))
tot <<= 1, ++bit;
for (int i = 0; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
for (int i = len; i < tot; i++)
a[i] = b[i] = 0;
ntt(a, tot, 1), ntt(b, tot, 1);
for (int i = 0; i < tot; ++i)
a[i] = (LL)a[i] * b[i] % P;
ntt(a, tot, -1);
for (int i = len; i < tot; i++)
a[i] = 0;
polyint(a, y, len);
for (int i = len; i < tot; i++)
y[i] = 0;
}
void polyexp(int x[], int y[], int len)
{
if (len == 1)
{
y[0] = 1;
return ;
}
int bit = 0, tot = 1;
while (tot < (len << 1))
tot <<= 1, ++bit;
polyexp(x, y, (len + 1) >> 1);
STC int t[N];
for (int i = 0; i < tot; ++i)
t[i] = 0;
polyln(y, t, len);
t[0] = (x[0] + 1 - t[0] + P) % P;
for (int i = 1; i < len; ++i)
t[i] = (x[i] - t[i] + P) % P;
for (int i = len; i < tot; ++i)
t[i] = 0;
for (int i = 0; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
ntt(t, tot, 1), ntt(y, tot, 1);
for (int i = 0; i < tot; ++i)
y[i] = (LL)y[i] * t[i] % P;
ntt(y, tot, -1);
for (int i = len; i < tot; ++i)
y[i] = 0;
}
int main()
{
STC int n, a[N], b[N];
scanf("%d", &n);
for (int i = 0; i < n; ++i)
scanf("%d", &a[i]), a[i] = (a[i] + P) % P;
polyexp(a, b, n);
for (int i = 0; i < n; ++i)
printf("%d ", b[i]);
return 0;
}

多项式次幂

问题:

板子

对于给定 $F(x), k$ ,求 $G(x)$ 使得 $G(x) \equiv F^k(x) \pmod {x^n}$

做法:

推式子:
$$
\begin{aligned}
& G(x) \equiv F^k(x) \pmod {x^n}\\
& \ln(G(x)) \equiv k \ln(F(x)) \pmod {x^n}\\
\end{aligned}
$$
对 $F(x)$ 取 $\ln$ ,乘 $k$ 以后 $\exp$ 即可

但是并不保证常数项为1,则求 $\exp$ 时边界不一定为1了,不能直接取对数,所以提公因式,设 $r$ 为 $F(x)$ 的第一个非0项次数,提取公因式 $a_rx^r$ 就可以保证最低位是1了

考虑提公因式对答案的影响:我们提了公因式后,最后答案的前 $k * r$ 项都为0,后面的项就是我们求出来的,最后一起乘一个 $a_r^m$ 即可

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
#include <bits/stdc++.h>
#define LL long long
#define STC static
using namespace std;
const int N = 4e5 + 5, P = 998244353, K = 1e5 + 5;
int r[N];
int qpow(int x, int y)
{
int res = 1;
while (y)
{
if (y & 1)
res = (LL)res * x % P;
x = (LL)x * x % P;
y >>= 1;
}
return res;
}
void ntt(int x[], int tot, int op)
{
int i, j, mid, len, gn, g, a, b;
for (i = 0; i < tot; ++i)
if (i < r[i])
swap(x[i], x[r[i]]);
for (mid = 1; mid < tot; mid <<= 1)
{
len = mid << 1;
gn = qpow(3, (P - 1) / len);
if (op == -1)
gn = qpow(gn, P - 2);
for (i = 0; i < tot; i += len)
{
g = 1;
for (j = 0; j < mid; ++j, g = (LL)g * gn % P)
{
a = x[i + j], b = (LL)g * x[i + j + mid] % P;
x[i + j] = (a + b) % P;
x[i + j + mid] = (a - b + P) % P;
}
}
}
if (op == 1)
return ;
gn = qpow(tot, P - 2);
for (i = 0; i < tot; ++i)
x[i] = (LL)x[i] * gn % P;
}
void polyinv(int x[], int y[], int len)
{
if (len == 1)
{
y[0] = qpow(x[0], P - 2);
return ;
}
polyinv(x, y, (len + 1) >> 1);
int bit = 0, tot = 1;
while (tot < (len << 1))
tot <<= 1, ++bit;
STC int t[N];
for (int i = ((len + 1) >> 1); i < tot; i++)
y[i] = 0;
for (int i = 0; i < len; ++i)
t[i] = x[i];
for (int i = len; i < tot; ++i)
t[i] = 0;
for (int i = 0; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
ntt(t, tot, 1), ntt(y, tot, 1);
for (int i = 0; i < tot; ++i)
y[i] = (2 - (LL)y[i] * t[i] % P + P) * y[i] % P;
ntt(y, tot, -1);
for (int i = len; i < tot; ++i)
y[i] = 0;
}
void polydif(int x[], int y[], int len)
{
for (int i = 1; i < len; ++i)
y[i - 1] = (LL)x[i] * i % P;
y[len - 1] = 0;
}
void polyint(int x[], int y[], int len)
{
for (int i = 1; i < len; ++i)
y[i] = (LL)x[i - 1] * qpow(i, P - 2) % P;
y[0] = 0;
}
void polyln(int x[], int y[], int len)
{
STC int a[N], b[N];
polydif(x, a, len);
polyinv(x, b, len);
int bit = 0, tot = 1;
while (tot < (len << 1))
tot <<= 1, ++bit;
for (int i = 0; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
for (int i = len; i < tot; i++)
a[i] = b[i] = 0;
ntt(a, tot, 1), ntt(b, tot, 1);
for (int i = 0; i < tot; ++i)
a[i] = (LL)a[i] * b[i] % P;
ntt(a, tot, -1);
for (int i = len; i < tot; i++)
a[i] = 0;
polyint(a, y, len);
for (int i = len; i < tot; i++)
y[i] = 0;
}
void polyexp(int x[], int y[], int len)
{
if (len == 1)
{
y[0] = 1;
return ;
}
int bit = 0, tot = 1;
while (tot < (len << 1))
tot <<= 1, ++bit;
polyexp(x, y, (len + 1) >> 1);
STC int t[N];
for (int i = 0; i < tot; ++i)
t[i] = 0;
polyln(y, t, len);
t[0] = (x[0] + 1 - t[0] + P) % P;
for (int i = 1; i < len; ++i)
t[i] = (x[i] - t[i] + P) % P;
for (int i = len; i < tot; ++i)
t[i] = 0;
for (int i = 0; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
ntt(t, tot, 1), ntt(y, tot, 1);
for (int i = 0; i < tot; ++i)
y[i] = (LL)y[i] * t[i] % P;
ntt(y, tot, -1);
for (int i = len; i < tot; ++i)
y[i] = 0;
}
/*
k1是模P后的次数
k2是模P-1的,用欧拉定理求第r项系数的k次幂
k3存真实的k的一部分,若r*k3 >= n说明前n项全是0
num记录r
*/
void polypow(int x[], int y[], int k1, int k2, int k3, int len)
{
int num = 0;
while (!x[num] && num < len)
++num;
if ((LL)num * k3 >= len)
{
for (int i = 0; i < len; ++i)
y[i] = 0;
return ;
}
STC int t[N];
int x0 = x[num], inv = qpow(x0, P - 2);
len -= num;
for (int i = 0; i < len; ++i)
x[i] = (LL)x[i + num] * inv % P;
int tot = 1;
while (tot < (len << 1))
tot <<= 1;
for (int i = 0; i < tot; ++i)
t[i] = 0;
for (int i = len; i < tot; ++i)
x[i] = 0;
polyln(x, t, len);
for (int i = 0; i < len; ++i)
t[i] = (LL)t[i] * k1 % P;
for (int i = len; i < tot; ++i)
t[i] = 0;
polyexp(t, y, len);
len += num;
x0 = qpow(x0, k2);
num = num * k3;
for (int i = len - 1; i >= num; --i)
y[i] = (LL)y[i - num] * x0 % P;
for (int i = num - 1; i >= 0; --i)
y[i] = 0;
}
int main()
{
STC int a[N], b[N];
STC char k[K];
int n, k1, k2, k3;
scanf("%d%s", &n, k + 1);
for (int i = 0; i < n; ++i)
scanf("%d", &a[i]), a[i] = (a[i] + P) % P;
k1 = k2 = k3 = 0;
for (int i = 1, l = strlen(k + 1); i <= l; ++i)
{
k1 = ((LL)k1 * 10 + (k[i] ^ 48)) % P;
k2 = ((LL)k2 * 10 + (k[i] ^ 48)) % (P - 1);
if ((LL)k3 * 10 + (k[i] ^ 48) <= P)
k3 = (LL)k3 * 10 + (k[i] ^ 48);
}
polypow(a, b, k1, k2, k3, n);
for (int i = 0; i < n; ++i)
printf("%d ", b[i]);
return 0;
}

多项式开根2

前置:Cipolla算法

如果 $y^2 \equiv x \pmod P$ ,则称 $x$ 为模 $P$ 的二次剩余(然鹅我们要求的是 $y$ )

而对于不保证 $f_0 = 0$ 的开根,我们必须要求出 $f_0$ 做 $x$ 时的 $y$

下面是一堆我听不懂的思路

可以找一个 $t$ ,使得 $t^2 - x$ 为非二次剩余,设 $w = t^2 - x$ ,则 $y = (t + \sqrt{w})^{\frac{P + 1}{2}}$ ,这里考虑把 $w$ 定义为负数中的 $i$ ,随机选 $t$ 尝试即可

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
#include <bits/stdc++.h>
#define LL long long
#define STC static
#define DRG default_random_engine
#define UID uniform_int_distribution
using namespace std;
int qpow(int x, int y);
const int N = 4e5 + 5, P = 998244353, K = 1e5 + 5;
namespace Cipolla
{
int w;
DRG e{114514};
UID<int> u(1, P - 1);
struct Complex
{
int x, y;
Complex operator * (const Complex t) const
{
return (Complex){((LL)x * t.x % P + (LL)y * t.y % P * w % P) % P, ((LL)x * t.y % P + (LL)y * t.x % P) % P};
}
};
Complex Cqpow(Complex x, int y)
{
Complex res = (Complex){1, 0};
while (y)
{
if (y & 1)
res = res * x;
x = x * x;
y >>= 1;
}
return res;
}
int Csqrt(int x)
{
if(qpow(x, (P - 1) >> 1) == P - 1)
return -1;
int t;
while (233)
{
t = u(e);
w = ((LL)t * t % P - x + P) % P;
if (qpow(w, (P - 1) >> 1) == P - 1)
break;
}
int res = Cqpow((Complex){t, 1}, (P + 1) >> 1).x;
return min(res, P - res);
}
}
using Cipolla::Csqrt;
int r[N];
int qpow(int x, int y)
{
int res = 1;
while (y)
{
if (y & 1)
res = (LL)res * x % P;
x = (LL)x * x % P;
y >>= 1;
}
return res;
}
void ntt(int x[], int tot, int op)
{
int i, j, mid, len, gn, g, a, b;
for (i = 0; i < tot; ++i)
if (i < r[i])
swap(x[i], x[r[i]]);
for (mid = 1; mid < tot; mid <<= 1)
{
len = mid << 1;
gn = qpow(3, (P - 1) / len);
if (op == -1)
gn = qpow(gn, P - 2);
for (i = 0; i < tot; i += len)
{
g = 1;
for (j = 0; j < mid; ++j, g = (LL)g * gn % P)
{
a = x[i + j], b = (LL)g * x[i + j + mid] % P;
x[i + j] = (a + b) % P;
x[i + j + mid] = (a - b + P) % P;
}
}
}
if (op == 1)
return ;
gn = qpow(tot, P - 2);
for (i = 0; i < tot; ++i)
x[i] = (LL)x[i] * gn % P;
}
void polyinv(int x[], int y[], int len)
{
if (len == 1)
{
y[0] = qpow(x[0], P - 2);
return ;
}
polyinv(x, y, (len + 1) >> 1);
int bit = 0, tot = 1;
while (tot < (len << 1))
tot <<= 1, ++bit;
STC int t[N];
for (int i = ((len + 1) >> 1); i < tot; i++)
y[i] = 0;
for (int i = 0; i < len; ++i)
t[i] = x[i];
for (int i = len; i < tot; ++i)
t[i] = 0;
for (int i = 0; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
ntt(t, tot, 1), ntt(y, tot, 1);
for (int i = 0; i < tot; ++i)
y[i] = (2 - (LL)y[i] * t[i] % P + P) * y[i] % P;
ntt(y, tot, -1);
for (int i = len; i < tot; ++i)
y[i] = 0;
}
void polydif(int x[], int y[], int len)
{
for (int i = 1; i < len; ++i)
y[i - 1] = (LL)x[i] * i % P;
y[len - 1] = 0;
}
void polyint(int x[], int y[], int len)
{
for (int i = 1; i < len; ++i)
y[i] = (LL)x[i - 1] * qpow(i, P - 2) % P;
y[0] = 0;
}
void polyln(int x[], int y[], int len)
{
STC int a[N], b[N];
polydif(x, a, len);
polyinv(x, b, len);
int bit = 0, tot = 1;
while (tot < (len << 1))
tot <<= 1, ++bit;
for (int i = 0; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
for (int i = len; i < tot; i++)
a[i] = b[i] = 0;
ntt(a, tot, 1), ntt(b, tot, 1);
for (int i = 0; i < tot; ++i)
a[i] = (LL)a[i] * b[i] % P;
ntt(a, tot, -1);
for (int i = len; i < tot; i++)
a[i] = 0;
polyint(a, y, len);
for (int i = len; i < tot; i++)
y[i] = 0;
}
void polyexp(int x[], int y[], int len)
{
if (len == 1)
{
y[0] = 1;
return ;
}
int bit = 0, tot = 1;
while (tot < (len << 1))
tot <<= 1, ++bit;
polyexp(x, y, (len + 1) >> 1);
STC int t[N];
for (int i = 0; i < tot; ++i)
t[i] = 0;
polyln(y, t, len);
t[0] = (x[0] + 1 - t[0] + P) % P;
for (int i = 1; i < len; ++i)
t[i] = (x[i] - t[i] + P) % P;
for (int i = len; i < tot; ++i)
t[i] = 0;
for (int i = 0; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
ntt(t, tot, 1), ntt(y, tot, 1);
for (int i = 0; i < tot; ++i)
y[i] = (LL)y[i] * t[i] % P;
ntt(y, tot, -1);
for (int i = len; i < tot; ++i)
y[i] = 0;
}
void polypow(int x[], int y[], int k1, int k2, int k3, int len)
{
int num = 0;
while (!x[num] && num < len)
++num;
if ((LL)num * k3 >= len)
{
for (int i = 0; i < len; ++i)
y[i] = 0;
return ;
}
STC int t[N];
int x0 = x[num], inv = qpow(x0, P - 2);
len -= num;
for (int i = 0; i < len; ++i)
x[i] = (LL)x[i + num] * inv % P;
int tot = 1;
while (tot < (len << 1))
tot <<= 1;
for (int i = 0; i < tot; ++i)
t[i] = 0;
for (int i = len; i < tot; ++i)
x[i] = 0;
polyln(x, t, len);
for (int i = 0; i < len; ++i)
t[i] = (LL)t[i] * k1 % P;
for (int i = len; i < tot; ++i)
t[i] = 0;
polyexp(t, y, len);
len += num;
x0 = qpow(x0, k2);
num = num * k3;
for (int i = len - 1; i >= num; --i)
y[i] = (LL)y[i - num] * x0 % P;
for (int i = num - 1; i >= 0; --i)
y[i] = 0;
}
void polysqrt(int x[], int y[], int len)
{
int sq = Csqrt(x[0]), inv = qpow(x[0], P - 2);
for (int i = 0; i < len; ++i)
x[i] = (LL)x[i] * inv % P;
polypow(x, y, (P + 1) >> 1, (P + 1) >> 1, 1, len);
for (int i = 0; i < len; ++i)
y[i] = (LL)y[i] * sq % P;
}
int main()
{
STC int a[N], b[N];
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i)
scanf("%d", &a[i]), a[i] = (a[i] + P) % P;
polysqrt(a, b, n);
for (int i = 0; i < n; ++i)
printf("%d ", b[i]);
return 0;
}

多项式三角函数

问题:

板子?

对于给定 $F(x)$ ,求 $\sin F(x), \cos F(x), \tan F(x)$

做法:
$$
\begin{aligned}
考虑欧拉公式: \\
& e^{i \theta} = \cos \theta + i \sin \theta \\
故: \\
& e^{i x} = \cos x + i \sin x \\
& e^{i (-x)} = \cos (-x) + i \sin (-x) = \cos x - i \sin x \\
所以: \\
& \cos x = \frac{e^{i x} + e^{-i x}}{2} \\
& \sin x = \frac{e^{i x} - e^{-i x}}{2i} \\
待入f(x): \\
& \cos F(x) = \frac{\exp(i F(x)) + \exp(-iF(x))}{2} \\
& \sin F(x) = \frac{\exp(i F(x)) - \exp(-iF(x))}{2i} \\
又因为: \\
& i^2 \equiv -1 \pmod P \\
所以: \\
& i^2 \equiv P - 1 \pmod P
\end{aligned}
$$
做一遍二次剩余求出 $i$ ,照着计算即可

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
#include <bits/stdc++.h>
#define LL long long
#define STC static
#define DRG default_random_engine
#define UID uniform_int_distribution
using namespace std;
int qpow(int x, int y);
const int N = 4e5 + 5, P = 998244353, K = 1e5 + 5;
const int img = 86583718; //直接计算出了二次剩余
//封装时记得清空数组,要重复使用
namespace Cipolla
{
int w;
DRG e{114514};
UID<int> u(1, P - 1);
struct Complex
{
int x, y;
Complex operator * (const Complex t) const
{
return (Complex){((LL)x * t.x % P + (LL)y * t.y % P * w % P) % P, ((LL)x * t.y % P + (LL)y * t.x % P) % P};
}
};
Complex Cqpow(Complex x, int y)
{
Complex res = (Complex){1, 0};
while (y)
{
if (y & 1)
res = res * x;
x = x * x;
y >>= 1;
}
return res;
}
int Csqrt(int x)
{
if(qpow(x, (P - 1) >> 1) == P - 1)
return -1;
int t;
while (233)
{
t = u(e);
w = ((LL)t * t % P - x + P) % P;
if (qpow(w, (P - 1) >> 1) == P - 1)
break;
}
int res = Cqpow((Complex){t, 1}, (P + 1) >> 1).x;
return min(res, P - res);
}
}
using Cipolla::Csqrt;
int r[N];
int qpow(int x, int y)
{
int res = 1;
while (y)
{
if (y & 1)
res = (LL)res * x % P;
x = (LL)x * x % P;
y >>= 1;
}
return res;
}
void ntt(int x[], int tot, int op)
{
int i, j, mid, len, gn, g, a, b;
for (i = 0; i < tot; ++i)
if (i < r[i])
swap(x[i], x[r[i]]);
for (mid = 1; mid < tot; mid <<= 1)
{
len = mid << 1;
gn = qpow(3, (P - 1) / len);
if (op == -1)
gn = qpow(gn, P - 2);
for (i = 0; i < tot; i += len)
{
g = 1;
for (j = 0; j < mid; ++j, g = (LL)g * gn % P)
{
a = x[i + j], b = (LL)g * x[i + j + mid] % P;
x[i + j] = (a + b) % P;
x[i + j + mid] = (a - b + P) % P;
}
}
}
if (op == 1)
return ;
gn = qpow(tot, P - 2);
for (i = 0; i < tot; ++i)
x[i] = (LL)x[i] * gn % P;
}
void polyinv(int x[], int y[], int len)
{
if (len == 1)
{
y[0] = qpow(x[0], P - 2);
return ;
}
polyinv(x, y, (len + 1) >> 1);
int bit = 0, tot = 1;
while (tot < (len << 1))
tot <<= 1, ++bit;
STC int t[N];
for (int i = ((len + 1) >> 1); i < tot; i++)
y[i] = 0;
for (int i = 0; i < len; ++i)
t[i] = x[i];
for (int i = len; i < tot; ++i)
t[i] = 0;
for (int i = 0; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
ntt(t, tot, 1), ntt(y, tot, 1);
for (int i = 0; i < tot; ++i)
y[i] = (2 - (LL)y[i] * t[i] % P + P) * y[i] % P;
ntt(y, tot, -1);
for (int i = len; i < tot; ++i)
y[i] = 0;
}
void polydif(int x[], int y[], int len)
{
for (int i = 1; i < len; ++i)
y[i - 1] = (LL)x[i] * i % P;
y[len - 1] = 0;
}
void polyint(int x[], int y[], int len)
{
for (int i = 1; i < len; ++i)
y[i] = (LL)x[i - 1] * qpow(i, P - 2) % P;
y[0] = 0;
}
void polyln(int x[], int y[], int len)
{
STC int a[N], b[N];
polydif(x, a, len);
polyinv(x, b, len);
int bit = 0, tot = 1;
while (tot < (len << 1))
tot <<= 1, ++bit;
for (int i = 0; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
for (int i = len; i < tot; i++)
a[i] = b[i] = 0;
ntt(a, tot, 1), ntt(b, tot, 1);
for (int i = 0; i < tot; ++i)
a[i] = (LL)a[i] * b[i] % P;
ntt(a, tot, -1);
for (int i = len; i < tot; i++)
a[i] = 0;
polyint(a, y, len);
for (int i = len; i < tot; i++)
y[i] = 0;
}
void polyexp(int x[], int y[], int len)
{
if (len == 1)
{
y[0] = 1;
return ;
}
int bit = 0, tot = 1;
while (tot < (len << 1))
tot <<= 1, ++bit;
polyexp(x, y, (len + 1) >> 1);
STC int t[N];
for (int i = 0; i < tot; ++i)
t[i] = 0;
polyln(y, t, len);
t[0] = (x[0] + 1 - t[0] + P) % P;
for (int i = 1; i < len; ++i)
t[i] = (x[i] - t[i] + P) % P;
for (int i = len; i < tot; ++i)
t[i] = 0;
for (int i = 0; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
ntt(t, tot, 1), ntt(y, tot, 1);
for (int i = 0; i < tot; ++i)
y[i] = (LL)y[i] * t[i] % P;
ntt(y, tot, -1);
for (int i = len; i < tot; ++i)
y[i] = 0;
}
void polypow(int x[], int y[], int k1, int k2, int k3, int len)
{
int num = 0;
while (!x[num] && num < len)
++num;
if ((LL)num * k3 >= len)
{
for (int i = 0; i < len; ++i)
y[i] = 0;
return ;
}
STC int t[N];
int x0 = x[num], inv = qpow(x0, P - 2);
len -= num;
for (int i = 0; i < len; ++i)
x[i] = (LL)x[i + num] * inv % P;
int tot = 1;
while (tot < (len << 1))
tot <<= 1;
for (int i = 0; i < tot; ++i)
t[i] = 0;
for (int i = len; i < tot; ++i)
x[i] = 0;
polyln(x, t, len);
for (int i = 0; i < len; ++i)
t[i] = (LL)t[i] * k1 % P;
for (int i = len; i < tot; ++i)
t[i] = 0;
polyexp(t, y, len);
len += num;
x0 = qpow(x0, k2);
num = num * k3;
for (int i = len - 1; i >= num; --i)
y[i] = (LL)y[i - num] * x0 % P;
for (int i = num - 1; i >= 0; --i)
y[i] = 0;
}
void polysqrt(int x[], int y[], int len)
{
int sq = Csqrt(x[0]), inv = qpow(x[0], P - 2);
for (int i = 0; i < len; ++i)
x[i] = (LL)x[i] * inv % P;
polypow(x, y, (P + 1) >> 1, (P + 1) >> 1, 1, len);
for (int i = 0; i < len; ++i)
y[i] = (LL)y[i] * sq % P;
}
//注意模数不为998244353是要重新算img
void polycos(int x[], int y[], int len)
{
STC int t[N], a[N], b[N];
for (int i = 0; i < len; ++i)
t[i] = (LL)x[i] * img % P;
polyexp(t, a, len);
polyinv(a, b, len);
for (int i = 0; i < len; ++i)
a[i] = (a[i] + b[i]) % P;
int inv = qpow(2, P - 2);
for (int i = 0; i < len; ++i)
y[i] = (LL)a[i] * inv % P;
}
void polysin(int x[], int y[], int len)
{
STC int t[N], a[N], b[N];
for (int i = 0; i < len; ++i)
t[i] = (LL)x[i] * img % P;
polyexp(t, a, len);
polyinv(a, b, len);
for (int i = 0; i < len; ++i)
a[i] = (a[i] - b[i] + P) % P;
int inv = qpow(img << 1, P - 2);
for (int i = 0; i < len; ++i)
y[i] = (LL)a[i] * inv % P;
}
void polytan(int x[], int y[], int len)
{
STC int t[N], a[N], b[N];
polysin(x, a, len);
polycos(x, b, len);
polyinv(b, t, len);
for (int i = 0; i < len; ++i)
y[i] = a[i] * t[i] % P;
}
int main()
{
STC int a[N], b[N];
int n, op;
scanf("%d%d", &n, &op);
for (int i = 0; i < n; ++i)
scanf("%d", &a[i]), a[i] = (a[i] + P) % P;
op ? polycos(a, b, n) : polysin(a, b, n);
for (int i = 0; i < n; ++i)
printf("%d ", b[i]);
return 0;
}

多项式反三角函数

问题:

板子?

对于给定 $F(x)$ ,求 $\arcsin F(x), \arccos F(x), \arctan F(x)$

做法:

直接积分得
$$
\begin{aligned}
& \arcsin F(x) =\int \frac{F’(x)}{\sqrt{1 - F^2(x)}} dx \\
& \arccos F(x) = - \int \frac{F’(x)}{\sqrt{1 - F^2(x)}} dx \\
& \arctan F(x) = \int \frac{F’(x)}{1 + F^2(x)} dx \\
\end{aligned}
$$

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
#include <bits/stdc++.h>
#define LL long long
#define STC static
#define DRG default_random_engine
#define UID uniform_int_distribution
using namespace std;
int qpow(int x, int y);
const int N = 4e5 + 5, P = 998244353, K = 1e5 + 5;
const int img = 86583718; //直接计算出了二次剩余
//封装时记得清空数组,要重复使用
namespace Cipolla
{
int w;
DRG e{114514};
UID<int> u(1, P - 1);
struct Complex
{
int x, y;
Complex operator * (const Complex t) const
{
return (Complex){((LL)x * t.x % P + (LL)y * t.y % P * w % P) % P, ((LL)x * t.y % P + (LL)y * t.x % P) % P};
}
};
Complex Cqpow(Complex x, int y)
{
Complex res = (Complex){1, 0};
while (y)
{
if (y & 1)
res = res * x;
x = x * x;
y >>= 1;
}
return res;
}
int Csqrt(int x)
{
if(qpow(x, (P - 1) >> 1) == P - 1)
return -1;
int t;
while (233)
{
t = u(e);
w = ((LL)t * t % P - x + P) % P;
if (qpow(w, (P - 1) >> 1) == P - 1)
break;
}
int res = Cqpow((Complex){t, 1}, (P + 1) >> 1).x;
return min(res, P - res);
}
}
using Cipolla::Csqrt;
int r[N];
int qpow(int x, int y)
{
int res = 1;
while (y)
{
if (y & 1)
res = (LL)res * x % P;
x = (LL)x * x % P;
y >>= 1;
}
return res;
}
void ntt(int x[], int tot, int op)
{
int i, j, mid, len, gn, g, a, b;
for (i = 0; i < tot; ++i)
if (i < r[i])
swap(x[i], x[r[i]]);
for (mid = 1; mid < tot; mid <<= 1)
{
len = mid << 1;
gn = qpow(3, (P - 1) / len);
if (op == -1)
gn = qpow(gn, P - 2);
for (i = 0; i < tot; i += len)
{
g = 1;
for (j = 0; j < mid; ++j, g = (LL)g * gn % P)
{
a = x[i + j], b = (LL)g * x[i + j + mid] % P;
x[i + j] = (a + b) % P;
x[i + j + mid] = (a - b + P) % P;
}
}
}
if (op == 1)
return ;
gn = qpow(tot, P - 2);
for (i = 0; i < tot; ++i)
x[i] = (LL)x[i] * gn % P;
}
void polymul(int x[], int y[], int len)
{
int bit = 0, tot = 1;
while (tot < (len << 1))
tot <<= 1, ++bit;
for (int i = 0; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
ntt(x, tot, 1), ntt(y, tot, 1);
for (int i = 0; i < tot; ++i)
x[i] = (LL)x[i] * y[i] % P;
ntt(x, tot, -1);
}
void polyinv(int x[], int y[], int len)
{
if (len == 1)
{
y[0] = qpow(x[0], P - 2);
return ;
}
polyinv(x, y, (len + 1) >> 1);
int bit = 0, tot = 1;
while (tot < (len << 1))
tot <<= 1, ++bit;
STC int t[N];
for (int i = ((len + 1) >> 1); i < tot; i++)
y[i] = 0;
for (int i = 0; i < len; ++i)
t[i] = x[i];
for (int i = len; i < tot; ++i)
t[i] = 0;
for (int i = 0; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
ntt(t, tot, 1), ntt(y, tot, 1);
for (int i = 0; i < tot; ++i)
y[i] = (2 - (LL)y[i] * t[i] % P + P) * y[i] % P;
ntt(y, tot, -1);
for (int i = len; i < tot; ++i)
y[i] = 0;
}
void polydif(int x[], int y[], int len)
{
for (int i = 1; i < len; ++i)
y[i - 1] = (LL)x[i] * i % P;
y[len - 1] = 0;
}
void polyint(int x[], int y[], int len)
{
for (int i = 1; i < len; ++i)
y[i] = (LL)x[i - 1] * qpow(i, P - 2) % P;
y[0] = 0;
}
void polyln(int x[], int y[], int len)
{
STC int a[N], b[N];
polydif(x, a, len);
polyinv(x, b, len);
int bit = 0, tot = 1;
while (tot < (len << 1))
tot <<= 1, ++bit;
for (int i = 0; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
for (int i = len; i < tot; i++)
a[i] = b[i] = 0;
ntt(a, tot, 1), ntt(b, tot, 1);
for (int i = 0; i < tot; ++i)
a[i] = (LL)a[i] * b[i] % P;
ntt(a, tot, -1);
for (int i = len; i < tot; i++)
a[i] = 0;
polyint(a, y, len);
for (int i = len; i < tot; i++)
y[i] = 0;
}
void polyexp(int x[], int y[], int len)
{
if (len == 1)
{
y[0] = 1;
return ;
}
int bit = 0, tot = 1;
while (tot < (len << 1))
tot <<= 1, ++bit;
polyexp(x, y, (len + 1) >> 1);
STC int t[N];
for (int i = 0; i < tot; ++i)
t[i] = 0;
polyln(y, t, len);
t[0] = (x[0] + 1 - t[0] + P) % P;
for (int i = 1; i < len; ++i)
t[i] = (x[i] - t[i] + P) % P;
for (int i = len; i < tot; ++i)
t[i] = 0;
for (int i = 0; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
ntt(t, tot, 1), ntt(y, tot, 1);
for (int i = 0; i < tot; ++i)
y[i] = (LL)y[i] * t[i] % P;
ntt(y, tot, -1);
for (int i = len; i < tot; ++i)
y[i] = 0;
}
void polypow(int x[], int y[], int k1, int k2, int k3, int len)
{
int num = 0;
while (!x[num] && num < len)
++num;
if ((LL)num * k3 >= len)
{
for (int i = 0; i < len; ++i)
y[i] = 0;
return ;
}
STC int t[N];
int x0 = x[num], inv = qpow(x0, P - 2);
len -= num;
for (int i = 0; i < len; ++i)
x[i] = (LL)x[i + num] * inv % P;
int tot = 1;
while (tot < (len << 1))
tot <<= 1;
for (int i = 0; i < tot; ++i)
t[i] = 0;
for (int i = len; i < tot; ++i)
x[i] = 0;
polyln(x, t, len);
for (int i = 0; i < len; ++i)
t[i] = (LL)t[i] * k1 % P;
for (int i = len; i < tot; ++i)
t[i] = 0;
polyexp(t, y, len);
len += num;
x0 = qpow(x0, k2);
num = num * k3;
for (int i = len - 1; i >= num; --i)
y[i] = (LL)y[i - num] * x0 % P;
for (int i = num - 1; i >= 0; --i)
y[i] = 0;
}
void polysqrt(int x[], int y[], int len)
{
int sq = Csqrt(x[0]), inv = qpow(x[0], P - 2);
for (int i = 0; i < len; ++i)
x[i] = (LL)x[i] * inv % P;
polypow(x, y, (P + 1) >> 1, (P + 1) >> 1, 1, len);
for (int i = 0; i < len; ++i)
y[i] = (LL)y[i] * sq % P;
}
//注意模数不为998244353是要重新算img
void polycos(int x[], int y[], int len)
{
STC int t[N], a[N], b[N];
for (int i = 0; i < len; ++i)
t[i] = (LL)x[i] * img % P;
polyexp(t, a, len);
polyinv(a, b, len);
for (int i = 0; i < len; ++i)
a[i] = (a[i] + b[i]) % P;
int inv = qpow(2, P - 2);
for (int i = 0; i < len; ++i)
y[i] = (LL)a[i] * inv % P;
}
void polysin(int x[], int y[], int len)
{
STC int t[N], a[N], b[N];
for (int i = 0; i < len; ++i)
t[i] = (LL)x[i] * img % P;
polyexp(t, a, len);
polyinv(a, b, len);
for (int i = 0; i < len; ++i)
a[i] = (a[i] - b[i] + P) % P;
int inv = qpow(img << 1, P - 2);
for (int i = 0; i < len; ++i)
y[i] = (LL)a[i] * inv % P;
}
void polytan(int x[], int y[], int len)
{
STC int t[N], a[N], b[N];
polysin(x, a, len);
polycos(x, b, len);
polyinv(b, t, len);
for (int i = 0; i < len; ++i)
y[i] = a[i] * t[i] % P;
}
void polyasin(int x[], int y[], int len)
{
STC int a[N], b[N], c[N];
polydif(x, a, len);
int bit = 0, tot = 1;
while (tot < (len << 1))
tot <<= 1, ++bit;
for (int i = 0; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
ntt(x, tot, 1);
for (int i = 0; i < tot; ++i)
b[i] = (1 - (LL)x[i] * x[i]% P + P) % P;
ntt(b, tot, -1);
for (int i = len; i < tot; ++i)
b[i] = 0;
polysqrt(b, c, len);
for (int i = 0; i < tot; ++i)
b[i] = 0;
polyinv(c, b, len);
ntt(a, tot, 1), ntt(b, tot, 1);
for (int i = 0; i < tot; ++i)
a[i] = (LL)a[i] * b[i] % P;
ntt(a, tot, -1);
polyint(a, y, len);
}
void polyacos(int x[], int y[], int len)
{
polyasin(x, y, len);
for (int i = 0; i < len; ++i)
y[i] = y[i] ? P - y[i] : 0;
}
void polyatan(int x[], int y[], int len)
{
STC int a[N], b[N], c[N];
polydif(x, a, len);
int bit = 0, tot = 1;
while (tot < (len << 1))
tot <<= 1, ++bit;
for (int i = 0; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
ntt(x, tot, 1);
for (int i = 0; i < tot; ++i)
b[i] = (1 + (LL)x[i] * x[i]% P) % P;
ntt(b, tot, -1);
for (int i = len; i < tot; ++i)
b[i] = 0;
polyinv(b, c, len);
ntt(a, tot, 1), ntt(c, tot, 1);
for (int i = 0; i < tot; ++i)
a[i] = (LL)a[i] * c[i] % P;
ntt(a, tot, -1);
polyint(a, y, len);
}
int main()
{
STC int a[N], b[N];
int n, op;
scanf("%d%d", &n, &op);
for (int i = 0; i < n; ++i)
scanf("%d", &a[i]), a[i] = (a[i] + P) % P;
op ? polyatan(a, b, n) : polyasin(a, b, n);
for (int i = 0; i < n; ++i)
printf("%d ", b[i]);
return 0;
}

拉格朗日反演

不造有啥用,给个式子吧:

若两个多项式 $F(x), G(x)$ 满足常数项为0且1次项不为0,且两者互为复合

逆,即 $G(F(x)) = x$ (或者 $F(G(x)) = x$ ,它们是等价的),则有:
$$
\begin{aligned}
[x^n] F(x)
& = \frac{1}{n} [x^{-1}] \frac{1}{G^n(x)} \\
& = \frac{1}{n} [x^{n - 1}] (\frac{x}{G(x)})^n
\end{aligned}
$$
简证:
$$
\begin{aligned}
\sum_{i = 0}^{n - 1} a_i G^i(x) & = x \\
\sum_{i = 0}^{n - 1} a_i i G^{i - 1}(x) G’(x) & = 1 \\
\sum_{i = 0}^{n - 1} a_i i G^{i - n - 1}(x) G’(x) & = \frac{1}{G^n(x)} \\
[x^{-1}] \sum_{i = 0}^{n - 1} a_i i G^{i - n - 1}(x) G’(x) & = [x^{-1}] \frac{1}{G^n(x)} \\
对于 i \ne n : \\
G^{i - n - 1}(x) G’(x) & = \frac{1}{i - n} (G^{i - n}(x))’ \\
求导, x^{-1} 系数为 0, 所以只考虑 i = n : \\
[x^{-1}] G^{-1}(x) G’(x) & = 1 \\
[x^n] F(x) & = \frac{1}{n} [x^{-1}] \frac{1}{G^n(x)} \\
\end{aligned}
$$

封装

由于那么多东西烦的很,于是封装了

千万别打 class 和shit一样,我用的 namespace

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
#include <bits/stdc++.h>
#define IL inline
#define LL long long
#define STC static
#define DRG default_random_engine
#define UID uniform_int_distribution
using namespace std;
const int N = 3e5 + 5;
/*
mod, quadratic residue, original root

Remember const int is faster than int

If you want to change, remember
I = min{qpow(G, (P - 1) / 4), P - qpow(G, (P - 1) / 4)}
*/
const int P = 998244353, I = 86583718, G = 3;
/*
This is a class for solving polynomial problems

Its default data type is int32, However, when multiplication is
out of range, it will conversion is enforced

Its default modulus is 998244353, but it can also be changed

Note that when changing the modulus, remember to change the
original root at the same time, and the modulus should not be
too large, if the modulus exceeding int32
may exceed int64 (that is, long long) when multiplying.

And you should note that N is defined outside the class,
it represents the longest possible length of the polynomial,
generally 3 to 4 times the original length

This is a class written by Dyd on December 30, 2021,
and hopes to help you
*/
namespace Poly
{
//This is for fast exponentiation
IL int qpow(int x, int y)
{
int res = 1;
while (y)
{
if (y & 1)
res = (LL)res * x % P;
x = (LL)x * x % P;
y >>= 1;
}
return res;
}
//This is Cipolla
namespace Cipolla
{
int w;
DRG e{114514};
UID<int> u(1, P - 1);
struct Complex
{
int x, y;
IL Complex operator * (const Complex t) const
{
return (Complex){((LL)x * t.x % P + (LL)y * t.y % P * w % P) % P, ((LL)x * t.y % P + (LL)y * t.x % P) % P};
}
};
IL Complex Cqpow(Complex x, int y)
{
Complex res = (Complex){1, 0};
while (y)
{
if (y & 1)
res = res * x;
x = x * x;
y >>= 1;
}
return res;
}
IL int Csqrt(int x)
{
if(qpow(x, (P - 1) >> 1) == P - 1)
return -1;
int t;
while (233)
{
t = u(e);
w = ((LL)t * t % P - x + P) % P;
if (qpow(w, (P - 1) >> 1) == P - 1)
break;
}
int res = Cqpow((Complex){t, 1}, (P + 1) >> 1).x;
return min(res, P - res);
}
}
//It records the flip subscript
int r[N];
//Fill x with y
IL void fill(int x[], int y, int l, int r)
{
for (int i = l; i < r; ++i)
x[i] = y;
}
IL void fill(int x[], int y[], int l, int r)
{
for (int i = l; i < r; ++i)
x[i] = y[i];
}
//Ready bit and tot
IL void ready(int &bit, int &tot, int len)
{
tot = 1, bit = 0;
while (tot < (len << 1))
tot <<= 1, ++bit;
}
//preprocess the flip subscript
IL void get_r(int bit, int tot)
{
for (int i = 0; i < tot; ++i)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (bit - 1));
}
//NTT, remember to preprocess the flip subscript
IL void ntt(int x[], int tot, int op)
{
for (int i = 0; i < tot; ++i)
if (i < r[i])
swap(x[i], x[r[i]]);
int mid, i, j, len, gn, g, a, b;
for (mid = 1; mid < tot; mid <<= 1)
{
len = mid << 1;
gn = qpow(3, (P - 1) / len);
if (op == -1)
gn = qpow(gn, P - 2);
for (i = 0; i < tot; i += len)
{
g = 1;
for (j = 0; j < mid; ++j, g = (LL)g * gn % P)
{
a = x[i + j], b = (LL)g * x[i + j + mid] % P;
x[i + j] = (a + b) % P;
x[i + j + mid] = (a - b + P) % P;
}
}
}
if (op == 1)
return ;
gn = qpow(tot, P - 2);
for (int i = 0; i < tot; ++i)
x[i] = (LL)x[i] * gn % P;
}
//Polynomial multiplication
IL void polymul(int x[], int y[], int len)
{
int bit, tot;
ready(bit, tot, len);
get_r(bit, tot);
ntt(x, tot, 1), ntt(y, tot, 1);
for (int i = 0; i < tot; ++i)
x[i] = (LL)x[i] * y[i] % P;
ntt(x, tot, -1);
//Remember to clear the number of bits that x does not need to 0
}
//Polynomial inversion
IL void polyinv(int x[], int y[], int len)
{

if (len == 1)
{
y[0] = qpow(x[0], P - 2);
return ;
}
polyinv(x, y, (len + 1) >> 1);
int bit = 0, tot = 1;
STC int t[N];
ready(bit, tot, len);
get_r(bit, tot);
fill(y, 0, (len + 1) >> 1, tot);
fill(t, x, 0, len);
fill(t, 0, len, tot);
ntt(t, tot, 1), ntt(y, tot, 1);
for (int i = 0; i < tot; ++i)
y[i] = (2 - (LL)t[i] * y[i] % P + P) % P * y[i] % P;
ntt(y, tot, -1);
fill(y, 0, len, tot);
}
/*
Polynomial Division
x divided by y quotient a remainder b
*/
IL void polydiv(int x[], int n, int y[], int m, int a[], int b[])
{
STC int ta[N], tb[N];
int dt = n - m + 1, tot, bit;
ready(bit, tot, n);
fill(ta, 0, 0, tot);
fill(tb, 0, 0, tot);
for (int i = 0; i < m; ++i)
ta[i] = y[m - i - 1];
polyinv(ta, tb, dt);
get_r(bit, tot);
for (int i = 0; i < n; ++i)
ta[i] = x[n - i - 1];
ntt(ta, tot, 1), ntt(tb, tot, 1);
for (int i = 0; i < tot; ++i)
ta[i] = (LL)ta[i] * tb[i] %P;
ntt(ta, tot, -1);
for (int i = 0; i < dt; ++i)
a[i] = ta[dt - i - 1];
fill(ta, 0, 0, tot);
fill(tb, 0, 0, tot);
fill(ta, a, 0, dt);
fill(tb, y, 0, m);
ntt(ta, tot, 1), ntt(tb, tot, 1);
for (int i = 0; i < tot; ++i)
ta[i] = (LL)ta[i] * tb[i] % P;
ntt(ta, tot, -1);
for (int i = 0; i < m - 1; ++i)
b[i] = (x[i] - ta[i] + P) % P;
}
//Polynomial Derivative
IL void polydif(int x[], int y[], int len)
{
for (int i = 1; i < len; ++i)
y[i - 1] = (LL)x[i] * i % P;
y[len - 1] = 0;
}
//Polynomial integral
IL void polyint(int x[], int y[], int len)
{
for (int i = 1; i < len; ++i)
y[i] = (LL)x[i - 1] * qpow(i, P - 2) % P;
y[0] = 0;
}
//Polynomial natural logarithm
IL void polyln(int x[], int y[], int len)
{
STC int a[N], b[N];
polydif(x, a, len);
polyinv(x, b, len);
int bit, tot;
ready(bit, tot, len);
get_r(bit,tot);
fill(a, 0, len, tot);
fill(b, 0, len, tot);
ntt(a, tot, 1), ntt(b, tot, 1);
for (int i = 0; i < tot; ++i)
a[i] = (LL)a[i] * b[i] % P;
ntt(a, tot, -1);
fill(a, 0, len, tot);
polyint(a, y, len);
fill(y, 0, len, tot);
}
//Polynomial natural index
IL void polyexp(int x[], int y[], int len)
{
if (len == 1)
{
y[0] = 1;
return ;
}
polyexp(x, y, (len + 1) >> 1);
int bit, tot;
STC int t[N];
ready(bit, tot, len);
fill(t, 0, 0, tot);
polyln(y, t, len);
get_r(bit, tot);
t[0] = (x[0] + 1 - t[0] + P) % P;
for (int i = 1; i < len; ++i)
t[i] = (x[i] - t[i] + P) % P;
fill(t, 0, len, tot);
ntt(t, tot, 1), ntt(y, tot, 1);
for (int i = 0; i < tot; ++i)
y[i] = (LL)y[i] * t[i] % P;
ntt(y, tot, -1);
fill(y, 0, len, tot);
}
/*
Polynomial power function

k1 is the real power(it could be modulo)
k2 is for Euler's theorem(it's modulo by P - 1)
k3 is to determine how many leading 0 there are
we use them as the power is too large
*/
IL void polypow(int x[], int y[], int k1, int k2, int k3, int len)
{
int num = 0;
while (!x[num] && num < len)
++num;
if ((LL)num * k3 >= len)
return fill(y, 0, 0, len);
STC int t[N];
int x0 = x[num], inv = qpow(x0, P - 2);
len -= num;
for (int i = 0; i < len; ++i)
x[i] = (LL)x[i + num] * inv % P;
int bit, tot;
ready(bit, tot, len);
fill(t, 0, 0, tot);
fill(x, 0, len, tot);
polyln(x, t, len);
for (int i = 0; i < len; ++i)
t[i] = (LL)t[i] * k1 % P;
fill(t, 0, len, tot);
polyexp(t, y, len);
len += num;
x0 = qpow(x0, k2);
num = num * k3;
for (int i = len - 1; i >= num; --i)
y[i] = (LL)y[i - num] * x0 % P;
fill(y, 0, 0, num);
}
/*
This is to get k1, k2, k3 for the k which is too large too be
used in polypow
*/
IL void ready_for_pow(char *k, int &k1, int &k2, int &k3)
{
k1 = k2 = k3 = 0;
for (int i = 1, l = strlen(k + 1); i <= l; ++i)
{
k1 = ((LL)k1 * 10 + (k[i] ^ 48)) % P;
k2 = ((LL)k2 * 10 + (k[i] ^ 48)) % (P - 1);
if ((LL)k3 * 10 + (k[i] ^ 48) <= P)
k3 = (LL)k3 * 10 + (k[i] ^ 48);
}
}
//Polynomial rooting
IL void polysqrt(int x[], int y[], int len)
{
int sq = Cipolla::Csqrt(x[0]), inv = qpow(x[0], P - 2);
for (int i = 0; i < len; ++i)
x[i] = (LL)x[i] * inv % P;
polypow(x, y, (P + 1) >> 1, (P + 1) >> 1, 1, len);
for (int i = 0; i < len; ++i)
y[i] = (LL)y[i] * sq % P;
}
/*
Polynomial trigonometric function

Note that if the modulus is not
998244353 I should be recalculated
*/
IL void polysin(int x[], int y[], int len)
{
STC int a[N], b[N], c[N];
for (int i = 0; i < len; ++i)
a[i] = (LL)x[i] * I % P;
polyexp(a, b, len);
polyinv(b, c, len);
for (int i = 0; i < len; ++i)
b[i] = (b[i] - c[i] + P) % P;
int inv = qpow((I << 1) % P, P - 2);
for (int i = 0; i < len; ++i)
y[i] = (LL)b[i] * inv % P;
}
IL void polycos(int x[], int y[], int len)
{
STC int a[N], b[N], c[N];
for (int i = 0; i < len; ++i)
a[i] = (LL)x[i] * I % P;
polyexp(a, b, len);
polyinv(b, c, len);
for (int i = 0; i < len; ++i)
b[i] = (b[i] + c[i]) % P;
int inv = qpow(2, P - 2);
for (int i = 0; i < len; ++i)
y[i] = (LL)b[i] * inv % P;
}
IL void polytan(int x[], int y[], int len)
{
STC int a[N], b[N], c[N];
polysin(x, a, len);
polycos(x, b, len);
polyinv(b, c, len);
for (int i = 0; i < len; ++i)
y[i] = a[i] * c[i] % P;
}
//Polynomial inverse trigonometric function
IL void polyasin(int x[], int y[], int len)
{
STC int a[N], b[N], c[N];
polydif(x, a, len);
int bit, tot;
ready(bit, tot, len);
get_r(bit, tot);
ntt(x, tot, 1);
for (int i = 0; i < tot; ++i)
b[i] = (1 - (LL)x[i] * x[i]% P + P) % P;
ntt(b, tot, -1);
fill(b, 0, len, tot);
polysqrt(b, c, len);
fill(b, 0, 0, tot);
polyinv(c, b, len);
ntt(a, tot, 1), ntt(b, tot, 1);
for (int i = 0; i < tot; ++i)
a[i] = (LL)a[i] * b[i] % P;
ntt(a, tot, -1);
polyint(a, y, len);
}
IL void polyacos(int x[], int y[], int len)
{
polyasin(x, y, len);
for (int i = 0; i < len; ++i)
y[i] = y[i] ? P - y[i] : 0;
}
IL void polyatan(int x[], int y[], int len)
{
STC int a[N], b[N], c[N];
polydif(x, a, len);
int bit, tot;
ready(bit, tot, len);
get_r(bit, tot);
ntt(x, tot, 1);
for (int i = 0; i < tot; ++i)
b[i] = (1 + (LL)x[i] * x[i]% P) % P;
ntt(b, tot, -1);
fill(b, 0, len, tot);
polyinv(b, c, len);
ntt(a, tot, 1), ntt(c, tot, 1);
for (int i = 0; i < tot; ++i)
a[i] = (LL)a[i] * c[i] % P;
ntt(a, tot, -1);
polyint(a, y, len);
}
}
int main()
{
/*
works
*/
return 0;
}