1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#include<bits/stdc++.h> #define re register typedef long long ll; typedef long double ld; template<class T> inline void read(T &x) { x=0; char ch=getchar(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar(); if(t) x=-x; } template<class T,class ...T1> inline void read(T &x,T1 &...x1){ read(x),read(x1...); } template<class T> inline void write(T x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } template<> inline void write(bool x){ putchar(x?'1':'0'); } template<> inline void write(char c){ putchar(c); } template<> inline void write(char *s){ while(*s!='\0') putchar(*s++); } template<> inline void write(const char *s){ while(*s!='\0') putchar(*s++); } template<class T,class ...T1> inline void write(T x,T1 ...x1){ write(x),write(x1...); } template<class T> inline bool checkMax(T &x,T y){ return x<y?x=y,1:0; } template<class T> inline bool checkMin(T &x,T y){ return x>y?x=y,1:0; } const int MAXN=4e5+10; const ld pi=std::acos(-1); const int Base=1<<15; int N,M,Mod; struct Complex { ld x,y; Complex operator+(const Complex &a) const { return {x+a.x,y+a.y}; } Complex operator-(const Complex &a) const { return {x-a.x,y-a.y}; } Complex operator*(const Complex &a) const { return {x*a.x-y*a.y,x*a.y+y*a.x}; } }A[MAXN],B[MAXN],C[MAXN],A1[MAXN],A2[MAXN],B1[MAXN],B2[MAXN]; ll F[MAXN],G[MAXN],ans[MAXN]; int Rev[MAXN],Tot,Bit; inline void FFT(Complex a[],int n,int inv) { for(int i=0;i<n;++i) if(i<Rev[i]) std::swap(a[i],a[Rev[i]]); for(int mid=1;mid<n;mid<<=1) { auto w1=Complex({std::cos(pi/mid),inv*std::sin(pi/mid)}); for(int i=0;i<n;i+=mid*2) { auto wk=Complex({1,0}); for(int j=0;j<mid;++j,wk=wk*w1) { auto x=a[i+j],y=a[i+j+mid]*wk; a[i+j]=x+y,a[i+j+mid]=x-y; } } } if(inv==-1) for(int i=0;i<n;++i) a[i].x/=n; } inline void MTT(ll f[],ll g[],ll ans[],int n,int p) { for(int i=0;i<n;++i) { A1[i].x=f[i]/Base,A2[i].x=f[i]%Base; B1[i].x=g[i]/Base,B2[i].x=g[i]%Base; } FFT(A1,n,1),FFT(A2,n,1),FFT(B1,n,1),FFT(B2,n,1); for(int i=0;i<n;++i) { A[i]=A1[i]*B1[i]; B[i]=A1[i]*B2[i]+A2[i]*B1[i]; C[i]=A2[i]*B2[i]; } FFT(A,n,-1),FFT(B,n,-1),FFT(C,n,-1); for(int i=0;i<n;++i) { ll av=(ll)(A[i].x+0.5)%p,bv=(ll)(B[i].x+0.5)%p,cv=(ll)(C[i].x+0.5)%p; ans[i]=((av*Base%p*Base)%p+(bv*Base)%p+cv)%p; } } int main() { read(N,M,Mod); for(int i=0;i<=N;++i) read(F[i]),F[i]=(F[i]+Mod)%Mod; for(int i=0;i<=M;++i) read(G[i]),G[i]=(G[i]+Mod)%Mod; while((1<<Bit)<=N+M) ++Bit; Tot=1<<Bit; for(int i=0;i<Tot;++i) Rev[i]=(Rev[i>>1]>>1)|((i&1)<<(Bit-1)); MTT(F,G,ans,Tot,Mod); for(int i=0;i<=N+M;++i) write(ans[i],' '); return 0; }
|