#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned int ui; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<long long> vll; const ll LINF = 1e18; const int INF = 1e9; int cnt[6 * 500 * 20000 + 16]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vi a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } vi prefix_sums(n + 1); prefix_sums[0] = 0; for (int i = 0; i < n; ++i) { prefix_sums[i + 1] = prefix_sums[i] + a[i]; } ll result = 0; // unordered_map<int, int> cnt; int shift = 3 * 500 * 20000 + 8; for (int j = 0; j <= n; ++j) { for (int i1 = 0; i1 <= n; ++i1) { for (int i2 = i1 + 1; i2 <= n; ++i2) { int sum_i = prefix_sums[i2] - prefix_sums[i1]; int sum = sum_i + prefix_sums[j]; result += cnt[sum + shift]; } } for (int i1 = 0; i1 <= n; ++i1) { for (int i2 = i1 + 1; i2 <= n; ++i2) { int sum_i = prefix_sums[i2] - prefix_sums[i1]; ++cnt[prefix_sums[j] - sum_i + shift]; // cerr << "S_i=" << sum_i << " j,i1,i2=" << j << "," << i1 << "," << i2 << " S_j=" << prefix_sums[j] << ' ' << sum << ' ' << prefix_sums[j] - sum_i << " CNT=" << cnt[sum] << endl; } } } // cerr << result << endl; unordered_map<int, int> dupes_counter; ll cnt_dupes = 0; ll cnt_zeros = 0; for (int i = 0; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { int sum = prefix_sums[j] - prefix_sums[i]; if (sum == 0) { ++cnt_zeros; } ++dupes_counter[2 * sum]; } } for (int i = 0; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { int sum = prefix_sums[j] - prefix_sums[i]; cnt_dupes += dupes_counter[-sum]; } } // Cerr << dupes_counter[0] << endl; // cerr << result << endl; // cerr << cnt_dupes << endl; cout << (result - 3 * (cnt_dupes - cnt_zeros) - cnt_zeros) / 6 << endl; 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned int ui; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<long long> vll; const ll LINF = 1e18; const int INF = 1e9; int cnt[6 * 500 * 20000 + 16]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vi a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } vi prefix_sums(n + 1); prefix_sums[0] = 0; for (int i = 0; i < n; ++i) { prefix_sums[i + 1] = prefix_sums[i] + a[i]; } ll result = 0; // unordered_map<int, int> cnt; int shift = 3 * 500 * 20000 + 8; for (int j = 0; j <= n; ++j) { for (int i1 = 0; i1 <= n; ++i1) { for (int i2 = i1 + 1; i2 <= n; ++i2) { int sum_i = prefix_sums[i2] - prefix_sums[i1]; int sum = sum_i + prefix_sums[j]; result += cnt[sum + shift]; } } for (int i1 = 0; i1 <= n; ++i1) { for (int i2 = i1 + 1; i2 <= n; ++i2) { int sum_i = prefix_sums[i2] - prefix_sums[i1]; ++cnt[prefix_sums[j] - sum_i + shift]; // cerr << "S_i=" << sum_i << " j,i1,i2=" << j << "," << i1 << "," << i2 << " S_j=" << prefix_sums[j] << ' ' << sum << ' ' << prefix_sums[j] - sum_i << " CNT=" << cnt[sum] << endl; } } } // cerr << result << endl; unordered_map<int, int> dupes_counter; ll cnt_dupes = 0; ll cnt_zeros = 0; for (int i = 0; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { int sum = prefix_sums[j] - prefix_sums[i]; if (sum == 0) { ++cnt_zeros; } ++dupes_counter[2 * sum]; } } for (int i = 0; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { int sum = prefix_sums[j] - prefix_sums[i]; cnt_dupes += dupes_counter[-sum]; } } // Cerr << dupes_counter[0] << endl; // cerr << result << endl; // cerr << cnt_dupes << endl; cout << (result - 3 * (cnt_dupes - cnt_zeros) - cnt_zeros) / 6 << endl; return 0; } |