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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#ifdef DEBUG
#include "debug.h"
#else
#define debug(...) 42
#endif

#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, n, m) for (int i = (n); i < (m); ++i)

#define repr(i, n) for (int i = (n) - 1; i >= 0; --i)
#define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i)

#define each(a,x) for(auto& a : (x))
#define sz(x) int((x).size())

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;

template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; }
template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; }

const int M = 500 * 20000 * 2;
array<int, 2*M+1> _occ;
auto occ = _occ.begin() + M;

void solve() {
    int n;
    cin >> n;

    vi a(n), pref = {0}, buc;
    for (auto &x : a) {
        cin >> x;
        pref.push_back(pref.back() + x);
    }
    rep(i, n+1) repp(j, i+1, n+1)
        buc.push_back(pref[j] - pref[i]);
    // debug(buc);
    // debug(pref);

    auto add = [&](int off, int multip) {
        for (int i : buc)
            occ[-i + off] += multip;
    };
    rep(i, n+1) {
        add(pref[i], +1);
    }

    ll ans = 0;

    repr(i, n+1) {
        add(pref[i], -1);
        // debug(i, pref[i]);
        for (int bv : buc)
            ans += occ[bv + pref[i]];
    }

    // {
    //     int sb = 0;
    //     for (int i : buc) for (int j : buc) for (int k : buc)
    //         sb += (i + j + k) == 0;
    //     assert(sb == ans);
    // }

    // ll bruted = 0;
    // rep(i, sz(buc)) rep(j, i) rep(k, j)
    //     if (buc[i] + buc[j] + buc[k] == 0)
    //         ++bruted;
    // debug(bruted);

    for (int i : buc)
        ++occ[i];
    ll two_one = 0, triples = occ[0];
    for (int i : buc)
        if (i % 2 == 0 && i)
            two_one += occ[-i/2];

    // repp(a, -5, 6) repp(b, -5, 6) repp(c, -5, 6) repp(d, -5, 6) repp(e, -5, 6)
    // {
    //     ll sc = ans * a + two_one * b + triples * (c + triples * (d + triples * e));
    //     if (sc % bruted == 0)
    //         debug(a, b, c, d, e);
    // }

    ll sc = ans * 1 + two_one * -3 + triples * (2 + triples * (-3 + triples * 0));

    cout << sc / 6 << '\n';
}

int main() {
#ifdef DEBUG
    const int MEMSIZE = 1024 * 1024 * 1024;
    static_assert(MEMSIZE % 16 == 0);
    static char stack[MEMSIZE];
    asm volatile (
        "mov %[newstack], %%rsp\n"
        "call *%[funcptr]"
        :: [funcptr] "r" (solve), [newstack] "r" (stack + MEMSIZE)
    );
    exit(0);
#else
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
#endif
}