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;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif
long long mod = 2;
struct mint {
long long value;
mint(long long x = 0) {
value = x % mod;
if (value < 0) value += mod;
}
mint& operator+=(const mint& other) {
if ((value += other.value) >= mod) value -= mod;
return *this;
}
mint& operator-=(const mint& other) {
if ((value -= other.value) < 0) value += mod;
return *this;
}
mint& operator*=(const mint& other) {
value = value * other.value % mod;
return *this;
}
mint& operator/=(const mint& other) {
long long a = 0, b = 1, c = other.value, m = mod;
while (c != 0) {
long long t = m / c;
m -= t * c;
swap(c, m);
a -= t * b;
swap(a, b);
}
a %= mod;
if (a < 0) a += mod;
value = value * a % mod;
return *this;
}
friend mint operator+(const mint& lhs, const mint& rhs) { return mint(lhs) += rhs; }
friend mint operator-(const mint& lhs, const mint& rhs) { return mint(lhs) -= rhs; }
friend mint operator*(const mint& lhs, const mint& rhs) { return mint(lhs) *= rhs; }
friend mint operator/(const mint& lhs, const mint& rhs) { return mint(lhs) /= rhs; }
mint& operator++() { return *this += 1; }
mint& operator--() { return *this -= 1; }
mint operator++(int) {
mint result(*this);
*this += 1;
return result;
}
mint operator--(int) {
mint result(*this);
*this -= 1;
return result;
}
mint operator-() const { return mint(-value); }
bool operator==(const mint& rhs) const { return value == rhs.value; }
bool operator!=(const mint& rhs) const { return value != rhs.value; }
bool operator<(const mint& rhs) const { return value < rhs.value; }
};
string to_string(const mint& x) {
return to_string(x.value);
}
ostream& operator<<(ostream& stream, const mint& x) {
return stream << x.value;
}
istream& operator>>(istream& stream, mint& x) {
stream >> x.value;
x.value %= mod;
if (x.value < 0) x.value += mod;
return stream;
}
mint power(mint a, long long n) {
mint res = 1;
while (n > 0) {
if (n & 1) {
res *= a;
}
a *= a;
n >>= 1;
}
return res;
}
vector<mint> fact(1, 1);
vector<mint> finv(1, 1);
mint C(int n, int k) {
if (n < k || k < 0) {
return mint(0);
}
while ((int) fact.size() < n + 1) {
fact.emplace_back(fact.back() * (int) fact.size());
finv.emplace_back(mint(1) / fact.back());
}
return fact[n] * finv[k] * finv[n - k];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n >> mod;
string s;
cin >> s;
vector<vector<vector<mint>>> dp(7, vector<vector<mint>>(7, vector<mint>(7)));
int mn = 3;
int mx = 3;
int now = 3;
for (int i = 0; i < n; i++) {
vector<vector<vector<mint>>> new_dp(7, vector<vector<mint>>(7, vector<mint>(7)));
for (int x = 1; x < 7; x++) {
for (int y = x + 1; y < min(6, x + 3); y++) {
for (int z = x; z <= y; z++) {
if (y - min(z - 1, x) < 3) {
new_dp[z - 1][min(z - 1, x)][y] += dp[z][x][y];
}
if (min(z + 1, y) - x < 3) {
new_dp[z + 1][x][max(z + 1, y)] += dp[z][x][y];
}
}
}
}
if (s[i] == 'P') {
if (mx - min(mn, now - 1) < 3) {
new_dp[now - 1][min(mn, now - 1)][mx]++;
}
now++;
mx = max(mx, now);
} else {
now--;
mn = min(mn, now);
}
swap(dp, new_dp);
}
mint ans = 1;
for (int i = 0; i < 7; i++) {
for (int j = i + 1; j < min(7, i + 3); j++) {
for (int k = i; k <= j; k++) {
ans += dp[k][i][j];
}
}
}
cout << ans << '\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... |