#include <bits/stdc++.h> #define int long long using namespace std; int n, cnt; long long res; int tab[505]; int pre[505]; int ntab[125255]; int values[500*20000][2]; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; ++i) { cin >> tab[i]; pre[i] = pre[i-1] + tab[i]; } for (int i = 1; i <= n; ++i) { for (int j = i; j <= n; ++j) { ntab[cnt] = (pre[j] - pre[i-1]); ++cnt; } } for (int i = 0; i < cnt; ++i) { for (int j = i+1; j < cnt; ++j) { if (ntab[i] + ntab[j] > 0) { res += values[ntab[i] + ntab[j]][0]; } else { res += values[-ntab[i] - ntab[j]][1]; } } if (ntab[i] < 0) { ++values[-ntab[i]][0]; } else { ++values[ntab[i]][1]; } } cout << res; }
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 | #include <bits/stdc++.h> #define int long long using namespace std; int n, cnt; long long res; int tab[505]; int pre[505]; int ntab[125255]; int values[500*20000][2]; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; ++i) { cin >> tab[i]; pre[i] = pre[i-1] + tab[i]; } for (int i = 1; i <= n; ++i) { for (int j = i; j <= n; ++j) { ntab[cnt] = (pre[j] - pre[i-1]); ++cnt; } } for (int i = 0; i < cnt; ++i) { for (int j = i+1; j < cnt; ++j) { if (ntab[i] + ntab[j] > 0) { res += values[ntab[i] + ntab[j]][0]; } else { res += values[-ntab[i] - ntab[j]][1]; } } if (ntab[i] < 0) { ++values[-ntab[i]][0]; } else { ++values[ntab[i]][1]; } } cout << res; } |