#include <iostream> #include <vector> #include <algorithm> #include <map> #include <cmath> #include <unordered_map> #include <string> #include <queue> #include <deque> #include <set> #include <cstdlib> #include <ctime> #include<chrono> #include<thread> #include<iomanip> #include<fstream> #define ll long long #define pb push_back #define mp make_pair #define ff first #define ss second using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); ll n, mini = 1000000000000000; cin >> n; vector<ll> a; unordered_map<ll, ll> collector; for(int i = 0; i < n; i++) { int b; cin >> b; a.pb(b); } for(int i = 1; i < n; i++) { a[i] += a[i - 1]; } for(int i = -1; i < n; i++) { for(int j = i + 1; j < n; j++) { if(i == -1) { collector[a[j]]++; mini = min(a[j], mini); } else { collector[a[j] - a[i]]++; mini = min(a[j] - a[i], mini); } } } auto it = collector.begin(); ll count = 0, s = 0, l = 0; for(; it != collector.end(); it++) { for(auto it2 = it; it2 != collector.end(); it2++) { //if(-(it->first+it2->first) < mini) break; if(collector.count(-(it->first+it2->first))) { if(-(it->first+it2->first) != it->first && it->first != it2->first && -(it->first+it2->first) != it2->first) { s += it->second * it2->second * collector[-(it->first+it2->first)]; } else if(it->first == it2->first && it->first == -(it->first+it2->first)) { count += (it->second * (it->second - 1) * (it->second - 2)) / 6; } else if(it->first == it2->first) { l += (it->second * (it->second - 1) * collector[-(it->first+it2->first)]) / 2; } else if(it->first == -(it->first+it2->first)) { l += (it->second * (it->second - 1) * it2->second) / 2; } else if(it2->first == -(it->first+it2->first)) { l += (it2->second * (it2->second - 1) * it->second) / 2; } } } } count += s/3 + l/2; cout << count << endl; 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 67 68 69 70 71 72 73 74 75 76 77 78 79 | #include <iostream> #include <vector> #include <algorithm> #include <map> #include <cmath> #include <unordered_map> #include <string> #include <queue> #include <deque> #include <set> #include <cstdlib> #include <ctime> #include<chrono> #include<thread> #include<iomanip> #include<fstream> #define ll long long #define pb push_back #define mp make_pair #define ff first #define ss second using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); ll n, mini = 1000000000000000; cin >> n; vector<ll> a; unordered_map<ll, ll> collector; for(int i = 0; i < n; i++) { int b; cin >> b; a.pb(b); } for(int i = 1; i < n; i++) { a[i] += a[i - 1]; } for(int i = -1; i < n; i++) { for(int j = i + 1; j < n; j++) { if(i == -1) { collector[a[j]]++; mini = min(a[j], mini); } else { collector[a[j] - a[i]]++; mini = min(a[j] - a[i], mini); } } } auto it = collector.begin(); ll count = 0, s = 0, l = 0; for(; it != collector.end(); it++) { for(auto it2 = it; it2 != collector.end(); it2++) { //if(-(it->first+it2->first) < mini) break; if(collector.count(-(it->first+it2->first))) { if(-(it->first+it2->first) != it->first && it->first != it2->first && -(it->first+it2->first) != it2->first) { s += it->second * it2->second * collector[-(it->first+it2->first)]; } else if(it->first == it2->first && it->first == -(it->first+it2->first)) { count += (it->second * (it->second - 1) * (it->second - 2)) / 6; } else if(it->first == it2->first) { l += (it->second * (it->second - 1) * collector[-(it->first+it2->first)]) / 2; } else if(it->first == -(it->first+it2->first)) { l += (it->second * (it->second - 1) * it2->second) / 2; } else if(it2->first == -(it->first+it2->first)) { l += (it2->second * (it2->second - 1) * it->second) / 2; } } } } count += s/3 + l/2; cout << count << endl; return 0; } |