//GRT_2018 #include <bits/stdc++.h> #define PB push_back #define ST first #define ND second #define all(x) x.begin(), x.end() //mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); using namespace std; using ll = long long; using pi = pair<int,int>; using vi = vector<int>; const int nax = 510, qax = 30 * 1000 * 1000 + 10; int n; int s[nax]; int ptrr[qax]; map<int, ll>c3; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; s[0] = 0; for (int i = 1; i <= n; ++i) { cin >> s[i]; s[i] += s[i - 1]; } int mi = 0; for (int k2 = 0; k2 <= n; ++k2) { for (int i2 = 0; i2 <= n; ++i2) { for (int i1 = 0; i1 < i2; ++i1) { int sum = s[i2] - s[i1] + s[k2]; mi = min(mi, sum); int sum2 = s[i1] - s[i2] + s[k2]; mi = min(mi, sum2); } } } ll ans = 0; for (int k2 = 0; k2 <= n; ++k2) { for (int i2 = 0; i2 <= n; ++i2) { for (int i1 = 0; i1 < i2; ++i1) { int sum = s[i2] - s[i1] + s[k2]; ans += ptrr[sum-mi]; } } for (int j1 = 0; j1 <= n; ++j1) { for (int j2 = j1 + 1; j2 <= n; ++j2) { int sum = s[j1] - s[j2] + s[k2]; ptrr[sum-mi]++; } } } /* for (int i = 0; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { int sum = s[j] - s[i]; ans += c[-sum]; } }*/ // 2 * tamten + ja = 0 for (int i = 0; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { c3[2 * (s[j] - s[i])]++; } } for (int i = 0; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { int sum = s[j] - s[i]; ans -= 3LL * c3[-sum]; } } for (int i = 0; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { if (s[j] - s[i] == 0) ans += 2; } } cout << ans / 6; }
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 | //GRT_2018 #include <bits/stdc++.h> #define PB push_back #define ST first #define ND second #define all(x) x.begin(), x.end() //mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); using namespace std; using ll = long long; using pi = pair<int,int>; using vi = vector<int>; const int nax = 510, qax = 30 * 1000 * 1000 + 10; int n; int s[nax]; int ptrr[qax]; map<int, ll>c3; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; s[0] = 0; for (int i = 1; i <= n; ++i) { cin >> s[i]; s[i] += s[i - 1]; } int mi = 0; for (int k2 = 0; k2 <= n; ++k2) { for (int i2 = 0; i2 <= n; ++i2) { for (int i1 = 0; i1 < i2; ++i1) { int sum = s[i2] - s[i1] + s[k2]; mi = min(mi, sum); int sum2 = s[i1] - s[i2] + s[k2]; mi = min(mi, sum2); } } } ll ans = 0; for (int k2 = 0; k2 <= n; ++k2) { for (int i2 = 0; i2 <= n; ++i2) { for (int i1 = 0; i1 < i2; ++i1) { int sum = s[i2] - s[i1] + s[k2]; ans += ptrr[sum-mi]; } } for (int j1 = 0; j1 <= n; ++j1) { for (int j2 = j1 + 1; j2 <= n; ++j2) { int sum = s[j1] - s[j2] + s[k2]; ptrr[sum-mi]++; } } } /* for (int i = 0; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { int sum = s[j] - s[i]; ans += c[-sum]; } }*/ // 2 * tamten + ja = 0 for (int i = 0; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { c3[2 * (s[j] - s[i])]++; } } for (int i = 0; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { int sum = s[j] - s[i]; ans -= 3LL * c3[-sum]; } } for (int i = 0; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { if (s[j] - s[i] == 0) ans += 2; } } cout << ans / 6; } |