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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #define PII pair<int,int> #define MP make_pair #define fir first #define sec second #define PB push_back #define db long double #define ll long long using namespace std; template <class T> inline void rd(T &x) { x=0; char c=getchar(); int f=1; while(!isdigit(c)) { if(c=='-') f=-1; c=getchar(); } while(isdigit(c)) x=x*10-'0'+c,c=getchar(); x*=f; } const int N=(1<<16)+10,mod=998244353; inline void Add(int &x,int y) { x+=y; if(x>=mod) x-=mod; } inline void Dec(int &x,int y) { x-=y; if(x<0) x+=mod; } inline int Add(int x) { return x>=mod?x-mod:x; } inline int Dec(int x) { return x<0?x+mod:x; } int Pow(int x,int y) { int res=1; while(y) { if(y&1) res=res*(ll)x%mod; x=x*(ll)x%mod,y>>=1; } return res; } int G[N],Q[N],InvQ[N],m; namespace Poly { int wn[2][N]; void getwn(int l) { for(int i=1;i<(1<<l);i<<=1) { int w0=Pow(3,(mod-1)/(i<<1)); int w1=Pow(3,mod-1-(mod-1)/(i<<1)); wn[0][i]=wn[1][i]=1; for(int j=1;j<i;++j) { wn[0][i+j]=wn[0][i+j-1]*(ll)w0%mod; wn[1][i+j]=wn[1][i+j-1]*(ll)w1%mod; } } } int rev[N]; void getr(int l) { for(int i=0;i<(1<<l);++i) rev[i]=(rev[i>>1]>>1)|((i&1)<<l-1); } void FFT(int *A,int len,int f) { for(int i=0;i<len;++i) if(rev[i]<i) swap(A[i],A[rev[i]]); for(int l=1;l<len;l<<=1) for(int p=l<<1,i=0;i<len;i+=p) for(int j=0;j<l;++j) { int t1=A[i+j],t2=A[i+l+j]*(ll)wn[f][l+j]%mod; A[i+j]=Add(t1+t2); A[i+l+j]=Dec(t1-t2); } if(f==1) { int ilen=Pow(len,mod-2); for(int i=0;i<len;++i) A[i]=A[i]*(ll)ilen%mod; } } void Mul(int *A,int *B,int *C,int l1,int l2,int l3) { static int a[N],b[N]; int len=1,cnt=0; while(len<=max(l1-1+l2-1,l3-1)) len<<=1,cnt++; getr(cnt); for(int i=0;i<len;++i) a[i]=b[i]=0; for(int i=0;i<l1;++i) a[i]=A[i]; for(int i=0;i<l2;++i) b[i]=B[i]; FFT(a,len,0),FFT(b,len,0); for(int i=0;i<len;++i) a[i]=a[i]*(ll)b[i]%mod; FFT(a,len,1); for(int i=0;i<l3;++i) C[i]=a[i]; } int C[N],P[N]; void Inv(int *A,int *B,int n) { if(n==1) return (void)(B[0]=Pow(A[0],mod-2)); int l=(n+1)>>1; Inv(A,B,l); Mul(B,B,C,l,l,n); Mul(C,A,C,n,n,n); for(int i=l;i<n;++i) B[i]=0; for(int i=0;i<n;++i) B[i]=(2ll*B[i]-C[i]+mod)%mod; } void Rev(int *A,int *B,int n) { for(int i=0;i<=n;++i) B[i]=A[i]; for(int i=0;i<=n;++i) { if(n-i<=i) break; swap(B[i],B[n-i]); } } void Div(int *F,int *B,int n) { static int A[N],D[N]; Rev(F,P,n);
Mul(InvQ,P,A,n-m+1,n-m+1,n-m+1); Rev(A,A,n-m); Mul(A,G,D,n-m+1,m+1,n+1); for(int i=0;i<=n;++i) B[i]=(F[i]-D[i]+mod)%mod; } void predoG(int n) { Rev(G,Q,m); Inv(Q,InvQ,n-m+1); } } using Poly::Mul; using Poly::Div; struct Mat { int a[N]; Mat () { memset(a,0,sizeof(a)); } int& operator [] (int i) { return a[i]; } friend Mat operator *(Mat A,Mat B) { Mat C; Mul(A.a,B.a,C.a,m,m,2*m-1); Div(C.a,C.a,2*m-2); return C; } }; Mat Pow(Mat x,int y) { Mat res; res[0]=1; while(y) { if(y&1) res=res*x; x=x*x,y>>=1; } return res; } int h[N]; int main() { Poly::getwn(16); int n; rd(n),rd(m); for(int i=1;i<=m;++i) { int x; rd(x); x=(x%mod+mod)%mod; G[m-i]=(mod-x)%mod; } G[m]=1; Poly::predoG(2*m-2); for(int i=0;i<m;++i) rd(h[i]),h[i]=(h[i]%mod+mod)%mod; if(n<m) { printf("%d\n",h[n]); return 0; } if(m==1) { printf("%d\n",h[0]*(ll)Pow((mod-G[0])%mod,n)%mod); return 0; } Mat A; A[1]=1; A=Pow(A,n); int ans=0; for(int i=0;i<m;++i) Add(ans,A[i]*(ll)h[i]%mod); printf("%d",ans); return 0; }
|