#include<cstdio> #include<algorithm> #include<map> #include<unordered_map> #define S 507 #define S2 10'000'000 using namespace std; typedef long long ll; int pref[S]; // unordered_map<int,int> M; // unordered_map<int,int> M2; int inf = 3 * S2 + S2/2; int M[7*S2]; int M2[7*S2]; int main(void){ int n; scanf("%d", &n); for(int i = 1; i <= n; i++){ int x; scanf("%d", &x); pref[i] = pref[i-1] + x; } ll ans = 0; ll twoSame1 = 0; ll threeSame = 0; for(int i = 0; i <= n; i++){ for(int j = i+1; j <= n; j++){ int p = pref[j] - pref[i]; if(p == 0){ threeSame++; } M2[p + inf]++; } } for(int i = 0; i <= n; i++){ for(int j = i+1; j <= n; j++){ int p = pref[j] - pref[i]; p *= 2; if(p == 0){ twoSame1 += M2[-p + inf] - 1; }else{ twoSame1 += M2[-p + inf]; } } } for(int j = 0; j <= n; j++){ for(int k = j+1; k <= n; k++){ int p = pref[k] - pref[j]; M[p + inf]++; } } for(int i = 1; i <= n; i++){ for(int j = 0; j <= n; j++){ for(int k = j+1; k <= n; k++){ int p = pref[k] - pref[j] + pref[i]; ans += M[-p + inf]; } } //teraz dodajemy for(int j = 0; j <= n; j++){ for(int k = j+1; k <= n; k++){ int p = pref[k] - pref[j] - pref[i]; M[p + inf]++; } } } // twoSame2 += twoSame3; // twoSame1 -= twoSame2; twoSame1 *= 3; ans -= twoSame1; ans -= threeSame; ans /= 6; printf("%lld\n",ans); // printf("%lld %lld %lld %lld %lld*\n",ans,twoSame1, twoSame2, twoSame3, threeSame); // printf("%lld\n",ans); 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | #include<cstdio> #include<algorithm> #include<map> #include<unordered_map> #define S 507 #define S2 10'000'000 using namespace std; typedef long long ll; int pref[S]; // unordered_map<int,int> M; // unordered_map<int,int> M2; int inf = 3 * S2 + S2/2; int M[7*S2]; int M2[7*S2]; int main(void){ int n; scanf("%d", &n); for(int i = 1; i <= n; i++){ int x; scanf("%d", &x); pref[i] = pref[i-1] + x; } ll ans = 0; ll twoSame1 = 0; ll threeSame = 0; for(int i = 0; i <= n; i++){ for(int j = i+1; j <= n; j++){ int p = pref[j] - pref[i]; if(p == 0){ threeSame++; } M2[p + inf]++; } } for(int i = 0; i <= n; i++){ for(int j = i+1; j <= n; j++){ int p = pref[j] - pref[i]; p *= 2; if(p == 0){ twoSame1 += M2[-p + inf] - 1; }else{ twoSame1 += M2[-p + inf]; } } } for(int j = 0; j <= n; j++){ for(int k = j+1; k <= n; k++){ int p = pref[k] - pref[j]; M[p + inf]++; } } for(int i = 1; i <= n; i++){ for(int j = 0; j <= n; j++){ for(int k = j+1; k <= n; k++){ int p = pref[k] - pref[j] + pref[i]; ans += M[-p + inf]; } } //teraz dodajemy for(int j = 0; j <= n; j++){ for(int k = j+1; k <= n; k++){ int p = pref[k] - pref[j] - pref[i]; M[p + inf]++; } } } // twoSame2 += twoSame3; // twoSame1 -= twoSame2; twoSame1 *= 3; ans -= twoSame1; ans -= threeSame; ans /= 6; printf("%lld\n",ans); // printf("%lld %lld %lld %lld %lld*\n",ans,twoSame1, twoSame2, twoSame3, threeSame); // printf("%lld\n",ans); return 0; } |