#include <cstdio> #include <cstdint> #include <vector> #include <map> using namespace std; const int MAX = 500 * 20016 * 4; int main () { int n; scanf("%d", &n); vector<int> ucb; for (int i = 0; i < n; ++i) { int x; scanf("%d", &x); ucb.push_back(x); } vector<int> P; P.push_back(0); for (int i = 0; i < n; ++i) { P.push_back(P[i] + ucb[i]); } vector<int64_t> counts(2 * MAX, 0); int64_t res = 0; for (int c = 0; c <= n; ++c) { for (int b = 0; b <= n; ++b) { for (int a = b + 1; a <= n; ++a) { res += counts[P[a] - P[b] + P[c] + MAX]; } } for (int b = 0; b <= n; ++b) { for (int a = b + 1; a <= n; ++a) { counts[P[b] - P[a] + P[c] + MAX]++; } } } map<int, int64_t> intervals; for (int i = 0; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { int sum = P[j] - P[i]; if (intervals.find(sum) == intervals.end()) { intervals[sum] = 1; } else { ++intervals[sum]; } } } for (int i = 0; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { int sum = 2 * (P[j] - P[i]); map<int, int64_t>::iterator it = intervals.find(-sum); if (it != intervals.end()) { res -= 3 * (it->second - (sum ? 0 : 1)); } } } res -= intervals[0]; printf("%lld\n", res / 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 54 55 56 57 58 59 60 61 62 | #include <cstdio> #include <cstdint> #include <vector> #include <map> using namespace std; const int MAX = 500 * 20016 * 4; int main () { int n; scanf("%d", &n); vector<int> ucb; for (int i = 0; i < n; ++i) { int x; scanf("%d", &x); ucb.push_back(x); } vector<int> P; P.push_back(0); for (int i = 0; i < n; ++i) { P.push_back(P[i] + ucb[i]); } vector<int64_t> counts(2 * MAX, 0); int64_t res = 0; for (int c = 0; c <= n; ++c) { for (int b = 0; b <= n; ++b) { for (int a = b + 1; a <= n; ++a) { res += counts[P[a] - P[b] + P[c] + MAX]; } } for (int b = 0; b <= n; ++b) { for (int a = b + 1; a <= n; ++a) { counts[P[b] - P[a] + P[c] + MAX]++; } } } map<int, int64_t> intervals; for (int i = 0; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { int sum = P[j] - P[i]; if (intervals.find(sum) == intervals.end()) { intervals[sum] = 1; } else { ++intervals[sum]; } } } for (int i = 0; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { int sum = 2 * (P[j] - P[i]); map<int, int64_t>::iterator it = intervals.find(-sum); if (it != intervals.end()) { res -= 3 * (it->second - (sum ? 0 : 1)); } } } res -= intervals[0]; printf("%lld\n", res / 6); return 0; } |