#include "bits/stdc++.h" using namespace std; using ll = long long; int const N = 505; ll n, ans; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; vector <ll> a(n), b; for(auto& x : a) cin >> x; for(int i = 0; i < n; ++i){ int sum = 0; for(int j = i; j < n; ++j){ sum += a[j]; b.push_back(sum); } } for(int i = 0; i < b.size(); ++i){ unordered_multiset<ll> seen; seen.insert(b[i+1]); for(int j = i + 2; j < b.size(); ++j){ ll x = -b[i] - b[j]; if(seen.count(x)){ ans += seen.count(x); } seen.insert(b[j]); } } cout << ans; }
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 | #include "bits/stdc++.h" using namespace std; using ll = long long; int const N = 505; ll n, ans; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; vector <ll> a(n), b; for(auto& x : a) cin >> x; for(int i = 0; i < n; ++i){ int sum = 0; for(int j = i; j < n; ++j){ sum += a[j]; b.push_back(sum); } } for(int i = 0; i < b.size(); ++i){ unordered_multiset<ll> seen; seen.insert(b[i+1]); for(int j = i + 2; j < b.size(); ++j){ ll x = -b[i] - b[j]; if(seen.count(x)){ ans += seen.count(x); } seen.insert(b[j]); } } cout << ans; } |