이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define db long double
#define N 1000005
#define II pair <ll,ll>
#define III pair <ll,II>
#define IV pair <vector <int>,vector <int> >
#define fst first
#define snd second
#define BIT(x,i) ((x>>i)&1)
#define pi acos(-1)
#define to_radian(x) (x*pi/180.0)
#define to_degree(x) (x*180.0/pi)
#define rand(l,r) (l+rng()%(r-l+1))
#define f(cnt,mi,ma) f[cnt+2][mi+2][ma]
#define g(cnt,mi,ma) g[cnt+2][mi+2][ma]
using namespace std;
string s;
int Mi[N],Ma[N],Cnt[N],f[5][3][3],g[5][3][3],cnt,mi,ma,mod,n,i,res;
void update(int &a,int b)
{
a+=b;
if(a>=mod) a-=mod;
}
int main()
{
// freopen("linear_garden.inp","r",stdin);
//freopen("linear_garden.out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n>>mod>>s; s=" "+s;
for(i=1;i<=n;i++)
{
Cnt[i]=Cnt[i-1]+(s[i]=='L')-(s[i]=='P');
Mi[i]=min(Mi[i-1],Cnt[i]);
Ma[i]=max(Ma[i-1],Cnt[i]);
}
for(cnt=-2;cnt<=2;cnt++)
for(mi=-2;mi<=0;mi++)
for(ma=0;ma<=2;ma++)
if(ma-mi<=2) f(cnt,mi,ma)=1;
for(i=n;i>=1;i--)
{
if(s[i]=='P' && Cnt[i-1]+1<=2)
update(res,f(Cnt[i-1]+1,Mi[i-1],max(Ma[i-1],Cnt[i-1]+1)));
for(cnt=-2;cnt<=2;cnt++)
for(mi=-2;mi<=0;mi++)
for(ma=0;ma<=2;ma++) g(cnt,mi,ma)=f(cnt,mi,ma),f(cnt,mi,ma)=0;
for(cnt=-2;cnt<=2;cnt++)
for(mi=-2;mi<=0;mi++)
for(ma=0;ma<=2;ma++)
{
if(cnt+1<=2) update(f(cnt,mi,ma),g(cnt+1,mi,max(ma,cnt+1)));
if(cnt-1>=-2) update(f(cnt,mi,ma),g(cnt-1,min(mi,cnt-1),ma));
}
}
update(res,1);
cout<<res;
}
# | 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... |