多项式除法

Posted mychael

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多项式除法相关的知识,希望对你有一定的参考价值。

多项式除法解决这样一个问题:
有一个(n)次多项式(A(x))和一个(m)次多项式(B(x)),你希望求得多项式(Q(x))(R(x)),使得
[A(x) = Q(x)B(x) + R(x)]
其中(deg_D le n - m)(deg_R < m)

那个余数特别难处理,考虑如何去掉
我们记(A^{R}(x) = x^nA(frac{1}{x}))表示系数翻转后的多项式
那么对于
[A(x) = Q(x)B(x) + R(x)]
我们写成
[A(frac{1}{x}) = Q(frac{1}{x})B(frac{1}{x}) + R(frac{1}{x})]
两边同乘(x^{n})
[x^{n}A(frac{1}{x}) = x^{n - m}Q(frac{1}{x})x^{n}B(frac{1}{x}) + x^{n}R(frac{1}{x})]

[A^{R}(frac{1}{x}) = Q^{R}(frac{1}{x})B^{R}(frac{1}{x}) + x^{n - m + 1}R^{R}(frac{1}{x})]
发现此时在模(x^{n - m + 1})意义下(R^{R}(x))就不存在了,而(Q(x))次的上界恰好就是(n - m + 1),所以在此模意义下我们求出的(Q^{R}(x))就是实际我们所需的(Q(x))的系数翻转后的多项式

化为
[Q^{R}(x) equiv A^{R}(x)(B^{R}(x))^{-1} pmod {x^{n - m + 1}}]
多项式求逆即可
(Q^{R}(x))翻转之后得到(Q(x)),回代求出(R(x))
复杂度(O(nlogn))

模板题

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 400005,maxm = 100005,INF = 1000000000;
inline int read(){
    int out = 0,flag = 1; char c = getchar();
    while (c < 48 || c > 57){if (c == ‘-‘) flag = -1; c = getchar();}
    while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
    return out * flag;
}
const int G = 3,P = 998244353;
inline int qpow(int a,int b){
    int re = 1;
    for (; b; b >>= 1,a = 1ll * a * a % P)
        if (b & 1) re = 1ll * re * a % P;
    return re;
}
int R[maxn];
inline void NTT(int* a,int n,int f){
    for (int i = 0; i < n; i++) if (i < R[i]) swap(a[i],a[R[i]]);
    for (int i = 1; i < n; i <<= 1){
        int gn = qpow(G,(P - 1) / (i << 1));
        for (int j = 0; j < n; j += (i << 1)){
            int g = 1,x,y;
            for (int k = 0; k < i; k++,g = 1ll * g * gn % P){
                x = a[j + k],y = 1ll * g * a[j + k + i] % P;
                a[j + k] = (x + y) % P,a[j + k + i] = ((x - y) % P + P) % P;
            }
        }
    }
    if (f == 1) return;
    int nv = qpow(n,P - 2); reverse(a + 1,a + n);
    for (int i = 0; i < n; i++) a[i] = 1ll * a[i] * nv % P;
}
int c[maxn];
void Inv(int* a,int* b,int deg){
    if (deg == 1){b[0] = qpow(a[0],P - 2); return;}
    Inv(a,b,(deg + 1) >> 1);
    int L = 0,n = 1;
    while (n < (deg << 1)) n <<= 1,L++;
    for (int i = 1; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
    for (int i = 0; i < deg; i++) c[i] = a[i];
    for (int i = deg; i < n; i++) c[i] = 0;
    NTT(c,n,1); NTT(b,n,1);
    for (int i = 0; i < n; i++)
        b[i] = 1ll * ((2ll - 1ll * c[i] * b[i] % P) + P) % P * b[i] % P;
    NTT(b,n,-1);
    for (int i = deg; i < n; i++) b[i] = 0;
}
int f[maxn],g[maxn],q[maxn],r[maxn],gv[maxn],N,M;
void work(){
    reverse(f,f + N + 1);
    reverse(g,g + M + 1);
    Inv(g,gv,N - M + 1);
    
    int n = 1,L = 0,E = N - M;
    while (n <= (E << 1)) n <<= 1,L++;
    for (int i = 1; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
    for (int i = 0; i <= E; i++) c[i] = f[i];
    for (int i = E + 1; i < n; i++) c[i] = 0;
    for (int i = E + 1; i < n; i++) gv[i] = 0;
    NTT(c,n,1); NTT(gv,n,1);
    for (int i = 0; i < n; i++) q[i] = 1ll * c[i] * gv[i] % P;
    NTT(q,n,-1);
    reverse(q,q + E + 1);
    for (int i = E + 1; i < n; i++) q[i] = 0;
    reverse(g,g + M + 1);
    reverse(f,f + N + 1);
    
    n = 1,L = 0;
    while (n <= N) n <<= 1,L++;
    for (int i = 1; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
    for (int i = 0; i <= E; i++) c[i] = q[i];
    for (int i = E + 1; i < n; i++) c[i] = 0;
    for (int i = M + 1; i < n; i++) g[i] = 0;
    NTT(g,n,1); NTT(c,n,1);
    for (int i = 0; i < n; i++) c[i] = 1ll * c[i] * g[i] % P;
    NTT(c,n,-1);
    for (int i = 0; i < M; i++) r[i] = ((f[i] - c[i]) % P + P) % P;
    
    for (int i = 0; i <= E; i++) printf("%d ",q[i]); puts("");
    for (int i = 0; i < M; i++) printf("%d ",r[i]);
}
int main(){
    N = read(); M = read();
    for (int i = 0; i <= N; i++) f[i] = read();
    for (int i = 0; i <= M; i++) g[i] = read();
    work();
    return 0;
}

















以上是关于多项式除法的主要内容,如果未能解决你的问题,请参考以下文章

P4512 模板多项式除法

多项式除法及取模

多项式 - 除法与取模

多项式除法&取模

高数 -- 多项式除法

如何用Matlab进行多项式除法运算