#include <cstdio> #include <map> using namespace std; int a[500]; map<int,int> b; int main() { int n; long r = 0; scanf("%i", &n); for (int i = 0; i<n; i++) scanf("%i", &a[i]); for (int i = 0; i<n; i++) for (int j = i, s = 0; j<n; j++) s += a[j], b[s]++; const int m = b.crbegin()->first; for (auto i = b.cbegin(); i!=b.cend(); ++i) { const int x = i->first; const long u = i->second; for (auto j = b.equal_range(max(x, -x-m)).first; j!=b.cend(); ++j) { const int y = j->first, z = -x-y; if (y>z) break; const auto k = b.find(z); if (k == b.cend()) continue; const long v = j->second, w = k->second; if (x==z) r += u*(v-1)*(w-2)/6; else if (x==y) r += u*(v-1)/2*w; else if (y==z) r += u*v*(w-1)/2; else r += u*v*w; } } printf("%li\n", r); 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 | #include <cstdio> #include <map> using namespace std; int a[500]; map<int,int> b; int main() { int n; long r = 0; scanf("%i", &n); for (int i = 0; i<n; i++) scanf("%i", &a[i]); for (int i = 0; i<n; i++) for (int j = i, s = 0; j<n; j++) s += a[j], b[s]++; const int m = b.crbegin()->first; for (auto i = b.cbegin(); i!=b.cend(); ++i) { const int x = i->first; const long u = i->second; for (auto j = b.equal_range(max(x, -x-m)).first; j!=b.cend(); ++j) { const int y = j->first, z = -x-y; if (y>z) break; const auto k = b.find(z); if (k == b.cend()) continue; const long v = j->second, w = k->second; if (x==z) r += u*(v-1)*(w-2)/6; else if (x==y) r += u*(v-1)/2*w; else if (y==z) r += u*v*(w-1)/2; else r += u*v*w; } } printf("%li\n", r); return 0; } |