// // main.cpp // buc // // Created by Michal Kowalski on 16/03/2024. // #include <iostream> #include <vector> #include <algorithm> #include <unordered_map> using namespace std; long long calculate(); int N; int L[501]; vector<int> K; unordered_map<int, int> NK; int main() { scanf("%d",&N); int zero_counter = 0; for (int n=0;n<N;++n) { scanf("%d",&L[n]); if (L[n] == 0) zero_counter++; } // check if all are positive or negative then it's zero int minVal = L[0]; int maxVal = L[0]; for (int n=0;n<N;++n) { minVal = min(L[n], minVal); maxVal = max(L[n], maxVal); } if (maxVal < 0 || minVal > 0) { printf("0\n"); return 0; } if (zero_counter == N && N>10) { long long x = N+2; long long res = x*(x-1)*(x-2)*(x-3)*(x*x-3*x-2)/48; printf("%lld\n", res); return 0; } K.reserve(N*N); for (int a=0;a<N;++a) { int sum = L[a]; K.push_back(sum); for(int b=a+1;b<N;++b) { sum += L[b]; K.push_back(sum); } } printf("%lld\n",calculate()); return 0; } long long calculate() { long long counter = 0; int len = K.size(); for(auto i: K) { if (NK.contains(i)) { NK[i] = NK[i] + 1; } else { NK[i] = 1; } } for(int a = 0;a<len;++a) for (int b = a+1;b<len;++b) { int l1 = K[a]; int l2 = K[b]; int sum = l1 + l2; int find = -sum; if (NK.contains(find)) { int mult = NK[-sum]; if (find == l1) mult--; if (find == l2) mult--; // if (mult == 1 ) { // printf("%d + %d => %d\n",l1,l2,find); // } counter += (long long)mult; } } return counter/3; }
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 | // // main.cpp // buc // // Created by Michal Kowalski on 16/03/2024. // #include <iostream> #include <vector> #include <algorithm> #include <unordered_map> using namespace std; long long calculate(); int N; int L[501]; vector<int> K; unordered_map<int, int> NK; int main() { scanf("%d",&N); int zero_counter = 0; for (int n=0;n<N;++n) { scanf("%d",&L[n]); if (L[n] == 0) zero_counter++; } // check if all are positive or negative then it's zero int minVal = L[0]; int maxVal = L[0]; for (int n=0;n<N;++n) { minVal = min(L[n], minVal); maxVal = max(L[n], maxVal); } if (maxVal < 0 || minVal > 0) { printf("0\n"); return 0; } if (zero_counter == N && N>10) { long long x = N+2; long long res = x*(x-1)*(x-2)*(x-3)*(x*x-3*x-2)/48; printf("%lld\n", res); return 0; } K.reserve(N*N); for (int a=0;a<N;++a) { int sum = L[a]; K.push_back(sum); for(int b=a+1;b<N;++b) { sum += L[b]; K.push_back(sum); } } printf("%lld\n",calculate()); return 0; } long long calculate() { long long counter = 0; int len = K.size(); for(auto i: K) { if (NK.contains(i)) { NK[i] = NK[i] + 1; } else { NK[i] = 1; } } for(int a = 0;a<len;++a) for (int b = a+1;b<len;++b) { int l1 = K[a]; int l2 = K[b]; int sum = l1 + l2; int find = -sum; if (NK.contains(find)) { int mult = NK[-sum]; if (find == l1) mult--; if (find == l2) mult--; // if (mult == 1 ) { // printf("%d + %d => %d\n",l1,l2,find); // } counter += (long long)mult; } } return counter/3; } |