#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long int lint;
const int MAX_N = 500;
const int MILION = 1000000;
int n;
int S;
int a[MAX_N + 1];
int sum_to_count[20 * MILION + 1];
set<int> achieved_sums;
void increment(int sum) {
sum_to_count[sum + 10 * MILION]++;
}
int get_count(int sum) {
return sum_to_count[sum + 10 * MILION];
}
void wczytaj() {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
S += a[i];
}
}
void fill_counts() {
for (int i = 1; i <= n; ++i) {
int s = 0;
for (int j = i; j <= n; ++j) {
s += a[j];
//cout << s << endl;
achieved_sums.insert(s);
increment(s);
}
}
}
lint how_many_ways_to_these_sums(int s1, int s2, int s3) {
if (s1 == s2 && s2 == s3) {
lint c = get_count(s1);
if (c < 3) {
return 0;
} else {
return c * (c - 1) * (c - 2) / 6;
}
}
if (s1 == s2) {
lint c = get_count(s2);
if (c < 2) {
return 0;
} else {
return c * (c - 1) * get_count(s3) / 2;
}
}
if (s2 == s3) {
lint c = get_count(s2);
if (c < 2) {
return 0;
} else {
return c * (c - 1) * get_count(s1) / 2;
}
}
lint c1 = get_count(s1);
lint c2 = get_count(s2);
lint c3 = get_count(s3);
return c1 * c2 * c3;
}
int count_combinations() {
vector<int> vector_of_sums(achieved_sums.begin(), achieved_sums.end());
lint total = 0;
for (unsigned int i = 0; i < vector_of_sums.size(); ++i) {
for (unsigned int j = i; j < vector_of_sums.size(); ++j) {
int s1 = vector_of_sums[i];
int s2 = vector_of_sums[j];
int s3 = 0 - s1 - s2;
//cout << "Considering " << s1 << " " << s2 << " " << s3;
if ((s3 > 10 * MILION) || (s3 < -10*MILION)) {
continue;
}
if (s3 < s2) {
//cout << "; s3 too small" << endl;
break;
}
if (get_count(s3) == 0) {
//cout << "; s3 not achievable" << endl;
continue;
}
lint x = how_many_ways_to_these_sums(s1, s2, s3);
//cout << "; achievable in " << x << " ways" << endl;
total += x;
}
}
return total;
}
int main() {
ios_base::sync_with_stdio(0);
wczytaj();
fill_counts();
cout << count_combinations() << endl;
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | #include <iostream> #include <set> #include <vector> #include <algorithm> using namespace std; typedef long long int lint; const int MAX_N = 500; const int MILION = 1000000; int n; int S; int a[MAX_N + 1]; int sum_to_count[20 * MILION + 1]; set<int> achieved_sums; void increment(int sum) { sum_to_count[sum + 10 * MILION]++; } int get_count(int sum) { return sum_to_count[sum + 10 * MILION]; } void wczytaj() { cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; S += a[i]; } } void fill_counts() { for (int i = 1; i <= n; ++i) { int s = 0; for (int j = i; j <= n; ++j) { s += a[j]; //cout << s << endl; achieved_sums.insert(s); increment(s); } } } lint how_many_ways_to_these_sums(int s1, int s2, int s3) { if (s1 == s2 && s2 == s3) { lint c = get_count(s1); if (c < 3) { return 0; } else { return c * (c - 1) * (c - 2) / 6; } } if (s1 == s2) { lint c = get_count(s2); if (c < 2) { return 0; } else { return c * (c - 1) * get_count(s3) / 2; } } if (s2 == s3) { lint c = get_count(s2); if (c < 2) { return 0; } else { return c * (c - 1) * get_count(s1) / 2; } } lint c1 = get_count(s1); lint c2 = get_count(s2); lint c3 = get_count(s3); return c1 * c2 * c3; } int count_combinations() { vector<int> vector_of_sums(achieved_sums.begin(), achieved_sums.end()); lint total = 0; for (unsigned int i = 0; i < vector_of_sums.size(); ++i) { for (unsigned int j = i; j < vector_of_sums.size(); ++j) { int s1 = vector_of_sums[i]; int s2 = vector_of_sums[j]; int s3 = 0 - s1 - s2; //cout << "Considering " << s1 << " " << s2 << " " << s3; if ((s3 > 10 * MILION) || (s3 < -10*MILION)) { continue; } if (s3 < s2) { //cout << "; s3 too small" << endl; break; } if (get_count(s3) == 0) { //cout << "; s3 not achievable" << endl; continue; } lint x = how_many_ways_to_these_sums(s1, s2, s3); //cout << "; achievable in " << x << " ways" << endl; total += x; } } return total; } int main() { ios_base::sync_with_stdio(0); wczytaj(); fill_counts(); cout << count_combinations() << endl; return 0; } |
English