Submission #262991

#TimeUsernameProblemLanguageResultExecution timeMemory
262991amiratouLinear Garden (IOI08_linear_garden)C++14
100 / 100
560 ms10268 KiB
#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) #define rando mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) #define fi first #define se second #define debug(x) cerr << " - " << #x << ": " << x << endl; #define debugs(x, y) cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl; #define debugii(x) cerr << " - " << #x << ": " << x.fi<<","<<x.se << endl; #define sep() cerr << "--------------------" << endl; #define all(x) (x).begin(),(x).end() #define sz(x) (ll)x.size() #define ld long double #define ll long long //#define int ll #define ii pair<int,int> #define v vector<int> #define vii vector<ii> #define vv vector<vector<int> > #define mp make_pair #define INF 1000000000 #define pb push_back #define EPS 1e-9 const int MOD = 1000000007; // 998244353 const int MX = 1000001; string S; int n,m,dp[2][16][5],state[MX],comp[MX]; int32_t main(){ boost; //freopen(".in","r",stdin); cin>>n>>m>>S; for (int mask = 0; mask < 16; ++mask) for (int cnt = 0; cnt < 5; ++cnt) dp[n&1][mask][cnt]=1; int ans=0,C=0,curr=0; for (int i = 0; i < n; ++i){ C+=((S[i]=='P')?-1:1); if(C==-1)curr|=(1<<2); else if(C==1)curr|=(1<<1); else if(C==2)curr|=(1<<0); else if(C==-2)curr|=(1<<3); state[i]=curr; comp[i]=C; } for (int idx = n-1; idx >= 0; idx--) { int k=(idx?comp[idx-1]:0); curr=(idx?state[idx-1]:0); if((S[idx]=='P') && ((k+1)<=2)){ k++; if((k==2) && !(((curr>>2)&1)|((curr>>3)&1)))ans=(ans+dp[(idx+1)&1][curr|(1<<0)][k+2])%m; if((k==1) && !((curr>>3)&1))ans=(ans+dp[(idx+1)&1][curr|(1<<1)][k+2])%m; if(k==0)ans=(ans+dp[(idx+1)&1][curr][k+2])%m; if((k==-1) && !((curr>>0)&1))ans=(ans+dp[(idx+1)&1][curr|(1<<2)][k+2])%m; if((k==-2) && !(((curr>>0)&1)|((curr>>1)&1)))ans=(ans+dp[(idx+1)&1][curr|(1<<3)][k+2])%m; k--; } for (int mask = 0; mask < 16; ++mask){ for (int cnt = 0; cnt < 5; ++cnt) { int h=cnt-2; if((h+1) <=2){ h++; if((h==2) && !(((mask>>2)&1)|((mask>>3)&1)))dp[idx&1][mask][cnt]=(dp[idx&1][mask][cnt]+dp[(idx+1)&1][mask|(1<<0)][h+2])%m; if((h==1) && !((mask>>3)&1))dp[idx&1][mask][cnt]=(dp[idx&1][mask][cnt]+dp[(idx+1)&1][mask|(1<<1)][h+2])%m; if(h==0)dp[idx&1][mask][cnt]=(dp[idx&1][mask][cnt]+dp[(idx+1)&1][mask][h+2])%m; if((h==-1) && !((mask>>0)&1))dp[idx&1][mask][cnt]=(dp[idx&1][mask][cnt]+dp[(idx+1)&1][mask|(1<<2)][h+2])%m; if((h==-2) && !(((mask>>0)&1)|((mask>>1)&1)))dp[idx&1][mask][cnt]=(dp[idx&1][mask][cnt]+dp[(idx+1)&1][mask|(1<<3)][h+2])%m; h--; } if((h-1) >=-2){ h--; if((h==2) && !(((mask>>2)&1)|((mask>>3)&1)))dp[idx&1][mask][cnt]=(dp[idx&1][mask][cnt]+dp[(idx+1)&1][mask|(1<<0)][h+2])%m; if((h==1) && !((mask>>3)&1))dp[idx&1][mask][cnt]=(dp[idx&1][mask][cnt]+dp[(idx+1)&1][mask|(1<<1)][h+2])%m; if(h==0)dp[idx&1][mask][cnt]=(dp[idx&1][mask][cnt]+dp[(idx+1)&1][mask][h+2])%m; if((h==-1) && !((mask>>0)&1))dp[idx&1][mask][cnt]=(dp[idx&1][mask][cnt]+dp[(idx+1)&1][mask|(1<<2)][h+2])%m; if((h==-2) && !(((mask>>0)&1)|((mask>>1)&1)))dp[idx&1][mask][cnt]=(dp[idx&1][mask][cnt]+dp[(idx+1)&1][mask|(1<<3)][h+2])%m; h++; } } } for (int mask = 0; mask < 16; ++mask) for (int cnt = 0; cnt < 5; ++cnt)dp[(idx+1)&1][mask][cnt]=0; } cout<<(ans+1)%m<<"\n"; return 0; } //do smth instead of nothing and stay organized //long long //array bounds //special cases //binary search
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...