poly

#include<bits/stdc++.h>
#define For(i,a,b) for(register int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(register int i=(a);i>=(b);--i)
using namespace std;
inline int read()
{
    char c=getchar();int x=0;bool f=0;
    for(;!isdigit(c);c=getchar())f^=!(c^45);
    for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
    if(f)x=-x;return x;
}
 
#define mod 998244353
struct modint{
    int x;
    modint(int o=0){x=o;}
    modint &operator = (int o){return x=o,*this;}
    modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
    modint &operator -=(modint o){return x=x-o.x<0?x-o.x+mod:x-o.x,*this;}
    modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
    modint &operator ^=(int b){
        modint a=*this,c=1;
        for(;b;b>>=1,a*=a)if(b&1)c*=a;
        return x=c.x,*this;
    }
    modint &operator /=(modint o){return *this *=o^=mod-2;}
    modint &operator +=(int o){return x=x+o>=mod?x+o-mod:x+o,*this;}
    modint &operator -=(int o){return x=x-o<0?x-o+mod:x-o,*this;}
    modint &operator *=(int o){return x=1ll*x*o%mod,*this;}
    modint &operator /=(int o){return *this *= ((modint(o))^=mod-2);}
    template<class I>friend modint operator +(modint a,I b){return a+=b;}
    template<class I>friend modint operator -(modint a,I b){return a-=b;}
    template<class I>friend modint operator *(modint a,I b){return a*=b;}
    template<class I>friend modint operator /(modint a,I b){return a/=b;}
    friend modint operator ^(modint a,int b){return a^=b;}
    friend bool operator ==(modint a,int b){return a.x==b;}
    friend bool operator !=(modint a,int b){return a.x!=b;}
    bool operator ! () {return !x;}
    modint operator - () {return x?mod-x:0;}
};
 
#define maxn 4000005
const modint G=3,Ginv=modint(1)/3;
struct poly{
    vector<modint>a;
    modint& operator [](int i){return a[i];}
    int size(){return a.size();}
    void resize(int n){a.resize(n);}
};
inline poly one(){poly a;a.a.push_back(1);return a;}
int rev[maxn];
inline int ext(int n){
	int k=0;
	while((1<<k)<n)++k;return k; 
}
inline void init(int k){
	int n=1<<k;
	For(i,0,n-1)rev[i]=(rev[i>>1]>>1)|((i&1)<<(k-1));
}
void ntt(poly&a,int k,int typ)
{
	int n=1<<k;
	For(i,0,n-1)if(i<rev[i])swap(a[i],a[rev[i]]); 
	for(int mid=1;mid<n;mid<<=1)
	{
		modint wn=(typ>0?G:Ginv)^((mod-1)/(mid<<1));
		for(int r=mid<<1,j=0;j<n;j+=r){
			modint w=1;
			for(int k=0;k<mid;++k,w=w*wn){
				modint x=a[j+k],y=w*a[j+k+mid];
				a[j+k]=x+y,a[j+k+mid]=x-y;
			}
		}
	}
	if(typ<0){
		modint inv=modint(1)/n;
		For(i,0,n-1)a[i]*=inv;
	}
}
 
poly operator +(poly a,poly b){
	int n=max(a.size(),b.size());a.resize(n),b.resize(n);
	For(i,0,n-1)a[i]+=b[i];return a;
}
poly operator -(poly a,poly b){
	int n=max(a.size(),b.size());a.resize(n),b.resize(n);
	For(i,0,n-1)a[i]-=b[i];return a;
}
poly operator *(poly a,poly b)
{
	int n=(int)a.size()+(int)b.size()-1,k=ext(n);
	a.resize(1<<k),b.resize(1<<k),init(k);
	ntt(a,k,1),ntt(b,k,1);
	For(i,0,(1<<k)-1)a[i]*=b[i];
	ntt(a,k,-1),a.resize(n);return a;
}
poly inv(poly a,int k){
	int n=1<<k;a.resize(n);
	if(n==1){a[0]=modint(1)/a[0];return a;}
	poly f,f0=inv(a,k-1);
	f.resize(n),f0.resize(n<<1),a.resize(n<<1);
	For(i,0,n/2-1)f[i]=f0[i]*2;
	init(k+1),ntt(f0,k+1,1),ntt(a,k+1,1);
	For(i,0,(n<<1)-1)f0[i]=f0[i]*f0[i]*a[i];
	ntt(f0,k+1,-1),f0.resize(n);
	For(i,0,n-1)f[i]-=f0[i];
	return f;
}
poly inv(poly a){
	int n=a.size();
	a=inv(a,ext(n)),a.resize(n);return a;
}
poly deriv(poly a){
	int n=(int)a.size()-1;
	For(i,0,n-1)a[i]=a[i+1]*(i+1);
	a.resize(n);return a;
}
poly inter(poly a){
	int n=a.size()+1;a.resize(n);
	Rep(i,n,1)a[i]=a[i-1]/i;
	a[0]=0;return a;
}
poly ln(poly a){
	int n=a.size();
	a=inter(deriv(a)*inv(a));
	a.resize(n);return a;
}
poly exp(poly a,int k){
	int n=1<<k;a.resize(n);
	if(n==1)return one();
	poly f0=exp(a,k-1);f0.resize(n);
	return f0*(one()+a-ln(f0)); 
}
poly exp(poly a){
	int n=a.size();
	a=exp(a,ext(n));a.resize(n);return a;
}

int main()
{
	
    return 0;
}
上一篇:多项式模板


下一篇:联考20200722 T1 集合划分