#include <bits/stdc++.h> #define fi first #define se second #define p_b push_back #define pll pair<ll,ll> #define pii pair<int,int> #define m_p make_pair #define all(x) x.begin(),x.end() #define sset ordered_set #define sqr(x) (x)*(x) #define pw(x) (1ll << (x)) #define sz(x) (int)x.size() #define fout(x) {cout << x << "\n"; return; } using namespace std; typedef long long ll; typedef long double ld; const int MAXN = 1e5; const int M = pw(16); const long long mod = 1e9 + 7; const int N = 5e5; const ll inf = 1e18; template <typename T> void vout(T s){cout << s << endl;exit(0);} void u(int &x, int a){ x += a; if(x >= mod){ x -= mod; } } struct node{ int sum; node *l, *r; node(): sum(0), l(r=nullptr) {} }; int sumM(int a, int b){ int r = a; u(r, b); return r; } node *rootE = new node(), *rootO = new node(); void modify(node *v, ll tl, ll tr, ll pos, int val){ if(tl == tr) { u(v -> sum, val); }else{ ll tm = (tl + tr) >> 1; if(pos <= tm){ if(v -> l == nullptr) v -> l = new node(); modify(v -> l, tl, tm, pos, val); }else{ if(v -> r == nullptr) v -> r = new node(); modify(v -> r, tm + 1, tr, pos, val); } v -> sum = 0; if(v -> l != nullptr) u(v -> sum, (v -> l) -> sum); if(v -> r != nullptr) u(v -> sum, (v -> r) -> sum); } } int get(node *v, ll tl, ll tr, ll l, ll r){ if(l > r || v == nullptr) return 0; if(tl == l && tr == r){ return v -> sum; } ll tm = (tl + tr) >> 1; return sumM(get(v -> l, tl, tm, l, min(r, tm)), get(v -> r, tm + 1, tr, max(l, tm + 1), r)); } void update(ll pos, ll val){ if(pos % 2 == 0) modify(rootO, 0, 4 * mod - 1, pos, val); else modify(rootE, 0, 4 * mod - 1, pos, val); if(pos % 2 == 0)modify(rootO, 0, 4 * mod - 1, pos + 2 * mod, val); else modify(rootE, 0, 4 * mod - 1, pos + 2 * mod, val); } int get(ll l, ll r, ll xe){ if(!xe){ return get(rootO, 0, 4 * mod - 1, l, r); }else{ return get(rootE, 0, 4 * mod - 1, l, r); } } void brute(vector <ll> a){ int n = a.size(); vector <int> dp(n + 1); dp[0] = 1; for(int i = 0; i < n; i++){ ll s = 0; for(int j = i; j < n; j++){ s += a[j]; s %= 2 * mod; if(s < mod){ if(s % 2 == 0) dp[j + 1] += dp[i]; }else{ if(s % 2) dp[j + 1] += dp[i]; } } } cout << dp[n] << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector <ll> a(n), dp(n); for(auto &i : a) cin >> i; //brute(a); ll s = 0; update(0, 1); s = 0; for(int i = 0; i < n; i++){ s += a[i]; if(s >= 2 * mod) s -= 2 * mod; ll a, b; dp[i] = sumM(get(s + 1, s + mod, (s + mod) % 2), get(s + mod + 1, s + 2 * mod, (s + 2 * mod) % 2)); update(s, dp[i]); } cout << dp[n - 1] << "\n"; 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 | #include <bits/stdc++.h> #define fi first #define se second #define p_b push_back #define pll pair<ll,ll> #define pii pair<int,int> #define m_p make_pair #define all(x) x.begin(),x.end() #define sset ordered_set #define sqr(x) (x)*(x) #define pw(x) (1ll << (x)) #define sz(x) (int)x.size() #define fout(x) {cout << x << "\n"; return; } using namespace std; typedef long long ll; typedef long double ld; const int MAXN = 1e5; const int M = pw(16); const long long mod = 1e9 + 7; const int N = 5e5; const ll inf = 1e18; template <typename T> void vout(T s){cout << s << endl;exit(0);} void u(int &x, int a){ x += a; if(x >= mod){ x -= mod; } } struct node{ int sum; node *l, *r; node(): sum(0), l(r=nullptr) {} }; int sumM(int a, int b){ int r = a; u(r, b); return r; } node *rootE = new node(), *rootO = new node(); void modify(node *v, ll tl, ll tr, ll pos, int val){ if(tl == tr) { u(v -> sum, val); }else{ ll tm = (tl + tr) >> 1; if(pos <= tm){ if(v -> l == nullptr) v -> l = new node(); modify(v -> l, tl, tm, pos, val); }else{ if(v -> r == nullptr) v -> r = new node(); modify(v -> r, tm + 1, tr, pos, val); } v -> sum = 0; if(v -> l != nullptr) u(v -> sum, (v -> l) -> sum); if(v -> r != nullptr) u(v -> sum, (v -> r) -> sum); } } int get(node *v, ll tl, ll tr, ll l, ll r){ if(l > r || v == nullptr) return 0; if(tl == l && tr == r){ return v -> sum; } ll tm = (tl + tr) >> 1; return sumM(get(v -> l, tl, tm, l, min(r, tm)), get(v -> r, tm + 1, tr, max(l, tm + 1), r)); } void update(ll pos, ll val){ if(pos % 2 == 0) modify(rootO, 0, 4 * mod - 1, pos, val); else modify(rootE, 0, 4 * mod - 1, pos, val); if(pos % 2 == 0)modify(rootO, 0, 4 * mod - 1, pos + 2 * mod, val); else modify(rootE, 0, 4 * mod - 1, pos + 2 * mod, val); } int get(ll l, ll r, ll xe){ if(!xe){ return get(rootO, 0, 4 * mod - 1, l, r); }else{ return get(rootE, 0, 4 * mod - 1, l, r); } } void brute(vector <ll> a){ int n = a.size(); vector <int> dp(n + 1); dp[0] = 1; for(int i = 0; i < n; i++){ ll s = 0; for(int j = i; j < n; j++){ s += a[j]; s %= 2 * mod; if(s < mod){ if(s % 2 == 0) dp[j + 1] += dp[i]; }else{ if(s % 2) dp[j + 1] += dp[i]; } } } cout << dp[n] << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector <ll> a(n), dp(n); for(auto &i : a) cin >> i; //brute(a); ll s = 0; update(0, 1); s = 0; for(int i = 0; i < n; i++){ s += a[i]; if(s >= 2 * mod) s -= 2 * mod; ll a, b; dp[i] = sumM(get(s + 1, s + mod, (s + mod) % 2), get(s + mod + 1, s + 2 * mod, (s + 2 * mod) % 2)); update(s, dp[i]); } cout << dp[n - 1] << "\n"; return 0; } |