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;
using ll = long long;
using ld = long double;
using db = double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)(x).size()
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define R0F(i, a) ROF(i, 0, a)
#define EACH(a, x) for (auto& a : x)
ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); }
ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
template<class T> void remDup(vector<T>& v) { sor(v); v.erase(unique(all(v)), v.end()); }
const int MX = 1e6 + 10;
const ll INF = 1e18;
int MOD = 1e9 + 7;
struct mint {
int v; explicit operator int() const { return v; }
mint() { v = 0; } mint(ll _v) { v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD); if (v < 0) v += MOD; }
friend bool operator==(const mint& a, const mint& b) { return a.v == b.v; }
friend bool operator!=(const mint& a, const mint& b) { return !(a == b); }
friend bool operator<(const mint& a, const mint& b) { return a.v < b.v; }
mint& operator+=(const mint& m) { if ((v += m.v) >= MOD) v -= MOD; return *this; }
mint& operator-=(const mint& m) { if ((v -= m.v) < 0) v += MOD; return *this; }
mint& operator*=(const mint& m) { v = int((ll) v * m.v % MOD); return *this; }
mint& operator/=(const mint& m) { return (*this) *= inv(m); }
friend mint pow(mint a, long long p) {
mint ans = 1; for ( ; p; p /= 2, a *= a) if (p & 1) ans *= a; return ans;
}
friend mint inv(const mint& a) { return pow(a, MOD - 2); }
mint operator-() const { return mint(-v); }
mint& operator++() { return *this += 1; }
mint& operator--() { return *this -= 1; }
friend mint operator+(mint a, const mint& b) { return a += b; }
friend mint operator-(mint a, const mint& b) { return a -= b; }
friend mint operator*(mint a, const mint& b) { return a *= b; }
friend mint operator/(mint a, const mint& b) { return a /= b; }
};
int N; str S; int high[MX], low[MX], pref[MX]; mint DP[2][5][5][5], ans = 0;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N >> MOD >> S;
FOR(i, 1, N + 1) {
pref[i] = pref[i - 1] + (S[i - 1] == 'L' ? 1 : -1);
high[i] = max(high[i - 1], pref[i]);
low[i] = min(low[i - 1], pref[i]);
}
DP[0][2][2][2] = 1;
FOR(i, 1, N + 1) {
F0R(A, 5) F0R(B, 5) F0R(C, 5) DP[i % 2][A][B][C] = 0;
F0R(MIN, 3) {
FOR(MAX, max(2, MIN), MIN + 3) {
FOR(CUR, MIN, MAX + 1) {
// Letter is a 'P'
if (CUR - 1 >= 0) {
DP[i % 2][min(MIN, CUR - 1)][MAX][CUR - 1] += DP[(i - 1) % 2][MIN][MAX][CUR];
}
// Letter is an 'L'
if (CUR + 1 < 5) {
DP[i % 2][MIN][max(MAX, CUR + 1)][CUR + 1] += DP[(i - 1) % 2][MIN][MAX][CUR];
}
}
}
}
if (S[N - i] == 'P') {
F0R(MIN, 3) {
FOR(MAX, max(2, MIN), MIN + 3) {
FOR(CUR, MIN, MAX + 1) {
if (max(high[N - i], pref[N - i] + MAX - 1) - min(low[N - i], pref[N - i] + MIN - 1) <= 2) {
ans += DP[(i - 1) % 2][MIN][MAX][CUR];
}
}
}
}
}
}
cout << int(ans + 1);
}
# | 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... |