// #include <bits/stdc++.h> #define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); #include <iostream> #include <vector> #include <map> using namespace std; #define MOD 1000000007 map<int, map<int, int64_t>> memo; vector<int64_t> a; int64_t wtf(int l, int r) { if (l == r) return !((a[l] - a[l - 1]) % 2); int64_t tmp = memo[l][r]; if (tmp) { if (tmp == -1) { return 0; } return tmp; } int64_t total = 0; if (!(((a[r] - a[l - 1]) % MOD) % 2)) { total++; } for (int i = 0; i <= (r - l - 1); i++) { if (!(((a[l + i] - a[l - 1]) % MOD) % 2)) total = (total + wtf(l + i + 1, r)) % MOD; } if (total == 0) { memo[l][r] = -1; } else { memo[l][r] = total; } return total; } int main() { FAST int n; cin >> n; a.resize(n + 1); a[0] = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; a[i] += a[i - 1]; } cout << wtf(1, n) << endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | // #include <bits/stdc++.h> #define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); #include <iostream> #include <vector> #include <map> using namespace std; #define MOD 1000000007 map<int, map<int, int64_t>> memo; vector<int64_t> a; int64_t wtf(int l, int r) { if (l == r) return !((a[l] - a[l - 1]) % 2); int64_t tmp = memo[l][r]; if (tmp) { if (tmp == -1) { return 0; } return tmp; } int64_t total = 0; if (!(((a[r] - a[l - 1]) % MOD) % 2)) { total++; } for (int i = 0; i <= (r - l - 1); i++) { if (!(((a[l + i] - a[l - 1]) % MOD) % 2)) total = (total + wtf(l + i + 1, r)) % MOD; } if (total == 0) { memo[l][r] = -1; } else { memo[l][r] = total; } return total; } int main() { FAST int n; cin >> n; a.resize(n + 1); a[0] = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; a[i] += a[i - 1]; } cout << wtf(1, n) << endl; return 0; } |