# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
838811 | teo_thrash | Linear Garden (IOI08_linear_garden) | C++14 | 123 ms | 48360 KiB |
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>
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn=1e6+3;
const int mod=1e9+7;
int n, m;
string a;
ll dp[maxn][6];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m;
cin>>a;
int sz=a.size();
if(sz%2==0){
dp[sz][1]=1;
dp[sz][2]=0;
dp[sz][3]=1;
dp[sz][4]=0;
dp[sz][5]=1;
}else{
dp[sz][1]=0;
dp[sz][2]=1;
dp[sz][3]=0;
dp[sz][4]=1;
dp[sz][5]=0;
}
for(int i=sz-1; i>=0; i--){
for(int j=1; j<=5; j++){
if(i%2==0){
dp[i][1]=dp[i+1][2]%m;
dp[i][5]=dp[i+1][4]%m;
if(i+3<=sz){
dp[i][1]-=dp[i+3][4];
dp[i][1]%=m;
dp[i][5]-=dp[i+3][2];
dp[i][5]%=m;
}
dp[i][3]=(dp[i+1][2]+dp[i+1][4])%m;
}else{
dp[i][2]=(dp[i+1][1]+dp[i+1][3])%m;
dp[i][4]=(dp[i+1][3]+dp[i+1][5])%m;
if(i+3<=sz){
dp[i][2]-=dp[i+3][5];
dp[i][2]%=m;
dp[i][4]-=dp[i+3][1];
dp[i][4]%=m;
}
}
}
}
/*
for(int i=0; i<=sz; i++){
for(int j=1; j<=5; j++){
cout<<dp[i][j]<<" ";
}
cout<<endl;
}*/
ll ans=0, bal=0;
for(int i=0; i<a.size(); i++){
if(a[i]=='P'){
bal--;
if(bal<1){
ans+=dp[i+1][bal+5];
ans%=m;
}
}else{
bal++;
}
}
//int add=!(n%2);
cout<<(ans+1)%m;
return 0;
}
Compilation message (stderr)
# | 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... |