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;
string s, wi;
bool is_valid(string s){
int l = s.size(), c_l[l+1], c_p[l+1];
f(i,0,l+1) c_l[i] = c_p[i] = 0;
f(i,1,l+1){
c_l[i] = c_l[i-1], c_p[i] = c_p[i-1];
if(s[i-1] == 'L') c_l[i]++;
else c_p[i]++;
}
f(i,1,l+1){
f(j,i+2,l+1){
int CL = c_l[j] - c_l[i-1], CP = c_p[j] - c_p[i-1];
if(abs(CL-CP) > 2) return 0;
}
}
return 1;
}
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');
continue;
}
// how many are there such that have lenght n-i
string aux = wi;
aux.push_back('L');
int cant = 0, ye = n-i-1;
for(string x: c){
string ra = aux + x;
cant += is_valid(ra);
}
if(cant == 2){ // 1, 2, 2, 4, 4 ,8, 8
// odd -> 2^(1-1), 2^(2-1)
// 5 / 2 = 2
ll add = (1LL<<(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 = (1LL<<((ye+1)/2))-1;
if(ye%2 == 0){
add += (1LL<<((ye-1)/2));
}
//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 = (1LL<<((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 = (1LL<<((ye+1)/2)+1)-1;;
if(ye%2 == 1){
add -= (1LL<<(ye/2));
}
//ans = (ans + add)%m;
ans = (ans + add)%m;
//cout << aux << " " << cant << " " << ye << " " << add << endl;
}
wi.push_back('P');
}
cout << ans << endl;
return 0;
}
Compilation message (stderr)
linear_garden.cpp: In function 'int main()':
linear_garden.cpp:76:38: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
76 | ll add = (1LL<<((ye+1)/2)+1)-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... |