#include <bits/stdc++.h>
using namespace std;
int main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> ucb(n, 0);
for (auto &a : ucb) cin >> a;
vector<int> prefix_sums(n + 1, 0);
for (int i = 1; i <= n; ++i)
prefix_sums[i] = prefix_sums[i - 1] + ucb[i - 1];
unordered_map<int, list<tuple<int, int, int>>> threes;
long long count = 0;
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= n; ++j) {
for (int k = 0; k <= n; ++k) {
int sum = prefix_sums[i] + prefix_sums[j] + prefix_sums[k];
auto found = threes.find(sum);
if (found != threes.end()) {
for (auto[li, lj, lk] : found->second) {
if (li < i && lj < j && lk < k &&
make_pair(li, i) < make_pair(lj, j) &&
make_pair(lj, j) < make_pair(lk, k)) {
++count;
}
}
}
if (i <= j && j <= k)
threes[sum].push_back({i, j, k});
}
}
}
cout << count << "\n";
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 | #include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<int> ucb(n, 0); for (auto &a : ucb) cin >> a; vector<int> prefix_sums(n + 1, 0); for (int i = 1; i <= n; ++i) prefix_sums[i] = prefix_sums[i - 1] + ucb[i - 1]; unordered_map<int, list<tuple<int, int, int>>> threes; long long count = 0; for (int i = 0; i <= n; ++i) { for (int j = 0; j <= n; ++j) { for (int k = 0; k <= n; ++k) { int sum = prefix_sums[i] + prefix_sums[j] + prefix_sums[k]; auto found = threes.find(sum); if (found != threes.end()) { for (auto[li, lj, lk] : found->second) { if (li < i && lj < j && lk < k && make_pair(li, i) < make_pair(lj, j) && make_pair(lj, j) < make_pair(lk, k)) { ++count; } } } if (i <= j && j <= k) threes[sum].push_back({i, j, k}); } } } cout << count << "\n"; return 0; } |
English