#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; const int N = 501; const int X = 6e7+100; const int B = 3e7+2; int a[N], p[N], cnt[X], n; void solve() { cin>>n; p[0]=0; for (int i=1; i<=n; ++i) cin>>a[i], p[i]=p[i-1]+a[i]; ll res=0; for (int j=0; j<=n; ++j) { for (int i=1; i<=n; ++i) { for (int ip=0; ip<i; ++ip) { res+=cnt[p[i]-p[ip]+p[j]+B]; } } for (int i=1; i<=n; ++i) { for (int ip=0; ip<i; ++ip) { ++cnt[p[ip]-p[i]+p[j]+B]; } } } memset(cnt, 0, sizeof(cnt)); for (int i=1; i<=n; ++i) { for (int ip=0; ip<i; ++ip) { ++cnt[2*(p[i]-p[ip])+B]; } } for (int i=1; i<=n; ++i) { for (int ip=0; ip<i; ++ip) { res-=3*cnt[-(p[i]-p[ip])+B]; } } for (int i=1; i<=n; ++i) { for (int ip=0; ip<i; ++ip) { if (p[i]-p[ip] == 0) res+=2; } } assert(res%6 == 0); cout<<(res/6)<<"\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t=1; //cin>>t; while (t--) solve(); }
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 61 62 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; const int N = 501; const int X = 6e7+100; const int B = 3e7+2; int a[N], p[N], cnt[X], n; void solve() { cin>>n; p[0]=0; for (int i=1; i<=n; ++i) cin>>a[i], p[i]=p[i-1]+a[i]; ll res=0; for (int j=0; j<=n; ++j) { for (int i=1; i<=n; ++i) { for (int ip=0; ip<i; ++ip) { res+=cnt[p[i]-p[ip]+p[j]+B]; } } for (int i=1; i<=n; ++i) { for (int ip=0; ip<i; ++ip) { ++cnt[p[ip]-p[i]+p[j]+B]; } } } memset(cnt, 0, sizeof(cnt)); for (int i=1; i<=n; ++i) { for (int ip=0; ip<i; ++ip) { ++cnt[2*(p[i]-p[ip])+B]; } } for (int i=1; i<=n; ++i) { for (int ip=0; ip<i; ++ip) { res-=3*cnt[-(p[i]-p[ip])+B]; } } for (int i=1; i<=n; ++i) { for (int ip=0; ip<i; ++ip) { if (p[i]-p[ip] == 0) res+=2; } } assert(res%6 == 0); cout<<(res/6)<<"\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t=1; //cin>>t; while (t--) solve(); } |