• P4512 【模板】多项式除法


    题意:多项式除法,A(x)=C(x)*B(x)+D(x),给定A(x),B(x),求C(x),D(x)
    题解:A(x)的度是n,B(x)的度是m
    定义(A'(x)=x^n*A(frac{1}{x})),可以发现(A'(x)=A(n-x))
    (A(frac{1}{x})=C(frac{1}{x})*B(frac{1}{x})+D(frac{1}{x}))
    (x^nA(frac{1}{x})=x^{n-m}*C(frac{1}{x})*x^{m}*B(frac{1}{x})+x^{n-m+1}*x^{m}D(frac{1}{x}))
    (A'(x)=C'(x)*B'(x)+x^{n-m+1}*D'(x))
    那么我们考虑在(mod(x^{n-m+1}))的等式,即可消去D'(x)
    这样我们便可求出(C'(x)=frac{A'(x)}{B'(x)}(mod(x^{n-m+1}))),用多项式求逆,再回退C,带入可求出D

    //#pragma GCC optimize(2)
    //#pragma GCC optimize(3)
    //#pragma GCC optimize(4)
    //#pragma GCC optimize("unroll-loops")
    //#pragma comment(linker, "/stack:200000000")
    //#pragma GCC optimize("Ofast,no-stack-protector")
    //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
    #include<bits/stdc++.h>
    #define fi first
    #define se second
    #define db double
    #define mp make_pair
    #define pb push_back
    #define pi acos(-1.0)
    #define ll long long
    #define vi vector<int>
    #define mod 998244353
    #define ld long double
    #define C 0.5772156649
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    #define pll pair<ll,ll>
    #define pil pair<int,ll>
    #define pli pair<ll,int>
    #define pii pair<int,int>
    //#define cd complex<double>
    #define ull unsigned long long
    #define base 1000000000000000000
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    #define fin freopen("a.txt","r",stdin)
    #define fout freopen("a.txt","w",stdout)
    #define fio ios::sync_with_stdio(false);cin.tie(0)
    template<typename T>
    inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
    template<typename T>
    inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
    inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
    inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
    inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
    inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
    
    using namespace std;
    
    const double eps=1e-8;
    const ll INF=0x3f3f3f3f3f3f3f3f;
    const int N=300000+10,maxn=400000+10,inf=0x3f3f3f3f;
    
    ll a[N<<3],b[N<<3],c[N<<3],d[N<<3],e[N<<3],tmp[N<<3];
    int rev[N<<3];
    void getrev(int bit)
    {
        for(int i=0;i<(1<<bit);i++)
            rev[i]=(rev[i>>1]>>1) | ((i&1)<<(bit-1));
    }
    void ntt(ll *a,int n,int dft)
    {
        for(int i=0;i<n;i++)
            if(i<rev[i])
                swap(a[i],a[rev[i]]);
        for(int step=1;step<n;step<<=1)
        {
            ll wn=qp(3,(mod-1)/(step*2));
            if(dft==-1)wn=qp(wn,mod-2);
            for(int j=0;j<n;j+=step<<1)
            {
                ll wnk=1;
                for(int k=j;k<j+step;k++)
                {
                    ll x=a[k];
                    ll y=wnk*a[k+step]%mod;
                    a[k]=(x+y)%mod;a[k+step]=(x-y+mod)%mod;
                    wnk=wnk*wn%mod;
                }
            }
        }
        if(dft==-1)
        {
            ll inv=qp(n,mod-2);
            for(int i=0;i<n;i++)a[i]=a[i]*inv%mod;
        }
    }
    void pol_inv(int deg,ll *a,ll *b)
    {
        if(deg==1){b[0]=qp(a[0],mod-2);return ;}
        pol_inv((deg+1)>>1,a,b);
        int sz=0;while((1<<sz)<=deg)sz++;sz++;
        getrev(sz);int len=1<<sz;
        for(int i=0;i<deg;i++)tmp[i]=a[i];
        for(int i=deg;i<len;i++)tmp[i]=0;
        ntt(tmp,len,1),ntt(b,len,1);
        for(int i=0;i<len;i++)
            b[i]=(2ll-tmp[i]*b[i]%mod+mod)%mod*b[i]%mod;
        ntt(b,len,-1);
        for(int i=deg;i<len;i++)b[i]=0;
    }
    void pol_div(int n,int m,ll *a,ll *b,ll *c,ll *d)
    {
        for(int i=0;i<=n-m;i++)if(m>=i)c[i]=b[m-i];
        int sz=0;while((1<<sz)<=MAX(n-m+1,m))sz++;sz++;
        int len=1<<sz;
        pol_inv(len,c,d);
        for(int i=0;i<=n-m;i++)c[i]=a[n-i];
        for(int i=n-m+1;i<len;i++)c[i]=d[i]=0;
        getrev(sz);
        ntt(c,len,1);ntt(d,len,1);
        for(int i=0;i<len;i++)c[i]=c[i]*d[i]%mod;
        ntt(c,len,-1);
        reverse(c,c+n-m+1);
        for(int i=n-m+1;i<len;i++)c[i]=0;
        sz=0;while((1<<sz)<=n+1)sz++;sz++;
        len=1<<sz;getrev(sz);
        ntt(c,len,1);ntt(b,len,1);
        for(int i=0;i<len;i++)d[i]=c[i]*b[i]%mod;
        ntt(d,len,-1);ntt(c,len,-1);
        for(int i=0;i<len;i++)d[i]=(a[i]-d[i]+mod)%mod;
        for(int i=m;i<len;i++)d[i]=0;
    }
    int main()
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=0;i<=n;i++)scanf("%lld",&a[i]);
        for(int i=0;i<=m;i++)scanf("%lld",&b[i]);
        pol_div(n,m,a,b,c,d);
        for(int i=0;i<=n-m;i++)printf("%lld ",c[i]);puts("");
        for(int i=0;i<m;i++)printf("%lld ",d[i]);puts("");
        return 0;
    }
    /********************
    5 1
    1 9 2 6 0 8
    1 7
    ********************/
    
  • 相关阅读:
    【python】requests库
    pycharm新建项目时选择virtualenv的说明
    cookie、session、token
    读写锁--DEMO
    锁降级--防止线程安全问题
    mysql-left join
    index-document-shard
    ES-常见搜索方式
    SpringBoot在自定义类中调用service层等Spring其他层
    mongodb crud
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/9530479.html
Copyright © 2020-2023  润新知