#include <bits/stdc++.h> #define ll long long using namespace std; const int MAX = 300005; const ll q = 1000000007, q2 = 2 * q, Q = q * q; int n; ll A[MAX], D[MAX]; template <typename T> ostream & operator << (ostream &out, const vector <T> &V) { if (!V.empty()) { out << "{"; for (auto v : V) out << v << ", "; out << "\b\b}"; // \b is backspace } else { out << "{}"; } return out; } template <class T1, class T2> ostream & operator << (ostream &out, const pair <T1, T2> &P) { out << "{" << P.first << ", " << P.second << "}"; return out; } void testcase_sum_less_than_q() { ll C[2] = {0, 0}; D[0] = 1; C[0] += D[0]; D[1] = 1 - (A[1] & 1); C[(A[1] & 1)] += D[1]; ll cur_par = A[1] & 1; for (int i = 2; i <= n; i++) { cur_par = (cur_par + A[i]) & 1; D[i] = C[cur_par]; C[cur_par] += D[i]; if (C[cur_par] >= Q) { C[cur_par] %= q; } } cout << D[n] % q <<endl; } void testcase_n_less_than_3000() { D[0] = 1; D[1] = 1 - (A[1] & 1); for (int i = 2; i <= n; i++) { ll sum = A[i]; for (int j = i-1; j >= 0; j--) { if (((sum % q) & 1) == 0) { D[i] += D[j]; } sum += A[j]; } D[i] %= q; } cout << D[n] % q <<endl; } void update(int l, int r, int b, int e, ll v, vector <ll> &Tree) { if (e <= l || r <= b) { } else if (l <= b && e <= r) { Tree[b+e] += v; } else { int be = (b+e)/2; update(l, r, b, be, v, Tree); update(l, r, be, e, v, Tree); Tree[b+e] = Tree[b+be] + Tree[be+e]; } } ll query(int l, int r, int b, int e, vector <ll> &Tree) { if (e <= l || r <= b) { return 0; } else if (l <= b && e <= r) { return Tree[b+e]; } else { int be = (b+e)/2; return query(l, r, b, be, Tree) + query(l, r, be, e, Tree); } } inline int get_cat(int val) { return (val < q) ? (val & 1) : (3 - (val & 1)); } inline int get_pos(int val, vector <ll> &SP) { return lower_bound(SP.begin(), SP.end(), val) - SP.begin(); } void testcase_all() { vector <ll> Pref(n+1), Sorted_prefs[4]; Pref[0] = 0; Sorted_prefs[get_cat(0)].push_back(0); for (int i = 1; i <= n; i++) { Pref[i] = (Pref[i-1] + A[i]) % q2; Sorted_prefs[get_cat(Pref[i])].push_back(Pref[i]); } Sorted_prefs[0].push_back(0); Sorted_prefs[0].push_back(q-1); Sorted_prefs[1].push_back(1); Sorted_prefs[1].push_back(q-2); Sorted_prefs[2].push_back(q); Sorted_prefs[2].push_back(q2-1); Sorted_prefs[3].push_back(q+1); Sorted_prefs[3].push_back(q2-2); int s = 1; for ( ; s < 2 * (n+4); s *= 2); vector <ll> Tree[4]; for (int c = 0; c < 4; c++) { sort(Sorted_prefs[c].begin(), Sorted_prefs[c].end()); Tree[c].resize(s, 0); // cerr << c << ": " << Sorted_prefs[c] <<endl; } int v = 0, c = get_cat(v), p = get_pos(v, Sorted_prefs[c]); D[v] = 1; update(p, +1, 0, s/2, D[v], Tree[c]); for (int i = 1; i <= n; i++) { int v = Pref[i], c = get_cat(v), p = get_pos(v, Sorted_prefs[c]); D[i] = 0; if (c == 0) { // 0 <= v < q, v % 2 == 0 D[i] += query(0, p+1, 0, s/2, Tree[c]); int c2 = 3-c, v2 = q2+v-(q-1), p2 = get_pos(v2, Sorted_prefs[c2]); D[i] += query(p2, s/2, 0, s/2, Tree[c2]); int c3 = 1-c, v3 = v+1, p3 = get_pos(v3, Sorted_prefs[c3]); D[i] += query(p3, s/2, 0, s/2, Tree[c3]); int c4 = 2+c, v4 = v+q, p4 = get_pos(v4, Sorted_prefs[c4]); while (0 <= p4 && Sorted_prefs[c4][p4] > v+q) p4--; D[i] += query(0, p4+1, 0, s/2, Tree[c4]); } else if (c == 1) { // o <= v < q, v % 2 == 1 D[i] += query(0, p+1, 0, s/2, Tree[c]); int c2 = 3-c, v2 = q2+v-(q-1), p2 = get_pos(v2, Sorted_prefs[c2]); D[i] += query(p2, s/2, 0, s/2, Tree[c2]); int c3 = 1-c, v3 = v+1, p3 = get_pos(v3, Sorted_prefs[c3]); D[i] += query(p3, s/2, 0, s/2, Tree[c3]); int c4 = 2+c, v4 = v+q, p4 = get_pos(v4, Sorted_prefs[c4]); while (0 <= p4 && Sorted_prefs[c4][p4] > v+q) p4--; D[i] += query(0, p4+1, 0, s/2, Tree[c4]); } else if (c == 2) { // q <= v < 2q, v % 2 == 1 D[i] += query(0, p+1, 0, s/2, Tree[c]); int c2 = 3-c, v2 = v-(q-1), p2 = get_pos(v2, Sorted_prefs[c2]); D[i] += query(p2, s/2, 0, s/2, Tree[c2]); int c3 = 5-c, v3 = v+1, p3 = get_pos(v3, Sorted_prefs[c3]); D[i] += query(p3, s/2, 0, s/2, Tree[c3]); int c4 = c-2, v4 = v-q, p4 = get_pos(v4, Sorted_prefs[c4]); while (0 <= p4 && Sorted_prefs[c4][p4] > v-q) p4--; D[i] += query(0, p4+1, 0, s/2, Tree[c4]); } else { // q <= v < 2q, v % 2 == 0 D[i] += query(0, p+1, 0, s/2, Tree[c]); int c2 = 3-c, v2 = v-(q-1), p2 = get_pos(v2, Sorted_prefs[c2]); D[i] += query(p2, s/2, 0, s/2, Tree[c2]); int c3 = 5-c, v3 = v+1, p3 = get_pos(v3, Sorted_prefs[c3]); D[i] += query(p3, s/2, 0, s/2, Tree[c3]); int c4 = c-2, v4 = v-q, p4 = get_pos(v4, Sorted_prefs[c4]); while (0 <= p4 && Sorted_prefs[c4][p4] > v-q) p4--; D[i] += query(0, p4+1, 0, s/2, Tree[c4]); } D[i] %= q; update(p, p+1, 0, s/2, D[i], Tree[c]); // cerr << i << " v=" << v << " c=" << c << " p=" << p << " | " << " _ " << D[i] <<endl; } cout << D[n] % q <<endl; } int main() { ios_base::sync_with_stdio(false); cin >> n; ll sum = 0; for (int i = 1; i <= n; i++) { cin >> A[i]; sum += A[i]; } /*if (sum < q) { testcase_sum_less_than_q(); } else */ if (n <= 3000) { testcase_n_less_than_3000(); } else { testcase_all(); } 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | #include <bits/stdc++.h> #define ll long long using namespace std; const int MAX = 300005; const ll q = 1000000007, q2 = 2 * q, Q = q * q; int n; ll A[MAX], D[MAX]; template <typename T> ostream & operator << (ostream &out, const vector <T> &V) { if (!V.empty()) { out << "{"; for (auto v : V) out << v << ", "; out << "\b\b}"; // \b is backspace } else { out << "{}"; } return out; } template <class T1, class T2> ostream & operator << (ostream &out, const pair <T1, T2> &P) { out << "{" << P.first << ", " << P.second << "}"; return out; } void testcase_sum_less_than_q() { ll C[2] = {0, 0}; D[0] = 1; C[0] += D[0]; D[1] = 1 - (A[1] & 1); C[(A[1] & 1)] += D[1]; ll cur_par = A[1] & 1; for (int i = 2; i <= n; i++) { cur_par = (cur_par + A[i]) & 1; D[i] = C[cur_par]; C[cur_par] += D[i]; if (C[cur_par] >= Q) { C[cur_par] %= q; } } cout << D[n] % q <<endl; } void testcase_n_less_than_3000() { D[0] = 1; D[1] = 1 - (A[1] & 1); for (int i = 2; i <= n; i++) { ll sum = A[i]; for (int j = i-1; j >= 0; j--) { if (((sum % q) & 1) == 0) { D[i] += D[j]; } sum += A[j]; } D[i] %= q; } cout << D[n] % q <<endl; } void update(int l, int r, int b, int e, ll v, vector <ll> &Tree) { if (e <= l || r <= b) { } else if (l <= b && e <= r) { Tree[b+e] += v; } else { int be = (b+e)/2; update(l, r, b, be, v, Tree); update(l, r, be, e, v, Tree); Tree[b+e] = Tree[b+be] + Tree[be+e]; } } ll query(int l, int r, int b, int e, vector <ll> &Tree) { if (e <= l || r <= b) { return 0; } else if (l <= b && e <= r) { return Tree[b+e]; } else { int be = (b+e)/2; return query(l, r, b, be, Tree) + query(l, r, be, e, Tree); } } inline int get_cat(int val) { return (val < q) ? (val & 1) : (3 - (val & 1)); } inline int get_pos(int val, vector <ll> &SP) { return lower_bound(SP.begin(), SP.end(), val) - SP.begin(); } void testcase_all() { vector <ll> Pref(n+1), Sorted_prefs[4]; Pref[0] = 0; Sorted_prefs[get_cat(0)].push_back(0); for (int i = 1; i <= n; i++) { Pref[i] = (Pref[i-1] + A[i]) % q2; Sorted_prefs[get_cat(Pref[i])].push_back(Pref[i]); } Sorted_prefs[0].push_back(0); Sorted_prefs[0].push_back(q-1); Sorted_prefs[1].push_back(1); Sorted_prefs[1].push_back(q-2); Sorted_prefs[2].push_back(q); Sorted_prefs[2].push_back(q2-1); Sorted_prefs[3].push_back(q+1); Sorted_prefs[3].push_back(q2-2); int s = 1; for ( ; s < 2 * (n+4); s *= 2); vector <ll> Tree[4]; for (int c = 0; c < 4; c++) { sort(Sorted_prefs[c].begin(), Sorted_prefs[c].end()); Tree[c].resize(s, 0); // cerr << c << ": " << Sorted_prefs[c] <<endl; } int v = 0, c = get_cat(v), p = get_pos(v, Sorted_prefs[c]); D[v] = 1; update(p, +1, 0, s/2, D[v], Tree[c]); for (int i = 1; i <= n; i++) { int v = Pref[i], c = get_cat(v), p = get_pos(v, Sorted_prefs[c]); D[i] = 0; if (c == 0) { // 0 <= v < q, v % 2 == 0 D[i] += query(0, p+1, 0, s/2, Tree[c]); int c2 = 3-c, v2 = q2+v-(q-1), p2 = get_pos(v2, Sorted_prefs[c2]); D[i] += query(p2, s/2, 0, s/2, Tree[c2]); int c3 = 1-c, v3 = v+1, p3 = get_pos(v3, Sorted_prefs[c3]); D[i] += query(p3, s/2, 0, s/2, Tree[c3]); int c4 = 2+c, v4 = v+q, p4 = get_pos(v4, Sorted_prefs[c4]); while (0 <= p4 && Sorted_prefs[c4][p4] > v+q) p4--; D[i] += query(0, p4+1, 0, s/2, Tree[c4]); } else if (c == 1) { // o <= v < q, v % 2 == 1 D[i] += query(0, p+1, 0, s/2, Tree[c]); int c2 = 3-c, v2 = q2+v-(q-1), p2 = get_pos(v2, Sorted_prefs[c2]); D[i] += query(p2, s/2, 0, s/2, Tree[c2]); int c3 = 1-c, v3 = v+1, p3 = get_pos(v3, Sorted_prefs[c3]); D[i] += query(p3, s/2, 0, s/2, Tree[c3]); int c4 = 2+c, v4 = v+q, p4 = get_pos(v4, Sorted_prefs[c4]); while (0 <= p4 && Sorted_prefs[c4][p4] > v+q) p4--; D[i] += query(0, p4+1, 0, s/2, Tree[c4]); } else if (c == 2) { // q <= v < 2q, v % 2 == 1 D[i] += query(0, p+1, 0, s/2, Tree[c]); int c2 = 3-c, v2 = v-(q-1), p2 = get_pos(v2, Sorted_prefs[c2]); D[i] += query(p2, s/2, 0, s/2, Tree[c2]); int c3 = 5-c, v3 = v+1, p3 = get_pos(v3, Sorted_prefs[c3]); D[i] += query(p3, s/2, 0, s/2, Tree[c3]); int c4 = c-2, v4 = v-q, p4 = get_pos(v4, Sorted_prefs[c4]); while (0 <= p4 && Sorted_prefs[c4][p4] > v-q) p4--; D[i] += query(0, p4+1, 0, s/2, Tree[c4]); } else { // q <= v < 2q, v % 2 == 0 D[i] += query(0, p+1, 0, s/2, Tree[c]); int c2 = 3-c, v2 = v-(q-1), p2 = get_pos(v2, Sorted_prefs[c2]); D[i] += query(p2, s/2, 0, s/2, Tree[c2]); int c3 = 5-c, v3 = v+1, p3 = get_pos(v3, Sorted_prefs[c3]); D[i] += query(p3, s/2, 0, s/2, Tree[c3]); int c4 = c-2, v4 = v-q, p4 = get_pos(v4, Sorted_prefs[c4]); while (0 <= p4 && Sorted_prefs[c4][p4] > v-q) p4--; D[i] += query(0, p4+1, 0, s/2, Tree[c4]); } D[i] %= q; update(p, p+1, 0, s/2, D[i], Tree[c]); // cerr << i << " v=" << v << " c=" << c << " p=" << p << " | " << " _ " << D[i] <<endl; } cout << D[n] % q <<endl; } int main() { ios_base::sync_with_stdio(false); cin >> n; ll sum = 0; for (int i = 1; i <= n; i++) { cin >> A[i]; sum += A[i]; } /*if (sum < q) { testcase_sum_less_than_q(); } else */ if (n <= 3000) { testcase_n_less_than_3000(); } else { testcase_all(); } return 0; } |