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>
#define ll long long
#define ull unsigned long long
#define dl double
#define st first
#define nd second
#define II pair <int, int>
using namespace std;
const int N = 5 + 1e5;
const int inf = 7 + 1e9;
int main() {
#define TASKNAME ""
ios_base :: sync_with_stdio (0);
cin.tie (0);
if ( fopen( TASKNAME".inp", "r" ) ) {
freopen (TASKNAME".inp", "r", stdin);
freopen (TASKNAME".out", "w", stdout);
}
int n, m;
string s;
cin >> n >> m >> s;
n = s.size();
vector <int> a(n + 1, 0);
for (int i = 1; i <= n; i ++)
a[i] = s[i - 1] == 'P';
vector <vector <vector <int>>> f(n + 1, vector <vector <int>> (5, vector <int> (3, 0)));
for (int mi = 0; mi <= 4; mi ++)
for (int ma = mi; ma <= min(mi + 2, 4); ma ++) {
vector <vector <vector <int>>> g(n + 1, vector <vector <int>> (5, vector <int> (3, 0)));
g[0][mi][ma - mi] = 1;
for (int num = 0; num < n; num ++)
for (int i = 0; i <= 4; i ++)
for (int delta = 0; delta <= 2; delta ++) {
int j = i + delta;
if (i > 0) {
int i_ = min(i - 1, 1), j_ = max(j - 1, 1);
g[num + 1][i_][j_ - i_] = (g[num + 1][i_][j_ - i_] + g[num][i][delta]) % m;
}
if (j < 4) {
int i_ = min(i + 1, 3), j_ = max(j + 1, 3);
g[num + 1][i_][j_ - i_] = (g[num + 1][i_][j_ - i_] + g[num][i][delta]) % m;
}
}
for (int num = 0; num <= n; num ++)
for (int i = 0; i <= 4; i ++)
for (int delta = 0; delta <= 2; delta ++)
f[num][mi][ma - mi] = (f[num][mi][ma - mi] + g[num][i][delta]) % m;
}
int mi = 2, ma = 2, ans = 1;
for (int i = 1; i <= n; i ++) {
if (a[i] && mi > 0) {
int mi_ = min(mi - 1, 1), ma_ = max(ma - 1, 1);
ans = (ans + f[n - i][mi_][ma_ - mi_]) % m;
}
if (!a[i]) {
mi = min(mi - 1, 1);
ma = max(ma - 1, 1);
}
else {
mi = min(mi + 1, 3);
ma = max(ma + 1, 3);
}
}
cout << ans;
return 0;
}
Compilation message (stderr)
linear_garden.cpp: In function 'int main()':
linear_garden.cpp:19:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen (TASKNAME".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
linear_garden.cpp:20:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | freopen (TASKNAME".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |