#ifdef NDEBUG #undef LOCAL #endif #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using pii = pair<int,int>; using pll = pair<ll,ll>; using vi = vector<int>; using vb = vector<bool>; using vc = vector<char>; using vvc = vector<vc>; using vvi = vector<vi>; using vvb = vector<vb>; using vpii = vector<pii>; using vpll = vector<pll>; using vll = vector<ll>; using vull = vector<ull>; using vvll = vector<vll>; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define f first #define s second #define pb emplace_back #define rep(i, begin, end) for(auto i = (begin); i <= (end); ++i) #define repr(i, begin, end) for(auto i = (begin); i >= (end); --i) #define bend(X) X.begin(), X.end() constexpr int INF = 1e9+1e7; constexpr ll INFl = INF; constexpr ll INFll = 1e18+1e16; #ifndef LOCAL #define endl '\n' #endif template <typename T> void print(T x) { cout << x << endl; } template <typename T, typename... Args> void print(T x, Args... args) { cout << x << ' '; print(args...); } #ifdef LOCAL template <typename T> void _deb(T x) { cout << x << endl; } template <typename T, typename... Args> void _deb(T x, Args... args) { cout << x << ", "; _deb(args...); } #define deb(...) do { cout << #__VA_ARGS__ << " = "; _deb(__VA_ARGS__); } while (false) #define say(...) _deb(__VA_ARGS__); template <typename T1, typename T2> ostream& operator<<(ostream& os, pair<T1,T2> x) { os << '(' << x.f << ", " << x.s << ')'; return os; } template <typename Ch, typename Tr, typename T> ostream& operator<<(basic_ostream<Ch,Tr>& os, T x) { bool first = true; os << '['; for(auto i : x) { if (!first) os << ", "; os << i; first = false; } os << ']'; return os; } #else #define deb(...) #define say(...) template <typename T1, typename T2> ostream& operator<<(ostream& os, pair<T1,T2> x) { os << x.f << ' ' << x.s; return os; } template <typename Ch, typename Tr, typename T> ostream& operator<<(basic_ostream<Ch,Tr>& os, T x) { for(auto i : x) os << i << ' '; return os; } #endif template <typename T1, typename T2> istream& operator>>(istream& is, pair<T1,T2>& x) { is >> x.f >> x.s; return is; } template <typename T> istream& operator>>(istream& is, vector<T>& x) { for(T& i : x) is >> i; return is; } template <typename T1, typename T2> pair<T1,T2> operator+(const pair<T1,T2>& A, const pair<T1,T2>& B) { return {A.f+B.f, A.s+B.s}; } template <typename T1, typename T2> pair<T1,T2> operator-(const pair<T1,T2>& A, const pair<T1,T2>& B) { return {A.f-B.f, A.s-B.s}; } template <typename T1, typename T2> pair<T1,T2>& operator+=(pair<T1,T2>& A, const pair<T1,T2>& B) { A.f += B.f; A.s += B.s; return A; } template <typename T1, typename T2> pair<T1,T2>& operator-=(pair<T1,T2>& A, const pair<T1,T2>& B) { A.f -= B.f; A.s -= B.s; return A; } template <typename T1, typename T2> pair<T1,T2> operator-(const pair<T1,T2>& A) { return {-A.f, -A.s}; } template <typename T1, typename T2> bool maxe(T1& A, const T2& B) { if(B>A) { A=B; return 1; } return 0; } template <typename T1, typename T2> bool mine(T1& A, const T2& B) { if(B<A) { A=B; return 1; } return 0; } class Nic{} nic; template <typename T> Nic operator<<(Nic, T) { return nic; } Nic operator<<(ostream&, Nic) { return nic; } template <typename T> T fastpow(const T& x, const unsigned long long& p) { if (p == 1) return x; return (p&1) ? fastpow(x*x, p/2)*x : fastpow(x*x, p/2); } template <ll mod> class nu { private: ll v; // constructor called when you know v will be in [0,mod), so no modulo is performed nu(ll val, void* _) : v{val} {}; public: nu(ll val = 0) : v{val} { if (v >= mod || v <= -mod) v %= mod; if (v < 0) v += mod; }; operator ll() { return v; } nu<mod> operator+(const nu<mod>& o) const { return nu<mod>{v+o.v >= mod ? v+o.v-mod : v+o.v, nullptr}; } nu<mod> operator-(const nu<mod>& o) const { return nu<mod>{v-o.v < 0 ? v-o.v+mod : v-o.v, nullptr}; } nu<mod> operator*(const nu<mod>& o) const { return nu<mod>{(v*o.v)%mod, nullptr}; } nu<mod> operator+=(const nu<mod>& o) { v += o.v; if (v >= mod) v -= mod; return *this; } nu<mod> operator-=(const nu<mod>& o) { v -= o.v; if (v < 0) v += mod; return *this; } nu<mod> operator*=(const nu<mod>& o) { v *= o.v; if (v >= mod) v %= mod; return *this; } // assumes mod is prime nu<mod> inv() const { return fastpow(*this, mod-2); } nu<mod> operator/(const nu<mod>& o) const { return (*this) * o.inv(); } nu<mod> operator/=(const nu<mod>& o) { v = ((*this)/o).v; return *this; } friend ostream& operator<<(ostream& os, const nu<mod>& x) { os << x.v; return os; } static vector<nu<mod>> all_inverses(size_t n) { vector<nu<mod>> inv(n+1); inv[1] = 1; for (int i = 2; i <= n; i++) inv[i] = nu<mod>(mod - (mod/i) * inv[mod%i] % mod, nullptr); return inv; } static vector<nu<mod>> all_inverses() { return all_inverses(mod-1); } }; constexpr ll mod = 1e9+7; using num = nu<mod>; ////////////////////////////////////////////////// struct Tree { struct node { num x = 0; node* l = nullptr; node* r = nullptr; }; node* root = nullptr; void add(node*& v, int l, int r, int a, const num& val) { if (v == nullptr) v = new node(); v->x += val; if (l < r) { int m = (l+r)/2; if (a <= m) add(v->l, l, m, a, val); else add(v->r, m+1, r, a, val); } } void add(int a, const num& val) { add(root, 0, mod-1, a, val); } num get(node* v, int l, int r, int L, int R) { if (v == nullptr || R < l || r < L) return num(0); if (L <= l && r <= R) return v->x; int m = (l+r)/2; return get(v->l, l, m, L, R) + get(v->r, m+1, r, L, R); } num get(int L, int R) { return L <= R ? get(root, 0, mod-1, L, R) : num(0); } } Tr[2][2]; int n; vi T; vector<num> DP; auto solve() { cin >> n; T.resize(n+1); DP.resize(n+1); rep (i, 1, n) { cin >> T[i]; } DP[0] = 1; Tr[0][0].add(0, DP[0]); int reszta = 0; bool przekrecilo = 0; bool parzystosc = 0; rep (i, 1, n) { reszta += T[i]; if (reszta >= mod) { przekrecilo = !przekrecilo; reszta -= mod; } if (T[i]%2) parzystosc = !parzystosc; deb(i); deb(reszta, przekrecilo, parzystosc); deb(Tr[0][0].get(0, mod-1)); deb(Tr[0][1].get(0, mod-1)); deb(Tr[1][0].get(0, mod-1)); deb(Tr[1][1].get(0, mod-1)); DP[i] += Tr[przekrecilo][parzystosc].get(0, reszta); DP[i] += Tr[!przekrecilo][!parzystosc].get(0, reszta); DP[i] += Tr[przekrecilo][!parzystosc].get(reszta+1, mod-1); DP[i] += Tr[!przekrecilo][parzystosc].get(reszta+1, mod-1); deb(DP[i]); Tr[przekrecilo][parzystosc].add(reszta, DP[i]); } return DP[n]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int noOfTests = 1; //cin >> noOfTests; while(noOfTests --> 0) cout << solve() << '\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 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 | #ifdef NDEBUG #undef LOCAL #endif #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using pii = pair<int,int>; using pll = pair<ll,ll>; using vi = vector<int>; using vb = vector<bool>; using vc = vector<char>; using vvc = vector<vc>; using vvi = vector<vi>; using vvb = vector<vb>; using vpii = vector<pii>; using vpll = vector<pll>; using vll = vector<ll>; using vull = vector<ull>; using vvll = vector<vll>; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define f first #define s second #define pb emplace_back #define rep(i, begin, end) for(auto i = (begin); i <= (end); ++i) #define repr(i, begin, end) for(auto i = (begin); i >= (end); --i) #define bend(X) X.begin(), X.end() constexpr int INF = 1e9+1e7; constexpr ll INFl = INF; constexpr ll INFll = 1e18+1e16; #ifndef LOCAL #define endl '\n' #endif template <typename T> void print(T x) { cout << x << endl; } template <typename T, typename... Args> void print(T x, Args... args) { cout << x << ' '; print(args...); } #ifdef LOCAL template <typename T> void _deb(T x) { cout << x << endl; } template <typename T, typename... Args> void _deb(T x, Args... args) { cout << x << ", "; _deb(args...); } #define deb(...) do { cout << #__VA_ARGS__ << " = "; _deb(__VA_ARGS__); } while (false) #define say(...) _deb(__VA_ARGS__); template <typename T1, typename T2> ostream& operator<<(ostream& os, pair<T1,T2> x) { os << '(' << x.f << ", " << x.s << ')'; return os; } template <typename Ch, typename Tr, typename T> ostream& operator<<(basic_ostream<Ch,Tr>& os, T x) { bool first = true; os << '['; for(auto i : x) { if (!first) os << ", "; os << i; first = false; } os << ']'; return os; } #else #define deb(...) #define say(...) template <typename T1, typename T2> ostream& operator<<(ostream& os, pair<T1,T2> x) { os << x.f << ' ' << x.s; return os; } template <typename Ch, typename Tr, typename T> ostream& operator<<(basic_ostream<Ch,Tr>& os, T x) { for(auto i : x) os << i << ' '; return os; } #endif template <typename T1, typename T2> istream& operator>>(istream& is, pair<T1,T2>& x) { is >> x.f >> x.s; return is; } template <typename T> istream& operator>>(istream& is, vector<T>& x) { for(T& i : x) is >> i; return is; } template <typename T1, typename T2> pair<T1,T2> operator+(const pair<T1,T2>& A, const pair<T1,T2>& B) { return {A.f+B.f, A.s+B.s}; } template <typename T1, typename T2> pair<T1,T2> operator-(const pair<T1,T2>& A, const pair<T1,T2>& B) { return {A.f-B.f, A.s-B.s}; } template <typename T1, typename T2> pair<T1,T2>& operator+=(pair<T1,T2>& A, const pair<T1,T2>& B) { A.f += B.f; A.s += B.s; return A; } template <typename T1, typename T2> pair<T1,T2>& operator-=(pair<T1,T2>& A, const pair<T1,T2>& B) { A.f -= B.f; A.s -= B.s; return A; } template <typename T1, typename T2> pair<T1,T2> operator-(const pair<T1,T2>& A) { return {-A.f, -A.s}; } template <typename T1, typename T2> bool maxe(T1& A, const T2& B) { if(B>A) { A=B; return 1; } return 0; } template <typename T1, typename T2> bool mine(T1& A, const T2& B) { if(B<A) { A=B; return 1; } return 0; } class Nic{} nic; template <typename T> Nic operator<<(Nic, T) { return nic; } Nic operator<<(ostream&, Nic) { return nic; } template <typename T> T fastpow(const T& x, const unsigned long long& p) { if (p == 1) return x; return (p&1) ? fastpow(x*x, p/2)*x : fastpow(x*x, p/2); } template <ll mod> class nu { private: ll v; // constructor called when you know v will be in [0,mod), so no modulo is performed nu(ll val, void* _) : v{val} {}; public: nu(ll val = 0) : v{val} { if (v >= mod || v <= -mod) v %= mod; if (v < 0) v += mod; }; operator ll() { return v; } nu<mod> operator+(const nu<mod>& o) const { return nu<mod>{v+o.v >= mod ? v+o.v-mod : v+o.v, nullptr}; } nu<mod> operator-(const nu<mod>& o) const { return nu<mod>{v-o.v < 0 ? v-o.v+mod : v-o.v, nullptr}; } nu<mod> operator*(const nu<mod>& o) const { return nu<mod>{(v*o.v)%mod, nullptr}; } nu<mod> operator+=(const nu<mod>& o) { v += o.v; if (v >= mod) v -= mod; return *this; } nu<mod> operator-=(const nu<mod>& o) { v -= o.v; if (v < 0) v += mod; return *this; } nu<mod> operator*=(const nu<mod>& o) { v *= o.v; if (v >= mod) v %= mod; return *this; } // assumes mod is prime nu<mod> inv() const { return fastpow(*this, mod-2); } nu<mod> operator/(const nu<mod>& o) const { return (*this) * o.inv(); } nu<mod> operator/=(const nu<mod>& o) { v = ((*this)/o).v; return *this; } friend ostream& operator<<(ostream& os, const nu<mod>& x) { os << x.v; return os; } static vector<nu<mod>> all_inverses(size_t n) { vector<nu<mod>> inv(n+1); inv[1] = 1; for (int i = 2; i <= n; i++) inv[i] = nu<mod>(mod - (mod/i) * inv[mod%i] % mod, nullptr); return inv; } static vector<nu<mod>> all_inverses() { return all_inverses(mod-1); } }; constexpr ll mod = 1e9+7; using num = nu<mod>; ////////////////////////////////////////////////// struct Tree { struct node { num x = 0; node* l = nullptr; node* r = nullptr; }; node* root = nullptr; void add(node*& v, int l, int r, int a, const num& val) { if (v == nullptr) v = new node(); v->x += val; if (l < r) { int m = (l+r)/2; if (a <= m) add(v->l, l, m, a, val); else add(v->r, m+1, r, a, val); } } void add(int a, const num& val) { add(root, 0, mod-1, a, val); } num get(node* v, int l, int r, int L, int R) { if (v == nullptr || R < l || r < L) return num(0); if (L <= l && r <= R) return v->x; int m = (l+r)/2; return get(v->l, l, m, L, R) + get(v->r, m+1, r, L, R); } num get(int L, int R) { return L <= R ? get(root, 0, mod-1, L, R) : num(0); } } Tr[2][2]; int n; vi T; vector<num> DP; auto solve() { cin >> n; T.resize(n+1); DP.resize(n+1); rep (i, 1, n) { cin >> T[i]; } DP[0] = 1; Tr[0][0].add(0, DP[0]); int reszta = 0; bool przekrecilo = 0; bool parzystosc = 0; rep (i, 1, n) { reszta += T[i]; if (reszta >= mod) { przekrecilo = !przekrecilo; reszta -= mod; } if (T[i]%2) parzystosc = !parzystosc; deb(i); deb(reszta, przekrecilo, parzystosc); deb(Tr[0][0].get(0, mod-1)); deb(Tr[0][1].get(0, mod-1)); deb(Tr[1][0].get(0, mod-1)); deb(Tr[1][1].get(0, mod-1)); DP[i] += Tr[przekrecilo][parzystosc].get(0, reszta); DP[i] += Tr[!przekrecilo][!parzystosc].get(0, reszta); DP[i] += Tr[przekrecilo][!parzystosc].get(reszta+1, mod-1); DP[i] += Tr[!przekrecilo][parzystosc].get(reszta+1, mod-1); deb(DP[i]); Tr[przekrecilo][parzystosc].add(reszta, DP[i]); } return DP[n]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int noOfTests = 1; //cin >> noOfTests; while(noOfTests --> 0) cout << solve() << '\n'; return 0; } |