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
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl;
#define show4(x,y) for(auto it:x) cout << it << " "; cout << #y << endl;
typedef pair<int,int>pii;
typedef pair<pii,pii>pi2;
int n,mod;
int arr[1000005];
int memo[1000005][3][3][2];
int dp(int index, int lotus, int pap, bool bound){
if(index==n) return 1;
if(memo[index][lotus][pap][bound]!=-1) return memo[index][lotus][pap][bound];
int ans=0;
if(bound){
for(int x=1;x<=arr[index];x++){
int hold=lotus;
int hold2=pap;
if(x==1){
hold++;
hold2=max(0LL,hold2-1);
}
else{
hold2++;
hold=max(0LL,hold-1);
}
if(hold>2||hold2>2) continue;
ans=(ans+dp(index+1,hold,hold2,x==arr[index]))%mod;
}
}
else{
for(int x=1;x<=2;x++){
int hold=lotus;
int hold2=pap;
if(x==1){
hold++;
hold2=max(0LL,hold2-1);
}
else{
hold2++;
hold=max(0LL,hold-1);
}
if(hold>2||hold2>2) continue;
ans=(ans+dp(index+1,hold,hold2,false))%mod;
}
}
return memo[index][lotus][pap][bound]=ans;
}
void solve(){
cin >> n >> mod;
string s;
cin >> s;
for(int x=0;x<n;x++){
if(s[x]=='L') arr[x]=1;
else arr[x]=2;
}
memset(memo,-1,sizeof(memo));
cout << dp(0,0,0,1);
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
//freopen("out.txt", "w", stdout);
int t=1;
//cin >> t;
while(t--){
solve();
}
}
# | 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... |