多项式/生成函数运算全家桶
多项式/生成函数全家桶
1. 生成函数
定义
指数生成函数为:
不同生成函数在不同题目中有不同用途。
本文不着重于如何使用生成函数,而在于有限项多项式的运算。
2. 基本操作
- 多项式乘法
- 多项式求逆
- 多项式
\ln - 多项式
\exp - 多项式快速幂
- 多项式除法
- 多项式开方
- 分治乘法
3. FFT, NTT
3.1 多项式乘法,变为 n 个值相乘
多项式最重要的操作就是乘法:
直接算当然是
事实上,对于任意一个
在这里,我们一般会选择单位根
3.2 分治与蝶形算法
在实践中,我们往往会认为
我们尝试计算一对
这个形式就非常分治。在这个分治中,我们是根据
例如,原本给定的数列是
我们很容易写出蝶形算法的代码。对于分治过程,由于分治区间的明确性,我们可以自底向上地进行分治,枚举一个
3.3 逆运算
事实上我们可以发现,刚才的过程可逆性非常的强。若简记
它的逆运算就是:
这两个步骤简直一模一样!观察到里面的
其实可以注意到,很多时候,我们写代码的时候,在逆运算里面没有出现
3.4 模意义下的变换, NTT
我们更常见的范围是在
对于第二个性质,我们发现原根就十分符合。那第一个性质呢?我们知道,对于任意一个原根
因而,我们可以写出集 NTT 与逆变换 INTT 的代码:
void fft(ll *f, int len, int on)
{
for (int i = 1, j = len >> 1; i < len - 1; i++)
{
if (i < j)
{
swap(f[i], f[j]);
}
int k = len >> 1;
while (j >= k)
{
j -= k;
k >>= 1;
}
j += k;
}
for (int h = 2; h <= len; h <<= 1)
{
ll w0 = pow(3, (MOD - 1) / h, MOD);
for (int j = 0; j < len; j += h)
{
ll w = 1;
for (int i = j; i < j + (h >> 1); i++)
{
ll u = f[i], t = f[i + (h >> 1)] * w % MOD;
f[i] = (u + t) % MOD;
f[i + (h >> 1)] = (u + MOD - t) % MOD;
w = w * w0 % MOD;
}
}
}
if (on == -1) // 实际上,在这种写法中,on 只会出现在这里,也就是说,它可以被换成任意值,比如一个 bool。在别的写法中,on 还会出现在第 19 行。
{
reverse(f + 1, f + len);
ll inl = pow(len, MOD - 2, MOD);
for (int i = 0; i < len; i++)
{
(f[i] *= inl) %= MOD;
}
}
}
4. 递归与分治
在很多情况下,我们无法一次性得到我们要的所有系数,这时候就要使用递归或分治 FFT 了。
4.1 递归 FFT
为了求
在更多情况下,我们会写作
在一般情况中,这种递推操作的复杂度为
很多多项式初等函数都利用了递归 FFT。例如,多项式求逆、多项式
4.2 分治 FFT
这里的分治 FFT 不是指将
有时候,我们会遇到一种“自卷积”的现象:
这个时候,一次卷积是不够用的。观察一下,对于
这个算法的复杂度为
事实上,根据上面的式子,
下面是分治乘法的代码。
/**
* calculate the [l, r) power of f(x) = f(x) * g(x) (self convolution), f(0) = 1
* it should be ensured that (r - l) is a power of 2
*/
void self_conv(ll __restrict__ *f, ll __restrict__ *g, int l, int r)
{
static ll buffer[MAXN];
if (l == r - 1)
{
if (!l)
{
f[l] = 1;
}
return;
}
int mid = (l + r) >> 1;
self_conv(f, g, l, mid);
for (int i = l; i < 2 * r - l; i++)
{
if (i < mid)
{
buffer[i - l] = f[i];
}
else
{
buffer[i - l] = 0;
}
}
conv(buffer, g, (r - l) << 1);
for (int i = mid; i < r; i++)
{
(f[i] += buffer[i - l]) %= MOD;
}
self_conv(f, g, mid, r);
}
5. 初等函数
5.1 多项式求逆
运用递归 FFT。
递归的终止条件是:
假设我们已经知道了在模
由于两者在前
因此可以递归计算。
/**
* calculate f(x) = 1 / g(x) with length of len
* it should be ensured that len is a power of 2
*/
void poly_inv(ll __restrict__ *f, ll __restrict__ *g, int len)
{
static ll buffer[MAXN];
f[0] = pow(g[0], MOD - 2, MOD);
for (int i = 1; i < (len << 1); i++)
{
f[i] = 0;
}
for (int t = 2; t <= len; t <<= 1)
{
int t2 = 2 * t;
for (int i = 0; i < t; i++)
{
buffer[i] = g[i];
}
for (int i = t; i < t2; i++)
{
buffer[i] = 0;
}
fft(f, t2, 1); // 注意我们出现了三个多项式相乘,所以要开 2 倍长度
fft(buffer, t2, 1);
for (int i = 0; i < t2; i++)
{
f[i] = f[i] * (MOD + 2 - f[i] * buffer[i] % MOD) % MOD;
}
fft(f, t2, -1);
for (int i = t; i < t2; i++)
{
f[i] = 0;
}
}
}
5.2 多项式 \ln
我们可以照搬
对于等式右边的式子,我们已经定义了它在
注意,在这里我们必须有
/**
* calculate f = g' with length of len
* it should be ensured that len is a power of 2
* this algorithm will work if f = g
*/
void poly_der(ll *f, ll *g, int len)
{
for (int i = 1; i < len; i++)
{
f[i - 1] = g[i] * i % MOD;
}
f[len - 1] = 0;
}
/**
* calculate f = ∫g with length of len
* it should be ensured that len is a power of 2
* this algorithm will work if f = g
*
* it's name shouldn't be int because int is a c++ keyword
*/
void poly_int(ll *f, ll *g, int len)
{
for (int i = len - 2; ~i; i--)
{
f[i + 1] = g[i] * pow(i + 1, MOD - 2, MOD) % MOD;
}
f[0] = 0;
}
/**
* calculate f(x) = ln(g(x)) with length of len
* it should be ensured that len is a power of 2
*
* ln(g(x)) = ∫(g'(x) / g(x))
*/
void poly_ln(ll __restrict__ *f, ll __restrict__ *g, int len)
{
static ll buffer[MAXN];
for (int i = len; i < (len << 1); i++)
{
g[i] = 0;
}
poly_inv(buffer, g, len); // this function clears buffer[len, 2len)
poly_der(g, g, len);
conv(buffer, g, len << 1, f);
poly_int(f, f, len);
poly_int(g, g, len);
g[0] = pow(buffer[0], MOD - 2, MOD);
}
5.3 多项式 \exp
和
对
观察其中的
因为此时
可以使用分治 FFT 解决,复杂度
事实上,更优的做法是使用牛顿公式,可以做到
5.4 多个多项式乘与快速幂
由数学知识,
当我们拓展到
有时,左侧运算需要保留很高的次项,而右侧会因为可能出现多个相同多项式乘在一起的情况,而大大减小
一个经典问题就是多项式快速幂。我们试图计算
当然它也有一个缺陷。当
/**
* calculate f(x) = (g(x))^p with length of len
* it should be ensured that g[0] = 1
*
* (g(x))^p = exp(p * ln(g(x)))
*/
void poly_pow_1(ll __restrict__ *f, ll __restrict__ *g, ll p, int len)
{
static ll buffer[MAXN];
poly_ln(buffer, g, len);
for (int i = 0; i < len; i++)
{
(buffer[i] *= p) %= MOD;
}
poly_exp(f, buffer, len);
}
/**
* "i won't write poly_pow because the mod of the polynomial and the mod of the coefficients are different"
*/
5.5 多项式开方
完全可以注意到,其实这就是
另外一种是使用牛顿公式,可以达到同样的复杂度。
6. 牛顿公式
又称牛顿迭代法。
6.1 基本操作
我们为了计算
-
G(0, f_1) = 0 -
\frac{\partial G}{\partial y}G(0, f_1) \not= 0
一般来说,这个
将
观察最右边的括号,
移项,得到:
在实际情况中,右侧的式子往往是好求的(一般为
6.2 多项式求逆
通过牛顿公式,我们能得到同样的式子。
假设我们要求
6.3 多项式 \exp
通过牛顿公式,我们能得到
假设我们要求
单次倍增显然是
/**
* calculate f(x) = exp(g(x)) with length of len
* if should be ensured that len is a power of 2
*
* exp(g(x)) = e^(g(x))
*
* f_len(x) = f_(len/2)(x)(1 - ln(f_(len/2)(x)) + g(x))
*/
void poly_exp(ll __restrict__ *f, ll __restrict__ *g, int len)
{
static ll buffer[MAXN];
for (int i = 0; i < (len << 1); i++)
{
f[i] = 0;
}
f[0] = 1;
for (int t = 2; t <= len; t <<= 1)
{
int t2 = t << 1;
poly_ln(buffer, f, t);
buffer[0] = (1 + g[0] - buffer[0] + MOD) % MOD;
for (int i = 1; i < t; i++)
{
buffer[i] = (g[i] - buffer[i] + MOD) % MOD;
}
for (int i = t; i < t2; i++)
{
buffer[i] = 0;
}
fft(f, t2, 1);
fft(buffer, t2, 1);
for (int i = 0; i < t2; i++)
{
(f[i] *= buffer[i]) %= MOD;
}
fft(f, t2, -1);
for (int i = t; i < t2; i++)
{
f[i] = 0;
}
}
}
6.4 多项式开方
假设要求
如果发现
那么假设
便可以在
如果直接将
/**
* calculate f(x) = sqrt(g(x)) with the length of len
* it should be ensured that g[0] = 1
*
* f(x) = (f^2(x) + g(x)) / 2f(x)
*/
void poly_sqrt_1(ll __restrict__ *f, ll __restrict__ *g, int len)
{
static ll buffer[MAXN];
for (int i = 0; i < (len << 1); i++)
{
f[i] = 0;
}
f[0] = 1;
for (int t = 2; t <= len; t <<= 1)
{
int t2 = t << 1;
poly_inv(buffer, f, t);
for (int i = 0; i < t; i++)
{
(buffer[i] *= I2) %= MOD;
}
for (int i = t; i < t2; i++)
{
buffer[i] = 0;
}
square(f, t);
for (int i = 0; i < t; i++)
{
(f[i] += g[i]) %= MOD;
}
fft(f, t2, 1);
fft(buffer, t2, 1);
for (int i = 0; i < t2; i++)
{
(f[i] *= buffer[i]) %= MOD;
}
fft(f, t2, -1);
for (int i = t; i < t2; i++)
{
f[i] = 0;
}
}
}
7. 其他操作
7.1 带余除法
假设我们要计算
显然我们不能直接让
这里有一个 trick,就是我们可以将
观察
注意到
/**
* calculate division: f(x) = g(x) * q(x) + r(x), where deg r < deg g
* if n < m, this function only modifies r, and q will stay unchanged
* otherwise, only the first m elements of r and the first (n - m + 1) elements of q will be meaningful
*/
void poly_div(ll __restrict__ *f, int n, ll __restrict__ *g, int m, ll __restrict__ *q, ll __restrict__ *r)
{
static ll buffer[MAXN];
if (n < m) // 若 n < m,我们无法使用上述式子,但是此时仅有余数有值,直接复制即可。
{
for (int i = 0; i <= n; i++)
{
r[i] = f[i];
}
return;
}
reverse(f, f + n + 1);
reverse(g, g + m + 1);
int len = 1;
while (len < (n + m + 2))
{
len <<= 1;
}
for (int i = n + 1; i < (len << 1); i++)
{
f[i] = 0;
}
for (int i = m + 1; i < len; i++)
{
g[i] = 0;
}
poly_inv(buffer, g, len);
for (int i = len; i < (len << 1); i++)
{
buffer[i] = 0;
}
conv(f, buffer, len << 1, q);
reverse(f, f + n + 1);
reverse(g, g + m + 1);
reverse(q, q + (n - m) + 1);
for (int i = n - m + 1; i < len; i++)
{
q[i] = 0;
}
conv(q, g, len, r);
for (int i = 0; i < m; i++)
{
r[i] = (f[i] - r[i] + MOD) % MOD;
}
for (int i = m; i < len; i++)
{
r[i] = 0;
}
}
7.2 任意模数 MTT
现在我们要对任意的模数取模。实际上,即使我们不取模,两个系数量级在
一种方式是三模 NTT。考虑将此多项式分别模三个 NTT 模数,最后再通过中国剩余定理得到具体的系数。这个方法常数疑似过大,但确实是可行的。
另一种方式是将所有系数拆成
由于这个实在太不常见了,就没写代码了。
8. 完整代码
#define MAXN (1 << 20)
#define MOD 998244353
#define I2 499122177
using ll = long long;
ll pow(ll b, ll p, ll m)
{
ll r = 1;
while (p)
{
if (p & 1)
{
r = r * b % m;
}
b = b * b % m;
p >>= 1;
}
return r;
}
namespace POLY
{
void fft(ll *f, int len, int on)
{
for (int i = 1, j = len >> 1; i < len - 1; i++)
{
if (i < j)
{
swap(f[i], f[j]);
}
int k = len >> 1;
while (j >= k)
{
j -= k;
k >>= 1;
}
j += k;
}
for (int h = 2; h <= len; h <<= 1)
{
ll w0 = pow(3, (MOD - 1) / h, MOD);
for (int j = 0; j < len; j += h)
{
ll w = 1;
for (int i = j; i < j + (h >> 1); i++)
{
ll u = f[i], t = f[i + (h >> 1)] * w % MOD;
f[i] = (u + t) % MOD;
f[i + (h >> 1)] = (u + MOD - t) % MOD;
w = w * w0 % MOD;
}
}
}
if (on == -1)
{
reverse(f + 1, f + len);
ll inl = pow(len, MOD - 2, MOD);
for (int i = 0; i < len; i++)
{
(f[i] *= inl) %= MOD;
}
}
}
/**
* calculate h(x) = f(x) * g(x)
* it should be ensured that len is a power of 2
* if you want to store the result to f, omit the h parameter
*/
void conv(ll __restrict__ *f, ll __restrict__ *g, int len, ll __restrict__ *h)
{
fft(f, len, 1);
fft(g, len, 1);
for (int i = 0; i < len; i++)
{
h[i] = f[i] * g[i] % MOD;
}
fft(f, len, -1);
fft(g, len, -1);
fft(h, len, -1);
}
/**
* calculate f(x) * g(x) and store it to f
* it should be ensured that len is a power of 2
*/
void conv(ll __restrict__ *f, ll __restrict__ *g, int len)
{
fft(f, len, 1);
fft(g, len, 1);
for (int i = 0; i < len; i++)
{
(f[i] *= g[i]) %= MOD;
}
fft(f, len, -1);
fft(g, len, -1);
}
/**
* calculate f(x) * f(x) and store it to f
*/
void square(ll *f, int len)
{
fft(f, len, 1);
for (int i = 0; i < len; i++)
{
(f[i] *= f[i]) %= MOD;
}
fft(f, len, -1);
}
/**
* calculate the [l, r) power of f(x) = f(x) * g(x) (self convolution), f(0) = 1
* it should be ensured that (r - l) is a power of 2
*/
void self_conv(ll __restrict__ *f, ll __restrict__ *g, int l, int r)
{
static ll buffer[MAXN];
if (l == r - 1)
{
if (!l)
{
f[l] = 1;
}
return;
}
int mid = (l + r) >> 1;
self_conv(f, g, l, mid);
for (int i = l; i < 2 * r - l; i++)
{
if (i < mid)
{
buffer[i - l] = f[i];
}
else
{
buffer[i - l] = 0;
}
}
conv(buffer, g, (r - l) << 1);
for (int i = mid; i < r; i++)
{
(f[i] += buffer[i - l]) %= MOD;
}
self_conv(f, g, mid, r);
}
/**
* calculate f(x) = 1 / g(x) with length of len
* it should be ensured that len is a power of 2
*/
void poly_inv(ll __restrict__ *f, ll __restrict__ *g, int len)
{
static ll buffer[MAXN];
f[0] = pow(g[0], MOD - 2, MOD);
for (int i = 1; i < (len << 1); i++)
{
f[i] = 0;
}
for (int t = 2; t <= len; t <<= 1)
{
int t2 = 2 * t;
for (int i = 0; i < t; i++)
{
buffer[i] = g[i];
}
for (int i = t; i < t2; i++)
{
buffer[i] = 0;
}
fft(f, t2, 1);
fft(buffer, t2, 1);
for (int i = 0; i < t2; i++)
{
f[i] = f[i] * (MOD + 2 - f[i] * buffer[i] % MOD) % MOD;
}
fft(f, t2, -1);
for (int i = t; i < t2; i++)
{
f[i] = 0;
}
}
}
/**
* calculate f = g' with length of len
* it should be ensured that len is a power of 2
* this algorithm will work if f = g
*/
void poly_der(ll *f, ll *g, int len)
{
for (int i = 1; i < len; i++)
{
f[i - 1] = g[i] * i % MOD;
}
f[len - 1] = 0;
}
/**
* calculate f = ∫g with length of len
* it should be ensured that len is a power of 2
* this algorithm will work if f = g
*
* it's name shouldn't be int because int is a c++ keyword
*/
void poly_int(ll *f, ll *g, int len)
{
for (int i = len - 2; ~i; i--)
{
f[i + 1] = g[i] * pow(i + 1, MOD - 2, MOD) % MOD;
}
f[0] = 0;
}
/**
* calculate f(x) = ln(g(x)) with length of len
* it should be ensured that len is a power of 2
*
* ln(g(x)) = ∫(g'(x) / g(x))
*/
void poly_ln(ll __restrict__ *f, ll __restrict__ *g, int len)
{
static ll buffer[MAXN];
for (int i = len; i < (len << 1); i++)
{
g[i] = 0;
}
poly_inv(buffer, g, len); // this function clears buffer[len, 2len)
poly_der(g, g, len);
conv(buffer, g, len << 1, f);
poly_int(f, f, len);
poly_int(g, g, len);
g[0] = pow(buffer[0], MOD - 2, MOD);
}
/**
* calculate f(x) = exp(g(x)) with length of len
* if should be ensured that len is a power of 2
*
* exp(g(x)) = e^(g(x))
*
* f_len(x) = f_(len/2)(x)(1 - ln(f_(len/2)(x)) + g(x))
*/
void poly_exp(ll __restrict__ *f, ll __restrict__ *g, int len)
{
static ll buffer[MAXN];
for (int i = 0; i < (len << 1); i++)
{
f[i] = 0;
}
f[0] = 1;
for (int t = 2; t <= len; t <<= 1)
{
int t2 = t << 1;
poly_ln(buffer, f, t);
buffer[0] = (1 + g[0] - buffer[0] + MOD) % MOD;
for (int i = 1; i < t; i++)
{
buffer[i] = (g[i] - buffer[i] + MOD) % MOD;
}
for (int i = t; i < t2; i++)
{
buffer[i] = 0;
}
fft(f, t2, 1);
fft(buffer, t2, 1);
for (int i = 0; i < t2; i++)
{
(f[i] *= buffer[i]) %= MOD;
}
fft(f, t2, -1);
for (int i = t; i < t2; i++)
{
f[i] = 0;
}
}
}
/**
* calculate f(x) = (g(x))^p with length of len
* it should be ensured that g[0] = 1
*
* (g(x))^p = exp(p * ln(g(x)))
*/
void poly_pow_1(ll __restrict__ *f, ll __restrict__ *g, ll p, int len)
{
static ll buffer[MAXN];
poly_ln(buffer, g, len);
for (int i = 0; i < len; i++)
{
(buffer[i] *= p) %= MOD;
}
poly_exp(f, buffer, len);
}
/**
* "i won't write poly_pow because the mod of the polynomial and the mod of the coefficients are different"
*/
/**
* calculate f(x) = sqrt(g(x)) with the length of len
* it should be ensured that g[0] = 1
*
* f(x) = (f^2(x) + g(x)) / 2f(x)
*/
void poly_sqrt_1(ll __restrict__ *f, ll __restrict__ *g, int len)
{
static ll buffer[MAXN];
for (int i = 0; i < (len << 1); i++)
{
f[i] = 0;
}
f[0] = 1;
for (int t = 2; t <= len; t <<= 1)
{
int t2 = t << 1;
poly_inv(buffer, f, t);
for (int i = 0; i < t; i++)
{
(buffer[i] *= I2) %= MOD;
}
for (int i = t; i < t2; i++)
{
buffer[i] = 0;
}
square(f, t);
for (int i = 0; i < t; i++)
{
(f[i] += g[i]) %= MOD;
}
fft(f, t2, 1);
fft(buffer, t2, 1);
for (int i = 0; i < t2; i++)
{
(f[i] *= buffer[i]) %= MOD;
}
fft(f, t2, -1);
for (int i = t; i < t2; i++)
{
f[i] = 0;
}
}
}
/**
* calculate division: f(x) = g(x) * q(x) + r(x), where deg r < deg g
* if n < m, this function only modifies r, and q will stay unchanged
* otherwise, only the first m elements of r and the first (n - m + 1) elements of q will be meaningful
*/
void poly_div(ll __restrict__ *f, int n, ll __restrict__ *g, int m, ll __restrict__ *q, ll __restrict__ *r)
{
static ll buffer[MAXN];
if (n < m)
{
for (int i = 0; i <= n; i++)
{
r[i] = f[i];
}
return;
}
reverse(f, f + n + 1);
reverse(g, g + m + 1);
int len = 1;
while (len < (n + m + 2))
{
len <<= 1;
}
for (int i = n + 1; i < (len << 1); i++)
{
f[i] = 0;
}
for (int i = m + 1; i < len; i++)
{
g[i] = 0;
}
poly_inv(buffer, g, len);
for (int i = len; i < (len << 1); i++)
{
buffer[i] = 0;
}
conv(f, buffer, len << 1, q);
reverse(f, f + n + 1);
reverse(g, g + m + 1);
reverse(q, q + (n - m) + 1);
for (int i = n - m + 1; i < len; i++)
{
q[i] = 0;
}
conv(q, g, len, r);
for (int i = 0; i < m; i++)
{
r[i] = (f[i] - r[i] + MOD) % MOD;
}
for (int i = m; i < len; i++)
{
r[i] = 0;
}
}
}
需要引用头文件 <algorithm>。