#include <bits/stdc++.h> using namespace std; typedef long long ll; const int T = 1e7 + 1; const int N = 501; ll dodatnie[T]; ll ujemne[T]; int ciag[N]; int pref[N][N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for(int i = 1; i <= n; i++) cin >> ciag[i]; for(int i = 1; i <= n; i++) { int sum = 0; for(int j = i; j <= n; j++) { sum += ciag[j]; pref[i][j] = sum; if(sum < 0) ujemne[-sum]++; else dodatnie[sum]++; } } ll wynik = 0; for(int i = 1; i <= n; i++) { for(int j = i; j <= n; j++) { int sum = pref[i][j]; if(sum < 0) ujemne[-sum]--; else dodatnie[sum]--; for(int i2 = 1; i2 <= n; i2++) { for(int j2 = i2; j2 <= n; j2++) { if(i == i2 && j == j2) continue; int sum2 = pref[i2][j2]; if(sum2 < 0) ujemne[-sum2]--; else dodatnie[sum2]--; int sum_all = sum + sum2; //cout << i << " " << j << " " << i2 << " " << j2 << " " << sum << " " << sum2 << " " << sum_all << "\n"; if(sum_all <= 0) { wynik += dodatnie[-sum_all]; //cout << dodatnie[-sum_all] << "\n"; } else { wynik += ujemne[sum_all]; //cout << ujemne[sum_all] << "\n"; } if(sum2 < 0) ujemne[-sum2]++; else dodatnie[sum2]++; } } if(sum < 0) ujemne[-sum]++; else dodatnie[sum]++; } } cout << wynik / 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | #include <bits/stdc++.h> using namespace std; typedef long long ll; const int T = 1e7 + 1; const int N = 501; ll dodatnie[T]; ll ujemne[T]; int ciag[N]; int pref[N][N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for(int i = 1; i <= n; i++) cin >> ciag[i]; for(int i = 1; i <= n; i++) { int sum = 0; for(int j = i; j <= n; j++) { sum += ciag[j]; pref[i][j] = sum; if(sum < 0) ujemne[-sum]++; else dodatnie[sum]++; } } ll wynik = 0; for(int i = 1; i <= n; i++) { for(int j = i; j <= n; j++) { int sum = pref[i][j]; if(sum < 0) ujemne[-sum]--; else dodatnie[sum]--; for(int i2 = 1; i2 <= n; i2++) { for(int j2 = i2; j2 <= n; j2++) { if(i == i2 && j == j2) continue; int sum2 = pref[i2][j2]; if(sum2 < 0) ujemne[-sum2]--; else dodatnie[sum2]--; int sum_all = sum + sum2; //cout << i << " " << j << " " << i2 << " " << j2 << " " << sum << " " << sum2 << " " << sum_all << "\n"; if(sum_all <= 0) { wynik += dodatnie[-sum_all]; //cout << dodatnie[-sum_all] << "\n"; } else { wynik += ujemne[sum_all]; //cout << ujemne[sum_all] << "\n"; } if(sum2 < 0) ujemne[-sum2]++; else dodatnie[sum2]++; } } if(sum < 0) ujemne[-sum]++; else dodatnie[sum]++; } } cout << wynik / 6 << "\n"; } |