多项式全家桶

· · 休闲·娱乐

注意文章分类。

你只需要会对多项式求导!

对于多项式 A(x),记 A_i 表示 [x^i]A(x)。

多项式乘法

呃这个肯定就不用讲了。

多项式求逆

假设现在有多项式 A(x),设 B 是 A 的逆,那么有 AB=1,所以:

\sum_{i=0}^n A_{i}B_{n-i}=[n=0]

当 n\ge 1 时,提取出来左边 i=0 的项,也就是 A_0B_n:

\begin{aligned} A_0B_n&=-\sum_{i=1}^nA_iB_{n-i}\\ B_n&=-\frac{1}{A_0}\sum_{i=1}^nA_iB_{n-i} \end{aligned}

对于 n=0 的情况,显然 B_0=\frac{1}{A_0},以此为边界递推即可求出 B。

多项式 ln

假设现在有多项式 A(x),设 B(x)=\ln A(x)。

先对两边求导得到 B'(x)=\frac{A'(x)}{A(x)},然后对 A(x) 做多项式求逆再积分即可求出 B(x),也就是 A'(x)=B'(x)A(x),系数对应一下:

\begin{aligned} (n+1)A_{n+1}=\sum_{i=0}^n(i+1)B_{i+1}A_{n-i} \end{aligned}

拉出来右边 i=n 时的项:

\begin{aligned} (n+1)B_{n+1}A_{0}&=(n+1)A_{n+1}-\sum_{i=0}^{n-1}(i+1)B_{i+1}A_{n-i}\\ B_{n+1}&=\frac{(n+1)A_{n+1}-\sum_{i=0}^{n-1}(i+1)B_{i+1}A_{n-i}}{(n+1)A_0} \end{aligned}

直接递推即可。

多项式 exp

假设现在有多项式 A(x),设 B(x)=\exp A(x),那么就有 A(x)=\ln B(x),再两边求导得到 A'(x)=\frac{B'(x)}{B(x)},也就是 B'(x)=B(x)A'(x),同样对应下系数:

\begin{aligned} (n+1)B_{n+1}&=\sum_{i=0}^n(i+1)A_{i+1}B_{n-i}\\ B_{n+1}&=\frac{1}{n+1}\sum_{i=0}^n(i+1)A_{i+1}B_{n-i} \end{aligned}

同样直接递推即可。

代码

对上面的操作进行了整理,可以通过洛谷模板所有该通过的数据(迫真)。

#include<bits/stdc++.h>
#define For(i, a, b) for(int i = (a); i <= (b); i++)
#define Rof(i, a, b) for(int i = (a); i >= (b); i--)
#define int long long
#define SZ(x) ((int)x.size())
using namespace std;
using poly = vector<int>;
const int mod = 998244353;
inline void add(int &x, int y){x += y; if(x >= mod) x -= mod;}
inline void sub(int &x, int y){x += mod - y; if(x >= mod) x -= mod;}
int qpow(int x, int y){
    int res = 1;
    while(y){
        if(y & 1) res = res * x % mod;
        x = x * x % mod; y /= 2;
    }
    return res;
}
poly mul(const poly &a, const poly &b){
    int n = SZ(a) - 1, m = SZ(b) - 1;
    poly res(n + m + 1);
    For(i, 0, n + m) For(j, 0, min(i, n))
        if(i - j <= m) add(res[i], a[j] * b[i - j] % mod);
    return res;
}
poly inv(const poly &a){
    int n = SZ(a) - 1;
    poly res(n + 1); int inv = qpow(a[0], mod - 2);
    res[0] = inv;
    For(i, 1, n) For(j, 1, i) sub(res[i], inv * a[j] % mod * res[i - j] % mod);
    return res;
}
poly ln(const poly &a){
    int n = SZ(a) - 1;
    poly res(n + 1); 
    For(i, 0, n - 1){
        int inv = qpow((i + 1) * a[0] % mod, mod - 2);
        int sum = (i + 1) * a[i + 1] % mod;
        For(j, 0, i - 1) sub(sum, (j + 1) * res[j + 1] % mod * a[i - j] % mod);
        res[i + 1] = sum * inv % mod;
    }
    return res;
}
poly exp(const poly &a){
    int n = SZ(a) - 1;
    poly res(n + 1);
    res[0] = 1;
    For(i, 0, n - 1){
        int inv = qpow(i + 1, mod - 2), sum = 0;
        For(j, 0, i) add(sum, (j + 1) * a[j + 1] % mod * res[i - j] % mod);
        res[i + 1] = sum * inv % mod;
    }
    return res;
}

