이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |