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
| #include<iostream> #include<cstdio> using namespace std; const int N=2e5+10,K=19,mod=998244353; template<class T> inline void read(T &x) { x=0;bool f=0; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();} while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); if(f)x=~x+1; } template<class T,class ...T1> inline void read(T &x,T1 &...x1) { read(x),read(x1...); } int n,m,c; int s[N]; int ne[N]; int tot,l[K],r[K],d[K]; int f[N],g[N][K]; inline void adj(int &x){x+=x>>31&mod;} inline int qpow(int x,int k=mod-2) { int res=1; while(k) { if(k&1)res=1ll*res*x%mod; x=1ll*x*x%mod; k>>=1; } return res; } int main() { read(m,c,n); for(int i=1;i<=n;i++)read(s[i]); for(int i=2,j=0;i<=n;i++) { while(j&&s[i]!=s[j+1])j=ne[j]; if(s[i]==s[j+1])j++; ne[i]=j; } for(int i=ne[n];i;i=ne[i]) { d[++tot]=i-ne[i],r[tot]=n-i; while(ne[i]&&i-ne[i]==d[tot])i=ne[i]; l[tot]=n-i; } for(int i=n,res=0,mul=1;i<=m;i++,mul=1ll*mul*c%mod) { adj(res=1ll*res*c%mod-f[i-n]),adj(f[i]=res+mul-mod); for(int j=1;j<=tot;j++) { if(i>=l[j]+d[j])adj(f[i]+=g[i-l[j]-d[j]][j]-mod); adj(f[i]-=g[i-r[j]][j]); } for(int j=1;j<=tot;j++)adj(g[i][j]=g[i-d[j]][j]+f[i]-mod); } int ans=0; for(int i=m,mul=1;i>=n;i--,mul=1ll*mul*c%mod) adj(ans+=1ll*mul*f[i]%mod-mod); printf("%d\n",1ll*ans*qpow(qpow(c,m))%mod); return 0; }
|
Gitalk 加载中 ...