#include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef LOCAL #include "debug.h" #else #define debug(...) 2137 #endif struct mp { array<int, 60000010> a, lst; int cnt = 0; int& operator[](int x) { x += 30000000; if (lst[x] != cnt) { a[x] = 0; } lst[x] = cnt; return a[x]; } void clear() { cnt++; } }; const int N = 505; int n; int s[N]; int m = 0; mp cc, c;//, cj; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; //n = 500; mt19937 rng(2137); for (int i = 0; i < n; i++) { int x; cin >> x; //x = true ? 20000 : -20000;//int(rng() % 40001) - 20000; s[i + 1] = s[i] + x; } ll ans = 0; for (int i = 0; i <= n; i++) { for (int j = i + 1; j <= n; j++) { cc[s[j] - s[i]]++; } } for (int i = 0; i <= n; i++) { c.clear(); for (int j = 0; j < i; j++) { for (int k = j + 1; k < i; k++) { int x = s[i] - s[j]; int y = s[i] - s[k]; int z = -(x + y); ans += c[s[i] - z]; } c[s[j]]++; } } c.clear(); for (int i = 0; i <= n; i++) { //cj.clear(); for (int j = 0; j < i; j++) { for (int k = j + 1; k < i; k++) { int x = s[i] - s[j]; int y = s[i] - s[k]; int z = -(x + y); // 3 takie same //ans += cj[s[i] - z]; // 2 takie same ans += cc[z] - c[s[i] - z]; } //cj[s[j]]++; } c[s[i]]++; } c.clear(); for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { for (int k = 0; k <= i; k++) { c[-s[j] + s[i + 1] - s[k]]--; } } for (int j = 0; j < n; j++) { for (int k = max(j, i + 1); k < n; k++) { c[-s[i] + s[k + 1] - s[j]]++; } } for (int j = 0; j < i; j++) { for (int k = j; k < i; k++) { ans += c[-(s[i + 1] + s[k + 1] - s[j])]; } } } cout << ans << '\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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | #include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef LOCAL #include "debug.h" #else #define debug(...) 2137 #endif struct mp { array<int, 60000010> a, lst; int cnt = 0; int& operator[](int x) { x += 30000000; if (lst[x] != cnt) { a[x] = 0; } lst[x] = cnt; return a[x]; } void clear() { cnt++; } }; const int N = 505; int n; int s[N]; int m = 0; mp cc, c;//, cj; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; //n = 500; mt19937 rng(2137); for (int i = 0; i < n; i++) { int x; cin >> x; //x = true ? 20000 : -20000;//int(rng() % 40001) - 20000; s[i + 1] = s[i] + x; } ll ans = 0; for (int i = 0; i <= n; i++) { for (int j = i + 1; j <= n; j++) { cc[s[j] - s[i]]++; } } for (int i = 0; i <= n; i++) { c.clear(); for (int j = 0; j < i; j++) { for (int k = j + 1; k < i; k++) { int x = s[i] - s[j]; int y = s[i] - s[k]; int z = -(x + y); ans += c[s[i] - z]; } c[s[j]]++; } } c.clear(); for (int i = 0; i <= n; i++) { //cj.clear(); for (int j = 0; j < i; j++) { for (int k = j + 1; k < i; k++) { int x = s[i] - s[j]; int y = s[i] - s[k]; int z = -(x + y); // 3 takie same //ans += cj[s[i] - z]; // 2 takie same ans += cc[z] - c[s[i] - z]; } //cj[s[j]]++; } c[s[i]]++; } c.clear(); for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { for (int k = 0; k <= i; k++) { c[-s[j] + s[i + 1] - s[k]]--; } } for (int j = 0; j < n; j++) { for (int k = max(j, i + 1); k < n; k++) { c[-s[i] + s[k + 1] - s[j]]++; } } for (int j = 0; j < i; j++) { for (int k = j; k < i; k++) { ans += c[-(s[i + 1] + s[k + 1] - s[j])]; } } } cout << ans << '\n'; } |