#include <bits/stdc++.h> using namespace std; const long long SUM = 20'000 * 500 * 3 + 999; int main() { int n; cin >> n; vector<long long> t(n); vector<long long> p(n + 1, 0); vector<long long> sums(2 * SUM, 0); for (int i = 0; i < n; i++) { cin >> t[i]; p[i + 1] = t[i] + p[i]; } // unordered_map<long long, long long> m; long long ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = j + 1; k < n + 1; k++) { // m[-((p[k] - p[j]) - p[i])]++; sums[-((p[k] - p[j]) - p[i]) + SUM]++; } } for (int j = 0; j < n; j++) { for (int k = j + 1; k < n + 1; k++) { // ans += m[(p[i + 1] + (p[k] - p[j]))]; ans += sums[(p[i + 1] + (p[k] - p[j])) + SUM]; } } } std::fill(sums.begin(), sums.end(), 0); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n + 1; j++) { sums[p[j] - p[i] + SUM]++; } } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n + 1; j++) { if (abs(p[j] - p[i]) > SUM - 99) continue; if ((p[j] - p[i]) != 0 && sums[-2 * (p[j] - p[i]) + SUM]){ ans -= sums[-2 * (p[j] - p[i]) + SUM] * 3 ; } else if (p[j] - p[i] == 0) { ans -= (sums[-2 * (p[j] - p[i]) + SUM] - 1) * 3; ans--; } } } cout << ans / 6 << "\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 | #include <bits/stdc++.h> using namespace std; const long long SUM = 20'000 * 500 * 3 + 999; int main() { int n; cin >> n; vector<long long> t(n); vector<long long> p(n + 1, 0); vector<long long> sums(2 * SUM, 0); for (int i = 0; i < n; i++) { cin >> t[i]; p[i + 1] = t[i] + p[i]; } // unordered_map<long long, long long> m; long long ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = j + 1; k < n + 1; k++) { // m[-((p[k] - p[j]) - p[i])]++; sums[-((p[k] - p[j]) - p[i]) + SUM]++; } } for (int j = 0; j < n; j++) { for (int k = j + 1; k < n + 1; k++) { // ans += m[(p[i + 1] + (p[k] - p[j]))]; ans += sums[(p[i + 1] + (p[k] - p[j])) + SUM]; } } } std::fill(sums.begin(), sums.end(), 0); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n + 1; j++) { sums[p[j] - p[i] + SUM]++; } } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n + 1; j++) { if (abs(p[j] - p[i]) > SUM - 99) continue; if ((p[j] - p[i]) != 0 && sums[-2 * (p[j] - p[i]) + SUM]){ ans -= sums[-2 * (p[j] - p[i]) + SUM] * 3 ; } else if (p[j] - p[i] == 0) { ans -= (sums[-2 * (p[j] - p[i]) + SUM] - 1) * 3; ans--; } } } cout << ans / 6 << "\n"; } |