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;
#define all(v) v.begin(), v.end()
ll dp[3][3][5]; //mx, mn, cur
ll ldp[3][3][5]; //mx, mn, cur
ll m;
int main()
{
int n; cin >> n; cin >> m;
string s; cin >> s;
memset(dp, 0, 3*3*5*sizeof(ll));
memset(ldp, 0, 3*3*5*sizeof(ll));
int cmx, cmn, ccur;
if(s[0] == 'P'){
dp[1][0][3] = 1;
cmx = 1, cmn = 0, ccur = 3;
}
else{
cmx = 0, cmn = 1, ccur = 1;
}
dp[0][1][1] = 1;
for (int i = 0; i < n - 1; i++)
{
swap(dp, ldp);
memset(dp, 0, 3*3*5*sizeof(ll));
bool st[3][3][5];
memset(st, 0, 3*3*5);
for (int mx = 0; mx < 3; mx++)
for (int mn = 0; mn < 3; mn++)
for (int cur = 0; cur < 5; cur++) {
for (ll a = -1; a < 2; a += 2) {
int acur = cur - 2 + a;
int amx = max(mx, acur);
int amn = min(-mn, acur);
if(amx > 2 || amn < -2 || amx - acur > 2 || acur - amn > 2) continue;
dp[amx][-amn][acur + 2] += max(ldp[mx][mn][cur], 0LL);
dp[amx][-amn][acur + 2] %= m;
st[amx][-amn][acur + 2] = 1;
}
}
if(ccur + 1 < 5 && s[i + 1] == 'L'){
int x = max(cmx, ccur - 2 + 1);
if(st[x][cmn][ccur + 1]){
dp[x][cmn][ccur + 1]--;
dp[x][cmn][ccur + 1] += m;
dp[x][cmn][ccur + 1] %= m;
}
}
if(s[i + 1] == 'P') ccur++;
else ccur--;
cmx = max(ccur - 2, cmx);
cmn = max(-(ccur - 2), cmn);
}
ll res = 0;
for (int mx = 0; mx < 3; mx++)
for (int mn = 0; mn < 3; mn++)
for (int cur = 0; cur < 5; cur++){
res += dp[mx][mn][cur];
res %= m;
}
cout << res << 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... |