이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n,mod;
cin >> n >> mod;
auto add=[&](int a,int b)->int{return a+b-(a+b>=mod?mod:0);};
auto sub=[&](int a,int b)->int{return a-b+(a-b<0?mod:0);};
string s;
cin >> s;
vector<int> a(n+1,0);
for(int i=1;i<=n;i++) a[i]=(s[i-1]=='P');
vector<array<int,3>> dp(n+1,{0,0,0});
dp[0]={1,1,1};
for(int i=1;i<=n;i++)
{
for(int j=0;j<3;j++)
{
for(int k=0;k<3;k++)
{
if(abs(j-k)==1) dp[i][j]=add(dp[i][j],dp[i-1][k]);
}
}
}
int res=1;
auto mv=[&](int l,int r,int p,int c)
{
l=min(l,--p);
if(r-l==1) res=add(res,sub(add(dp[c][0],dp[c][1]),1));
if(r-l==2) res=add(res,dp[c][p-l]);
};
int l=0,r=0,p=0;
for(int i=1;i<=n;i++)
{
if(a[i]==1) mv(l,r,p,n-i);
if(a[i]==1) r=max(r,++p);
else l=min(l,--p);
}
cout << res << "\n";
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... |