제출 #637504

#제출 시각아이디문제언어결과실행 시간메모리
637504lto5Linear Garden (IOI08_linear_garden)C++14
0 / 100
35 ms65536 KiB
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i <= (b); i++) int n, mod; string s; int f[1000001][5][16]; int id[5][5], cnt_id; // -2 -1 0 1 2 // 0 1 2 3 4 #define offset(x) (x + 2) void add_mod (int &a, int b) { a += b; if (a >= mod) a -= mod; if (a < 0) a += mod; } int add (int a, int b) { add_mod(a, b); return a; } int dp (int idx, int pre, int min_pre, int max_pre) { if (abs(pre) > 2 || min_pre > max_pre) return 0; if (idx > n) { return 1; } int &ans = f[idx][offset(pre)][id[offset(min_pre)][offset(max_pre)]]; if (ans != -1) return ans; ans = 0; int new_pre = pre + 1; if (abs(new_pre) <= 2 && min_pre >= new_pre - 2 && max_pre <= new_pre + 2) { add_mod(ans, dp(idx + 1, new_pre, min(min_pre, new_pre), max(max_pre, new_pre))); } new_pre = pre - 1; if (abs(new_pre) <= 2 && min_pre >= new_pre - 2 && max_pre <= new_pre + 2) { add_mod(ans, dp(idx + 1, new_pre, min(min_pre, new_pre), max(max_pre, new_pre))); } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); freopen("garden.inp", "r", stdin); freopen("garden.out", "w", stdout); cin >> n >> mod >> s; s = ' ' + s; for (int i = 0; i <= 4; i++) for (int j = i; j <= 4; j++) { id[i][j] = cnt_id++; } memset(f, -1, sizeof(f)); int ans = 0; int cur_pre = 0, min_pre = 0, max_pre = 0; for (int i = 1; i <= n; i++) { if (s[i] == 'B') { int tmp_pre = cur_pre + 1; int tmp_min = min(min_pre, tmp_pre); int tmp_max = max(max_pre, tmp_pre); if (abs(tmp_pre) <= 2 && min_pre >= tmp_pre - 2 && max_pre <= tmp_pre + 2) add_mod(ans, dp(i + 1, tmp_pre, tmp_min, tmp_max)); } cur_pre += s[i] == 'A' ? 1 : -1; min_pre = min(min_pre, cur_pre); max_pre = max(max_pre, cur_pre); } cout << add(ans, 1); // f[0][offset(0)][id[offset(0)][offset(0)]] = 1; // // for (int i = 0; i < n; i++) { // for (int last_pre = -2; last_pre <= 2; last_pre++) { // for (int min_pre = -2; min_pre <= 2; min_pre++) { // for (int max_pre = min_pre; max_pre <= 2; max_pre++) { // int last_id = id[offset(min_pre)][offset(max_pre)]; // int last_dp = f[i][offset(last_pre)][last_id]; // // if (last_dp == 0) // continue; // // int new_pre = last_pre + 1; // if (abs(new_pre) <= 2 && min_pre >= new_pre - 2 && max_pre <= new_pre + 2) { // int next_min = min(min_pre, new_pre); // int next_max = max(max_pre, new_pre); // // add_mod(f[i + 1][offset(new_pre)][id[offset(next_min)][offset(next_max)]], last_dp); // } // // new_pre = last_pre - 1; // if (abs(new_pre) <= 2 && min_pre >= new_pre - 2 && max_pre <= new_pre + 2) { // int next_min = min(min_pre, new_pre); // int next_max = max(max_pre, new_pre); // // add_mod(f[i + 1][offset(new_pre)][id[offset(next_min)][offset(next_max)]], last_dp); // } // } // } // } // } // // int ans = 0; // for (int last_pre = -2; last_pre <= 2; last_pre++) // for (int i = 0; i < cnt_id; i++) { // add_mod(ans, f[n][offset(last_pre)][i]); // } // cout << ans; return 0; }

컴파일 시 표준 에러 (stderr) 메시지

linear_garden.cpp: In function 'int main()':
linear_garden.cpp:58:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |   freopen("garden.inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
linear_garden.cpp:59:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |   freopen("garden.out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...