【模板】分治 FFT
题目大意:给定长度为 \(n - 1\) 的序列 \(g\),求 \(f\) 序列,其中 \(f\) 为
\[f[i]=\sum_{j=1}^{i} f[i-j] g[j]
\]
学会了分治 \(fft\)。
发现这个式子中也含有卷积,但是这是一个递推式,即:\(f\) 数组是未知的。
考虑分治策略,即:假设已经算出区间 \([l, mid]\) 的 \(f\) 值,现在要计算区间 \([mid + 1, r]\) 的 \(f\)。
考虑左半部分对右半部分的贡献,对于 $$x \in [mid + 1, r],\ contribution(left\rightarrow x) = \sum\limits_{i = l}^{mid}f(i)*g(x - i)$$
通过下标转换得:
\[\begin{aligned} f(x) &=\sum_{i=l}^{mid} f(i) * g(x-i) \\ &=\sum_{i=0}^{mid-l} f(i+l) * g(x-l-i) \end{aligned}
\]
换元得
\[foo(i)=f(i+l), bar(i)=g(i)
\]
式子变成了
\[f(x)=foo(x-l)=\sum_{i=0}^{mid-l} A(i) * B(x-l-i)
\]
即:卷积序列的第 \(x - l\) 项为 \(x\) 的贡献。
注意:对于 \(g\) 来说,下标不能移动到 \(0\) 开始,因为 \(f\) 数组本身下标就是从 \(0\) 开始的,这就意味着 \(g[0] = 0\),即:输入从 \(g[1]\) 开始。
另外,关于c11的 lambda 表达式,对于 [=] 来说,捕获到的变量均为其常量的副本,这个值仅仅由该变量的初始化决定,不会改变。
// update at 2019.10.11
代码如下
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod = 998244353, g = 3, ig = 332748118;
inline LL fpow(LL a, LL b) {
LL ret = 1 % mod;
for (; b; b >>= 1, a = a * a % mod) {
if (b & 1) {
ret = ret * a % mod;
}
}
return ret;
}
void ntt(vector<LL> &v, vector<int> &rev, int opt) {
int tot = v.size();
for (int i = 0; i < tot; i++) if (i < rev[i]) swap(v[i], v[rev[i]]);
for (int mid = 1; mid < tot; mid <<= 1) {
LL wn = fpow(opt == 1 ? g : ig, (mod - 1) / (mid << 1));
for (int j = 0; j < tot; j += mid << 1) {
LL w = 1;
for (int k = 0; k < mid; k++) {
LL x = v[j + k], y = v[j + mid + k] * w % mod;
v[j + k] = (x + y) % mod, v[j + mid + k] = (x - y + mod) % mod;
w = w * wn % mod;
}
}
}
if (opt == -1) {
LL itot = fpow(tot, mod - 2);
for (int i = 0; i < tot; i++) v[i] = v[i] * itot % mod;
}
}
vector<LL> convolution(vector<LL> &a, int sa, int cnta, vector<LL> &b, int sb, int cntb, const function<LL(LL, LL)> &calc) {
int bit = 0, tot = 1;
while (tot <= 2 * max(cnta, cntb)) bit++, tot <<= 1;
vector<int> rev(tot);
for (int i = 0; i < tot; i++) rev[i] = rev[i >> 1] >> 1 | (i & 1) << (bit - 1);
vector<LL> foo(tot), bar(tot);
for (int i = 0, j = sa; i < cnta; i++, j++) foo[i] = a[j];
for (int i = 0, j = sb; i < cntb; i++, j++) bar[i] = b[j];
ntt(foo, rev, 1), ntt(bar, rev, 1);
for (int i = 0; i < tot; i++) foo[i] = calc(foo[i], bar[i]);
ntt(foo, rev, -1);
return foo;
}
void cdq(int l, int r, vector<LL> &f, vector<LL> &w) {
if (l == r) return;
int mid = l + r >> 1;
cdq(l, mid, f, w);
int sz = r - l + 1;
vector<LL> h = convolution(f, l, mid - l + 1, w, 0, sz, [&](LL a, LL b) {return a * b % mod;});
for (int i = mid + 1; i <= r; i++) f[i] = (f[i] + h[i - l]) % mod;
cdq(mid + 1, r, f, w);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n;
cin >> n;
vector<LL> f(n), w(n);
f[0] = 1;
for (int i = 1; i < n; i++) {
cin >> w[i];
}
cdq(0, n - 1, f, w);
for (auto v : f) {
cout << v << " ";
}
return 0;
}