#include <bits/stdc++.h> using namespace std; #define int long long int n, ans; int arr[507]; vector<int> v; constexpr int MAXW = 1e7; int ile[2*MAXW+7]; void init(){ for(int i=1; i<=n; i++){ int act = 0; for(int j=i; j<=n; j++){ act += arr[j]; v.push_back(act); ile[act+MAXW]++; } } sort(v.begin(), v.end()); } set<tuple<int, int, int>> z; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for(int i=1; i<=n; i++) cin >> arr[i]; init(); for(int i=0; i<v.size()-2; i++){ int a = v[i]; int l = i+1, r = v.size()-1; while(l < r){ int b = v[l], c = v[r]; if(a+b+c > 0) r--; else if(a+b+c < 0) l++; else{ z.insert(make_tuple(a,b,c)); l++; r--; } } } for(auto i : z){ int tmp = ile[get<0>(i)+MAXW]*ile[get<1>(i)+MAXW]*ile[get<2>(i)+MAXW]; if(get<0>(i) == get<1>(i) && get<1>(i) == get<2>(i)){ int x = ile[get<0>(i)+MAXW]; tmp = x*(x-1LL)*(x-2LL)/6LL; } else if(get<0>(i) == get<1>(i)){ int x = ile[get<0>(i)+MAXW]; tmp = x*(x-1LL) * get<2>(i); } else if(get<1>(i) == get<2>(i)){ int x = ile[get<1>(i)+MAXW]; tmp = x*(x-1LL) * get<0>(i); } ans += tmp; } cout << ans << '\n'; 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 | #include <bits/stdc++.h> using namespace std; #define int long long int n, ans; int arr[507]; vector<int> v; constexpr int MAXW = 1e7; int ile[2*MAXW+7]; void init(){ for(int i=1; i<=n; i++){ int act = 0; for(int j=i; j<=n; j++){ act += arr[j]; v.push_back(act); ile[act+MAXW]++; } } sort(v.begin(), v.end()); } set<tuple<int, int, int>> z; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for(int i=1; i<=n; i++) cin >> arr[i]; init(); for(int i=0; i<v.size()-2; i++){ int a = v[i]; int l = i+1, r = v.size()-1; while(l < r){ int b = v[l], c = v[r]; if(a+b+c > 0) r--; else if(a+b+c < 0) l++; else{ z.insert(make_tuple(a,b,c)); l++; r--; } } } for(auto i : z){ int tmp = ile[get<0>(i)+MAXW]*ile[get<1>(i)+MAXW]*ile[get<2>(i)+MAXW]; if(get<0>(i) == get<1>(i) && get<1>(i) == get<2>(i)){ int x = ile[get<0>(i)+MAXW]; tmp = x*(x-1LL)*(x-2LL)/6LL; } else if(get<0>(i) == get<1>(i)){ int x = ile[get<0>(i)+MAXW]; tmp = x*(x-1LL) * get<2>(i); } else if(get<1>(i) == get<2>(i)){ int x = ile[get<1>(i)+MAXW]; tmp = x*(x-1LL) * get<0>(i); } ans += tmp; } cout << ans << '\n'; return 0; } |