#include<bits/stdc++.h> using namespace std; #define ll int64_t #define pb push_back #define pii pair<int, int> #define st first #define nd second #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() const int MAXN = 509; const int MAX = 3e7+9; int tab[MAXN]; int pre[MAXN]; int _cnt[2*MAX]; int *cnt = _cnt+MAX; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for(int i=1;i<=n;i++) { cin >> tab[i]; pre[i] = pre[i-1]+tab[i]; } ll cnt1 = 0; for(int i=1;i<=n;i++) { for(int j=i;j<=n;j++) { if(pre[j]-pre[i-1]==0) { cnt1++; } } } ll cnt2 = 0; for(int i=1;i<=n;i++) { for(int j=i;j<=n;j++) { int sum = pre[j]-pre[i-1]; cnt2 += cnt[-2*sum]; if(sum%2==0) { cnt2 += cnt[-sum/2]; } cnt[sum]++; } } ll ans = 0; memset(_cnt, 0, sizeof(_cnt)); for(int i=n-1;i>=0;i--) { for(int j=1;j<=n;j++) { for(int k=j;k<=n;k++) { cnt[pre[i+1]+pre[k]-pre[j-1]]++; } } for(int j=1;j<=n;j++) { for(int k=j;k<=n;k++) { ans += cnt[-(-pre[i]+pre[k]-pre[j-1])]; } } } cout << (ans-cnt2*3-cnt1)/6 << "\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 61 62 63 64 65 | #include<bits/stdc++.h> using namespace std; #define ll int64_t #define pb push_back #define pii pair<int, int> #define st first #define nd second #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() const int MAXN = 509; const int MAX = 3e7+9; int tab[MAXN]; int pre[MAXN]; int _cnt[2*MAX]; int *cnt = _cnt+MAX; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for(int i=1;i<=n;i++) { cin >> tab[i]; pre[i] = pre[i-1]+tab[i]; } ll cnt1 = 0; for(int i=1;i<=n;i++) { for(int j=i;j<=n;j++) { if(pre[j]-pre[i-1]==0) { cnt1++; } } } ll cnt2 = 0; for(int i=1;i<=n;i++) { for(int j=i;j<=n;j++) { int sum = pre[j]-pre[i-1]; cnt2 += cnt[-2*sum]; if(sum%2==0) { cnt2 += cnt[-sum/2]; } cnt[sum]++; } } ll ans = 0; memset(_cnt, 0, sizeof(_cnt)); for(int i=n-1;i>=0;i--) { for(int j=1;j<=n;j++) { for(int k=j;k<=n;k++) { cnt[pre[i+1]+pre[k]-pre[j-1]]++; } } for(int j=1;j<=n;j++) { for(int k=j;k<=n;k++) { ans += cnt[-(-pre[i]+pre[k]-pre[j-1])]; } } } cout << (ans-cnt2*3-cnt1)/6 << "\n"; } |