This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int n, m;
cin >> n >> m;
string s;
cin >> s;
vector<vector<vector<int>>> dp(n+1, vector<vector<int>>(3, vector<int>(3)));
dp[n][0][0] = dp[n][1][1] = 1;
dp[n][1][0] = dp[n][0][1] = 1;
dp[n][2][0] = dp[n][0][2] = 1;
for (int i = n-1; i >= 0; i--) {
dp[i][0][0] = (dp[i+1][1][0] + dp[i+1][0][1]) % m;
dp[i][1][1] = (dp[i+1][2][0] + dp[i+1][0][2]) % m;
dp[i][1][0] = (dp[i+1][2][0] + dp[i+1][0][1]) % m;
dp[i][0][1] = (dp[i+1][1][0] + dp[i+1][0][2]) % m;
dp[i][2][0] = dp[i][0][2] = dp[i+1][1][1] % m;
}
int res = 1;
int low = 0, high = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'P') {
if (low > -2) res += dp[i+1][-(low-1)][max(0, high-1)];
res %= m;
low = min(0, low+1);
high++;
}
else {
low--;
high = max(0, high-1);
}
}
cout << res;
}
# | 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... |