#include <bits/stdc++.h>
using namespace std;
// Flattened index for state (hl in [0..2], hp in [0..2], ok in [0..1])
inline int idx(int hl, int hp, int ok) {
return (hl * 3 + hp) * 2 + ok;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
int m;
if(!(cin >> n >> m)) return 0;
string s;
cin >> s; // expected length n
// Convert to 0 for 'L' and 1 for 'P'
vector<int> a(n);
for (int i = 0; i < n; ++i) a[i] = (s[i] == 'P');
const int STATES = 3 * 3 * 2; // 18
vector<int> next(STATES), cur(STATES);
// Base: DP at position n+1 (i > n) is 1 for every (hl,hp,ok)
for (int t = 0; t < STATES; ++t) next[t] = 1 % m;
// iterate i from n down to 1 (we use zero-based index i = n-1 ... 0)
for (int i = n - 1; i >= 0; --i) {
int si = a[i]; // target char at position i (0 for L, 1 for P)
// compute cur = dp[i]
for (int hl = 0; hl <= 2; ++hl) {
for (int hp = 0; hp <= 2; ++hp) {
for (int ok = 0; ok <= 1; ++ok) {
int value = 0;
// Option 1: place 'L' (chosenChar = 0)
if (hl < 2) {
int okNext = ok | (0 < si); // becomes 1 if chosenChar < target
int nhl = hl + 1;
int nhp = max(0, hp - 1);
value = next[idx(nhl, nhp, okNext)];
}
// Option 2: place 'P' (chosenChar = 1)
// allowed only if (already smaller) OR (target char is 'P')
if (((ok == 1) || (ok == 0 && si == 1)) && (hp < 2)) {
int okNext = ok | (1 < si); // (1 < si) is always 0 here so okNext == ok
int nhl = max(0, hl - 1);
int nhp = hp + 1;
value += next[idx(nhl, nhp, okNext)];
if (value >= m) value -= m;
}
cur[idx(hl, hp, ok)] = value % m;
}
}
}
// roll
next.swap(cur);
}
// result is dp[1][0][0][0] i.e. dp at index 0 with hl=0,hp=0,ok=0
int result = next[idx(0,0,0)] % m;
if (result < 0) result += m;
cout << result << '\n';
return 0;
}
# | 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... |