This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef pair<int,int>pii;
int n,m;
string s,s2;
int memo[1000005][5][5][2][2];
int dp(int index, int k, int k2, bool bound, int last){
if(index==n) return 1;
if(memo[index][k+2][k2+2][bound][last]!=-1) return memo[index][k+2][k2+2][bound][last];
int ans=0;
if(bound){
int target=s[index]-'0';
int next=max(0,k)+1;
int next2=k2-1;
if(abs(next)<=2&&abs(next2)<=2){
ans=(ans+dp(index+1,next,next2,(target==1),1))%m;
}
if(target==2){
next=k;
next--;
next2=max(k2,0)+1;
if(abs(next)<=2&&abs(next2)<=2) ans=(ans+dp(index+1,next,next2,(target==2),2))%m;
}
}
else{
int next=max(0,k)+1;
int next2=k2-1;
if(abs(next)<=2&&abs(next2)<=2) ans=(ans+dp(index+1,next,next2,0,1))%m;
next=k;
next2=max(k2,0)+1;
next--;
if(abs(next)<=2&&abs(next2)<=2) ans=(ans+dp(index+1,next,next2,0,2))%m;
}
return memo[index][k+2][k2+2][bound][last]=(ans)%m;
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
memset(memo,-1,sizeof(memo));
cin >>n >> m >> s2;
for(int x=0;x<n;x++){
if(s2[x]=='L')s.push_back('1');
else s.push_back('2');
}
int hold=(dp(0,0,0,1,0))%m;
cout << hold;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |