#include <iostream> #include <vector> #include <unordered_map> int main() { int n; std::cin >> n; std::vector<int> uc; std::unordered_map<int, int64_t> subsums; for (int i = 0; i < n; ++i) { int in; std::cin >> in; uc.emplace_back(in); int subsum = 0; for (auto it = uc.rbegin(); it != uc.rend(); ++it) { subsum += *it; subsums[subsum] += 1; } } int64_t ret = 0; for (auto [s1, c1]: subsums) { for (auto [s2, c2]: subsums) { if (s1 >= s2) { continue; } int s12 = s1 + s2; int64_t c12 = c1 * c2; if (s2 >= -s12) { continue; } if (subsums.count(-s12)) { int64_t c3 = subsums[-s12]; int c123 = c12 * c3; ret += c123; } } } for (auto [s, c]: subsums) { int s12 = s * 2; int64_t c12 = c * (c - 1) / 2; if (s == 0) { continue; } if (subsums.count(-s12)) { int64_t c3 = subsums[-s12]; int c123 = c12 * c3; ret += c123; } } if (subsums.count(0)) { int64_t c123 = subsums[0] * (subsums[0] - 1) * (subsums[0] - 2) / 6; ret += c123; } std::cout << ret << "\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 | #include <iostream> #include <vector> #include <unordered_map> int main() { int n; std::cin >> n; std::vector<int> uc; std::unordered_map<int, int64_t> subsums; for (int i = 0; i < n; ++i) { int in; std::cin >> in; uc.emplace_back(in); int subsum = 0; for (auto it = uc.rbegin(); it != uc.rend(); ++it) { subsum += *it; subsums[subsum] += 1; } } int64_t ret = 0; for (auto [s1, c1]: subsums) { for (auto [s2, c2]: subsums) { if (s1 >= s2) { continue; } int s12 = s1 + s2; int64_t c12 = c1 * c2; if (s2 >= -s12) { continue; } if (subsums.count(-s12)) { int64_t c3 = subsums[-s12]; int c123 = c12 * c3; ret += c123; } } } for (auto [s, c]: subsums) { int s12 = s * 2; int64_t c12 = c * (c - 1) / 2; if (s == 0) { continue; } if (subsums.count(-s12)) { int64_t c3 = subsums[-s12]; int c123 = c12 * c3; ret += c123; } } if (subsums.count(0)) { int64_t c123 = subsums[0] * (subsums[0] - 1) * (subsums[0] - 2) / 6; ret += c123; } std::cout << ret << "\n"; } |