//和以前写的fft不太一样,可能是因为要取模?? #include<iostream> #include<cstring> #include<cmath> #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; const int mod=998244353,maxn=270005; int mx,n,m,inv[maxn],a[maxn],b[maxn],c[maxn],na[maxn],w[2][maxn],pos[maxn]; int qmi(int x,int y){ int t=1; for(;y;y>>=1,x=(ll)x*x%mod)if(y&1)t=(ll)t*x%mod; return t; } void pre(int n){ int i,x=qmi(3,(mod-1)/n);//以前这里的取值都和mod无关啊,取了模了不一样了? w[0][0]=w[1][0]=1; for(int i=1;i<n;++i)w[0][i]=w[1][n-i]=(ll)w[0][i-1]*x%mod; for(int i=0;i<n;++i){ pos[i]=pos[i>>1]>>1; if(i&1)pos[i]|=n>>1; } } void fnt(int *a,int n,int flag){ if(n>mx)mx=n; int i,j,k,l,x,u,v; for(i=0;i<n;++i)na[pos[i]]=a[i]; memcpy(a,na,sizeof(int)*n); for(k=1;k<n;k<<=1){ for(i=0,x=n/k>>1;i<n;i+=k<<1) for(j=i,l=0;j<i+k;++j,l+=x){ u=a[j];v=(ll)a[j+k]*w[flag][l]%mod; a[j]=(u+v)%mod;a[j+k]=(u-v+mod)%mod; } } if(flag){ x=qmi(n,mod-2); for(i=0;i<n;++i)a[i]=(ll)a[i]*x%mod; } } void solve_inv(int *a,int *b,int n){ if(n==1){b[0]=qmi(a[0],mod-2);return;} int i;solve_inv(a,b,n>>1); memcpy(c,a,sizeof(int)*n);memset(c+n,0,sizeof(int)*n); pre(n<<1); fnt(b,n<<1,0);fnt(c,n<<1,0); for(i=0;i<(n<<1);++i)b[i]=(2-(ll)b[i]*c[i]%mod+mod)*b[i]%mod; fnt(b,n<<1,1);memset(b+n,0,sizeof(int)*n); } int main(){ int i,n;scanf("%d",&n); inv[0]=inv[1]=a[0]=m=1; while(m<=n)m<<=1; for(int i=2;i<=n;++i)inv[i]=mod-(ll)inv[mod%i]*(mod/i)%mod; for(int i=3;i<=n;++i)inv[i]=(ll)inv[i-1]*inv[i]%mod; for(int i=1;i<=n;++i)a[i]=((mod-inv[i])<<1)%mod; solve_inv(a,b,m); int ans=b[n]; for(int i=n;i;--i)ans=((ll)ans*i+b[i-1])%mod; printf("%d\n",ans); return 0; }
学习地址:http://blog.csdn.net/lych_cys/article/details/51512278