이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
int n, m, dp[2][1000100][2][2];
int a[1000100];
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++){
char c;
cin >> c;
a[i] = (c == 'P');
}
if (a[1] == 0){
dp[1][1][0][0] = 1;
dp[1][1][0][1] = 1;
} else {
dp[0][1][0][0] = 1;
dp[0][1][0][1] = 1;
dp[1][1][1][0] = 1;
dp[1][1][1][1] = 1;
}
for (int i = 1; i < n; i++){
for (int cur = 0; cur < 2; cur++){
for (int nxt = 0; nxt < 2; nxt++){
for (int pref = 0; pref < 2; pref++){
if (pref && nxt > a[i+1])
continue;
for (int used = 0; used < 2; used++){
if (cur == nxt && cur == 0 && used == 1)
continue;
if (cur == nxt && cur == 1 && used == 0)
continue;
int nxtpref = pref;
if (nxt < a[i+1])
nxtpref = 0;
int nxtused = used;
if (cur == nxt)
nxtused ^= 1;
dp[nxtpref][i+1][nxt][nxtused] = (dp[nxtpref][i+1][nxt][nxtused] + dp[pref][i][cur][used])%m;
}
}
}
}
}
int ans = 0;
for (int pref = 0; pref < 2; pref++){
ans = (ans + dp[pref][n][0][0])%m;
ans = (ans + dp[pref][n][1][0])%m;
ans = (ans + dp[pref][n][0][1])%m;
ans = (ans + dp[pref][n][1][1])%m;
}
memset(dp, 0, sizeof(dp));
if (a[1] == 0){
dp[1][1][0][0] = 1;
} else {
dp[0][1][0][0] = 1;
dp[1][1][1][0] = 1;
}
for (int i = 1; i < n; i++){
for (int cur = 0; cur < 2; cur++){
for (int nxt = 0; nxt < 2; nxt++){
if (cur == nxt)
continue;
for (int pref = 0; pref < 2; pref++){
if (pref && nxt > a[i+1])
continue;
for (int used = 0; used < 2; used++){
if (cur == nxt && cur == 0 && used == 1)
continue;
if (cur == nxt && cur == 1 && used == 0)
continue;
int nxtpref = pref;
if (nxt < a[i+1])
nxtpref = 0;
int nxtused = used;
if (cur == nxt)
nxtused ^= 1;
dp[nxtpref][i+1][nxt][nxtused] = (dp[nxtpref][i+1][nxt][nxtused] + dp[pref][i][cur][used])%m;
}
}
}
}
}
int ans2 = 0;
for (int pref = 0; pref < 2; pref++){
ans2 = (ans2 + dp[pref][n][0][0])%m;
ans2 = (ans2 + dp[pref][n][1][0])%m;
}
ans = (ans - ans2 + m)%m;
cout << ans;
}
/**
5 7
LLPPL
5 1000
LPLPL
*/
# | 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... |