#include<bits/stdc++.h> using namespace std; #ifdef DEBUG auto&operator <<(auto& o, pair<auto, auto> p) {return o<<"("<<p.first<<", "<<p.second<<")";} auto operator <<(auto& o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto v : x) o<<v<<", "; return o<<"}";} #define debug(X) cout<<"["#X"]"<<X<<endl; #else #define debug(X) {} #endif #define int long long int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin>>n; vector<int> noms(n); for(auto& v : noms) cin>>v; int result = 0; vector<pair<int, int> > buc; unordered_map<int, int> ones; unordered_map<int, int> twos; for(int i=0;i<n;i++) { int sum = 0; for(int j=i;j<n;j++) { sum += noms[j]; ones[sum]++; } } for(auto v : ones) buc.push_back(v); for(int i=0;i<buc.size();i++) for(int j=0;j<buc.size();j++) { if(i == j) { if(buc[i].first == 0) result-=2*buc[i].second*(buc[i].second-1); twos[buc[i].first+buc[i].first] += buc[i].second*(buc[i].second-1); continue; } if(buc[i].first*2+buc[j].first == 0) result-=buc[i].second*buc[j].second; if(buc[i].first+buc[j].first*2 == 0) result-=buc[i].second*buc[j].second; twos[buc[i].first+buc[j].first] += buc[i].second*buc[j].second; } for(auto v : ones) result += v.second*twos[-v.first]; cout<<result/6; }
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 | #include<bits/stdc++.h> using namespace std; #ifdef DEBUG auto&operator <<(auto& o, pair<auto, auto> p) {return o<<"("<<p.first<<", "<<p.second<<")";} auto operator <<(auto& o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto v : x) o<<v<<", "; return o<<"}";} #define debug(X) cout<<"["#X"]"<<X<<endl; #else #define debug(X) {} #endif #define int long long int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin>>n; vector<int> noms(n); for(auto& v : noms) cin>>v; int result = 0; vector<pair<int, int> > buc; unordered_map<int, int> ones; unordered_map<int, int> twos; for(int i=0;i<n;i++) { int sum = 0; for(int j=i;j<n;j++) { sum += noms[j]; ones[sum]++; } } for(auto v : ones) buc.push_back(v); for(int i=0;i<buc.size();i++) for(int j=0;j<buc.size();j++) { if(i == j) { if(buc[i].first == 0) result-=2*buc[i].second*(buc[i].second-1); twos[buc[i].first+buc[i].first] += buc[i].second*(buc[i].second-1); continue; } if(buc[i].first*2+buc[j].first == 0) result-=buc[i].second*buc[j].second; if(buc[i].first+buc[j].first*2 == 0) result-=buc[i].second*buc[j].second; twos[buc[i].first+buc[j].first] += buc[i].second*buc[j].second; } for(auto v : ones) result += v.second*twos[-v.first]; cout<<result/6; } |