#include <bits/stdc++.h> using namespace std; vector <int> a{0}, pref{0}; map <int, int > m; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; a.push_back(x); pref.push_back(pref[i] + x); } for (int i = 1; i <= n; i++) { int sum = 0; for (int j = i; j <= n; j++) { sum += a[j]; m[sum]++; } } long long ile = 0; for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) { for (int k = 1; k <= n; k++) { for (int l = k; l <= n; l++) { if (i == k && j == l) continue; // case normal int s1 = pref[j] - pref[i-1], s2 = pref[l] - pref[k-1]; int s = s1 + s2; auto v = m[-1 * s]; ile += v; if (s1 == -1 * s) ile--; if (s2 == -1 * s) ile--; } } } } cout << ile / 6; 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 | #include <bits/stdc++.h> using namespace std; vector <int> a{0}, pref{0}; map <int, int > m; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; a.push_back(x); pref.push_back(pref[i] + x); } for (int i = 1; i <= n; i++) { int sum = 0; for (int j = i; j <= n; j++) { sum += a[j]; m[sum]++; } } long long ile = 0; for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) { for (int k = 1; k <= n; k++) { for (int l = k; l <= n; l++) { if (i == k && j == l) continue; // case normal int s1 = pref[j] - pref[i-1], s2 = pref[l] - pref[k-1]; int s = s1 + s2; auto v = m[-1 * s]; ile += v; if (s1 == -1 * s) ile--; if (s2 == -1 * s) ile--; } } } } cout << ile / 6; return 0; } |