#include <bits/stdc++.h>
using namespace std;
int m;
int add (int a, int b) {
a += b; if (a >= m) a -= m;
return a;
}
int sub (int a, int b) {
a -= b; if (a < 0) a += m;
return a;
}
int mul (int a, int b) {
return (a * 1ll * b) % m;
}
int dp[2][5][5][5];
string s;
inline int get (int a, int b, int c, int d) {
if (d < 0 || d > 4) return 0;
if (b < 0 || b > 4) return 0;
if (c < 0 || c > 4) return 0;
return dp[a & 1][b][c][d];
}
int main () {
int n;
cin >> n >> m;
cin >> s;
vector <vector <int>> x;
int a = 2, b = 2, c = 2;
int sum = 1;
for (int i = 0; i < n; i++) {
if (s[i] == 'P') {
if (i == n - 1) {
sum++;
break;
}
x.push_back({(int)s.length() - i - 1, min(c + 1, a), max(c + 1, b), c + 1});
c--; a = min(a, c); b = max(b, c);
} else {
c++; a = min(c, a); b = max(b, c);
}
}
for (int j = 0; j <= 2; j++) for (int k = 2; k <= min(4, j + 2); k++) for (int l = j; l <= k; l++) dp[0][j][k][l] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= 2; j++) { //low
for (int k = 2; k <= min(4, j + 2); k++) { //high
for (int l = j; l <= k; l++) {
int &ret = dp[i & 1][j][k][l];
ret = 0;
ret = add(ret, get(i - 1, min(j, l - 1), max(k, l - 1), l - 1));
ret = add(ret, get(i - 1, min(j, l + 1), max(k, l + 1), l + 1));
}
}
}
if (!x.empty() && x.back()[0] == i) {
sum = add(sum, get(i, x.back()[1], x.back()[2], x.back()[3]));
x.pop_back();
}
}
cout << sum << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
3088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
11884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
12396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
14812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
21560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
126 ms |
28800 KB |
Output is correct |
2 |
Correct |
125 ms |
28808 KB |
Output is correct |
3 |
Correct |
120 ms |
28704 KB |
Output is correct |