#include <bits/stdc++.h> #define ll long long const int MAX_A = 4e7; ll sum2[MAX_A + 10]; ll cnt_j[MAX_A + 10]; const int MID = 2e7 + 3; void make_unique(std::vector<int> &v) { std::sort(v.begin(), v.end()); v.erase(std::unique(v.begin(), v.end()), v.end()); } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); int n; std::cin >> n; std::vector<int> w(n + 1); for (int i = 1; i <= n; i++) std::cin >> w[i]; std::vector<int> p(n + 1, 0); for (int i = 1; i <= n; i++) p[i] = p[i - 1] + w[i]; // std::unordered_map<int, int> sum2; std::vector<int> sum2vals; for (int i = 0; i <= n - 1; i++) for (int j = i + 1; j <= n; j++) { sum2[p[j] - p[i] + MID]++; sum2vals.push_back(p[j] - p[i]); } make_unique(sum2vals); ll res = 0; ll v; for (int i = n - 1; i >= 0; i--) { for (auto &k : sum2vals) cnt_j[k + p[i + 1] + MID] += sum2[k + MID]; for (auto &k : sum2vals) { v = sum2[k + MID]; if (v > 0) res += v * cnt_j[p[i] - k + MID]; } } res -= sum2[0 + MID]; for (auto k : sum2vals) { v = sum2[k + MID]; if (k % 2 == 0) { if (k != 0) { res -= 3ll * v * sum2[(-k / 2) + MID]; } else if (k == 0 && v > 0) res -= 3ll * v * (v - 1); } } std::cout << res / 6ll << "\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 | #include <bits/stdc++.h> #define ll long long const int MAX_A = 4e7; ll sum2[MAX_A + 10]; ll cnt_j[MAX_A + 10]; const int MID = 2e7 + 3; void make_unique(std::vector<int> &v) { std::sort(v.begin(), v.end()); v.erase(std::unique(v.begin(), v.end()), v.end()); } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); int n; std::cin >> n; std::vector<int> w(n + 1); for (int i = 1; i <= n; i++) std::cin >> w[i]; std::vector<int> p(n + 1, 0); for (int i = 1; i <= n; i++) p[i] = p[i - 1] + w[i]; // std::unordered_map<int, int> sum2; std::vector<int> sum2vals; for (int i = 0; i <= n - 1; i++) for (int j = i + 1; j <= n; j++) { sum2[p[j] - p[i] + MID]++; sum2vals.push_back(p[j] - p[i]); } make_unique(sum2vals); ll res = 0; ll v; for (int i = n - 1; i >= 0; i--) { for (auto &k : sum2vals) cnt_j[k + p[i + 1] + MID] += sum2[k + MID]; for (auto &k : sum2vals) { v = sum2[k + MID]; if (v > 0) res += v * cnt_j[p[i] - k + MID]; } } res -= sum2[0 + MID]; for (auto k : sum2vals) { v = sum2[k + MID]; if (k % 2 == 0) { if (k != 0) { res -= 3ll * v * sum2[(-k / 2) + MID]; } else if (k == 0 && v > 0) res -= 3ll * v * (v - 1); } } std::cout << res / 6ll << "\n"; } |