Two issues of polynomial - polynomial division

Polynomial inverse polynomial division is the foundation, if you will not inverse polynomial, please see here

Problem: Given two polynomials $ F (x) $ (the number is n), $ G (x) $ (the number is m), the sum of two polynomials $ Q (x) $ and $ R (x) $, satisfying $ F (x) = G (x) Q (x) + R (x) $, all operations carried out in the sense die 998 244 353

Pushed out of the equation:

$F(x)=G(x)Q(x)+R(x)$

With $ \ frac {1} {x} $ alternative $ x $, to give:

$F(\frac{1}{x})=G(\frac{1}{x})Q(\frac{1}{x})+R(\frac{1}{x})$

Both sides by a $ x ^ {n} $, to give:

$x^{n}F(\frac{1}{x})=x^{m}G(\frac{1}{x})x^{n-m}Q(\frac{1}{x})+x^{n}R(\frac{1}{x})$

The expression $ F (x) = G (x) Q (x) + R (x) $ analyzed, it can be seen if $ F (x) $ is the number of n, (x) $ $ G is m times , the $ Q (x) $ is the number of $ nm $, $ R (x) $ does not exceed the number of m-1

Then take the inverse of the first argument and then by a maximum number of times a structure equivalent to the original polynomial coefficients of the polynomial is just the opposite!

也即若$F(x)=\sum_{i=0}^{n}a_{i}x^{i}$,则$x^{n}F(\frac{1}{x})=\sum_{i=0}^{n}a_{n-i}x^{i}$

We note $ x ^ {n} F (\ frac {1} {x}) = \ sum_ {i = 0} ^ {n} a_ {ni} x ^ {i} = F ^ {T} (x) $

Then the above equation can be simplified into $ F ^ {T} (x) = G ^ {T} (x) Q ^ {T} (x) + R ^ {T} (x) $

Can be found, $ R ^ {T} (x) $ before the polynomial $ (n-m + 1) coefficients are items $ 0!

So if we at $ mod $ $ x ^ {n-m + 1} $ sense, this equation can be derived immediately:

$F^{T}(x)\equiv G^{T}(x)Q^{T}(x)$($mod$ $x^{n-m+1}$)

So transposition that was:

$Q^{T}(x)\equiv \frac{F^{T}(x)}{G^{T}(x)}$($mod$ $x^{n-m+1}$)

This determined the $ Q (x) $, then based on the original expression, we get:

$R(x)=F(x)-G(x)Q(x)$

$ R (x) $ even out

#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#define ll long long
using namespace std;
const ll mode=998244353;
int to[(1<<20)+5];
int n,m;
ll FF[100005],GG[100005],F[100005],G[100005],Q[100005],GF[100005];
struct node
{
    ll g[100005];
    int len;
}las,now;
ll pow_mul(ll x,ll y)
{
    ll ret=1;
    while(y)
    {
        if(y&1)ret=ret*x%mode;
        x=x*x%mode,y>>=1;
    }
    return ret;
}
void NTT(ll *a,int len,int k)
{
    for(int i=0;i<len;i++)if(i<to[i])swap(a[i],a[to[i]]);
    for(int i=1;i<len;i<<=1)
    {
        ll w0=pow_mul(3,(mode-1)/(i<<1));
        for(int j=0;j<len;j+=(i<<1))
        {
            ll w=1;
            for(int o=0;o<i;o++,w=w*w0%mode)
            {
                ll w1=a[j+o],w2=a[j+o+i]*w%mode;
                a[j+o]=(w1+w2)%mode,a[j+o+i]=((w1-w2)%mode+mode)%mode;
            }
        }
    }
    if(k==-1)
    {
        ll inv=pow_mul(len,mode-2);
        for(int i=1;i<(len>>1);i++)swap(a[i],a[len-i]);
        for(int i=0;i<len;i++)a[i]=a[i]*inv%mode;
    }
}
ll a[(1<<20)+5],b[(1<<20)+5],c[(1<<20)+5];
void solve(int dep)
{
    if(dep==1)
    {
        now.g[0]=pow_mul(G[0],mode-2);
        now.len=1;
        las=now;
        return;
    }
    int nxt=(dep+1)/2;
    solve(nxt);
    int lim=1,l=0;
    while(lim<=2*dep)lim<<=1,l++;
    for(int i=0;i<lim;i++)to[i]=((to[i>>1]>>1)|((i&1)<<(l-1)));
    for(int i=0;i<lim;i++)a[i]=b[i]=0;
    for(int i=0;i<dep;i++)a[i]=G[i];
    for(int i=0;i<nxt;i++)b[i]=las.g[i];
    NTT(a,lim,1),NTT(b,lim,1);
    for(int i=0;i<lim;i++)c[i]=a[i]*b[i]%mode*b[i]%mode;
    NTT(c,lim,-1);
    now.len=dep;
    for(int i=0;i<dep;i++)now.g[i]=((2*las.g[i]-c[i])%mode+mode)%mode;
    las=now;
}
void mul(ll *A,ll *B,int len)
{
    int lim=1,l=0;
    while(lim<=2*len)lim<<=1,l++;
    for(int i=1;i<lim;i++)to[i]=((to[i>>1]>>1)|((i&1)<<(l-1)));
    for(int i=0;i<lim;i++)a[i]=b[i]=0;
    for(int i=0;i<len;i++)a[i]=A[i],b[i]=B[i];
    NTT(a,lim,1),NTT(b,lim,1);
    for(int i=0;i<lim;i++)c[i]=a[i]*b[i]%mode;
    NTT(c,lim,-1);
    
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<=n;i++)scanf("%lld",&FF[i]),F[n-i]=FF[i];
    for(int i=0;i<=m;i++)scanf("%lld",&GG[i]),G[m-i]=GG[i];
    for(int i=n-m+2;i<=m;i++)G[i]=0;
    for(int i=n-m+1;i<=n;i++)F[i]=0;
    solve(n-m+1);
    for(int i=0;i<=n-m;i++)GF[i]=now.g[i];
    mul(F,GF,n-m+1);
    for(int i=0;i<=n-m;i++)Q[i]=c[i];    
    for(int i=0;i<=n-m;i++)Q[i]=c[n-m-i];
    for(int i=0;i<=n-m;i++)printf("%lld ",Q[i]);
    printf("\n");
    mul(Q,GG,max(m+1,n-m+1));
    for(int i=0;i<m;i++)printf("%lld ",((FF[i]-c[i])%mode+mode)%mode);
    printf("\n");
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/zhangleo/p/11007443.html