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
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>

int c;

#define SKIP_WHITESPACE \
{   \
    while (1) { \
        c = fgetc(stdin);   \
        if (c != ' ' && c != '\n' && c != '\r') \
            break;  \
    }   \
}   \

#define READ_INT    \
({   \
    SKIP_WHITESPACE \
    int m = 1;  \
    if (c == '-') { \
        m = -1; \
        c = fgetc(stdin);   \
    }   \
    int ret = c - '0';  \
    while (1) { \
        c = fgetc(stdin);   \
        if (c < '0' || c > '9') \
            break;  \
        ret = ret * 10 + c - '0';   \
    }   \
    ret *= m;   \
    ret; \
})   \

#define N 500
#define M 125250
//int zug[N];
int schnellzug[N+1];
int sums[M];

int main(int argc, char* argv[]) {
    std::ios_base::sync_with_stdio (false);
    int i, j, k, n, b;
    uint64_t ret = 0ULL;

    n = READ_INT;

    schnellzug[0] = 0;
    for (i = 0; i < n; ++i) {
        b = READ_INT;
//        zug[i] = b;
        schnellzug[i + 1] = schnellzug[i] + b;
    }

    k = 0;
    for (i = 0; i < n ; ++i) {
        for (j = i + 1; j < n + 1; ++j) {
            sums[k++] = schnellzug[j] - schnellzug[i];
        }
    }

    std::sort(&sums[0], &sums[k]);

    int l, r, t;
    uint64_t lg, rg;
    for (i = 0; i < k - 2; ++i) {
        t = -sums[i];
        l = i + 1;
        r = k - 1;
        while (l < r) {
            if (sums[l] + sums[r] == t) {
                if (sums[l] == sums[r]) {
                    ret += (r - l) * (r - l + 1ULL) / 2ULL;
                    break;
                }
                lg = 1ULL;
                while (sums[l + 1] == sums[l]) {
                    lg++;
                    l++;
                }
                rg = 1ULL;
                while (sums[r - 1] == sums[r]) {
                    rg++;
                    r--;
                }
                ret = ret + lg * rg;
                l++;
                r--;
            } else if (sums[l] + sums[r] < t) {
                l++;
            } else {
                r--;
            }
        }
    }

    // for (i = 0; i < n; ++i) {
    //     std::cout << "i: " << i << "; zug=" << zug[i] << "\n";
    // }
    // for (i = 0; i <= n; ++i) {
    //     std::cout << "i: " << i << "; schnellzug=" << schnellzug[i] << "\n";
    // }
    // for (i = 0; i < k; ++i) {
    //     std::cout << "i: " << i << "; sums=" << sums[i] << "\n";
    // }

    std::cout << ret;
    std::cout << "\n";

    return EXIT_SUCCESS;
}