#include <iostream> #include <unordered_map> #include <vector> int main() { int n; std::cin >> n; std::vector< int > uc(n); std::vector< int > buc; std::unordered_map< int, int > trzecia; for (int i = 0; i < n; ++i) { std::cin >> uc[i]; } for (int i = 0; i < n; ++i) { int suma = 0; for (int k = i; k < n; ++k) { suma += uc[k]; buc.push_back(suma); trzecia[suma] += 1; } } int wynik = 0; for (int i = 0; i < buc.size(); ++i) { for (int j = i + 1; j < buc.size(); ++j) { int suma = buc[i] + buc[j]; auto it = trzecia.find(-suma); if (it == trzecia.end()) continue; int delta = it->second; if (it->first == buc[i]) { delta -= 1; }; if (it->first == buc[j]) { delta -= 1; }; wynik += delta; } } std::cout << (wynik/3) << "\n";; }
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 | #include <iostream> #include <unordered_map> #include <vector> int main() { int n; std::cin >> n; std::vector< int > uc(n); std::vector< int > buc; std::unordered_map< int, int > trzecia; for (int i = 0; i < n; ++i) { std::cin >> uc[i]; } for (int i = 0; i < n; ++i) { int suma = 0; for (int k = i; k < n; ++k) { suma += uc[k]; buc.push_back(suma); trzecia[suma] += 1; } } int wynik = 0; for (int i = 0; i < buc.size(); ++i) { for (int j = i + 1; j < buc.size(); ++j) { int suma = buc[i] + buc[j]; auto it = trzecia.find(-suma); if (it == trzecia.end()) continue; int delta = it->second; if (it->first == buc[i]) { delta -= 1; }; if (it->first == buc[j]) { delta -= 1; }; wynik += delta; } } std::cout << (wynik/3) << "\n";; } |