#ifdef DEBUG #define _GLIBCXX_DEBUG #endif //#pragma GCC optimize("O3") #include<bits/stdc++.h> using namespace std; #define pb push_back typedef long long ll; typedef long double ld; const int SHIFT = 3e7 + 10; const int maxM = 2 * SHIFT + 100; int cnt[maxM]; const int maxN = 505; int pref[maxN]; int a[maxN]; int n; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifdef DEBUG freopen("input.txt", "r", stdin); #endif cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; pref[i] = pref[i - 1] + a[i]; } ll S = 0; for (int L = n - 1; L >= 0; L--) { for (int X = 0; X < n; X++) { for (int Y = X + 1; Y <= n; Y++) { int F = pref[Y] - pref[X] + pref[L + 1]; cnt[F + SHIFT]++; } } for (int X = 0; X < n; X++) { for (int Y = X + 1; Y <= n; Y++) { int F = -pref[Y] + pref[X] + pref[L]; S += cnt[F + SHIFT]; } } } //now sub with equal memset(cnt, 0, sizeof cnt); for (int L = 0; L < n; L++) { for (int R = L + 1; R <= n; R++) { cnt[2 * (pref[R] - pref[L]) + SHIFT]++; } } for (int L = 0; L < n; L++) { for (int R = L + 1; R <= n; R++) { S -= 3 * cnt[(pref[L] - pref[R]) + SHIFT]; } } for (int L = 0; L < n; L++) { for (int R = L + 1; R <= n; R++) { if (pref[R] == pref[L]) S += 2; } } assert(S % 6 == 0); cout << S / 6; return 0; }
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 | #ifdef DEBUG #define _GLIBCXX_DEBUG #endif //#pragma GCC optimize("O3") #include<bits/stdc++.h> using namespace std; #define pb push_back typedef long long ll; typedef long double ld; const int SHIFT = 3e7 + 10; const int maxM = 2 * SHIFT + 100; int cnt[maxM]; const int maxN = 505; int pref[maxN]; int a[maxN]; int n; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifdef DEBUG freopen("input.txt", "r", stdin); #endif cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; pref[i] = pref[i - 1] + a[i]; } ll S = 0; for (int L = n - 1; L >= 0; L--) { for (int X = 0; X < n; X++) { for (int Y = X + 1; Y <= n; Y++) { int F = pref[Y] - pref[X] + pref[L + 1]; cnt[F + SHIFT]++; } } for (int X = 0; X < n; X++) { for (int Y = X + 1; Y <= n; Y++) { int F = -pref[Y] + pref[X] + pref[L]; S += cnt[F + SHIFT]; } } } //now sub with equal memset(cnt, 0, sizeof cnt); for (int L = 0; L < n; L++) { for (int R = L + 1; R <= n; R++) { cnt[2 * (pref[R] - pref[L]) + SHIFT]++; } } for (int L = 0; L < n; L++) { for (int R = L + 1; R <= n; R++) { S -= 3 * cnt[(pref[L] - pref[R]) + SHIFT]; } } for (int L = 0; L < n; L++) { for (int R = L + 1; R <= n; R++) { if (pref[R] == pref[L]) S += 2; } } assert(S % 6 == 0); cout << S / 6; return 0; } |