2019.01.01洛谷 P4725/P4726 多項式對數/指數函式(牛頓迭代)
阿新 • • 發佈:2019-01-02
#include<bits/stdc++.h>
#define ri register int
using namespace std;
inline int read(){
int ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
return ans;
}
typedef long long ll;
const int mod=998244353;
int n,lim,tim;
vector<int>A,B,pos,Inv;
inline int add(const int&a,const int&b){return a+b>=mod?a+b-mod:a+b;}
inline int dec(const int&a,const int&b){return a>=b?a-b:a-b+mod;}
inline int mul(const int&a,const int&b){return (ll)a*b%mod;}
inline int ksm(int a,int p){int ret=1;for(;p;p>>=1,a=mul(a,a))if(p&1)ret=mul(ret,a);return ret;}
inline void ntt(vector<int>&a,int type){
for(ri i=0;i<lim;++i)if(i<pos[i])swap(a[i],a[pos[i]]);
for(ri mid=1,wn,mult=(mod-1)/2,typ=type==1?3:(mod+1)/3;mid<lim;mid<<=1,mult>>= 1){
wn=ksm(typ,mult);
for(ri j=0,len=mid<<1;j<lim;j+=len)for(ri w=1,a0,a1,k=0;k<mid;++k,w=mul(w,wn)){
a0=a[j+k],a1=mul(w,a[j+k+mid]);
a[j+k]=add(a0,a1),a[j+k+mid]=dec(a0,a1);
}
}
if(type==-1)for(ri i=0,inv=ksm(lim,mod-2);i<lim;++i)a[i]=mul(a[i],inv);
}
inline void init(int up){
lim=1,tim=0;
while(lim<=up)lim<<=1,++tim;
pos.resize(lim-1),pos[0]=0;
for(ri i=0;i<lim;++i)pos[i]=(pos[i>>1]>>1)|((i&1)<<(tim-1));
}
struct poly{
vector<int>a;
inline int deg()const{return a.size()-1;}
poly(int k,int x=0){a.resize(k+1),a[k]=x;}
inline int&operator[](const int&k){return a[k];}
inline const int&operator[](const int&k)const{return a[k];}
inline poly extend(const int&k){poly ret=*this;return ret.a.resize(k+1),ret;}
friend inline poly operator+(const poly&a,const poly&b){
poly ret(max(a.deg(),b.deg()));
for(ri i=0;i<=a.deg();++i)ret[i]=add(ret[i],a[i]);
for(ri i=0;i<=b.deg();++i)ret[i]=add(ret[i],b[i]);
return ret;
}
friend inline poly operator-(const poly&a,const poly&b){
poly ret(max(a.deg(),b.deg()));
for(ri i=0;i<=a.deg();++i)ret[i]=add(ret[i],a[i]);
for(ri i=0;i<=b.deg();++i)ret[i]=dec(ret[i],b[i]);
return ret;
}
friend inline poly operator*(const int&a,const poly&b){
poly ret(b.deg());
for(ri i=0;i<=b.deg();++i)ret[i]=mul(a,b[i]);
return ret;
}
friend inline poly operator*(const poly&a,const poly&b){
int n=a.deg(),m=b.deg();
init(n+m),A.resize(lim),B.resize(lim);
poly ret(lim-1);
for(ri i=0;i<=n;++i)A[i]=a[i];
for(ri i=0;i<=m;++i)B[i]=b[i];
for(ri i=n+1;i<lim;++i)A[i]=0;
for(ri i=m+1;i<lim;++i)B[i]=0;
ntt(A,1),ntt(B,1);
for(ri i=0;i<lim;++i)A[i]=mul(A[i],B[i]);
return ntt(A,-1),ret.a=A,ret;
}
inline poly poly_inv(poly a,const int&k){
a=a.extend(k);
if(k==1)return poly(0,ksm(a[0],mod-2));
poly f0=poly_inv(a,(k+1)>>1);
return (2*f0-((f0*f0.extend(k))*a).extend(k)).extend(k);
}
inline poly poly_direv(poly a){
poly ret(a.deg()-1);
for(ri i=0;i<=ret.deg();++i)ret[i]=mul(a[i+1],i+1);
return ret;
}
inline poly poly_inter(poly a){
poly ret(a.deg()+1);
for(ri i=1;i<=ret.deg();++i)ret[i]=mul(Inv[i],a[i-1]);
return ret;
}
inline poly poly_ln(poly a,int len){
poly ret=a.poly_direv(a);
return ret=ret*a.poly_inv(a,len),ret.poly_inter(ret);
}
inline poly poly_exp(poly a,const int&k){
a=a.extend(k);
if(k==1)return poly(0,1);
poly f0=poly_exp(a,(k+1)>>1).extend(k);
poly delt=a-poly_ln(f0,k);
++delt[0];
return (f0*delt).extend(k);
}
};
int main(){
n=read()-1;
poly ans(n);
for(ri i=0;i<=n;++i)ans[i]=read();
int len=1;
while(len<=n*2)len<<=1;
Inv.resize(len),Inv[1]=1;
for(ri i=2;i<len;++i)Inv[i]=mul(Inv[mod%i],mod-mod/i);
ans=ans.poly_exp(ans,len);
for(ri i=0;i<=n;++i)cout<<ans[i]<<' ';
return 0;
}