yaoxi-std 的博客

$\text{开}\mathop{\text{卷}}\limits^{ju\check{a}n}\text{有益}$

0%

P4725 【模版】多项式对数函数(多项式ln)

P4725 【模版】多项式对数函数(多项式ln)

题面

题目链接

解法

定义:给定多项式$A(x)$,求多项式$B(x) \equiv \ln(A(x)) \pmod{x^n}$。

根据题目有$B(x) = \ln(A(x))$,两边同时求导:

于是将$A(x)$求导,再乘上$A(x)$的逆,最后做积分,就得到了$B(x)$。

求导和积分复杂度为$O(n)$,多项式求逆和NTT都是$O(n \log n)$,总时间复杂度$O(n \log n)$。

AC代码

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
/**
* @file: P4726.cpp
* @author: yaoxi-std
* @url: https://www.luogu.com.cn/problem/P4726
*/
// #pragma GCC optimize ("O2")
// #pragma GCC optimize ("Ofast", "inline", "-ffast-math")
// #pragma GCC target ("avx,sse2,sse3,sse4,mmx")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define resetIO(x) \
freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout)
#define debug(fmt, ...) \
fprintf(stderr, "[%s:%d] " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)
template <class _Tp>
inline _Tp& read(_Tp& x) {
bool sign = false;
char ch = getchar();
long double tmp = 1;
for (; !isdigit(ch); ch = getchar())
sign |= (ch == '-');
for (x = 0; isdigit(ch); ch = getchar())
x = x * 10 + (ch ^ 48);
if (ch == '.')
for (ch = getchar(); isdigit(ch); ch = getchar())
tmp /= 10.0, x += tmp * (ch ^ 48);
return sign ? (x = -x) : x;
}
template <class _Tp>
inline void write(_Tp x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
write(x / 10);
putchar((x % 10) ^ 48);
}
const int MAXN = 1 << 19;
const int INF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 998244353;
namespace maths {
int add(int x, int y) {
x += y;
return x >= MOD ? x - MOD : x;
}
int sub(int x, int y) {
x -= y;
return x < 0 ? x + MOD : x;
}
int qpow(int x, int y, int p = MOD) {
int ret = 1;
for (; y; y >>= 1, x = x * x % p)
if (y & 1)
ret = ret * x % p;
return ret;
}
int inverse(int x, int p = MOD) {
return qpow(x, p - 2, p);
}
template <class _Tp>
void change(_Tp* f, int len) {
static int rev[MAXN] = {};
for (int i = 0; i < len; ++i) {
rev[i] = rev[i >> 1] >> 1;
if (i & 1)
rev[i] |= len >> 1;
}
for (int i = 0; i < len; ++i)
if (i < rev[i])
swap(f[i], f[rev[i]]);
}
void ntt(int* f, int len, int on) {
change(f, len);
for (int h = 2; h <= len; h <<= 1) {
int gn = qpow(3, (MOD - 1) / h);
for (int j = 0; j < len; j += h) {
int g = 1;
for (int k = j; k < j + h / 2; ++k) {
int u = f[k], t = g * f[k + h / 2] % MOD;
f[k] = add(u, t), f[k + h / 2] = sub(u, t);
g = g * gn % MOD;
}
}
}
if (on == -1) {
reverse(f + 1, f + len);
int inv = inverse(len);
for (int i = 0; i < len; ++i)
f[i] = f[i] * inv % MOD;
}
}
struct polynomial {
int a[MAXN], len;
polynomial() { memset(a, 0, sizeof(a)); }
polynomial(int len) : len(len) { memset(a, 0, sizeof(a)); }
int operator[](int i) const { return a[i]; }
int& operator[](int i) { return a[i]; }
polynomial operator*(const polynomial& o) const {
static int f[MAXN], g[MAXN];
polynomial ret(len + o.len - 1);
int slen = 1;
while (slen < ret.len)
slen <<= 1;
copy(a, a + slen, f);
copy(o.a, o.a + slen, g);
ntt(f, slen, 1), ntt(g, slen, 1);
for (int i = 0; i < slen; ++i)
f[i] = f[i] * g[i] % MOD;
ntt(f, slen, -1);
copy(f, f + slen, ret.a);
return ret;
}
polynomial inverse() const {
static int tmp[MAXN] = {};
int slen = 1;
while (slen < len)
slen <<= 1;
polynomial ret(slen);
ret[0] = maths::inverse(a[0]);
for (int h = 2; h <= slen; h <<= 1) {
copy(a, a + h, tmp);
fill(tmp + h, tmp + h + h, 0);
ntt(tmp, h + h, 1);
ntt(ret.a, h + h, 1);
for (int i = 0; i < h + h; ++i)
ret[i] = ret[i] * (2 - tmp[i] * ret[i] % MOD + MOD) % MOD;
ntt(ret.a, h + h, -1);
fill(ret.a + h, ret.a + h + h, 0);
}
return ret;
}
polynomial derivation() const {
polynomial ret(len - 1);
for (int i = 0; i < len - 1; ++i)
ret[i] = a[i + 1] * (i + 1) % MOD;
return ret;
}
polynomial integral() const {
polynomial ret(len + 1);
for (int i = 1; i <= len; ++i)
ret[i] = a[i - 1] * maths::inverse(i) % MOD;
return ret;
}
polynomial ln() const {
return (derivation() * inverse()).integral();
}
};
}; // namespace maths
using namespace maths;
int n;
polynomial a;
signed main() {
read(n);
for (int i = 0; i < n; ++i)
read(a[i]);
a.len = n;
a = a.ln();
for (int i = 0; i < n; ++i)
write(a[i]), putchar(" \n"[i == n - 1]);
return 0;
}