#include<bits/stdc++.h> using namespace std; struct custom_hash { size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return x + FIXED_RANDOM; } }; void solve() { int n; cin >> n; vector<int> a(n + 1, 0); for(int i = 1; i <= n; i++) cin >> a[i]; vector<int> pref(n + 1, 0), suf(n + 1, 0); for(int i = 1; i <= n; i++) pref[i] = pref[i - 1] + a[i]; for(int i = n-1; i >= 0; i--) suf[i] = suf[i + 1] + a[i + 1]; vector<int> presuf; for(int a = 0; a < n; a++) { for(int b = n - a - 1; b >= 0; b--) { presuf.push_back(pref[a] + suf[n - b]); } } //cout << "\n"; unordered_map<int, int, custom_hash> rhs; long long ans = 0; for(int i = n - 1; i >= 0; i--) { // Dodajemy 2 polowki do zbioru for(auto x : presuf) { rhs[x + suf[i + 1]]++; } for(auto x : presuf) { if(rhs.count(3 * pref[n] - pref[i] - x) != 0) { //cout << "lval: " << x << " lpref: " << pref[i] << " rval + rsuf: " << 3 * pref[n] - pref[i] - x << "\n"; ans += rhs[3 * pref[n] - pref[i] - x]; } } } //cout << ans << "\n"; unordered_map<int, int, custom_hash> powt; //for(auto x : presuf) cout << x << " "; //cout << "\n"; for(auto x : presuf) powt[x]++; for(auto x : presuf) { if(powt.count(3 * pref[n] - 2 * x) != 0) { ans -= 3 * powt[3 * pref[n] - 2 * x]; } } //cout << ans << "\n"; ans += 2 * powt[pref[n]]; //cout << ans << "\n"; cout << ans / 6 << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); 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 | #include<bits/stdc++.h> using namespace std; struct custom_hash { size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return x + FIXED_RANDOM; } }; void solve() { int n; cin >> n; vector<int> a(n + 1, 0); for(int i = 1; i <= n; i++) cin >> a[i]; vector<int> pref(n + 1, 0), suf(n + 1, 0); for(int i = 1; i <= n; i++) pref[i] = pref[i - 1] + a[i]; for(int i = n-1; i >= 0; i--) suf[i] = suf[i + 1] + a[i + 1]; vector<int> presuf; for(int a = 0; a < n; a++) { for(int b = n - a - 1; b >= 0; b--) { presuf.push_back(pref[a] + suf[n - b]); } } //cout << "\n"; unordered_map<int, int, custom_hash> rhs; long long ans = 0; for(int i = n - 1; i >= 0; i--) { // Dodajemy 2 polowki do zbioru for(auto x : presuf) { rhs[x + suf[i + 1]]++; } for(auto x : presuf) { if(rhs.count(3 * pref[n] - pref[i] - x) != 0) { //cout << "lval: " << x << " lpref: " << pref[i] << " rval + rsuf: " << 3 * pref[n] - pref[i] - x << "\n"; ans += rhs[3 * pref[n] - pref[i] - x]; } } } //cout << ans << "\n"; unordered_map<int, int, custom_hash> powt; //for(auto x : presuf) cout << x << " "; //cout << "\n"; for(auto x : presuf) powt[x]++; for(auto x : presuf) { if(powt.count(3 * pref[n] - 2 * x) != 0) { ans -= 3 * powt[3 * pref[n] - 2 * x]; } } //cout << ans << "\n"; ans += 2 * powt[pref[n]]; //cout << ans << "\n"; cout << ans / 6 << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; } |