#include <bits/stdc++.h> using namespace std; using LL = long long; #define e1 first #define e2 second #define pb push_back #define OUT(x) {cout << x << "\n"; exit(0); } #define TCOUT(x) {cout << x << "\n"; return; } #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define rep(i, l, r) for(int i = (l); i < (r); ++i) #define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } #define sz(x) int(x.size()) #define trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; ll safe_mod(ll x, ll m) { ll t = x%m; return t >= 0 ? t : t + m; } struct mint { static const ll mod = 1000000007; // change to something else ll x; ll val() { x %= mod; return x; } mint() : x(0) {} mint(ll xx) : x(safe_mod(xx, mod)) {} mint operator+(mint b) { return mint((x + b.x) % mod); } void operator+=(mint b) { x = (x + b.x) % mod; } mint operator-(mint b) { return mint((x - b.x + mod) % mod); } mint operator*(mint b) { return mint((x * b.x) % mod); } mint operator^(ll e) { ll ans = 1, b = x; for (; e; b = b * b % mod, e /= 2) if (e & 1) ans = ans * b % mod; return ans; } }; #include <algorithm> #ifdef _MSC_VER #include <intrin.h> #endif namespace atcoder { namespace internal { // @param n `0 <= n` // @return minimum non-negative `x` s.t. `n <= 2**x` int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } // @param n `1 <= n` // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0` int bsf(unsigned int n) { #ifdef _MSC_VER unsigned long index; _BitScanForward(&index, n); return index; #else return __builtin_ctz(n); #endif } } // namespace internal } // namespace atcoder #include <cassert> #include <vector> namespace atcoder { template <class S, S (*op)(S, S), S (*e)()> struct segtree { public: segtree() : segtree(0) {} segtree(int n) : segtree(std::vector<S>(n, e())) {} segtree(const std::vector<S>& v) : _n(int(v.size())) { log = internal::ceil_pow2(_n); size = 1 << log; d = std::vector<S>(2 * size, e()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p, S x) { assert(0 <= p && p < _n); p += size; d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } void add(int p, S nowe) { assert(0 <= p && p < _n); auto prev = get(p); set(p, op(prev, nowe)); } S get(int p) { assert(0 <= p && p < _n); return d[p + size]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); S sml = e(), smr = e(); l += size; r += size; while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } private: int _n, size, log; std::vector<S> d; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } }; } // namespace atcoder using namespace atcoder; mt19937_64 rng(time(0)); int random(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } #ifdef DEBUG template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #endif #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif //Did you REAALLY consider sample tests? mint brut(vi vec) { int n = sz(vec); vec.insert(vec.begin(), 0); vector <mint> dp(n+1, 0); dp[0] = 1; FOR(i, 1, n) { mint sum = 0; for (int j=i; j>0; --j) { sum += vec[j]; if (sum.val() % 2 == 0) dp[i] += dp[j-1]; } } return dp[n]; } struct S{ mint even; mint odd; }; S op(S a, S b) { return S{a.even+b.even, a.odd+b.odd}; } S id() { return S{0, 0}; } const ll p = 1000000007; ll normalize(ll value, ll mod) { while (value < 0) value += mod; while (value >= mod) value -= mod; return value; } mint wzo(vi vec) { int n = sz(vec); vec.insert(vec.begin(), 0); vector <mint> dp(n+1, 0); dp[0] = 0; ll pref = 0; //modulujemy przez 2p vector <ll> used_values = {0, 1, 2 * p, 2 * p - 1}; map<ll, int> skal; auto fake_sum = [&](ll a, ll b, int parity) -> void { a = normalize(a, 2 * p); b = normalize(b, 2 * p); used_values.pb(a); used_values.pb(b); }; FOR(i, 1, n) { pref += vec[i]; while (pref >= 2 * p) { pref -= 2 * p; } bool r = (pref % 2); ll x = pref; used_values.pb(pref); fake_sum(x - p + 1, x, r); fake_sum(x + 1, x + p, 1^r); } pref = 0; sort(all(used_values)); used_values.erase(unique(all(used_values)), used_values.end()); rep(i, 0, sz(used_values)) skal[used_values[i]] = i; vector <S> nothing = vector<S>(sz(used_values), id()); segtree<S, op, id> tree(nothing); tree.set(skal[0], S{1, 0}); auto get = [](S item, bool parity) -> mint { if (parity) return item.odd; else return item.even; }; auto get_sum = [&](ll a, ll b, int parity) -> mint { a = normalize(a, 2 * p); b = normalize(b, 2 * p); if (a <= b) return get(tree.prod(skal[a], skal[b] + 1), parity); else { // a > b return get(tree.prod(skal[a], skal[2*p-1] + 1), parity) + get(tree.prod(skal[0], skal[b] + 1), parity); } }; pref = 0; FOR(i, 1, n) { pref += vec[i]; while (pref >= 2 * p) { pref -= 2 * p; } bool r = (pref % 2LL); ll x = pref; dp[i] = get_sum(x - p + 1, x, r); dp[i] = dp[i] + get_sum(x + 1, x + p, 1^r); if (pref % 2LL == 0) tree.add(skal[pref], S{dp[i], 0}); else tree.add(skal[pref], S{0, dp[i]}); } return dp[n]; } bool TEST = 0; signed main() { boost; if (!TEST) { int n; cin >> n; vi vec(n); rep(i, 0, n) cin >> vec[i]; cout << wzo(vec).val() << "\n"; } else { int tests; cin >> tests; FOR(step, 1, tests) { if (step % 100 == 0) cout << "Zaliczono: " << step << endl; int n = random(10, 100); vi vec(n); rep(i, 0, n) vec[i] = random(0, p-1); auto model = wzo(vec).val(); auto bru = brut(vec).val(); if (model != bru) { debug(vec); debug(bru); debug(model); OUT("WA"); } } } }
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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | #include <bits/stdc++.h> using namespace std; using LL = long long; #define e1 first #define e2 second #define pb push_back #define OUT(x) {cout << x << "\n"; exit(0); } #define TCOUT(x) {cout << x << "\n"; return; } #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define rep(i, l, r) for(int i = (l); i < (r); ++i) #define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } #define sz(x) int(x.size()) #define trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; ll safe_mod(ll x, ll m) { ll t = x%m; return t >= 0 ? t : t + m; } struct mint { static const ll mod = 1000000007; // change to something else ll x; ll val() { x %= mod; return x; } mint() : x(0) {} mint(ll xx) : x(safe_mod(xx, mod)) {} mint operator+(mint b) { return mint((x + b.x) % mod); } void operator+=(mint b) { x = (x + b.x) % mod; } mint operator-(mint b) { return mint((x - b.x + mod) % mod); } mint operator*(mint b) { return mint((x * b.x) % mod); } mint operator^(ll e) { ll ans = 1, b = x; for (; e; b = b * b % mod, e /= 2) if (e & 1) ans = ans * b % mod; return ans; } }; #include <algorithm> #ifdef _MSC_VER #include <intrin.h> #endif namespace atcoder { namespace internal { // @param n `0 <= n` // @return minimum non-negative `x` s.t. `n <= 2**x` int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } // @param n `1 <= n` // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0` int bsf(unsigned int n) { #ifdef _MSC_VER unsigned long index; _BitScanForward(&index, n); return index; #else return __builtin_ctz(n); #endif } } // namespace internal } // namespace atcoder #include <cassert> #include <vector> namespace atcoder { template <class S, S (*op)(S, S), S (*e)()> struct segtree { public: segtree() : segtree(0) {} segtree(int n) : segtree(std::vector<S>(n, e())) {} segtree(const std::vector<S>& v) : _n(int(v.size())) { log = internal::ceil_pow2(_n); size = 1 << log; d = std::vector<S>(2 * size, e()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p, S x) { assert(0 <= p && p < _n); p += size; d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } void add(int p, S nowe) { assert(0 <= p && p < _n); auto prev = get(p); set(p, op(prev, nowe)); } S get(int p) { assert(0 <= p && p < _n); return d[p + size]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); S sml = e(), smr = e(); l += size; r += size; while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } private: int _n, size, log; std::vector<S> d; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } }; } // namespace atcoder using namespace atcoder; mt19937_64 rng(time(0)); int random(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } #ifdef DEBUG template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #endif #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif //Did you REAALLY consider sample tests? mint brut(vi vec) { int n = sz(vec); vec.insert(vec.begin(), 0); vector <mint> dp(n+1, 0); dp[0] = 1; FOR(i, 1, n) { mint sum = 0; for (int j=i; j>0; --j) { sum += vec[j]; if (sum.val() % 2 == 0) dp[i] += dp[j-1]; } } return dp[n]; } struct S{ mint even; mint odd; }; S op(S a, S b) { return S{a.even+b.even, a.odd+b.odd}; } S id() { return S{0, 0}; } const ll p = 1000000007; ll normalize(ll value, ll mod) { while (value < 0) value += mod; while (value >= mod) value -= mod; return value; } mint wzo(vi vec) { int n = sz(vec); vec.insert(vec.begin(), 0); vector <mint> dp(n+1, 0); dp[0] = 0; ll pref = 0; //modulujemy przez 2p vector <ll> used_values = {0, 1, 2 * p, 2 * p - 1}; map<ll, int> skal; auto fake_sum = [&](ll a, ll b, int parity) -> void { a = normalize(a, 2 * p); b = normalize(b, 2 * p); used_values.pb(a); used_values.pb(b); }; FOR(i, 1, n) { pref += vec[i]; while (pref >= 2 * p) { pref -= 2 * p; } bool r = (pref % 2); ll x = pref; used_values.pb(pref); fake_sum(x - p + 1, x, r); fake_sum(x + 1, x + p, 1^r); } pref = 0; sort(all(used_values)); used_values.erase(unique(all(used_values)), used_values.end()); rep(i, 0, sz(used_values)) skal[used_values[i]] = i; vector <S> nothing = vector<S>(sz(used_values), id()); segtree<S, op, id> tree(nothing); tree.set(skal[0], S{1, 0}); auto get = [](S item, bool parity) -> mint { if (parity) return item.odd; else return item.even; }; auto get_sum = [&](ll a, ll b, int parity) -> mint { a = normalize(a, 2 * p); b = normalize(b, 2 * p); if (a <= b) return get(tree.prod(skal[a], skal[b] + 1), parity); else { // a > b return get(tree.prod(skal[a], skal[2*p-1] + 1), parity) + get(tree.prod(skal[0], skal[b] + 1), parity); } }; pref = 0; FOR(i, 1, n) { pref += vec[i]; while (pref >= 2 * p) { pref -= 2 * p; } bool r = (pref % 2LL); ll x = pref; dp[i] = get_sum(x - p + 1, x, r); dp[i] = dp[i] + get_sum(x + 1, x + p, 1^r); if (pref % 2LL == 0) tree.add(skal[pref], S{dp[i], 0}); else tree.add(skal[pref], S{0, dp[i]}); } return dp[n]; } bool TEST = 0; signed main() { boost; if (!TEST) { int n; cin >> n; vi vec(n); rep(i, 0, n) cin >> vec[i]; cout << wzo(vec).val() << "\n"; } else { int tests; cin >> tests; FOR(step, 1, tests) { if (step % 100 == 0) cout << "Zaliczono: " << step << endl; int n = random(10, 100); vi vec(n); rep(i, 0, n) vec[i] = random(0, p-1); auto model = wzo(vec).val(); auto bru = brut(vec).val(); if (model != bru) { debug(vec); debug(bru); debug(model); OUT("WA"); } } } } |