多项式全家桶

Posted miracevin

tags:

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

Include

多项式乘法

多项式求逆

多项式除法

多项式取模

多项式对数函数

多项式指数函数

多项式正弦函数

多项式余弦函数

技术图片
#include<bits/stdc++.h>
#define reg register int
#define il inline
#define fi first
#define se second
#define mk(a,b) make_pair(a,b)
#define numb (ch^‘0‘)
using namespace std;
typedef long long ll;
template<class T>il void rd(T &x){
    char ch;x=0;bool fl=false;
    while(!isdigit(ch=getchar()))(ch==-)&&(fl=true);
    for(x=numb;isdigit(ch=getchar());x=x*10+numb);
    (fl==true)&&(x=-x);
}
template<class T>il void output(T x){if(x/10)output(x/10);putchar(x%10+0);}
template<class T>il void ot(T x){if(x<0) putchar(-),x=-x;output(x);putchar( );}
template<class T>il void prt(T a[],int st,int nd){for(reg i=st;i<=nd;++i) ot(a[i]);putchar(
);}

namespace Miracle{
const int mod=998244353;
const int G=3;
const int GI=332748118;
const int I=86583718;
const int iv2=499122177;
il int qm(int x,int y){int ret=1;while(y){if(y&1) ret=(ll)ret*x%mod;x=(ll)x*x%mod;y>>=1;}return ret;}
il int ad(int x,int y){return x+y>=mod?x+y-mod:x+y;}
il int sub(int x,int y){return ad(x,mod-y);}
il int mul(int x,int y){return (ll)x*y%mod;}
struct Poly{
    vector<int>f;
    Poly(){f.clear();}
    il int &operator[](const int &x){return f[x];}
    il const int &operator[](const int &x) const {return f[x];}
    il void resize(const int &n){f.resize(n);}
    il int size() const {return f.size();}
    il void cpy(Poly &b){f.resize(b.size());for(reg i=0;i<(int)f.size();++i)f[i]=b[i];}
    il void rev(){reverse(f.begin(),f.end());}
    il void clear(){f.clear();}
    il void read(const int &n){f.resize(n);for(reg i=0;i<n;++i)rd(f[i]);}
    il void out() const {for(reg i=0;i<(int)f.size();++i)ot(f[i]);putchar(
);}
}R;
il int init(const int &n){int m;for(m=1;m<n;m<<=1);return m;}
il void rev(Poly &f){
    int lp=f.size();
    if(R.size()!=f.size()) {
        R.resize(f.size());
        for(reg i=0;i<lp;++i){
            R[i]=(R[i>>1]>>1)|((i&1)?lp>>1:0);
        }
    }
    for(reg i=0;i<lp;++i){
        if(i<R[i]) swap(f[i],f[R[i]]);
    }
}
il void NTT(Poly &f,int c){
    int n=f.size();rev(f);
    for(reg p=2;p<=n;p<<=1){
        int gen=(c==1)?qm(G,(mod-1)/p):qm(GI,(mod-1)/p);
        for(reg l=0;l<n;l+=p){
            int buf=1;
            for(reg k=l;k<l+p/2;++k){
                int tmp=mul(f[k+p/2],buf);
                f[k+p/2]=sub(f[k],tmp);
                f[k]=ad(f[k],tmp);
                buf=mul(buf,gen);
            }
        }
    }
    if(c==-1){
        int iv=qm(n,mod-2);for(reg i=0;i<n;++i) f[i]=mul(f[i],iv);
    }
}
il Poly Inv(const Poly &f,int n){
    if(n==1){
        Poly g;g.resize(1);g[0]=qm(f[0],mod-2);return g;
    }
    Poly g=Inv(f,(n+1)>>1),t;
    int m=init(n*2);
    t.resize(m);
    for(reg i=0;i<n;++i)t[i]=f[i];
    g.resize(m);
    NTT(g,1);NTT(t,1);
    for(reg i=0;i<m;++i)g[i]=mul(sub(2,mul(g[i],t[i])),g[i]);
    NTT(g,-1);g.resize(n);
    return g;
}
il void operator *=(Poly &f,Poly g){
    int st=f.size()+g.size()-1;
    int len=init(f.size()+g.size()-1);f.resize(len);g.resize(len);
    NTT(f,1);NTT(g,1);for(reg i=0;i<len;++i) f[i]=mul(f[i],g[i]);
    NTT(f,-1);
    f.resize(st);
}
il void operator *=(Poly &f,const int &c){for(reg i=0;i<f.size();++i) f[i]=mul(f[i],c);}
il Poly operator *(Poly f,const Poly &g){f*=g;return f;}
il Poly operator *(Poly f,const int &c){for(reg i=0;i<f.size();++i) f[i]=mul(f[i],c);return f;}
il void operator +=(Poly &f,const Poly &g){for(reg i=0;i<f.size();++i) f[i]=ad(f[i],g[i]);}
il void operator +=(Poly &f,const int &c){f[0]=ad(f[0],c);}
il Poly operator +(Poly f,const Poly &g){for(reg i=0;i<f.size();++i) f[i]=ad(f[i],g[i]);return f;}
il Poly operator +(Poly f,const int &c){f[0]=ad(f[0],c);return f;}
il void operator -=(Poly &f,const Poly &g){for(reg i=0;i<f.size();++i) f[i]=sub(f[i],g[i]);}
il void operator -=(Poly &f,const int &c){f[0]=sub(f[0],c);}
il Poly operator -(Poly f,const Poly &g){for(reg i=0;i<f.size();++i) f[i]=sub(f[i],g[i]);return f;}
il Poly operator -(Poly f,const int &c){f[0]=sub(f[0],c);return f;}
il Poly operator ~(const Poly &f){return Inv(f,f.size());}
il Poly operator /(Poly f,Poly g){int len=f.size()-g.size()+1;f.rev();g.rev();g.resize(len);f=f*(~g);f.resize(len);f.rev();return f;}
il Poly operator %(Poly f,Poly g){Poly s=f/g;f=f-g*s;f.resize(g.size()-1);return f;}
il Poly Inter(Poly f){int st=f.size();f.resize(st+1);for(reg i=st;i>=1;--i){f[i]=mul(f[i-1],qm(i,mod-2));}f[0]=0;return f;}
il Poly Diff(Poly f){int st=f.size();for(reg i=0;i<st-1;++i) f[i]=mul(f[i+1],(i+1));f.resize(st-1);return f;}
il Poly Ln(const Poly &f){Poly g=Diff(f),h=(~f);g=g*h;g.resize(f.size()-1);return Inter(g);}
il Poly Exp(const Poly &f,int n){
    if(n==1){
        Poly g;g.resize(1);g[0]=1;
        return g;
    }
    Poly g=Exp(f,(n+1)>>1);
    g.resize(n);
    g=g*(((Ln(g)*(mod-1))+1)+f);
    g.resize(n);
    return g;
}
il Poly Exp(const Poly &f){
    return Exp(f,f.size());
}
//i^2=998244352 i=86583718
il Poly Cos(const Poly &f){
    Poly g=Exp(f*I);return (g+(~g))*iv2;
}
il Poly Sin(const Poly &f){
    Poly g=Exp(f*I);return (g-(~g))*qm(ad(I,I),mod-2);
}
int main(){
    int n,t;rd(n);rd(t);
    Poly f;f.read(n);
    if(t==0) (Sin(f)).out();
    else (Cos(f)).out();
    return 0;
}

}
signed main(){
    Miracle::main();
    return 0;
}

/*
   Author: *Miracle*
   Date: 2019/4/8 18:57:00
*/
View Code

持(yi)续(ding)更(bu)新(gu)~

 

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

多项式全家桶(持续更新中)

学习笔记多项式全家桶(包含全套证明)

多项式全家桶

多项式全家桶

多项式全家桶

多项式全家桶