#include <bits/stdc++.h> long long s3(long long n) { return n * (n - 1) * (n - 2) / 6; } long long s2(long long n) { return n * (n - 1) / 2; } std::map<int, long long> allNums; constexpr int MAX_N = 503; int T[MAX_N]; int prefix[MAX_N]; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); long long n; std::cin >> n; for (int i = 1; i <= n; ++i) { std::cin >> T[i]; prefix[i] = prefix[i - 1] + T[i]; } for (int i = 1; i <= n; ++i) { for (int j = i; j <= n; ++j) { auto value = prefix[j] - prefix[i - 1]; // std::cout << value << '\n'; if (allNums.find(value) == allNums.end()) { allNums[value] = 1; } else { allNums[value]++; } } } // for (auto & [key, value] : allNums) { // std::cout << key << " " << value << '\n'; // } long long output = 0; int count = 0; for (auto it = allNums.begin(); it != allNums.end(); ++it) { // 3x = 0 if (it->first == 0) { output += s3(it->second); } // 2x + y = 0 or x + 2y = 0 if (count + 1 >= allNums.size()) { continue; } for (auto jt = it; jt != allNums.end(); ++jt) { if (jt == it) { continue; } if (it->first * 2 + jt->first == 0) { output += s2(it->second) * jt->second; } else if (it->first + 2 * jt->first == 0) { output += s2(jt->second) * it->second; } } // x + y + z = 0 if (count + 2 >= allNums.size()) { continue; } for (auto jt = allNums.begin(); jt != allNums.end(); ++jt) { if (allNums.find(-(it->first + jt->first)) == allNums.end()) { continue; } auto v1 = it->first; auto v2 = jt->first; auto v3 = -(v1 + v2); if (v1 == v2 || v2 == v3 || v1 == v3) { continue; } if (v1 < v2 && v2 < v3) { output += it->second * jt->second * allNums[-(v1 + v2)]; } } ++count; } std::cout << output; 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 | #include <bits/stdc++.h> long long s3(long long n) { return n * (n - 1) * (n - 2) / 6; } long long s2(long long n) { return n * (n - 1) / 2; } std::map<int, long long> allNums; constexpr int MAX_N = 503; int T[MAX_N]; int prefix[MAX_N]; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); long long n; std::cin >> n; for (int i = 1; i <= n; ++i) { std::cin >> T[i]; prefix[i] = prefix[i - 1] + T[i]; } for (int i = 1; i <= n; ++i) { for (int j = i; j <= n; ++j) { auto value = prefix[j] - prefix[i - 1]; // std::cout << value << '\n'; if (allNums.find(value) == allNums.end()) { allNums[value] = 1; } else { allNums[value]++; } } } // for (auto & [key, value] : allNums) { // std::cout << key << " " << value << '\n'; // } long long output = 0; int count = 0; for (auto it = allNums.begin(); it != allNums.end(); ++it) { // 3x = 0 if (it->first == 0) { output += s3(it->second); } // 2x + y = 0 or x + 2y = 0 if (count + 1 >= allNums.size()) { continue; } for (auto jt = it; jt != allNums.end(); ++jt) { if (jt == it) { continue; } if (it->first * 2 + jt->first == 0) { output += s2(it->second) * jt->second; } else if (it->first + 2 * jt->first == 0) { output += s2(jt->second) * it->second; } } // x + y + z = 0 if (count + 2 >= allNums.size()) { continue; } for (auto jt = allNums.begin(); jt != allNums.end(); ++jt) { if (allNums.find(-(it->first + jt->first)) == allNums.end()) { continue; } auto v1 = it->first; auto v2 = jt->first; auto v3 = -(v1 + v2); if (v1 == v2 || v2 == v3 || v1 == v3) { continue; } if (v1 < v2 && v2 < v3) { output += it->second * jt->second * allNums[-(v1 + v2)]; } } ++count; } std::cout << output; return 0; } |