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;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int nax = 1e6+5;
int n,mod;
string s;
int dp[3][3][nax];
int calc(int idx,int a,int b){
if(a < 0)a = 0;
if(b < 0)b = 0;
if(a == 3)return 0;
if(b == 3)return 0;
if(idx == n)
return 1;
if(dp[a][b][idx] != -1)
return dp[a][b][idx];
dp[a][b][idx] = (calc(idx+1,a+1,b-1) + calc(idx+1,a-1,b+1)) % mod;
return dp[a][b][idx];
}
int ans;
void solve(int idx,int a,int b){
if(a < 0)a = 0;
if(b < 0)b = 0;
if(a == 3)return;
if(b == 3)return;
if(idx == n)
return;
if(s[idx] == 'P'){
ans = (ans + calc(idx+1,a+1,b-1)) % mod;
solve(idx+1,a-1,b+1);
}else{
solve(idx+1,a+1,b-1);
}
}
int main(){
memset(dp,-1,sizeof(dp));
cin>>n>>mod>>s;
solve(0,0,0);
cout << (ans + 1)%mod << endl;
}
# | 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... |