#include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define ll long long #ifdef LOC auto &operator << (auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator << (auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif const int maxN = 500 + 10; int n; int a[maxN]; int b[maxN * maxN]; int N = 0; const int magic = 31 * 1000 * 1000; int tab[2 * magic]; void prep() { rep(i, n) { int suma = 0; fwd(j, i, n) { suma += a[j]; b[N ++] = suma; } } sort(b, b + N); } ll count_one() { ll ans = 0; rep(i, N) if (b[i] == 0) ans += 1; return ans; } ll count_two() { rep(i, N) tab[magic + b[i]] += 1; ll ans = 0; rep(i, N) ans += tab[magic - 2 * b[i]]; rep(i, N) tab[magic + b[i]] -= 1; return ans; } ll count_three() { ll ans = 0; int pref = 0; rep(i, n) { rep(j, N) tab[magic + b[j] - pref] += 1; pref += a[i]; rep(j, N) ans += tab[magic - b[j] - pref]; } return ans; } int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); cin >> n; rep(i, n) cin >> a[i]; prep(); ll c1 = count_one(); ll c2 = count_two() - c1; ll c3 = count_three(); c3 -= c1 + 3 * c2; assert(c3 % 6 == 0); c3 /= 6; cout << c3 << '\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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | #include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define ll long long #ifdef LOC auto &operator << (auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator << (auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif const int maxN = 500 + 10; int n; int a[maxN]; int b[maxN * maxN]; int N = 0; const int magic = 31 * 1000 * 1000; int tab[2 * magic]; void prep() { rep(i, n) { int suma = 0; fwd(j, i, n) { suma += a[j]; b[N ++] = suma; } } sort(b, b + N); } ll count_one() { ll ans = 0; rep(i, N) if (b[i] == 0) ans += 1; return ans; } ll count_two() { rep(i, N) tab[magic + b[i]] += 1; ll ans = 0; rep(i, N) ans += tab[magic - 2 * b[i]]; rep(i, N) tab[magic + b[i]] -= 1; return ans; } ll count_three() { ll ans = 0; int pref = 0; rep(i, n) { rep(j, N) tab[magic + b[j] - pref] += 1; pref += a[i]; rep(j, N) ans += tab[magic - b[j] - pref]; } return ans; } int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); cin >> n; rep(i, n) cin >> a[i]; prep(); ll c1 = count_one(); ll c2 = count_two() - c1; ll c3 = count_three(); c3 -= c1 + 3 * c2; assert(c3 % 6 == 0); c3 /= 6; cout << c3 << '\n'; return 0; } |