Luogu5245 模板多项式快速幂(多项式exp)
Posted gloid
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Luogu5245 模板多项式快速幂(多项式exp)相关的知识,希望对你有一定的参考价值。
A(x)k=eklnA(x),丝毫不懂为什么指数要对p取模,只是写下exp板子。
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; #define ll long long #define N 600010 #define P 998244353 char getc(){char c=getchar();while ((c<‘A‘||c>‘Z‘)&&(c<‘a‘||c>‘z‘)&&(c<‘0‘||c>‘9‘)) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1;char c=getchar(); while (c<‘0‘||c>‘9‘) {if (c==‘-‘) f=-1;c=getchar();} while (c>=‘0‘&&c<=‘9‘) x=(10ll*x+c-48)%P,c=getchar(); return x*f; } int n,m,a[N],r[N],b[N],c[N],d[N],A[N],B[N],t; int ksm(int a,int k) { int s=1; for (;k;k>>=1,a=1ll*a*a%P) if (k&1) s=1ll*s*a%P; return s; } int inv(int a){return ksm(a,P-2);} void DFT(int *a,int n,int g) { for (int i=0;i<n;i++) r[i]=(r[i>>1]>>1)|(i&1)*(n>>1); for (int i=0;i<n;i++) if (i<r[i]) swap(a[i],a[r[i]]); for (int i=2;i<=n;i<<=1) { int wn=ksm(g,(P-1)/i); for (int j=0;j<n;j+=i) { int w=1; for (int k=j;k<j+(i>>1);k++,w=1ll*w*wn%P) { int x=a[k],y=1ll*w*a[k+(i>>1)]%P; a[k]=(x+y)%P,a[k+(i>>1)]=(x-y+P)%P; } } } } void IDFT(int *a,int n) { DFT(a,n,inv(3)); int u=inv(n); for (int i=0;i<n;i++) a[i]=1ll*a[i]*u%P; } void mul(int *a,int *b,int n) { DFT(a,n,3),DFT(b,n,3); for (int i=0;i<n;i++) a[i]=1ll*a[i]*b[i]%P; IDFT(a,n); } void Inv(int *a,int *b,int n) { if (n==1) {for (int i=0;i<t;i++) b[i]=0;b[0]=inv(a[0]);return;} Inv(a,b,n>>1); for (int i=0;i<n;i++) A[i]=a[i]; for (int i=n;i<(n<<1);i++) A[i]=0; n<<=1; DFT(A,n,3),DFT(b,n,3); for (int i=0;i<n;i++) b[i]=1ll*b[i]*(P+2-1ll*A[i]*b[i]%P)%P; IDFT(b,n); n>>=1; for (int i=n;i<(n<<1);i++) b[i]=0; } //G(B(x))=A(x)B(x)-1 //B(x)=B0(x)-G(B0(x))/G‘(B0(x)) //B(x)=B0(x)-(A(x)B0(x)-1)/A(x) //B(x)=B0(x)(2-A(x)B0(x)) void trans(int *a,int *b,int n){for (int i=n-1;i>=0;i--) b[i]=1ll*a[i+1]*(i+1)%P;} void dx(int *a,int *b,int n){b[0]=0;for (int i=1;i<n;i++) b[i]=1ll*a[i-1]*inv(i)%P;} void Ln(int *a,int n) { for (int i=0;i<n;i++) b[i]=c[i]=0; trans(a,b,n>>1); Inv(a,c,n>>1); mul(b,c,n); dx(b,a,n); } //ln(F(x))=G(x) //F‘(x)/F(x)=G‘(x) //dx F‘(x)/F(x)=G(x) void Exp(int *a,int *b,int n) { if (n==1){b[0]=1;return;} Exp(a,b,n>>1); for (int i=0;i<(n>>1);i++) B[i]=b[i]; for (int i=(n>>1);i<n;i++) B[i]=0; Ln(B,n); for (int i=0;i<n;i++) B[i]=(a[i]-B[i]+P)%P; B[0]=(B[0]+1)%P; n<<=1; for (int i=(n>>1);i<n;i++) B[i]=0; mul(b,B,n); n>>=1; for (int i=n;i<(n<<1);i++) b[i]=0; } //exp(A(x))=B(x) //A(x)=ln(B(x)) //G(B(x))=ln(B(x))-A(x) //B(x)=B0(x)(A(x)+1-ln(B0(x))) int main() { #ifndef ONLINE_JUDGE freopen("a.in","r",stdin); freopen("a.out","w",stdout); const char LL[]="%I64d\n"; #else const char LL[]="%lld\n"; #endif n=read(),m=read(); for (int i=0;i<n;i++) a[i]=read(); t=1;while (t<=(n<<1)) t<<=1; Ln(a,t); for (int i=0;i<t;i++) a[i]=1ll*a[i]*m%P; Exp(a,d,t); for (int i=0;i<n;i++) printf("%d ",d[i]); return 0; }
以上是关于Luogu5245 模板多项式快速幂(多项式exp)的主要内容,如果未能解决你的问题,请参考以下文章
多项式FFT/NTT模板(含乘法/逆元/log/exp/求导/积分/快速幂)