#include <cstdio> #include <vector> #include <unordered_set> #include <map> #include <algorithm> using namespace std; int main() { int n; vector<long long> sums; scanf("%d", &n); vector<int> a(n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int i = 0; i < a.size(); i++) { long long sum = a[i]; sums.push_back(sum); for (int j = i + 1; j < a.size(); j++) { sum += a[j]; sums.push_back(sum); } } unsigned long long result = 0; for (int i = 0; i < sums.size(); i++) { map<long long, long long> seen; for (int j = i + 1; j < sums.size(); j++) { if (seen.contains(-(sums[i] + sums[j]))) { result += seen[-(sums[i] + sums[j])]; } if (seen.contains(sums[j])) { seen[sums[j]]++; } else { seen[sums[j]] = 1; } } } printf("%llu\n", result); 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 | #include <cstdio> #include <vector> #include <unordered_set> #include <map> #include <algorithm> using namespace std; int main() { int n; vector<long long> sums; scanf("%d", &n); vector<int> a(n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int i = 0; i < a.size(); i++) { long long sum = a[i]; sums.push_back(sum); for (int j = i + 1; j < a.size(); j++) { sum += a[j]; sums.push_back(sum); } } unsigned long long result = 0; for (int i = 0; i < sums.size(); i++) { map<long long, long long> seen; for (int j = i + 1; j < sums.size(); j++) { if (seen.contains(-(sums[i] + sums[j]))) { result += seen[-(sums[i] + sums[j])]; } if (seen.contains(sums[j])) { seen[sums[j]]++; } else { seen[sums[j]] = 1; } } } printf("%llu\n", result); return 0; } |