咳咳,这里有份正经的,依据洛谷模板题的表现常数好像还挺小的。

const int mod = 998244353, G = 3, Gi = 332748118;
using poly = vector<int>;
int tot, bit, rev[4000050];
inline void init(int n){
    tot = 1, bit = 0;
    while(tot <= n) tot <<= 1, bit++;
    For(i, 0, tot - 1) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << bit - 1);
}
void ntt(poly &a, int n, int op){
    a.resize(n);
    For(i, 0, n - 1) if(i < rev[i]) swap(a[i], a[rev[i]]);
    int base = op == 1 ? G : Gi;
    for(int mid = 1; mid < n; mid <<= 1){
        int w1 = qpow(base, (mod - 1) / (mid << 1));
        for(int i = 0; i < n; i += mid << 1)
            for(int j = 0, wk = 1; j < mid; j++, wk = wk * w1 % mod){
                int u = a[i + j], v = wk * a[i + j + mid] % mod;
                a[i + j] = (u + v) % mod; a[i + j + mid] = (u + mod - v) % mod;
            }
    }
    if(op == -1){
        int inv = qpow(n, mod - 2);
        For(i, 0, n - 1) a[i] = a[i] * inv % mod;
    }
}
poly mul(poly a, poly b){
    int n = SZ(a) - 1, m = SZ(b) - 1;
    init(n + m); ntt(a, tot, 1); ntt(b, tot, 1);
    For(i, 0, tot - 1) a[i] = a[i] * b[i] % mod;
    ntt(a, tot, -1); a.resize(n + m + 1);
    return a;
}
poly diff(const poly &a){
    int n = SZ(a) - 1; poly b(n);
    For(i, 1, n) b[i - 1] = a[i] * i % mod;
    return b;
}
poly integ(const poly &a){
    int n = SZ(a); poly b(n + 1);
    For(i, 1, n) b[i] = a[i - 1] * qpow(i, mod - 2) % mod;
    return b;
}
void getinv(int deg, const poly &a, poly &b){
    if(deg == 1) return b[0] = qpow(a[0], mod - 2), void();
    getinv((deg + 1) >> 1, a, b); init(deg * 2 + 1);
    poly c(tot); For(i, 0, deg - 1) c[i] = a[i];
    ntt(b, tot, 1); ntt(c, tot, 1);
    For(i, 0, tot - 1) b[i] = (2 - b[i] * c[i] % mod + mod) % mod * b[i] % mod;
    ntt(b, tot, -1); b.resize(deg);
}
poly inv(const poly &a){
    int n = SZ(a) - 1;
    poly b(n); getinv(n, a, b);
    return b;
}
poly ln(const poly &a){
    int n = SZ(a) - 1;
    poly res = integ(mul(diff(a), inv(a)));
    return res.resize(n + 1), res;
}
void getexp(int l, int r, const poly &a, poly &b){
    if(l == r){
        b[l] = !l ? 1 : b[l] * qpow(l, mod - 2) % mod;
        return;
    }
    int mid = (l + r) >> 1;
    getexp(l, mid, a, b);
    poly A(r - l + 1), B(mid - l + 1);
    For(i, 0, r - l) A[i] = a[i];
    For(i, l, mid) B[i - l] = b[i];
    poly res = mul(A, B);
    For(i, mid + 1, r) add(b[i], res[i - l - 1]);
    getexp(mid + 1, r, a, b);
}
poly exp(const poly &a){
    int n = SZ(a) - 1;
    poly b(n + 1); getexp(0, n, diff(a), b);
    return b;
}