#include <cstdio> #include <vector> #include <map> #include <set> #include <queue> #include <unordered_map> #include <unordered_set> #include <algorithm> using namespace std; int nextInt() { int n; scanf("%d", &n); return n; } const int N = 500; const int NN = N * (N + 1) / 2; const int V = 1000000; int a[N]; int cnt[V + V + 1]; int values[NN]; int vcnt; int main() { //if (0) { int n = nextInt(); for (int i = 0; i < n; ++i) a[i] = nextInt(); int nn = 0; for (int i = 0; i < n; ++i) { int s = 0; for (int j = i; j < n; ++j) { s += a[j]; int& count = cnt[V + s]; if (0 == count) values[vcnt++] = s; ++count; } } } if (0) { int test = 400; int test2 = test * (test + 1) / 2; for (int i = 0; i < test2; ++i) { int v = i - test2 / 2; values[i] = v; cnt[V + v] = 1; } vcnt = test2; } long long res = 0; if (cnt[V] >= 3) res = 1LL * cnt[V] * (cnt[V] - 1) * (cnt[V] - 2); for (int i = 0; i < vcnt; ++i) { int v = values[i]; if (cnt[V + v] < 2) continue; if (v == 0) continue; if (v + v > V) continue; if (v + v < -V) continue; res += 3LL * cnt[V + v] * (cnt[V + v] - 1) * cnt[V - v - v]; } for (int i = 0; i < vcnt; ++i) { int vi = values[i]; for (int j = i + 1; j < vcnt; ++j) { int vj = values[j]; int vk = 0 - vi - vj; if (vk != vi && vk != vj) res += 2LL * cnt[V + vi] * cnt[V + vj] * cnt[V + vk]; } } res /= 6; printf("%lld\n", res); 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 80 81 82 83 84 85 86 | #include <cstdio> #include <vector> #include <map> #include <set> #include <queue> #include <unordered_map> #include <unordered_set> #include <algorithm> using namespace std; int nextInt() { int n; scanf("%d", &n); return n; } const int N = 500; const int NN = N * (N + 1) / 2; const int V = 1000000; int a[N]; int cnt[V + V + 1]; int values[NN]; int vcnt; int main() { //if (0) { int n = nextInt(); for (int i = 0; i < n; ++i) a[i] = nextInt(); int nn = 0; for (int i = 0; i < n; ++i) { int s = 0; for (int j = i; j < n; ++j) { s += a[j]; int& count = cnt[V + s]; if (0 == count) values[vcnt++] = s; ++count; } } } if (0) { int test = 400; int test2 = test * (test + 1) / 2; for (int i = 0; i < test2; ++i) { int v = i - test2 / 2; values[i] = v; cnt[V + v] = 1; } vcnt = test2; } long long res = 0; if (cnt[V] >= 3) res = 1LL * cnt[V] * (cnt[V] - 1) * (cnt[V] - 2); for (int i = 0; i < vcnt; ++i) { int v = values[i]; if (cnt[V + v] < 2) continue; if (v == 0) continue; if (v + v > V) continue; if (v + v < -V) continue; res += 3LL * cnt[V + v] * (cnt[V + v] - 1) * cnt[V - v - v]; } for (int i = 0; i < vcnt; ++i) { int vi = values[i]; for (int j = i + 1; j < vcnt; ++j) { int vj = values[j]; int vk = 0 - vi - vj; if (vk != vi && vk != vj) res += 2LL * cnt[V + vi] * cnt[V + vj] * cnt[V + vk]; } } res /= 6; printf("%lld\n", res); return 0; } |