#include <iostream> #include <cstdint> #include <vector> namespace { using std::ios_base, std::cin, std::cout; using std::vector; using uint_t = uint32_t; using int_t = int32_t; using vec_t = std::vector<int_t>; uint_t generate_buc(vec_t const & a, vec_t & buc) { uint_t n = a.size(), prev = 0, beg = 0, prev_beg; uint_t cnt_0 = 0, tmp; for (uint_t j = 0; j < n; ++j) { tmp = a[j] + prev; buc.push_back(tmp); if (tmp == 0) ++cnt_0; prev += a[j]; } while (buc.size() - beg > 1) { prev_beg = beg; beg = buc.size(); for (uint_t j = prev_beg + 1; j < beg; ++j) { tmp = buc[j] - buc[prev_beg]; buc.push_back(tmp); if (tmp == 0) ++cnt_0; } } return cnt_0; } uint_t count_3_numbers_no_zero(vec_t const & buc) { uint_t cnt = 0, tmp; for (uint_t i = 0; i < buc.size() - 2; ++i) for (uint_t j = i + 1; j < buc.size() - 1; ++j) { if (buc[i] || buc[j]) { tmp = buc[i] + buc[j]; for (uint_t k = j + 1; k < buc.size(); ++k) { if (tmp + buc[k] == 0) ++cnt; } } } return cnt; } uint_t n_over_3(uint_t n) { if (n < 3) return 0; if (n == 3) return 1; if (n == 4) return 4; vector<uint_t> vec({6, 4}); uint_t prev; for (uint_t i = 5; i <= n; ++i) { prev = i - 1; vec[0] += prev; prev = vec[0] - prev; vec[1] += prev; } return vec[1]; } bool all_the_same_sign(vec_t const & buc) { bool same = true; bool positive = false, negative = false; uint_t k = 0; while (same && k < buc.size()) { if (buc[k] == 0) { same = false; } else if (!positive && buc[k] > 0) { positive = true; } else if (!negative && buc[k] < 0) { negative = true; } if (positive && negative) same = false; ++k; } return same; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); uint_t n; int_t ai; bool positive = false; bool negative = false; bool zero = false; vec_t a; cin >> n; for (uint_t k = 0; k < n; ++k) { cin >> ai; a.push_back(ai); if (!zero && ai == 0) zero = true; if (!positive && ai > 0) positive = true; if (!negative && ai < 0) negative = true; } vec_t buc; uint_t result; uint_t cnt_0 = generate_buc(a, buc); if (!zero && positive != negative) { result = 0; } else if (cnt_0 == buc.size()) { result = n_over_3(buc.size()); } else if (all_the_same_sign(buc)) { result = 0; } else { result = n_over_3(cnt_0) + count_3_numbers_no_zero(buc); } cout << result << '\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 | #include <iostream> #include <cstdint> #include <vector> namespace { using std::ios_base, std::cin, std::cout; using std::vector; using uint_t = uint32_t; using int_t = int32_t; using vec_t = std::vector<int_t>; uint_t generate_buc(vec_t const & a, vec_t & buc) { uint_t n = a.size(), prev = 0, beg = 0, prev_beg; uint_t cnt_0 = 0, tmp; for (uint_t j = 0; j < n; ++j) { tmp = a[j] + prev; buc.push_back(tmp); if (tmp == 0) ++cnt_0; prev += a[j]; } while (buc.size() - beg > 1) { prev_beg = beg; beg = buc.size(); for (uint_t j = prev_beg + 1; j < beg; ++j) { tmp = buc[j] - buc[prev_beg]; buc.push_back(tmp); if (tmp == 0) ++cnt_0; } } return cnt_0; } uint_t count_3_numbers_no_zero(vec_t const & buc) { uint_t cnt = 0, tmp; for (uint_t i = 0; i < buc.size() - 2; ++i) for (uint_t j = i + 1; j < buc.size() - 1; ++j) { if (buc[i] || buc[j]) { tmp = buc[i] + buc[j]; for (uint_t k = j + 1; k < buc.size(); ++k) { if (tmp + buc[k] == 0) ++cnt; } } } return cnt; } uint_t n_over_3(uint_t n) { if (n < 3) return 0; if (n == 3) return 1; if (n == 4) return 4; vector<uint_t> vec({6, 4}); uint_t prev; for (uint_t i = 5; i <= n; ++i) { prev = i - 1; vec[0] += prev; prev = vec[0] - prev; vec[1] += prev; } return vec[1]; } bool all_the_same_sign(vec_t const & buc) { bool same = true; bool positive = false, negative = false; uint_t k = 0; while (same && k < buc.size()) { if (buc[k] == 0) { same = false; } else if (!positive && buc[k] > 0) { positive = true; } else if (!negative && buc[k] < 0) { negative = true; } if (positive && negative) same = false; ++k; } return same; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); uint_t n; int_t ai; bool positive = false; bool negative = false; bool zero = false; vec_t a; cin >> n; for (uint_t k = 0; k < n; ++k) { cin >> ai; a.push_back(ai); if (!zero && ai == 0) zero = true; if (!positive && ai > 0) positive = true; if (!negative && ai < 0) negative = true; } vec_t buc; uint_t result; uint_t cnt_0 = generate_buc(a, buc); if (!zero && positive != negative) { result = 0; } else if (cnt_0 == buc.size()) { result = n_over_3(buc.size()); } else if (all_the_same_sign(buc)) { result = 0; } else { result = n_over_3(cnt_0) + count_3_numbers_no_zero(buc); } cout << result << '\n'; } |