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>
#pragma GCC optimize("Ofast")
using namespace std;
const int MAX=1000005;
int dp[MAX][5][5][5],mod;
int add(int a,int b){
return a+b-(a+b>=mod?mod:0);
}
int solve(int n,int mn,int mx,int d){
if(mx-mn>2)return 0;
if(!n)return 1;
int&ret=dp[n][mn][mx][d];
if(~ret)return ret;
ret=add(solve(n-1,mn,max(mx,d+1),d+1),solve(n-1,min(mn,d-1),mx,d-1));
return ret;
}
void calc(int N){
for(int n=0;n<=N;n++){
for(int mn=0;mn<=2;mn++)for(int mx=2;mx<=mn+2;mx++)for(int d=mn;d<=mx;d++){
int&ret=dp[n][mn][mx][d];
if(!n)ret=1;
else{
if(d+1<5)ret=dp[n-1][mn][max(mx,d+1)][d+1];
if(d-1>=0)ret=add(ret,dp[n-1][min(mn,d-1)][mx][d-1]);
}
}
}
}
int index(string&s){
int mn=2,mx=2,d=2,res=0,n=s.size();
solve(n,2,2,2);
for(int i=0;i<n;i++){
if(s[i]=='L'){
mn=min(mn,d-1);
d--;
}else{
res=add(res,solve(n-i-1,min(mn,d-1),mx,d-1));
mx=max(mx,d+1);
d++;
}
}
return add(res,1);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//memset(dp,-1,sizeof(dp));
int n;
string s;
cin>>n>>mod>>s;
calc(n);
cout<<index(s);
return 0;
}
# | 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... |