This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define e1 first
#define e2 second
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define float long double
#define size(x) (int)x.size()
#define satori int testCases; cin>>testCases; while(testCases--)
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define all(r) begin(r),end(r)
#define time chrono::high_resolution_clock().now().time_since_epoch().count()
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const int MAXN=1e6+10;
int dp[2][10][10][10],mn[MAXN],mx[MAXN],cs[MAXN];
string s;
int32_t main()
{
fastio;
int n,m,mod,l,r,res=0;
cin>>n>>mod>>s;
dp[0][2][2][2]=1;
mn[0]=mx[0]=cs[0]=2;
for(int i=1;i<=n;i++){
cs[i]=(s[i-1]=='P'?1:-1)+cs[i-1];
mn[i]=min(mn[i-1],cs[i]);
mx[i]=max(mx[i-1],cs[i]);
}
for(int i=1;i<=n;i++){
for(l=0;l<5;l++)
for(r=l;r<=l+2;r++)
for(m=l;m<=r;m++)
dp[(i&1)][l][r][m]=0;
for(l=0;l<5;l++)
for(r=l;r<=min(4,l+2);r++)
for(m=l;m<=r;m++){
if(m-1>=0){
dp[(i&1)][min(m-1,l)][r][m-1]+=dp[((i-1)&1)][l][r][m];
if(dp[(i&1)][min(m-1,l)][r][m-1]>=mod)
dp[(i&1)][min(m-1,l)][r][m-1]-=mod;
}
if(m+1<5){
dp[(i&1)][l][max(m+1,r)][m+1]+=dp[((i-1)&1)][l][r][m];
if(dp[(i&1)][l][max(m+1,r)][m+1]>=mod)
dp[(i&1)][l][max(m+1,r)][m+1]-=mod;
}
}
if(s[n-i]=='P'){
for(l=0;l<5;l++)
for(r=l;r<=l+2;r++)
for(m=l;m<=r;m++)
if(max(r+cs[n-i]-3,mx[n-i])-min({l+cs[n-i]-3,mn[n-i],cs[n-i]-1})<=2){
res+=dp[((i-1)&1)][l][r][m];
if(res>=mod)
res-=mod;
}
}
}
cout<<(res+1)%mod<<'\n';
}
Compilation message (stderr)
linear_garden.cpp:2: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
2 | #pragma GCC optimization ("O3")
|
linear_garden.cpp:3: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
3 | #pragma GCC optimization ("unroll-loops")
|
# | 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... |