#include <cstdio> #include <cstring> #include <vector> #include <algorithm> #include <map> using namespace std; typedef unsigned long long ull; constexpr int maxn = 505; constexpr int maxsize = maxn*(maxn+1)/2; int n; int k; int a[maxn]; int buc[maxsize]; int num_zeros; vector<int> neg; vector<int> pos; map<int,int> mpos; map<int,int> mneg; int max_key_pos; int max_key_neg; void print_tab(int* tab, int n) { for(int i = 1; i <= n; ++i) { printf("%d\n", tab[i]); } printf("#####\n"); } void print_vect(vector<int> v) { for(auto x : v) { printf("%d\n", x); } printf("#####\n"); } void gen_buc() { int pos = 1; for(int i = 1; i <= n; ++i) { buc[pos++] = a[i]; for(int j = i+1; j <= n; ++j) { buc[pos] = buc[pos-1] + a[j]; ++pos; } } } void split_pos_neg() { for(int i = 1; i <= k; ++i) { if (buc[i] == 0) { ++num_zeros; continue; } if (buc[i] > 0) { pos.push_back(buc[i]); } else { neg.push_back(-buc[i]); } } } ull calc_3_or_more_zeros() { ull z = num_zeros; if (num_zeros < 3) { return 0; } return (((z*(z-1))/2)*(z-2))/3; } ull calc_1_zero() { ull res=0; if (num_zeros < 1) { return 0; } for ( const auto &[key, value] : mpos ) { if (mneg.contains(key)) { res += ((ull) value) * ((ull) mneg[key]); } } return res; } ull calc_no_zeros() { ull res=0; for ( const auto &[key, value] : mpos ) { auto it = mpos.lower_bound(key); if (value >= 2) { if (mneg.contains(key*2)) { res += value * (value-1)/2 * mneg[key*2]; } } ++it; for(; it != mpos.end(); ++it) { int sum; int key2 = it->first; int value2 = it->second; sum = key + key2; if (sum > max_key_neg) { break; } if (mneg.contains(sum)) { res += value * value2 * mneg[sum]; } } } for ( const auto &[key, value] : mneg ) { auto it = mneg.lower_bound(key); if (value >= 2) { if (mpos.contains(key*2)) { res += value * (value-1)/2 * mpos[key*2]; } } ++it; for(; it != mneg.end(); ++it) { int key2 = it->first; int value2 = it->second; int sum; sum = key + key2; if (sum > max_key_pos) { break; } if (mpos.contains(sum)) { res += value * value2 * mpos[sum]; } } } return res; } void gen_maps() { for(auto x : pos) { if (mpos.contains(x)) { mpos[x]++; } else { mpos.insert( pair<int,int>(x,1) ); } } for(auto x : neg) { if (mneg.contains(x)) { mneg[x]++; } else { mneg.insert( pair<int,int>(x,1) ); } } if (mpos.size() > 0) { max_key_pos = mpos.rbegin()->first; } if (mneg.size() > 0) { max_key_neg = mneg.rbegin()->first; } } ull calc_result() { ull res=0; gen_buc(); // print_tab(buc, k); split_pos_neg(); // sort(pos.begin(), pos.end()); // sort(neg.begin(), neg.end()); gen_maps(); // print_vect(neg); // print_vect(pos); res += calc_3_or_more_zeros(); res += calc_1_zero(); res += calc_no_zeros(); return res; } int main() { scanf("%d\n", &n); k = n*(n+1)/2; for(int i = 1; i <= n; ++i) { scanf("%d", a+i); } printf("%llu\n", calc_result()); 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | #include <cstdio> #include <cstring> #include <vector> #include <algorithm> #include <map> using namespace std; typedef unsigned long long ull; constexpr int maxn = 505; constexpr int maxsize = maxn*(maxn+1)/2; int n; int k; int a[maxn]; int buc[maxsize]; int num_zeros; vector<int> neg; vector<int> pos; map<int,int> mpos; map<int,int> mneg; int max_key_pos; int max_key_neg; void print_tab(int* tab, int n) { for(int i = 1; i <= n; ++i) { printf("%d\n", tab[i]); } printf("#####\n"); } void print_vect(vector<int> v) { for(auto x : v) { printf("%d\n", x); } printf("#####\n"); } void gen_buc() { int pos = 1; for(int i = 1; i <= n; ++i) { buc[pos++] = a[i]; for(int j = i+1; j <= n; ++j) { buc[pos] = buc[pos-1] + a[j]; ++pos; } } } void split_pos_neg() { for(int i = 1; i <= k; ++i) { if (buc[i] == 0) { ++num_zeros; continue; } if (buc[i] > 0) { pos.push_back(buc[i]); } else { neg.push_back(-buc[i]); } } } ull calc_3_or_more_zeros() { ull z = num_zeros; if (num_zeros < 3) { return 0; } return (((z*(z-1))/2)*(z-2))/3; } ull calc_1_zero() { ull res=0; if (num_zeros < 1) { return 0; } for ( const auto &[key, value] : mpos ) { if (mneg.contains(key)) { res += ((ull) value) * ((ull) mneg[key]); } } return res; } ull calc_no_zeros() { ull res=0; for ( const auto &[key, value] : mpos ) { auto it = mpos.lower_bound(key); if (value >= 2) { if (mneg.contains(key*2)) { res += value * (value-1)/2 * mneg[key*2]; } } ++it; for(; it != mpos.end(); ++it) { int sum; int key2 = it->first; int value2 = it->second; sum = key + key2; if (sum > max_key_neg) { break; } if (mneg.contains(sum)) { res += value * value2 * mneg[sum]; } } } for ( const auto &[key, value] : mneg ) { auto it = mneg.lower_bound(key); if (value >= 2) { if (mpos.contains(key*2)) { res += value * (value-1)/2 * mpos[key*2]; } } ++it; for(; it != mneg.end(); ++it) { int key2 = it->first; int value2 = it->second; int sum; sum = key + key2; if (sum > max_key_pos) { break; } if (mpos.contains(sum)) { res += value * value2 * mpos[sum]; } } } return res; } void gen_maps() { for(auto x : pos) { if (mpos.contains(x)) { mpos[x]++; } else { mpos.insert( pair<int,int>(x,1) ); } } for(auto x : neg) { if (mneg.contains(x)) { mneg[x]++; } else { mneg.insert( pair<int,int>(x,1) ); } } if (mpos.size() > 0) { max_key_pos = mpos.rbegin()->first; } if (mneg.size() > 0) { max_key_neg = mneg.rbegin()->first; } } ull calc_result() { ull res=0; gen_buc(); // print_tab(buc, k); split_pos_neg(); // sort(pos.begin(), pos.end()); // sort(neg.begin(), neg.end()); gen_maps(); // print_vect(neg); // print_vect(pos); res += calc_3_or_more_zeros(); res += calc_1_zero(); res += calc_no_zeros(); return res; } int main() { scanf("%d\n", &n); k = n*(n+1)/2; for(int i = 1; i <= n; ++i) { scanf("%d", a+i); } printf("%llu\n", calc_result()); return 0; } |