이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int n, m, dp[1000001][5][5]; // i, min, max
// L: -1
// P: 1
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] = 1;
}
}
for (int i = n; i--;) {
for (int mn = 0; mn <= 2; ++mn) {
for (int mx = max(mn, 2); mx <= 4; ++mx) {
// try add L
if (mn > 0) dp[i][mn][mx] += dp[i + 1][mn - 1][max(2, mx - 1)];
// try add P
if (mx < 4) dp[i][mn][mx] += dp[i + 1][min(2, mn + 1)][mx + 1];
dp[i][mn][mx] %= 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 += dp[i + 1][mn - 1][max(2, mx - 1)];
ans %= 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... |