#include <iostream> #include <vector> #include <unordered_map> #include <numeric> #include <algorithm> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<int> uc(n); for (int i = 0; i < n; i++) { cin >> uc[i]; } vector<int> buc; for (int l = 1; l < uc.size() + 1; l++) { for (int i = 0; i < uc.size() + 1 - l; i++) { buc.push_back(accumulate(uc.begin() + i, uc.begin() + i + l, decltype(uc)::value_type(0))); } } //sort(buc.begin(), buc.end()); int res = 0; for (int i = 0; i < buc.size(); i++) { //cout << buc[i] << " " ; unordered_map<int, int> count; for (int j = 0; j < buc.size(); j++) { if (j == i) continue; count[-buc[j] - buc[i]]++; } for (int j = 0; j < buc.size(); j++) { if (j == i) continue; if (-buc[i] == 2 * buc[j]) { res += count[buc[j]] - 1; } else { res += count[buc[j]]; } } } cout << res / 6 << endl; }
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 | #include <iostream> #include <vector> #include <unordered_map> #include <numeric> #include <algorithm> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<int> uc(n); for (int i = 0; i < n; i++) { cin >> uc[i]; } vector<int> buc; for (int l = 1; l < uc.size() + 1; l++) { for (int i = 0; i < uc.size() + 1 - l; i++) { buc.push_back(accumulate(uc.begin() + i, uc.begin() + i + l, decltype(uc)::value_type(0))); } } //sort(buc.begin(), buc.end()); int res = 0; for (int i = 0; i < buc.size(); i++) { //cout << buc[i] << " " ; unordered_map<int, int> count; for (int j = 0; j < buc.size(); j++) { if (j == i) continue; count[-buc[j] - buc[i]]++; } for (int j = 0; j < buc.size(); j++) { if (j == i) continue; if (-buc[i] == 2 * buc[j]) { res += count[buc[j]] - 1; } else { res += count[buc[j]]; } } } cout << res / 6 << endl; } |