#include <bits/stdc++.h> using namespace std; using ll = long long; #define FOR(i,a,b) for(ll i=a; i<=b; ++i) #define pb push_back #define ssize(v) (ll)(v.size()) #define fi first #define se second constexpr int MAX_N=5e3+14,ZERO_SUM=1e7+14, MAX_SUM=2e7+28; ll N, inp[MAX_N], qtSum[MAX_SUM], rres=0; vector<pair<ll,ll>> sqnc; void Input(){ ll cSum; cin>>N; FOR(i,0,N-1) cin>>inp[i]; FOR(i,0,N-1){ cSum=0; FOR(j,i,N-1){ cSum+=inp[j]; ++qtSum[ZERO_SUM+cSum]; } } FOR(i,0,MAX_SUM-1){ if(qtSum[i]==0)continue; sqnc.pb({i-ZERO_SUM,qtSum[i]}); } } void Solve(){ FOR(i,0,ssize(sqnc)-1){ if(-(2*sqnc[i].fi)>sqnc[i].fi) rres+=(((qtSum[ZERO_SUM+sqnc[i].fi]-1)*qtSum[ZERO_SUM+sqnc[i].fi])/2)*qtSum[ZERO_SUM-(2*sqnc[i].fi)]; FOR(j,i+1,ssize(sqnc)-1){ if(-sqnc[i].fi-sqnc[j].fi<sqnc[j].fi) break; if(-sqnc[i].fi-sqnc[j].fi==sqnc[j].fi) { rres+=qtSum[ZERO_SUM+sqnc[i].fi]*((qtSum[ZERO_SUM+sqnc[j].fi]*(qtSum[ZERO_SUM+sqnc[j].fi]-1))/2); // cout<<"dwa - i:"<<i<<" j:"<<j<<" rres:"<<rres<<"\n"; continue; } rres+=qtSum[ZERO_SUM+sqnc[i].fi]*qtSum[ZERO_SUM+sqnc[j].fi]*qtSum[ZERO_SUM-sqnc[i].fi-sqnc[j].fi]; // cout<<"i:"<<i<<" j:"<<j<<" rres:"<<rres<<"\n"; } // cout<<"i:"<<i<<" rres:"<<rres<<"\n"; } rres+=((qtSum[ZERO_SUM]*(qtSum[ZERO_SUM]-1)*(qtSum[ZERO_SUM]-2))/6); cout<<rres<<"\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); Input(); Solve(); // cout<<"\nsqnc:\n"; // for(auto cE:sqnc){ // cout<<" "<<cE.fi<<", "<<cE.se<<"\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 58 59 60 | #include <bits/stdc++.h> using namespace std; using ll = long long; #define FOR(i,a,b) for(ll i=a; i<=b; ++i) #define pb push_back #define ssize(v) (ll)(v.size()) #define fi first #define se second constexpr int MAX_N=5e3+14,ZERO_SUM=1e7+14, MAX_SUM=2e7+28; ll N, inp[MAX_N], qtSum[MAX_SUM], rres=0; vector<pair<ll,ll>> sqnc; void Input(){ ll cSum; cin>>N; FOR(i,0,N-1) cin>>inp[i]; FOR(i,0,N-1){ cSum=0; FOR(j,i,N-1){ cSum+=inp[j]; ++qtSum[ZERO_SUM+cSum]; } } FOR(i,0,MAX_SUM-1){ if(qtSum[i]==0)continue; sqnc.pb({i-ZERO_SUM,qtSum[i]}); } } void Solve(){ FOR(i,0,ssize(sqnc)-1){ if(-(2*sqnc[i].fi)>sqnc[i].fi) rres+=(((qtSum[ZERO_SUM+sqnc[i].fi]-1)*qtSum[ZERO_SUM+sqnc[i].fi])/2)*qtSum[ZERO_SUM-(2*sqnc[i].fi)]; FOR(j,i+1,ssize(sqnc)-1){ if(-sqnc[i].fi-sqnc[j].fi<sqnc[j].fi) break; if(-sqnc[i].fi-sqnc[j].fi==sqnc[j].fi) { rres+=qtSum[ZERO_SUM+sqnc[i].fi]*((qtSum[ZERO_SUM+sqnc[j].fi]*(qtSum[ZERO_SUM+sqnc[j].fi]-1))/2); // cout<<"dwa - i:"<<i<<" j:"<<j<<" rres:"<<rres<<"\n"; continue; } rres+=qtSum[ZERO_SUM+sqnc[i].fi]*qtSum[ZERO_SUM+sqnc[j].fi]*qtSum[ZERO_SUM-sqnc[i].fi-sqnc[j].fi]; // cout<<"i:"<<i<<" j:"<<j<<" rres:"<<rres<<"\n"; } // cout<<"i:"<<i<<" rres:"<<rres<<"\n"; } rres+=((qtSum[ZERO_SUM]*(qtSum[ZERO_SUM]-1)*(qtSum[ZERO_SUM]-2))/6); cout<<rres<<"\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); Input(); Solve(); // cout<<"\nsqnc:\n"; // for(auto cE:sqnc){ // cout<<" "<<cE.fi<<", "<<cE.se<<"\n"; // } } |