ICPC网络赛 L DP
1 #include<bits/stdc++.h> 2 #define M 2000005 3 #define L long long 4 #define MOD 998244353 5 using namespace std; 6 7 L I[M],IC[M],ICP[M],ICPC[M]; 8 L C[M],CP[M],CPC[M]; 9 L P[M],PC[M]; 10 11 L c(int l,int r){return (C[r]-C[l-1]+MOD)%MOD;} 12 L pc(int l,int r){ 13 L sumpc=(PC[r]-PC[l-1]+MOD)%MOD; 14 L p_c=P[l-1]*c(l,r)%MOD; 15 return (sumpc-p_c+MOD)%MOD; 16 } 17 18 L cpc(int l,int r){ 19 L sumcpc=(CPC[r]-CPC[l-1]+MOD)%MOD; 20 L c_pc=C[l-1]*pc(l,r)%MOD; 21 L cp_c=CP[l-1]*c(l,r)%MOD; 22 return (sumcpc-c_pc-cp_c+MOD+MOD)%MOD; 23 } 24 25 L icpc(int l,int r){ 26 L sumicpc=(ICPC[r]-ICPC[l-1]+MOD)%MOD; 27 L i_cpc=I[l-1]*cpc(l,r)%MOD; 28 L ic_pc=IC[l-1]*pc(l,r)%MOD; 29 L icp_c=ICP[l-1]*c(l,r)%MOD; 30 return (sumicpc-i_cpc-ic_pc-icp_c+MOD*3LL)%MOD; 31 } 32 33 int n,q; 34 L ans=0; 35 char ch[M]; 36 37 L x,a,b,p; 38 39 L get(){ 40 x=(a*x+b)%p; 41 return x%n+1; 42 } 43 44 L u[M],v[M]; 45 46 int main(){ 47 cin>>n>>q; 48 scanf("%s",ch+1); 49 for(int i=1;i<=n;i++){ 50 I[i]=I[i-1]+(ch[i]=='I'); 51 C[i]=C[i-1]+(ch[i]=='C'); 52 P[i]=P[i-1]+(ch[i]=='P'); 53 54 IC[i]=(IC[i-1]+I[i-1]*(ch[i]=='C'))%MOD; 55 CP[i]=(CP[i-1]+C[i-1]*(ch[i]=='P'))%MOD; 56 PC[i]=(PC[i-1]+P[i-1]*(ch[i]=='C'))%MOD; 57 58 ICP[i]=(ICP[i-1]+IC[i-1]*(ch[i]=='P'))%MOD; 59 CPC[i]=(CPC[i-1]+CP[i-1]*(ch[i]=='C'))%MOD; 60 61 ICPC[i]=(ICPC[i-1]+ICP[i-1]*(ch[i]=='C'))%MOD; 62 } 63 cin>>x>>a>>b>>p; 64 for(int i=1;i<=q;i++) u[i]=get(); 65 for(int i=1;i<=q;i++) v[i]=get(); 66 for(int i=1;i<=q;i++){ 67 int l=u[i],r=v[i]; 68 //int l=get(),r=get(); 69 if(l>r) swap(l,r); 70 L out=icpc(l,r); 71 ans=(out+ans)%MOD; 72 } 73 cout<<ans<<endl; 74 }