#pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; #define FOR(i, b, e) for(int i = (b); i < (e); i++) #define sz(x) int(x.size()) #define all(x) x.begin(), x.end() #define pb push_back #define mp make_pair #define st first #define nd second using ll = long long; using vi = vector<int>; using pii = pair<int, int>; auto &operator<<(auto &o, pair<auto, auto> p) { return o << "(" << p.st << ", " << p.nd << ")"; } auto operator<<(auto &o, auto x)->decltype(end(x), o) { o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e; return o << "}"; } #ifdef LOCAL #define deb(x...) cerr << "[" #x "]: ", [](auto...$) { \ ((cerr << $ << "; "),...) << endl; }(x) #else #define deb(...) #endif #include <ext/pb_ds/assoc_container.hpp> /** keep-include */ using namespace __gnu_pbds; // For places where hacking might be a problem: const int R = 124'412'612; struct chash { // To use most bits rather than just the lowest const uint64_t C = ll(4e18 * acos(0)) | 71; ll operator()(ll x) const { return __builtin_bswap64((x^R)*C); } }; void solve() { int n; cin >> n; vi ac(n); for(int &x: ac) cin >> x; vi pref(n + 1); FOR(i, 0, n) pref[i + 1] = pref[i] + ac[i]; ll ans = 0; { gp_hash_table<int, int, chash> kox({},{},{},{}, {1 << 16}); FOR(mid_r, 1, n) { FOR(first_l, 0, mid_r - 1) FOR(mid_l, 0, mid_r) { int sum = pref[mid_r - 1] - pref[first_l] - pref[mid_l]; kox[sum]++; } FOR(first_r, 1, mid_r - 1) FOR(first_l, 0, first_r) { int sum = -pref[mid_r - 1] + pref[first_r] - pref[first_l]; kox[sum]++; } FOR(last_r, mid_r + 1, n + 1) FOR(last_l, 0, last_r) { int sum = pref[last_r] - pref[last_l] + pref[mid_r]; ans += kox[-sum]; } } } gp_hash_table<int, int, chash> cnt({},{},{},{}, {1 << 16}); FOR(i, 1, n + 1) FOR(j, 0, i) cnt[pref[i] - pref[j]]++; FOR(i, 1, n + 1) { FOR(j, 0, i) cnt[pref[i] - pref[j]]--; FOR(j, 0, i) { FOR(k, 0, j) { int sum = pref[i] * 2 - pref[j] - pref[k]; ans += cnt[-sum]; cnt[pref[i] - pref[k]]++; } FOR(k, 0, j) cnt[pref[i] - pref[k]]--; } FOR(j, 0, i) cnt[pref[i] - pref[j]]++; } cout << ans << '\n'; } int main() { cin.tie(0)->sync_with_stdio(0); int tt = 1; // cin >> tt; FOR(te, 0, tt) 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | #pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; #define FOR(i, b, e) for(int i = (b); i < (e); i++) #define sz(x) int(x.size()) #define all(x) x.begin(), x.end() #define pb push_back #define mp make_pair #define st first #define nd second using ll = long long; using vi = vector<int>; using pii = pair<int, int>; auto &operator<<(auto &o, pair<auto, auto> p) { return o << "(" << p.st << ", " << p.nd << ")"; } auto operator<<(auto &o, auto x)->decltype(end(x), o) { o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e; return o << "}"; } #ifdef LOCAL #define deb(x...) cerr << "[" #x "]: ", [](auto...$) { \ ((cerr << $ << "; "),...) << endl; }(x) #else #define deb(...) #endif #include <ext/pb_ds/assoc_container.hpp> /** keep-include */ using namespace __gnu_pbds; // For places where hacking might be a problem: const int R = 124'412'612; struct chash { // To use most bits rather than just the lowest const uint64_t C = ll(4e18 * acos(0)) | 71; ll operator()(ll x) const { return __builtin_bswap64((x^R)*C); } }; void solve() { int n; cin >> n; vi ac(n); for(int &x: ac) cin >> x; vi pref(n + 1); FOR(i, 0, n) pref[i + 1] = pref[i] + ac[i]; ll ans = 0; { gp_hash_table<int, int, chash> kox({},{},{},{}, {1 << 16}); FOR(mid_r, 1, n) { FOR(first_l, 0, mid_r - 1) FOR(mid_l, 0, mid_r) { int sum = pref[mid_r - 1] - pref[first_l] - pref[mid_l]; kox[sum]++; } FOR(first_r, 1, mid_r - 1) FOR(first_l, 0, first_r) { int sum = -pref[mid_r - 1] + pref[first_r] - pref[first_l]; kox[sum]++; } FOR(last_r, mid_r + 1, n + 1) FOR(last_l, 0, last_r) { int sum = pref[last_r] - pref[last_l] + pref[mid_r]; ans += kox[-sum]; } } } gp_hash_table<int, int, chash> cnt({},{},{},{}, {1 << 16}); FOR(i, 1, n + 1) FOR(j, 0, i) cnt[pref[i] - pref[j]]++; FOR(i, 1, n + 1) { FOR(j, 0, i) cnt[pref[i] - pref[j]]--; FOR(j, 0, i) { FOR(k, 0, j) { int sum = pref[i] * 2 - pref[j] - pref[k]; ans += cnt[-sum]; cnt[pref[i] - pref[k]]++; } FOR(k, 0, j) cnt[pref[i] - pref[k]]--; } FOR(j, 0, i) cnt[pref[i] - pref[j]]++; } cout << ans << '\n'; } int main() { cin.tie(0)->sync_with_stdio(0); int tt = 1; // cin >> tt; FOR(te, 0, tt) solve(); return 0; } |