#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define ll long long
const ll maxn = 2e7 + 7, N = 1e7;
ll n, suma, ind, res;
ll tab[1000], t[maxn];
vector<int> v;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
cin >> tab[i];
for(int i = 1; i <= n; i++){
for(int j = i; j <= n; j++){
suma += tab[j];
t[suma + N]++;
}
suma = 0;
}
for(int i = -N; i <= N; i++){
if(t[i + N] > 0)
v.push_back(i);
}
for(int i = 0; i < v.size(); i++){
for(int j = i; j < v.size(); j++){
ind = -(v[i] + v[j]);
if(v[j] <= ind){
if(ind > N)
continue;
if(i != j){
if(v[i] == ind)
res += ((t[v[i] + N] * (t[v[i] + N] - 1) / 2) * t[v[j] + N]);
else if(v[j] == ind)
res += ((t[v[j] + N] * (t[v[j] + N] - 1) / 2) * t[v[i] + N]);
else
res += (t[v[i] + N] * t[v[j] + N] * t[ind + N]);
}
else{
if(v[j] == ind)
res+= ((t[v[i] + N] * (t[v[i] + N] - 1) * (t[v[i] + N] - 2)) / 6);
else
res += ((t[v[i] + N] * (t[v[i] + N] - 1)) / 2) * t[ind + N];
}
}
else
break;
}
}
cout << res << '\n';
}
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 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second #define ll long long const ll maxn = 2e7 + 7, N = 1e7; ll n, suma, ind, res; ll tab[1000], t[maxn]; vector<int> v; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for(int i = 1; i <= n; i++) cin >> tab[i]; for(int i = 1; i <= n; i++){ for(int j = i; j <= n; j++){ suma += tab[j]; t[suma + N]++; } suma = 0; } for(int i = -N; i <= N; i++){ if(t[i + N] > 0) v.push_back(i); } for(int i = 0; i < v.size(); i++){ for(int j = i; j < v.size(); j++){ ind = -(v[i] + v[j]); if(v[j] <= ind){ if(ind > N) continue; if(i != j){ if(v[i] == ind) res += ((t[v[i] + N] * (t[v[i] + N] - 1) / 2) * t[v[j] + N]); else if(v[j] == ind) res += ((t[v[j] + N] * (t[v[j] + N] - 1) / 2) * t[v[i] + N]); else res += (t[v[i] + N] * t[v[j] + N] * t[ind + N]); } else{ if(v[j] == ind) res+= ((t[v[i] + N] * (t[v[i] + N] - 1) * (t[v[i] + N] - 2)) / 6); else res += ((t[v[i] + N] * (t[v[i] + N] - 1)) / 2) * t[ind + N]; } } else break; } } cout << res << '\n'; } |
English