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;
typedef long long ll;
typedef vector <int> vi;
#define f(i,a,b) for(int i = a; i < b; i++)
const int N = 1e7 + 5;
ll n, m, ans = 1, mini, maxi, pre;
string s, wi;
bool is_valid(string s){
ll _mini = mini, _maxi = maxi, _pre = pre;
f(i,0,3){
if(s[i] == 'L') _pre++;
else _pre--;
_mini = min(_mini, _pre);
_maxi = max(_maxi, _pre);
}
return _maxi-_mini <= 2;
}
ll bp(ll x, ll y){
if(y == 0) return 1;
ll res = bp(x, y/2); res = (res*res)%m;
if(y%2 == 0) return res;
return (x*res)%m;
}
int main(){
cin >> n >> m >> s;
vector <string> c = {"LLL", "LLP", "LPL", "LPP","PLL","PLP", "PPL", "PPP"};
f(i,0,n){
if(s[i] == 'L'){
wi.push_back('L');
pre++;
mini = min(mini, pre), maxi = max(maxi, pre);
continue;
}
// how many strings are there such that have lenght n-i
string aux = wi;
aux.push_back('L');
ll k_mini = mini, k_maxi = maxi, k_pre = pre;
pre++;
mini = min(mini, pre), maxi = max(maxi, pre);
ll cant = 0, ye = n-i-1;
for(string x: c)
cant += is_valid(x);
if(cant == 2){ // 1, 2, 2, 4, 4 ,8, 8
// odd -> 2^(1-1), 2^(2-1)
// 5 / 2 = 2
ll add = bp(2, ye/2);
//ans = (ans + add)%m;
//cout << aux << " " << cant << " " << ye << " " << add << endl;
ans = (ans + add)%m;
}
if(cant == 3){ // 1, 2, 3, 5, 7, 11, 15
// +1, +1, +2, +2, +4, +4
ll add = bp(2, (ye+1)/2) - 1;
if(ye%2 == 0){
add = (add + bp(1, (ye-1)/2))%m;
}
//ans = (ans + add)%m;
ans = (ans + add)%m;
// cout << aux << " " << cant << " " << ye << " " << add << endl;
}
if(cant == 4){ // 2, 2, 4, 4,
// + 1 / 2
ll add = bp(2, (ye+1)/2);
//ans = (ans + add)%m;
ans = (ans + add)%m;
// cout << aux << " " << cant << " " << ye << " " << add << endl;
}
if(cant == 5){ // 2, 3, 5, 7, 11, 15
// +1, +2, +2, +3
ll add = bp(2, (ye+1)/2+1) -1;
if(ye%2 == 1){
add = (add - bp(2,ye/2) + m)%m;
}
//ans = (ans + add)%m;
ans = (ans + add)%m;
//cout << aux << " " << cant << " " << ye << " " << add << endl;
}
mini = k_mini, maxi = k_maxi, pre = k_pre;
pre--;
mini = min(mini, pre), maxi = max(maxi, pre);
wi.push_back('P');
}
cout << ans << endl;
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... |