#include <cstdio> #include <map> #include <set> #include <unordered_map> #include <unordered_set> typedef long long int i64; int tab[505]; std::set<std::pair<int, int>> set; std::unordered_map<int, int> map; std::set<std::pair<int, int>> used; inline void inc(std::unordered_map<int, int> &m, int k) { if (!m.contains(k)) { m[k] = 1; } else { ++m[k]; } } inline int or_zero(std::unordered_map<int, int> &m, int k) { return m.contains(k) ? m[k] : 0; } int main() { int N; scanf("%d", &N); for (int i = 0; i < N; ++i) { scanf("%d", &tab[i]); inc(map, tab[i]); } for (int i = 0; i < N; ++i) { int s = tab[i]; for (int j = i + 1; j < N; ++j) { s += tab[j]; inc(map, s); } } for (auto &p : map) { set.insert(p); } // for (auto &p : set) { // printf("[%d, %d] ", p.first, p.second); // } i64 res = 0; for (auto it = set.begin(); it != set.end() && it->first <= 0; ++it) { for (auto jt = it; jt != set.end(); ++jt) { if (jt == it) { if (it->first == 0) { if (it->second >= 3) { i64 n = it->second; res += n * (n - 1) * (n - 2) / 6; } } else if (it->second > 1) { i64 self = ((i64)it->second) * (it->second - 1) / 2; res += self * or_zero(map, -2 * it->first); } } else { int sum = it->first + jt->first; if (-sum < jt->first ) { break; } if (-sum != jt->first) { i64 them = ((i64)it->second) * jt->second; res += them * or_zero(map, -sum); } else { res += ((i64)it->second) * (jt->second) * (jt->second - 1) / 2; } } } } printf("%lld\n", res); 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 | #include <cstdio> #include <map> #include <set> #include <unordered_map> #include <unordered_set> typedef long long int i64; int tab[505]; std::set<std::pair<int, int>> set; std::unordered_map<int, int> map; std::set<std::pair<int, int>> used; inline void inc(std::unordered_map<int, int> &m, int k) { if (!m.contains(k)) { m[k] = 1; } else { ++m[k]; } } inline int or_zero(std::unordered_map<int, int> &m, int k) { return m.contains(k) ? m[k] : 0; } int main() { int N; scanf("%d", &N); for (int i = 0; i < N; ++i) { scanf("%d", &tab[i]); inc(map, tab[i]); } for (int i = 0; i < N; ++i) { int s = tab[i]; for (int j = i + 1; j < N; ++j) { s += tab[j]; inc(map, s); } } for (auto &p : map) { set.insert(p); } // for (auto &p : set) { // printf("[%d, %d] ", p.first, p.second); // } i64 res = 0; for (auto it = set.begin(); it != set.end() && it->first <= 0; ++it) { for (auto jt = it; jt != set.end(); ++jt) { if (jt == it) { if (it->first == 0) { if (it->second >= 3) { i64 n = it->second; res += n * (n - 1) * (n - 2) / 6; } } else if (it->second > 1) { i64 self = ((i64)it->second) * (it->second - 1) / 2; res += self * or_zero(map, -2 * it->first); } } else { int sum = it->first + jt->first; if (-sum < jt->first ) { break; } if (-sum != jt->first) { i64 them = ((i64)it->second) * jt->second; res += them * or_zero(map, -sum); } else { res += ((i64)it->second) * (jt->second) * (jt->second - 1) / 2; } } } } printf("%lld\n", res); return 0; } |