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
#include <bits/stdc++.h>
using namespace std;
int tab[509];
int sum[509];
map<int, int> mp;
vector<pair<int, int>> v;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> tab[i];
    }
    for(int i = 1; i <= n; i++) {
        sum[i] = sum[i-1] + tab[i];
    }
    int c = 0;
    for(int i = 1; i <= n; i++) {
        for(int j = i; j <= n; j++) {
            int ans = sum[j] - sum[i-1];
            mp[ans]+=1;
        }
    }

    long long count = 0;
    if(mp[0] >= 3){
        int zeros = mp[0];
        int a = zeros;
        int b = 3*2;
        for(int i = 1; i <= (2); i++) {
            a = a*(zeros-i);
        }
        count += (a/b);
    }

    for(auto i : mp) {
        c++;
        int a = i.first;
        int b = i.second;
        v.push_back(i);
        if(a == 0 or b < 2){
            continue;
        }
        int look_for = -2*a;
        if(mp[look_for] >= 1){
            int q = (b*(b-1))/2;
            count += (q*mp[look_for]);
        }
    }
    sort(v.begin(), v.end());
    for(int i = 0; i<(c-2); i++){
        if((v[i].first + v[i+1].first) > 0){
            break;
        }
        for(int j = i+1; j<(c-1); j++){
            if((v[i].first + v[j].first + v[j+1].first) > 0){
                break;
            }
            int a = v[i].first + v[j].first;
            pair<int,int> look_for = {-a, 0};
            auto z = lower_bound(v.begin()+j+1, v.end(), look_for);
            if(z != v.end() and (*z).first == -a){
                if (mp[-a] >= 1 and -a != v[i].first and -a != v[j].first) {
                    count += mp[-a] * v[i].second * v[j].second;;
                }
            }
        }
    }
    cout << count << "\n";
    return 0;
}