Submission #762014

#TimeUsernameProblemLanguageResultExecution timeMemory
762014gun_ganLinear Garden (IOI08_linear_garden)C++17
100 / 100
146 ms48864 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MX = 1e6 + 5; int dp[MX][3][2][2]; // pos, condition, prefix, last int N, mod; string s; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N >> mod >> s; // P = 1, L = 0 if(s[0] == 'P') { dp[0][0][0][0] = 1; dp[0][0][1][1] = 1; } else { dp[0][0][1][0] = 1; } for(int i = 0; i + 1 < N; i++) { for(int cond = 0; cond < 3; cond++) { // condition for(int p = 0; p < 2; p++) { // prefix for(int lst = 0; lst < 2; lst++) { // last 0 / 1 // tarok 0 int nx = p & (s[i + 1] == 'L'); if(cond == 2 && lst) { dp[i + 1][cond][nx][0] += dp[i][cond][p][lst]; if(dp[i + 1][cond][nx][0] >= mod) dp[i + 1][cond][nx][0] -= mod; } if(cond == 2 && !lst) { dp[i + 1][1][nx][0] += dp[i][cond][p][lst]; if(dp[i + 1][1][nx][0] >= mod) dp[i + 1][1][nx][0] -= mod; } if(cond == 1 && lst) { dp[i + 1][cond][nx][0] += dp[i][cond][p][lst]; if(dp[i + 1][cond][nx][0] >= mod) dp[i + 1][cond][nx][0] -= mod; } if(cond == 0 && lst) { dp[i + 1][cond][nx][0] += dp[i][cond][p][lst]; if(dp[i + 1][cond][nx][0] >= mod) dp[i + 1][cond][nx][0] -= mod; } if(cond == 0 && !lst) { dp[i + 1][1][nx][0] += dp[i][cond][p][lst]; if(dp[i + 1][1][nx][0] >= mod) dp[i + 1][1][nx][0] -= mod; } // tarok 1 if(p && s[i + 1] == 'L') continue; nx = p & (s[i + 1] == 'P'); if(cond == 2 && !lst) { dp[i + 1][cond][nx][1] += dp[i][cond][p][lst]; if(dp[i + 1][cond][nx][1] >= mod) dp[i + 1][cond][nx][1] -= mod; } if(cond == 1 && !lst) { dp[i + 1][cond][nx][1] += dp[i][cond][p][lst]; if(dp[i + 1][cond][nx][1] >= mod) dp[i + 1][cond][nx][1] -= mod; } if(cond == 1 && lst) { dp[i + 1][2][nx][1] += dp[i][cond][p][lst]; if(dp[i + 1][2][nx][1] >= mod) dp[i + 1][2][nx][1] -= mod; } if(cond == 0 && lst) { dp[i + 1][2][nx][1] += dp[i][cond][p][lst]; if(dp[i + 1][2][nx][1] >= mod) dp[i + 1][2][nx][1] -= mod; } if(cond == 0 && !lst) { dp[i + 1][cond][nx][1] += dp[i][cond][p][lst]; if(dp[i + 1][cond][nx][1] >= mod) dp[i + 1][cond][nx][1] -= mod; } } } } } int ans = 0; for(int cond = 0; cond < 3; cond++) { // condition for(int p = 0; p < 2; p++) { // prefix for(int lst = 0; lst < 2; lst++) { // last 0 / 1 ans += dp[N - 1][cond][p][lst]; if(ans >= mod) ans -= mod; } } } cout << ans << '\n'; }
#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...