多项式除法

多项式除法

定义

\(给一n次多项式F(x),m次多项式G(x),求一多项式Q(x),R(x),满足\)

  • \(Q(x)次数为n-m,R(x)次数小于m\)
  • \(F(x)=G(x)Q(x)+R(x)\)

前置知识

  • \(NTT\)
  • \(\text{多项式求逆}\)
  • \(同余\)

推式子:

\[令f_r(x)表示函数f(x)系数翻转后的函数\]
\[\because F(x)=a_0x^n+a_1x^{n-1}+...a_n\]
\[\therefore F_r(x)=a_nx^n+a_{n-1}x^{n-1}+...a_0\]
\[=x^n(a_n+a_{n-1}\frac{1}{x}+...a_0\frac{1}{x^n})\]
\[=x^nF(\frac{1}{x})\]
\[\because F(x)=G(x)Q(x)+R(x)\]
\[\therefore F(\frac{1}{x})=G(\frac{1}{x})Q(\frac{1}{x})+R(\frac{1}{x})\]
\[左右同乘x^n\]
\[\because x^nF(\frac{1}{x})=x^mG(\frac{1}{x})x^{n-m}Q(\frac{1}{x})+x^{n-m+1}x^{m-1}R(\frac{1}{x})\]
\[\therefore F_r(\frac{1}{x})=G_r(\frac{1}{x})Q_r(\frac{1}{x})+x^{n-m+1}R(\frac{1}{x})\]
\[\because F_r(\frac{1}{x})\equiv G_r(\frac{1}{x})Q_r(\frac{1}{x})+x^{n-m+1}R(\frac{1}{x})\ (mod\ x^{n-m+1})\]
\[\therefore F_r(\frac{1}{x})\equiv G_r(\frac{1}{x})Q_r(\frac{1}{x})\ (mod\ x^{n-m+1})\]
\[\because F_r(x)\equiv G_r(x)Q_r(x)\ (mod\ x^{n-m+1})\]
\[\therefore \frac{F_r(x)}{G_r(x)}\equiv Q_r(x)\ (mod\ x^{n-m+1})\]

步骤:

\(1.求出G_r(x)在mod\ x^{n-m+1}意义下的逆元,与F_r(x)相乘\)

\(2.翻转\)

【模板】多项式除法

\(\mathfrak{Talk\ is\ cheap,show\ you\ the\ code.}\)

#include
#include
#include
#include
using namespace std;
# define read read1()
# define Type template
Type inline T read1(){
    T n=0;
    char k;
    bool fl=0;
    do (k=getchar())=='-'&&(fl=1);while('9'a;
    public:
        Array(const int size,const int f):a(size,f){}
        void push(int n){a.push_back(n);}
        Array(int* l=NULL,int* r=NULL){while(l!=r)push(*l),++l;}
        inline int size(){return a.size();}
        inline int& operator [] (const int x){return a[x];}
        void resize(int n){a.resize(n);}
        void clear(){a.clear();}
        void swap(){reverse(a.begin(),a.end());}
};
Array operator -(Array a,Array b){
    int N=a.size(),M=b.size();
    Array t;
    for(int i=0;i>=1,tem=(ll)tem*tem%mod)
        if(m&1)ans=(ll)ans*tem%mod;
    return ans;
}
const int mod=998244353,g=3;
int* NTT(const int len,Array& a,const bool Ty,int* r=NULL){
    if(!r){
        r=new int[len];
        r[0]=0;int L=log2(len);
        f(i,0,len-1)
            r[i]=(r[i>>1]>>1)|((i&1)<mod)&&(a[j+k]-=mod);
                a[i+j+k]=x-y+mod;(a[i+j+k]>mod)&&(a[i+j+k]-=mod);
            }
        }
    }
    return r;
}
Array operator * (Array x,Array y){
    int n=x.size()-1,m=y.size()-1;
    int limit=1;
    while(limit<=n+m)limit<<=1;
    Array ans;
    x.resize(limit+1);
    y.resize(limit+1);
    int *r;
    r=NTT(limit,x,1);
    NTT(limit,y,1,r);
    f(i,0,limit)x[i]=(ll)x[i]*y[i]%mod;
    NTT(limit,x,0,r);
    int tem=qkpow(limit,mod-2,mod);
    f(i,0,n+m)ans.push((ll)x[i]*tem%mod);
    return ans;
}
Array& operator *= (Array& x,Array y){
    return x=x*y;
}
void Rev(Array &x,Array y){
    int n=x.size()-1,m=y.size()-1;
    int limit=1;
    while(limit<=n+m)limit<<=1;
    Array ans;
    x.resize(limit+1);
    y.resize(limit+1);
    int *r;
    r=NTT(limit,x,1);
    NTT(limit,y,1,r);
    f(i,0,limit)x[i]=(ll)(2ll-(ll)x[i]*y[i]%mod+mod)%mod*y[i]%mod;
    NTT(limit,x,0,r);
    int tem=qkpow(limit,mod-2,mod);
    f(i,0,n+m)x[i]=(ll)x[i]*tem%mod;
    x.resize(n+m+1);
}
Array Inv(Array a){
    int N=a.size();
    if(N==1)return Array(1,qkpow(a[0],mod-2,mod));
    Array b=a;b.resize(N+1>>1);
    b=Inv(b);b.resize(N);
    Rev(a,b);
    a.resize(N);
    return a;
}
Array operator / (Array x,Array y){
    int N=x.size()-1,M=y.size()-1;
    if(N

你可能感兴趣的:(多项式除法)