#include <bits/stdc++.h> using namespace std; typedef long long int ll; const int N = 505; int Original[N]; vector<ll> Numbers; unordered_map<ll, int> Count; ll Solve() { ll sol = 0; for(int i = 0; i < Numbers.size(); i++) { for(int j = i+1; j < Numbers.size(); j++) { ll left = 0 - (Numbers[i] + Numbers[j]); sol += Count[left]; sol -= (left == Numbers[i]) + (left == Numbers[j]); } } return sol/3; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for(int i = 0; i < n; i++) cin >> Original[i]; for(int i = 0; i < n; i++) { ll in = 0; for(int j = i; j < n; j++) { in += Original[j]; Numbers.push_back(in); Count[in]++; } } cout << Solve(); }
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 <bits/stdc++.h> using namespace std; typedef long long int ll; const int N = 505; int Original[N]; vector<ll> Numbers; unordered_map<ll, int> Count; ll Solve() { ll sol = 0; for(int i = 0; i < Numbers.size(); i++) { for(int j = i+1; j < Numbers.size(); j++) { ll left = 0 - (Numbers[i] + Numbers[j]); sol += Count[left]; sol -= (left == Numbers[i]) + (left == Numbers[j]); } } return sol/3; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for(int i = 0; i < n; i++) cin >> Original[i]; for(int i = 0; i < n; i++) { ll in = 0; for(int j = i; j < n; j++) { in += Original[j]; Numbers.push_back(in); Count[in]++; } } cout << Solve(); } |