#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 3e5 + 7; const int M = 1e9 + 7; int as[N], xs[N]; int mod(int x, int a) { return (ll(x) + a) % M; } struct fenwick { void init(int n) { this->n = n; memset(s, 0, sizeof(s[0]) * (n+1)); } void add(int v, int x) { for (++v; v <= n; v += (v & -v)) s[v] = mod(s[v], x); } int query(int v) { int ans = 0; for (++v; v; v -= (v & -v)) ans = mod(ans, s[v]); return ans; } int query(int l, int r) { return ((query(r) - query(l - 1)) % M + M) % M; } int n; int s[N]; } t[2][2]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &as[i]); xs[i + 1] = mod(xs[i], as[i]); } sort(xs, xs + n+1); int e = unique(xs, xs + n+1) - xs; for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) t[i][j].init(e); t[0][0].add(0, 1); int x = 0, m = 0, b = 0, dp; for (int i = 0; i < n; ++i) { int a = as[i]; b ^= a & 1; if ((m += a) >= M) { x ^= 1; m -= M; } int l = lower_bound(xs, xs + e, m) - xs; dp = t[x][b].query(0, l); dp = mod(dp, t[x ^ 1][b].query(l+1, e-1)); dp = mod(dp, t[x][b ^ 1].query(l+1, e-1)); dp = mod(dp, t[x ^ 1][b ^ 1].query(0, l)); t[x][b].add(l, dp); } printf("%d", dp); 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 | #include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 3e5 + 7; const int M = 1e9 + 7; int as[N], xs[N]; int mod(int x, int a) { return (ll(x) + a) % M; } struct fenwick { void init(int n) { this->n = n; memset(s, 0, sizeof(s[0]) * (n+1)); } void add(int v, int x) { for (++v; v <= n; v += (v & -v)) s[v] = mod(s[v], x); } int query(int v) { int ans = 0; for (++v; v; v -= (v & -v)) ans = mod(ans, s[v]); return ans; } int query(int l, int r) { return ((query(r) - query(l - 1)) % M + M) % M; } int n; int s[N]; } t[2][2]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &as[i]); xs[i + 1] = mod(xs[i], as[i]); } sort(xs, xs + n+1); int e = unique(xs, xs + n+1) - xs; for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) t[i][j].init(e); t[0][0].add(0, 1); int x = 0, m = 0, b = 0, dp; for (int i = 0; i < n; ++i) { int a = as[i]; b ^= a & 1; if ((m += a) >= M) { x ^= 1; m -= M; } int l = lower_bound(xs, xs + e, m) - xs; dp = t[x][b].query(0, l); dp = mod(dp, t[x ^ 1][b].query(l+1, e-1)); dp = mod(dp, t[x][b ^ 1].query(l+1, e-1)); dp = mod(dp, t[x ^ 1][b ^ 1].query(0, l)); t[x][b].add(l, dp); } printf("%d", dp); return 0; } |