이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX = 1e6 + 5;
int dp[MX][3][2][2]; // pos, condition, prefix, last
int N, mod;
string s;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> mod >> s;
// P = 1, L = 0
if(s[0] == 'P') {
dp[0][0][0][0] = 1;
dp[0][0][1][1] = 1;
} else {
dp[0][0][1][0] = 1;
}
for(int i = 0; i + 1 < N; i++) {
for(int cond = 0; cond < 3; cond++) { // condition
for(int p = 0; p < 2; p++) { // prefix
for(int lst = 0; lst < 2; lst++) { // last 0 / 1
// tarok 0
int nx = p & (s[i + 1] == 'L');
if(cond == 2 && lst) {
dp[i + 1][cond][nx][0] +=
dp[i][cond][p][lst];
if(dp[i + 1][cond][nx][0] >= mod) dp[i + 1][cond][nx][0] -= mod;
}
if(cond == 2 && !lst) {
dp[i + 1][1][nx][0] +=
dp[i][cond][p][lst];
if(dp[i + 1][1][nx][0] >= mod) dp[i + 1][1][nx][0] -= mod;
}
if(cond == 1 && lst) {
dp[i + 1][cond][nx][0] +=
dp[i][cond][p][lst];
if(dp[i + 1][cond][nx][0] >= mod) dp[i + 1][cond][nx][0] -= mod;
}
if(cond == 0 && lst) {
dp[i + 1][cond][nx][0] +=
dp[i][cond][p][lst];
if(dp[i + 1][cond][nx][0] >= mod) dp[i + 1][cond][nx][0] -= mod;
}
if(cond == 0 && !lst) {
dp[i + 1][1][nx][0] +=
dp[i][cond][p][lst];
if(dp[i + 1][1][nx][0] >= mod) dp[i + 1][1][nx][0] -= mod;
}
// tarok 1
if(p && s[i + 1] == 'L') continue;
nx = p & (s[i + 1] == 'P');
if(cond == 2 && !lst) {
dp[i + 1][cond][nx][1] +=
dp[i][cond][p][lst];
if(dp[i + 1][cond][nx][1] >= mod) dp[i + 1][cond][nx][1] -= mod;
}
if(cond == 1 && !lst) {
dp[i + 1][cond][nx][1] +=
dp[i][cond][p][lst];
if(dp[i + 1][cond][nx][1] >= mod) dp[i + 1][cond][nx][1] -= mod;
}
if(cond == 1 && lst) {
dp[i + 1][2][nx][1] +=
dp[i][cond][p][lst];
if(dp[i + 1][2][nx][1] >= mod) dp[i + 1][2][nx][1] -= mod;
}
if(cond == 0 && lst) {
dp[i + 1][2][nx][1] +=
dp[i][cond][p][lst];
if(dp[i + 1][2][nx][1] >= mod) dp[i + 1][2][nx][1] -= mod;
}
if(cond == 0 && !lst) {
dp[i + 1][cond][nx][1] +=
dp[i][cond][p][lst];
if(dp[i + 1][cond][nx][1] >= mod) dp[i + 1][cond][nx][1] -= mod;
}
}
}
}
}
int ans = 0;
for(int cond = 0; cond < 3; cond++) { // condition
for(int p = 0; p < 2; p++) { // prefix
for(int lst = 0; lst < 2; lst++) { // last 0 / 1
ans += dp[N - 1][cond][p][lst];
if(ans >= mod) ans -= mod;
}
}
}
cout << ans << '\n';
}
# | 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... |