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;
int n, m, dp[1000001][3][3];
int main() {
cin >> n >> m;
for (int mn = 0; mn <= 2; ++mn) {
for (int mx = max(mn, 2); mx <= 4; ++mx) {
dp[n][mn][mx - 2] = 1;
}
}
for (int i = n; i--;) {
for (int mn = 0; mn <= 2; ++mn) {
for (int mx = max(mn, 2); mx <= 4; ++mx) {
if (mn > 0) dp[i][mn][mx - 2] += dp[i + 1][mn - 1][max(2, mx - 1) - 2];
if (mx < 4) dp[i][mn][mx - 2] += dp[i + 1][min(2, mn + 1)][mx + 1 - 2];
dp[i][mn][mx - 2] %= m;
}
}
}
string s;
cin >> s;
int ans = 1;
for (int i = 0, mn = 2, mx = 2; i < n; ++i) {
if (s[i] == 'P') {
if (mn > 0) ans = (ans + dp[i + 1][mn - 1][max(2, mx - 1) - 2]) % m;
mn = min(2, mn + 1);
mx++;
} else {
mn--;
mx = max(2, mx - 1);
}
}
cout << ans << 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... |