#include <bits/stdc++.h> using namespace std; constexpr int maxN = 500; int n; int a[maxN + 1]; void fft(vector<complex<long double>> &v, bool invert = false) { int sz = v.size(); for(int i=1,j=0; i<sz; i++) { int bit = sz >> 1; for(; j&bit; bit>>=1) { j ^= bit; } j ^= bit; if(i < j) { swap(v[i], v[j]); } } for(int len=2; len<=sz; len<<=1) { long double ang = 2 * M_PI / len * (invert ? -1 : 1); complex<long double> wlen(cos(ang), sin(ang)); for(int i=0; i<sz; i+=len) { complex<long double> w(1); for(int j=0; j<(len>>1); j++) { complex<long double> u1 = v[i + j]; complex<long double> u2 = v[i + j + (len >> 1)] * w; v[i + j] = u1 + u2; v[i + j + (len >> 1)] = u1 - u2; w *= wlen; } } } if(invert) { for(complex<long double> &x : v) { x /= sz; } } } vector<long long> multiply(vector<long long> &v1, vector<long long> &v2) { vector<complex<long double>> f1(v1.begin(), v1.end()); vector<complex<long double>> f2(v2.begin(), v2.end()); int sz = 1; while(sz < v1.size() + v2.size()) { sz <<= 1; } f1.resize(sz); f2.resize(sz); fft(f1); fft(f2); for(int i=0; i<sz; i++) { f1[i] *= f2[i]; } fft(f1, true); vector<long long> res; for(int i=0; i<sz; i++) { res.push_back(round(f1[i].real())); } return res; } long long solve_fft(int min_sum, int max_sum) { int sz = max_sum - min_sum + 1; vector<long long> cnt(sz, 0); for(int i=1; i<=n; i++) { int sum = 0; for(int j=i; j<=n; j++) { sum += a[j]; cnt[sum - min_sum]++; } } vector<long long> cnt_sq = multiply(cnt, cnt); while(!cnt_sq.empty() && cnt_sq.back() == 0) { cnt_sq.pop_back(); } cnt.resize(sz - min_sum); for(int i=sz-min_sum-1; i>=-min_sum; i--) { cnt[i] = cnt[i + min_sum]; } for(int i=0; i<-min_sum; i++) { cnt[i] = 0; } vector<long long> cnt_cb = multiply(cnt_sq, cnt); for(int i=min_sum; i<=max_sum; i++) { if(i == 0) { cnt_cb[-min_sum << 2] -= 3 * cnt[-min_sum << 1] * (cnt[-min_sum << 1] - 1); } else if(-((i + min_sum) << 1) < cnt.size()) { cnt_cb[-min_sum << 2] -= 3 * cnt[i - (min_sum << 1)] * cnt[-((i + min_sum) << 1)]; } } cnt_cb[-min_sum << 2] -= cnt[-min_sum << 1]; return cnt_cb[-min_sum << 2] / 6; } long long choose_2(int &val) { return (long long)val * (val - 1) / 2; } long long choose_3(int &val) { return (long long)val * (val - 1) * (val - 2) / 6; } long long solve_brute(int min_sum, int max_sum) { int sz = max_sum - min_sum + 1; vector<int> cnt(sz); for(int i=1; i<=n; i++) { int sum = 0; for(int j=i; j<=n; j++) { sum += a[j]; cnt[sum - min_sum]++; } } vector<int> sums; for(int i=0; i<sz; i++) { if(cnt[i] > 0) { sums.push_back(i); } } long long res = 0; for(int i=0; i<(int)sums.size(); i++) { for(int j=i; j<(int)sums.size(); j++) { int sum = sums[i] + sums[j]; int neg = -(sum + (3 * min_sum)); if(sums[j] <= neg) { if(neg < sz) { if(i == j) { if(sums[j] == neg) { res += choose_3(cnt[sums[i]]); } else { res += choose_2(cnt[sums[i]]) * cnt[neg]; } } else { if(sums[j] == neg) { res += (long long)cnt[sums[i]] * choose_2(cnt[sums[j]]); } else { res += (long long)cnt[sums[i]] * cnt[sums[j]] * cnt[neg]; } } } } else { break; } } } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; for(int i=1; i<=n; i++) { cin >> a[i]; } int min_sum = 0, max_sum = 0; set<int> sums; for(int i=1; i<=n; i++) { int sum = 0; for(int j=i; j<=n; j++) { sum += a[j]; min_sum = min(min_sum, sum); max_sum = max(max_sum, sum); sums.insert(sum); } } if((long long)sums.size() * sums.size() <= 1e10) { cout << solve_brute(min_sum, max_sum); return 0; } cout << solve_fft(min_sum, max_sum); }
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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | #include <bits/stdc++.h> using namespace std; constexpr int maxN = 500; int n; int a[maxN + 1]; void fft(vector<complex<long double>> &v, bool invert = false) { int sz = v.size(); for(int i=1,j=0; i<sz; i++) { int bit = sz >> 1; for(; j&bit; bit>>=1) { j ^= bit; } j ^= bit; if(i < j) { swap(v[i], v[j]); } } for(int len=2; len<=sz; len<<=1) { long double ang = 2 * M_PI / len * (invert ? -1 : 1); complex<long double> wlen(cos(ang), sin(ang)); for(int i=0; i<sz; i+=len) { complex<long double> w(1); for(int j=0; j<(len>>1); j++) { complex<long double> u1 = v[i + j]; complex<long double> u2 = v[i + j + (len >> 1)] * w; v[i + j] = u1 + u2; v[i + j + (len >> 1)] = u1 - u2; w *= wlen; } } } if(invert) { for(complex<long double> &x : v) { x /= sz; } } } vector<long long> multiply(vector<long long> &v1, vector<long long> &v2) { vector<complex<long double>> f1(v1.begin(), v1.end()); vector<complex<long double>> f2(v2.begin(), v2.end()); int sz = 1; while(sz < v1.size() + v2.size()) { sz <<= 1; } f1.resize(sz); f2.resize(sz); fft(f1); fft(f2); for(int i=0; i<sz; i++) { f1[i] *= f2[i]; } fft(f1, true); vector<long long> res; for(int i=0; i<sz; i++) { res.push_back(round(f1[i].real())); } return res; } long long solve_fft(int min_sum, int max_sum) { int sz = max_sum - min_sum + 1; vector<long long> cnt(sz, 0); for(int i=1; i<=n; i++) { int sum = 0; for(int j=i; j<=n; j++) { sum += a[j]; cnt[sum - min_sum]++; } } vector<long long> cnt_sq = multiply(cnt, cnt); while(!cnt_sq.empty() && cnt_sq.back() == 0) { cnt_sq.pop_back(); } cnt.resize(sz - min_sum); for(int i=sz-min_sum-1; i>=-min_sum; i--) { cnt[i] = cnt[i + min_sum]; } for(int i=0; i<-min_sum; i++) { cnt[i] = 0; } vector<long long> cnt_cb = multiply(cnt_sq, cnt); for(int i=min_sum; i<=max_sum; i++) { if(i == 0) { cnt_cb[-min_sum << 2] -= 3 * cnt[-min_sum << 1] * (cnt[-min_sum << 1] - 1); } else if(-((i + min_sum) << 1) < cnt.size()) { cnt_cb[-min_sum << 2] -= 3 * cnt[i - (min_sum << 1)] * cnt[-((i + min_sum) << 1)]; } } cnt_cb[-min_sum << 2] -= cnt[-min_sum << 1]; return cnt_cb[-min_sum << 2] / 6; } long long choose_2(int &val) { return (long long)val * (val - 1) / 2; } long long choose_3(int &val) { return (long long)val * (val - 1) * (val - 2) / 6; } long long solve_brute(int min_sum, int max_sum) { int sz = max_sum - min_sum + 1; vector<int> cnt(sz); for(int i=1; i<=n; i++) { int sum = 0; for(int j=i; j<=n; j++) { sum += a[j]; cnt[sum - min_sum]++; } } vector<int> sums; for(int i=0; i<sz; i++) { if(cnt[i] > 0) { sums.push_back(i); } } long long res = 0; for(int i=0; i<(int)sums.size(); i++) { for(int j=i; j<(int)sums.size(); j++) { int sum = sums[i] + sums[j]; int neg = -(sum + (3 * min_sum)); if(sums[j] <= neg) { if(neg < sz) { if(i == j) { if(sums[j] == neg) { res += choose_3(cnt[sums[i]]); } else { res += choose_2(cnt[sums[i]]) * cnt[neg]; } } else { if(sums[j] == neg) { res += (long long)cnt[sums[i]] * choose_2(cnt[sums[j]]); } else { res += (long long)cnt[sums[i]] * cnt[sums[j]] * cnt[neg]; } } } } else { break; } } } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; for(int i=1; i<=n; i++) { cin >> a[i]; } int min_sum = 0, max_sum = 0; set<int> sums; for(int i=1; i<=n; i++) { int sum = 0; for(int j=i; j<=n; j++) { sum += a[j]; min_sum = min(min_sum, sum); max_sum = max(max_sum, sum); sums.insert(sum); } } if((long long)sums.size() * sums.size() <= 1e10) { cout << solve_brute(min_sum, max_sum); return 0; } cout << solve_fft(min_sum, max_sum); } |