#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
map<int,long long> m;
long long ans=0;
vector<int> s(n+1);
s[0]=0;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
s[i+1]=s[i]+x;
}
for(int i=0;i<=n;i++)
for(int j=0;j<i;j++)
m[s[i]-s[j]]++;
for(pair<int,long long> p1:m)
{
//cout<<p1.first<<" "<<p1.second<<endl;
for(pair<int,int> p2:m)
{
int a=p1.first;
int b=p2.first;
int c=0-a-b;
if(a<b || b<c) continue;
if(a==b)
{
if(a==c) ans+=(long long) ((p1.second*(p1.second-1)*(p1.second-2))/6);
else ans+=(long long)((p1.second*(p1.second-1))/2*m[c]);
}
else if(b==c) ans+=(p2.second*(p2.second-1))/2*p1.second;
else ans+=(long long) (p1.second*p2.second*m[c]);
}
}
cout<<ans<<endl;
}
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 | #include <bits/stdc++.h> using namespace std; int main() { int n; cin>>n; map<int,long long> m; long long ans=0; vector<int> s(n+1); s[0]=0; for(int i=0;i<n;i++) { int x; cin>>x; s[i+1]=s[i]+x; } for(int i=0;i<=n;i++) for(int j=0;j<i;j++) m[s[i]-s[j]]++; for(pair<int,long long> p1:m) { //cout<<p1.first<<" "<<p1.second<<endl; for(pair<int,int> p2:m) { int a=p1.first; int b=p2.first; int c=0-a-b; if(a<b || b<c) continue; if(a==b) { if(a==c) ans+=(long long) ((p1.second*(p1.second-1)*(p1.second-2))/6); else ans+=(long long)((p1.second*(p1.second-1))/2*m[c]); } else if(b==c) ans+=(p2.second*(p2.second-1))/2*p1.second; else ans+=(long long) (p1.second*p2.second*m[c]); } } cout<<ans<<endl; } |
English