#include <vector> #include <unordered_map> #include <iostream> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n; std::cin >> n; std::vector<int> inputNumbers; inputNumbers.reserve(n); for (int i = 0; i < n; i++) { int tmp; std::cin >> tmp; inputNumbers.push_back(tmp); } std::vector<int> sums; int sum{0}; for (auto num : inputNumbers) { sum += num; sums.push_back(sum); } std::vector<int> numbers; numbers.reserve(n * (n + 1) / 2); for (int i = 0; i < sums.size(); i++) { for (int j = i; j < sums.size(); j++) { int number = sums[j]; if (i > 0) { number -= sums[i - 1]; } numbers.push_back(number); } } /////////////////////////////////////////////////// //for (auto n : numbers) { // std::cout << n << " "; //} //std::cout << "\n"; int result{0}; for (int i = 0; i < numbers.size(); i++) { std::unordered_map<int, int> usedNumbers; for (int j = i + 1; j < numbers.size(); j++) { int currentTarget = -(numbers[j] + numbers[i]); if (usedNumbers.find(currentTarget) != usedNumbers.end()) { //std::cout << "i=" << i << " j=" << j << " , currentTarget=" << currentTarget << ", numbers[i]=" << numbers[i] // << ", numbers[j]=" << numbers[j] << "\n"; result += usedNumbers[currentTarget]; } usedNumbers[numbers[j]]++; } } std::cout << result << "\n"; 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 | #include <vector> #include <unordered_map> #include <iostream> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n; std::cin >> n; std::vector<int> inputNumbers; inputNumbers.reserve(n); for (int i = 0; i < n; i++) { int tmp; std::cin >> tmp; inputNumbers.push_back(tmp); } std::vector<int> sums; int sum{0}; for (auto num : inputNumbers) { sum += num; sums.push_back(sum); } std::vector<int> numbers; numbers.reserve(n * (n + 1) / 2); for (int i = 0; i < sums.size(); i++) { for (int j = i; j < sums.size(); j++) { int number = sums[j]; if (i > 0) { number -= sums[i - 1]; } numbers.push_back(number); } } /////////////////////////////////////////////////// //for (auto n : numbers) { // std::cout << n << " "; //} //std::cout << "\n"; int result{0}; for (int i = 0; i < numbers.size(); i++) { std::unordered_map<int, int> usedNumbers; for (int j = i + 1; j < numbers.size(); j++) { int currentTarget = -(numbers[j] + numbers[i]); if (usedNumbers.find(currentTarget) != usedNumbers.end()) { //std::cout << "i=" << i << " j=" << j << " , currentTarget=" << currentTarget << ", numbers[i]=" << numbers[i] // << ", numbers[j]=" << numbers[j] << "\n"; result += usedNumbers[currentTarget]; } usedNumbers[numbers[j]]++; } } std::cout << result << "\n"; return 0; } |