#include <bits/stdc++.h> using namespace std; #pragma GCC maksuje_zadania_na_essie("pot200"); #define loop(i, a, b) for(ll i = a; i <= b; i++) #define loop_rev(i, a, b) for(ll i = a; i >= b; i--) #define all(x) x.begin(), x.end() #define sz(x) int(x.size()) #define pb push_back using ll = int64_t; constexpr ll OFFSET = 20'000'000; ll _cnt[2 * OFFSET + 1]; ll& cnt(ll sum) { return _cnt[sum + OFFSET]; } ll _cnt2[2 * OFFSET + 1]; ll& cnt2(ll sum) { return _cnt2[sum + OFFSET]; } constexpr ll MAX_N = 500 + 1; ll arr[MAX_N], ps[MAX_N]; int main() { cin.tie(0)->sync_with_stdio(false); ll n; cin >> n; loop(i, 1, n) { cin >> arr[i]; ps[i] = ps[i-1] + arr[i]; } loop(i, 1, n) { loop(j, i, n) { loop(a, 1, n) { loop(b, a, n) { if(i == a && j == b) continue; ++cnt(ps[b] - ps[a-1] + ps[j] - ps[i-1]); } } } } ll res = 0; loop(i, 1, n) { loop(j, i, n) { res += cnt(ps[i-1] - ps[j]); } } loop(i, 1, n) { loop(j, i, n) { ++cnt2(ps[j] - ps[i-1]); } } loop(i, 1, n) { loop(j, i, n) { if(ps[j] - ps[i-1] == 0) { res -= 2 * (cnt2(0) - 1); } else { res -= 2 * cnt2(-2LL * (ps[j] - ps[i-1])); } } } cout << res / 6 << '\n'; }
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 | #include <bits/stdc++.h> using namespace std; #pragma GCC maksuje_zadania_na_essie("pot200"); #define loop(i, a, b) for(ll i = a; i <= b; i++) #define loop_rev(i, a, b) for(ll i = a; i >= b; i--) #define all(x) x.begin(), x.end() #define sz(x) int(x.size()) #define pb push_back using ll = int64_t; constexpr ll OFFSET = 20'000'000; ll _cnt[2 * OFFSET + 1]; ll& cnt(ll sum) { return _cnt[sum + OFFSET]; } ll _cnt2[2 * OFFSET + 1]; ll& cnt2(ll sum) { return _cnt2[sum + OFFSET]; } constexpr ll MAX_N = 500 + 1; ll arr[MAX_N], ps[MAX_N]; int main() { cin.tie(0)->sync_with_stdio(false); ll n; cin >> n; loop(i, 1, n) { cin >> arr[i]; ps[i] = ps[i-1] + arr[i]; } loop(i, 1, n) { loop(j, i, n) { loop(a, 1, n) { loop(b, a, n) { if(i == a && j == b) continue; ++cnt(ps[b] - ps[a-1] + ps[j] - ps[i-1]); } } } } ll res = 0; loop(i, 1, n) { loop(j, i, n) { res += cnt(ps[i-1] - ps[j]); } } loop(i, 1, n) { loop(j, i, n) { ++cnt2(ps[j] - ps[i-1]); } } loop(i, 1, n) { loop(j, i, n) { if(ps[j] - ps[i-1] == 0) { res -= 2 * (cnt2(0) - 1); } else { res -= 2 * cnt2(-2LL * (ps[j] - ps[i-1])); } } } cout << res / 6 << '\n'; } |