// PA 2024 5C - Bardzo ulubiony ciąg #include <iostream> #include <string> #include <vector> #include <algorithm> #include <stack> #include <unordered_map> using namespace std; int main() { ios_base::sync_with_stdio(0); int n, a, currSum; long long result; vector<int> prefixSum; unordered_map<int, int> availVals; unordered_map<int, int>::iterator it; prefixSum.push_back(0); currSum = 0; result = 0; cin >> n; for (int i = 0; i < n; ++i) { cin >> a; currSum += a; prefixSum.push_back(currSum); } for (int i1 = 1; i1 <= n; ++i1) { for (int j1 = i1; j1 <= n; ++j1) { availVals.clear(); for (int i2 = i1; i2 <= n; ++i2) { for (int j2 = (i2 == i1) ? j1 + 1 : i2; j2 <= n; ++j2) { it = availVals.find(- prefixSum[j1] + prefixSum[i1 - 1] - prefixSum[j2] + prefixSum[i2 - 1]); if (it != availVals.end()) { result += it->second; } if (availVals.find(prefixSum[j2] - prefixSum[i2 - 1]) == availVals.end()) { availVals.insert({prefixSum[j2] - prefixSum[i2 - 1], 1}); } else { ++availVals[prefixSum[j2] - prefixSum[i2 - 1]]; } } } } } cout << 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 | // PA 2024 5C - Bardzo ulubiony ciąg #include <iostream> #include <string> #include <vector> #include <algorithm> #include <stack> #include <unordered_map> using namespace std; int main() { ios_base::sync_with_stdio(0); int n, a, currSum; long long result; vector<int> prefixSum; unordered_map<int, int> availVals; unordered_map<int, int>::iterator it; prefixSum.push_back(0); currSum = 0; result = 0; cin >> n; for (int i = 0; i < n; ++i) { cin >> a; currSum += a; prefixSum.push_back(currSum); } for (int i1 = 1; i1 <= n; ++i1) { for (int j1 = i1; j1 <= n; ++j1) { availVals.clear(); for (int i2 = i1; i2 <= n; ++i2) { for (int j2 = (i2 == i1) ? j1 + 1 : i2; j2 <= n; ++j2) { it = availVals.find(- prefixSum[j1] + prefixSum[i1 - 1] - prefixSum[j2] + prefixSum[i2 - 1]); if (it != availVals.end()) { result += it->second; } if (availVals.find(prefixSum[j2] - prefixSum[i2 - 1]) == availVals.end()) { availVals.insert({prefixSum[j2] - prefixSum[i2 - 1], 1}); } else { ++availVals[prefixSum[j2] - prefixSum[i2 - 1]]; } } } } } cout << result; return 0; } |