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
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

void unique(vector<LL> &v)
{
	set<int> s(v.begin(), v.end());
	v.resize(0);
	v.reserve(s.size());
	for (auto &e : s)
		v.push_back(e);
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0);

	int n;
	cin >> n;
	vector<LL> v(n);
	for (auto &e : v)
		cin >> e;
	vector<LL> w;
	map<LL, LL> cnt;
	for (int i = 0; i < n; i++)
	{
		LL sum = 0;
		for (int j = i; j < n; j++)
		{
			sum += v[j];
			w.push_back(sum);
			cnt[sum]++;
		}
	}
	unique(w);
	LL ans = 0;
	for (int i = 0; i < (int)w.size(); i++)
	{
		if (cnt[w[i]] >= 3 && 3*w[i] == 0)
			ans += cnt[w[i]] * (cnt[w[i]]-1) * (cnt[w[i]]-2) / 6;
	}
	for (int i = 0; i < (int)w.size(); i++)
	{
		for (int j = 0; j < (int)w.size(); j++)
		{
			if (i == j)
				continue;
			if (cnt[w[i]] >= 2 && 2*w[i]+w[j] == 0)
				ans += cnt[w[i]] * (cnt[w[i]]-1) / 2 * cnt[w[j]];
		}
	}
	for (int i = 0; i < (int)w.size()-2; i++)
	{
		for (int j = i+1; j < (int)w.size()-1; j++)
		{
			LL sum2 = w[i] + w[j];
			
			int poczatek = j+1;
			int koniec = (int)w.size()-1;
			while (poczatek < koniec)
			{
				int srodek = (poczatek + koniec) / 2;
				if (sum2 + w[srodek] >= 0)
					koniec = srodek;
				else
					poczatek = srodek+1;
			}
			int l = poczatek;
			if (sum2 + w[l] == 0)
				ans += cnt[w[i]] * cnt[w[j]] * cnt[w[l]];
		}
	}
	cout << ans << "\n";
}