版本比较
FFT 两次 9log9
正文
增加 220 行删除 4 行
449 行未改动全部展开45045145245345445445545645645745945845946046146246446346446546646747 行未改动515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735449 行未改动全部展开[link(加强版)](https://www.luogu.com.cn/record/237494907)
::::info[code]```cppconstexpr pl exp() constconstexpr pl sqr() const{ assert((*this)[0] == 0); i32 m = this->size(); i32 n = m > 1 ? 1 << 32 - __builtin_clz(m - 1) : 1; POLY f = *this, g(1); f.resize(n), f[0] = g[0] = 1; POLY f = *this, g(1); f.resize(n), g[0] = 1; for(int k = 2; k <= n; k <<= 1) { g.resize(k); auto tmpf = f; tmpf.resize(k); g = (tmpf - g.log()) * g, g.resize(k); g = (tmpf + g * g) / (g * 2); } return g.resize(m), g;}```47 行未改动 return f.resize(m), f;}```::::
# 分治 FFT
## 半在线卷积
在考虑左右贡献进行卷积那一步,记 $m=R-L+1$,可以发现对 $\frac{m}{2}$ 次和 $m$ 次的多项式进行了卷积,得到一个 $\frac{3}{2}m$ 次的多项式,但只取了 $x^{m/2}\sim x^m$ 项的系数
因此,考虑进行 $\bmod (x^m-1)$ 的循环卷积,使后 $\frac{m}{2}$ 项溢出到前 $\frac{m}{2}$ 项上,而对中间 $\frac{m}{2}$ 项无影响,减少常数($\frac{3}{2}m$ 次的卷积通常会扩充至 $2m$ 次)
同时在考虑贡献时,如果把区间均分为 $B$ 份,会产生 $\binom{B}{2}$ 次贡献,直接做 $O(B^2)$ 次卷积贡献仍然是 $O(n\log^2{n})$ 的
但考虑点值的线性性,先做 $O(B)$ 次 DFT,再做 $O(B^2)$ 次点乘,最后做 $O(B)$ 次 IDFT,复杂度降为 $O((\frac{n}{B}\log\frac{n}{B}\cdot B+\frac{n}{B}\cdot B^2)\log_B{n})$,取 $B=O(\log{n})$,得 $O(\frac{n\log^2{n}}{\log\log{n}})$
实测在 $n$ 补至 2 的幂时,取 $B=8$ 较为理想
[link](https://www.luogu.com.cn/record/238019328)
::::info[code]```cppconstexpr pl gen(const i32& m) const{ assert((*this)[0] == 0); i32 n = std::max((int)this->size(), m); n = n > 1 ? 1 << 32 - __builtin_clz(n - 1) : 1; pl f(n), g = *this; f[0] = 1, g.resize(n); auto calc = [&g](auto&& self, pl::iterator f, i32 w) -> void { if(w < 8) { for(i32 i = 0; i < w; i ++) for(i32 j = 0; j < i; j ++) f[i] += f[j] * g[i - j]; return; }
pl a[7], b[7], c; w >>= 3; for(i32 i = 0; i < 7; i ++) b[i] = pl(g.begin() + w * i, g.begin() + w * (i + 2)), b[i].DFT();
self(self, f, w); for(i32 i = 1; i < 8; i ++) { a[i - 1] = pl(f, f + w), f += w; a[i - 1].resize(w << 1), a[i - 1].DFT(); c = a[0] | b[i - 1]; for(i32 j = 1; j < i; j ++) c += a[j] | b[i - j - 1]; c.IDFT(); for(i32 j = 0; j < w; j ++) f[j] += c[j + w]; self(self, f, w); } }; calc(calc, f.begin(), n); return f.resize(m), f;}```::::
## 多项式指数函数
基于分治 FFT 的做法往往优于牛顿迭代,通常常数更小码量更低
对 `gen()` 稍作修改即可,注意不能把 `/n` 放到 `gen()` 完成之后,而是递归到叶子时进行
[link](https://www.luogu.com.cn/record/238036553)
::::info[code]```cppconstexpr pl exp() const{ assert((*this)[0] == 0); i32 m = this->size(); i32 n = m > 1 ? 1 << 32 - __builtin_clz(m - 1) : 1; pl f(n), g = *this; f[0] = 1, g.resize(n); for(i32 i = 1; i < m; i ++) g[i] *= i; auto tmp = f.begin(); auto calc = [&g, &tmp](auto&& self, pl::iterator f, i32 w) -> void { if(w < 8) { for(i32 i = 0; i < w; i ++) { for(i32 j = 0; j < i; j ++) f[i] += f[j] * g[i - j]; if(f + i != tmp) f[i] /= f + i - tmp; } return; }
pl a[7], b[7], c; w >>= 3; for(i32 i = 0; i < 7; i ++) b[i] = pl(g.begin() + w * i, g.begin() + w * (i + 2)), b[i].DFT();
self(self, f, w); for(i32 i = 1; i < 8; i ++) { a[i - 1] = pl(f, f + w), f += w; a[i - 1].resize(w << 1), a[i - 1].DFT(); c = a[0] | b[i - 1]; for(i32 j = 1; j < i; j ++) c += a[j] | b[i - j - 1]; c.IDFT(); for(i32 j = 0; j < w; j ++) f[j] += c[j + w]; self(self, f, w); } }; calc(calc, f.begin(), n); return f.resize(m), f;}```::::
## 全在线卷积
这里由于 $A,B$ 地位相等,因此直接写成了 `*2`
[link](https://atcoder.jp/contests/abc315/submissions/69797604)
::::info[code]```cppusing poly = polyNomial::POLY;using plit = poly::iterator;
void semiRelaxedConvolution(const plit& a, const plit& g, const plit& fb, const plit& f, const int& w){ if(w == 1) { g[0] = g[-1] + f[0], f[0] = g[0] * a[0]; return; } semiRelaxedConvolution(a, g, fb, f, w >> 1); poly h = poly(fb, fb + (w << 1)) * poly(f, f + (w >> 1)); FOR(i, w >> 1, w - 1) f[i] += h[i - 1] * 2; semiRelaxedConvolution(a + (w >> 1), g + (w >> 1), fb, f + (w >> 1), w >> 1);}
void relaxedConvolution(const plit& a, const plit& g, const plit& f, const int& w){ if(w == 1) return; relaxedConvolution(a, g, f, w >> 1); poly h = poly(f, f + (w >> 1)) * poly(f, f + (w >> 1)); FOR(i, w >> 1, w - 1) f[i] += h[i - 1]; semiRelaxedConvolution(a + (w >> 1), g + (w >> 1), f, f + (w >> 1), w >> 1);}
void mainSolve(){ int n; read(n);
poly a(n + 1); FOR(i, 1, n) {int v; read(v), a[i] = v;}
int m = n > 1 ? 1 << 32 - __builtin_clz(n - 1) : 1; poly f(m), g(m); f[0] = 1, a.resize(m); relaxedConvolution(a.begin(), g.begin(), f.begin(), m); FOR(i, 1, n) write(f[i].val(), ' ');}```::::
# 求值插值
## 多项式多点求值
调用 `f.evaluate(x)`,将返回 $F(x_{1\sim m})$
[link](https://www.luogu.com.cn/record/238225471)
::::info[code]```cppconstexpr pl evaluate(const pl& x) const{ i32 m = x.size(); pl g[m << 2], y(m); auto calc1 = [&g, &x](auto&& self, i32 p, i32 l, i32 r) { if(l == r) return g[p] = {-x[l], 1}; g[p] = self(self, p << 1, l, l + r >> 1); return g[p] *= self(self, p << 1 | 1, (l + r >> 1) + 1, r); }; auto calc2 = [&g, &y](auto&& self, i32 p, i32 l, i32 r, const pl& f) { if(l == r) return y[l] = f[0], void(); self(self, p << 1, l, l + r >> 1, f % g[p << 1]); self(self, p << 1 | 1, (l + r >> 1) + 1, r, f % g[p << 1 | 1]); }; calc2(calc2, 1, 0, m - 1, *this % calc1(calc1, 1, 0, m - 1)); return y;}```::::
## 多项式快速插值
调用 `x.interpolate(y)`,将返回 $F(x)$
[link](https://www.luogu.com.cn/record/238307477)
::::info[code]```cppconstexpr pl interpolate(const pl& y) const{ i32 n = this->size(); pl x = *this, g[n << 2]; auto calc1 = [&g, &x](auto&& self, i32 u, i32 l, i32 r) { if(l == r) return g[u] = {-x[l], 1}; g[u] = self(self, u << 1, l, l + r >> 1); return g[u] *= self(self, u << 1 | 1, (l + r >> 1) + 1, r); }; auto calc2 = [&g, &x, &y](auto&& self, i32 u, i32 l, i32 r, const pl& p) { if(l == r) return (pl){y[l] / p[l]}; return g[u << 1] * self(self, u << 1 | 1, (l + r >> 1) + 1, r, p) + self(self, u << 1, l, l + r >> 1, p) * g[u << 1 | 1]; }; return calc2(calc2, 1, 0, n - 1, calc1(calc1, 1, 0, n - 1).diff().evaluate(x));}```::::