#include <bits/stdc++.h> using namespace std; using ll = long long; int n; vector<int> a; const int MAXN = 4e7 + 7; int cnt[MAXN]; int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n; a.resize(n); for(int i = 0; i < n; i++){ cin >> a[i]; } vector<ll> vec; for(int i = 0; i < n; i++){ ll s = 0; for(int j = i; j < n; j++){ s += a[j]; vec.push_back(s); } } sort(vec.begin(), vec.end()); vector<pair<ll, int>> tab; n = vec.size(); int y = 0; for(y = 0; y < n; y++){ int pocz = y; // cout << vec[pocz] << '\n'; while(y < n && vec[y] == vec[pocz]){ y++; } y--; tab.push_back({vec[pocz], y - pocz + 1}); } ll ans = 0; n = tab.size(); for(int i = 0; i < n; i++){ //cout << tab[i].first << '\n'; if(tab[i].first == 0){ ans = (ll)ans + (ll)tab[i].second * (tab[i].second - 1) * (tab[i].second - 2) / 6; } ans = (ll)ans + cnt[-tab[i].first + 10000000] * tab[i].second; if(i + 1 < n){ for(int j = 0; j < i; j++){ if(tab[i].first + tab[j].first > -tab[i + 1].first){ break; } cnt[tab[i].first + tab[j].first + 10000000] += (tab[i].second * tab[j].second); } } } cout << ans << '\n'; 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 | #include <bits/stdc++.h> using namespace std; using ll = long long; int n; vector<int> a; const int MAXN = 4e7 + 7; int cnt[MAXN]; int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n; a.resize(n); for(int i = 0; i < n; i++){ cin >> a[i]; } vector<ll> vec; for(int i = 0; i < n; i++){ ll s = 0; for(int j = i; j < n; j++){ s += a[j]; vec.push_back(s); } } sort(vec.begin(), vec.end()); vector<pair<ll, int>> tab; n = vec.size(); int y = 0; for(y = 0; y < n; y++){ int pocz = y; // cout << vec[pocz] << '\n'; while(y < n && vec[y] == vec[pocz]){ y++; } y--; tab.push_back({vec[pocz], y - pocz + 1}); } ll ans = 0; n = tab.size(); for(int i = 0; i < n; i++){ //cout << tab[i].first << '\n'; if(tab[i].first == 0){ ans = (ll)ans + (ll)tab[i].second * (tab[i].second - 1) * (tab[i].second - 2) / 6; } ans = (ll)ans + cnt[-tab[i].first + 10000000] * tab[i].second; if(i + 1 < n){ for(int j = 0; j < i; j++){ if(tab[i].first + tab[j].first > -tab[i + 1].first){ break; } cnt[tab[i].first + tab[j].first + 10000000] += (tab[i].second * tab[j].second); } } } cout << ans << '\n'; return 0; } |