#include <bits/stdc++.h> using namespace std; const int N = 1'000'007; const int M = 1e9 + 7; const int T = 1 << 20; int add(int a, int b) { return a += b, a < M ? a : a - M; } int sub(int a, int b) { return a -= b, a < 0 ? a + M : a; } struct Node { int odd; int even; int left; }; Node operator+ (const Node &a, const Node &b) { Node res{0, 0, a.left}; //cerr << "adding: (" << a.left << ' ' << a.odd << ' ' << a.even << ") (" << b.left << ' ' << b.odd << ' ' << b.even << ")\n"; if((b.left - a.left) & 1) { res.even = add(a.even, b.odd); res.odd = add(a.odd, b.even); } else { res.even = add(a.even, b.even); res.odd = add(a.odd, b.odd); } return res; } Node tree[T << 1]; int n, m, a[N], s[N], ways[N], offset; void upd(int p, int v) { //cerr << "> " << p << " -> "; p = lower_bound(s, s + m, p) - s; //cerr << p << " v = " << v << '\n'; tree[p + T].even = add(tree[p + T].even, v); for(p += T; p >>= 1; ) tree[p] = tree[p << 1 | 0] + tree[p << 1 | 1]; } Node sum(int l, int r) { //cerr << "> " << l << ' ' << r << " -> "; Node L{0, 0, l}; Node R{0, 0, 0}; l = lower_bound(s, s + m, l) - s; r = upper_bound(s, s + m, r) - s - 1; //cerr << l << ' ' << r << '\n'; // cerr << "sum: " << l << ' ' << r << '\n'; for(l += T, r += T + 1; l < r; l >>= 1, r >>= 1) { if(l & 1) L = L + tree[l++]; if(r & 1) R = tree[--r] + R; } // cerr << "= " << (L + R).odd << ' ' << (L + R).even << '\n'; // cerr << '\n'; return L + R; } int main() { ios::sync_with_stdio(false), cin.tie(nullptr); cin >> n; for(int i = 0; i < n; i++) cin >> a[i]; s[0] = sub(0, a[0]); for(int i = 1; i < n; i++) s[i] = sub(s[i - 1], a[i]); for(int i = n; i < 2 * n; i++) { s[i] = sub(s[i - n], 1); } s[2 * n] = M - 1; // cerr << "p: "; // for(int i = 0; i < 2 * n + 2; i++) { // cerr << s[i] << ' '; //} //cerr << '\n'; sort(s, s + 2 * n + 2); m = unique(s, s + 2 * n + 2) - s; //cerr << "s: "; //for(int i = 0; i < m; i++) // cerr << s[i] << ' '; //cerr << '\n'; for(int i = 0; i < m; i++) tree[i + T].left = s[i]; for(int i = m; i < T; i++) tree[i + T].left = M; for(int i = T - 1; i; i--) tree[i].left = tree[i << 1 | 0].left; upd(0, 1); for(int i = 0; i < n; i++) { offset = sub(offset, a[i]); //cerr << "offset: " << offset << '\n'; //cerr << "----\n"; ways[i] = sum(offset, M - 1).even; if(offset & 1) ways[i] = add(ways[i], sum(0, offset - 1).even); else ways[i] = add(ways[i], sum(0, offset - 1).odd); //cerr << "----\n"; //cerr << i << ":\n"; //cerr << sum(offset, T - 1).even << ' ' << sum(offset, T - 1).odd << '\n'; //cerr << sum(0, offset - 1).even << ' ' << sum(0, offset - 1).odd << '\n'; //cerr << '\n'; // ways[i] = add(sum(offset, T - 1).even, sum(0, offset - 1).odd); upd(offset, ways[i]); //cerr << "ways[" << i << "] = " << ways[i] << '\n'; } cout << ways[n - 1] << '\n'; }
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 | #include <bits/stdc++.h> using namespace std; const int N = 1'000'007; const int M = 1e9 + 7; const int T = 1 << 20; int add(int a, int b) { return a += b, a < M ? a : a - M; } int sub(int a, int b) { return a -= b, a < 0 ? a + M : a; } struct Node { int odd; int even; int left; }; Node operator+ (const Node &a, const Node &b) { Node res{0, 0, a.left}; //cerr << "adding: (" << a.left << ' ' << a.odd << ' ' << a.even << ") (" << b.left << ' ' << b.odd << ' ' << b.even << ")\n"; if((b.left - a.left) & 1) { res.even = add(a.even, b.odd); res.odd = add(a.odd, b.even); } else { res.even = add(a.even, b.even); res.odd = add(a.odd, b.odd); } return res; } Node tree[T << 1]; int n, m, a[N], s[N], ways[N], offset; void upd(int p, int v) { //cerr << "> " << p << " -> "; p = lower_bound(s, s + m, p) - s; //cerr << p << " v = " << v << '\n'; tree[p + T].even = add(tree[p + T].even, v); for(p += T; p >>= 1; ) tree[p] = tree[p << 1 | 0] + tree[p << 1 | 1]; } Node sum(int l, int r) { //cerr << "> " << l << ' ' << r << " -> "; Node L{0, 0, l}; Node R{0, 0, 0}; l = lower_bound(s, s + m, l) - s; r = upper_bound(s, s + m, r) - s - 1; //cerr << l << ' ' << r << '\n'; // cerr << "sum: " << l << ' ' << r << '\n'; for(l += T, r += T + 1; l < r; l >>= 1, r >>= 1) { if(l & 1) L = L + tree[l++]; if(r & 1) R = tree[--r] + R; } // cerr << "= " << (L + R).odd << ' ' << (L + R).even << '\n'; // cerr << '\n'; return L + R; } int main() { ios::sync_with_stdio(false), cin.tie(nullptr); cin >> n; for(int i = 0; i < n; i++) cin >> a[i]; s[0] = sub(0, a[0]); for(int i = 1; i < n; i++) s[i] = sub(s[i - 1], a[i]); for(int i = n; i < 2 * n; i++) { s[i] = sub(s[i - n], 1); } s[2 * n] = M - 1; // cerr << "p: "; // for(int i = 0; i < 2 * n + 2; i++) { // cerr << s[i] << ' '; //} //cerr << '\n'; sort(s, s + 2 * n + 2); m = unique(s, s + 2 * n + 2) - s; //cerr << "s: "; //for(int i = 0; i < m; i++) // cerr << s[i] << ' '; //cerr << '\n'; for(int i = 0; i < m; i++) tree[i + T].left = s[i]; for(int i = m; i < T; i++) tree[i + T].left = M; for(int i = T - 1; i; i--) tree[i].left = tree[i << 1 | 0].left; upd(0, 1); for(int i = 0; i < n; i++) { offset = sub(offset, a[i]); //cerr << "offset: " << offset << '\n'; //cerr << "----\n"; ways[i] = sum(offset, M - 1).even; if(offset & 1) ways[i] = add(ways[i], sum(0, offset - 1).even); else ways[i] = add(ways[i], sum(0, offset - 1).odd); //cerr << "----\n"; //cerr << i << ":\n"; //cerr << sum(offset, T - 1).even << ' ' << sum(offset, T - 1).odd << '\n'; //cerr << sum(0, offset - 1).even << ' ' << sum(0, offset - 1).odd << '\n'; //cerr << '\n'; // ways[i] = add(sum(offset, T - 1).even, sum(0, offset - 1).odd); upd(offset, ways[i]); //cerr << "ways[" << i << "] = " << ways[i] << '\n'; } cout << ways[n - 1] << '\n'; } |