This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* author: Duarte Nobrega
* created: 28.05.2020
**/
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#pragma GCC target("fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
const int N = 1e6 + 5;
int dp [N][5][5][3], n, MOD;
string w;
int add (int a, int b) {
int res = a + b;
if (res >= MOD) res -= MOD;
return res;
}
int calc (int estado, char cur, char is) {
if (estado > 0) return estado;
if (cur < is) return 1;
if (cur > is) return 2;
return 0;
}
int solve (int p = 0, int maior = 2, int menor = 2, int estado = 0) {
if (maior > 4) return 0;
if (menor < 0) return 0;
if (p >= n) return (estado < 2);
int &res = dp[p][maior][menor][estado];
if (~res) return res;
res = 0;
// Place a P
res = add (res, solve (p + 1, max (maior + 1, 3), menor + 1, calc (estado, 'P', w[p])));
// Place a L
res = add (res, solve (p + 1, maior - 1, min (menor - 1, 1) , calc (estado, 'L', w[p])));
return res;
}
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> MOD >> w;
memset (dp, -1, sizeof(dp));
cout << solve () << '\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... |