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;
const int maxn = 1000010;
int N, M;
int smask[maxn]; //store mask of values or -1 if impossible
int cdp[1 << 5]; //stores the dp count (build right to left)
int ndp[1 << 5];
string gar;
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> N >> M;
	// M = 1000000000;
	cin >> gar;
	gar = " " + gar; //1-indexed
	int cmask = 0;
	cmask |= (1 << 2);
	for (int i = 1; i <= N; i++) {
		smask[i] = -1;
		if (gar[i] == 'P') {
			int om = cmask;
			if ((cmask & (1 << 4)) == 0) {
				om = om << 1;
				om |= (1 << 2);
				smask[i] = om;
			}
			cmask = cmask >> 1;
			cmask |= (1 << 2);
		}
		else {
			cmask = cmask << 1;
			cmask |= (1 << 2);
		}
	}
	cdp[1 << 2] = 1;
	int res = 0;
	for (int i = N; i >= 1; i--) {
		if (smask[i] >= 0) {
			bool h0 = smask[i] & (1 << 0);
			bool h1 = smask[i] & (1 << 1);
			bool h3 = smask[i] & (1 << 3);
			bool h4 = smask[i] & (1 << 4);
			bool ok;
			for (int j = 0; j < (1 << 5); j++) {
				ok = true;
				//try all other masks
				if (cdp[j] == 0) continue; //does not contribute
				if (h0) {
					if (j & (1 << 0)) {
						ok = false;
					}
					if (j & (1 << 1)) {
						ok = false;
					}
				}
				if (h1) {
					if (j & (1 << 0)) {
						ok = false;
					}
				}
				if (h3) {
					if (j & (1 << 4)) {
						ok = false;
					}
				}
				if (h4) {
					if (j & (1 << 4)) {
						ok = false;
					}
					if (j & (1 << 3)) {
						ok = false;
					}
				}
				if (ok) {
					// cout << "index " << i << ": "  << j << " \t:: " << cdp[j] << endl;
					res = (res + cdp[j])%M;
				}
			}
		}
		//update cdp 
		for (int j = 0; j < 32; j++) {
			ndp[j] = 0;
		}
		int nv;
		for (int j = 0; j < 32; j++) {
			//consider adding an L 
			if ( (j & (1 << 4)) == 0) {
				nv = j << 1;
				nv |= 1 << 2;
				ndp[nv] = (ndp[nv] + cdp[j])%M;
			}
			if ((j & (1 << 0)) == 0) {
				nv = j >> 1;
				nv |= 1 << 2;
				ndp[nv] = (ndp[nv] + cdp[j])%M;
			}
		}
		for (int j = 0; j < 32; j++) {
			cdp[j] = ndp[j];
		}
	}
	res = (res + 1)%M;
	cout << res << endl;
}
| # | 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... |