#include <bits/stdc++.h> using namespace std; #define ll long long #define maxn 510 #define maxa 30 #define maxv 20010 #define result_t pair<bool, pair<int,char>> #define cd complex<double> const double PI = acos(-1); int tab[maxn]; void fft(vector<cd> &a, bool invert) { int n = a.size(); for (int i = 1, j = 0; i < n; i++) { int bit = n >> 1; for (; j & bit; bit >>= 1) j ^= bit; j ^= bit; if (i < j) swap(a[i], a[j]); } for (int len = 2; len <= n; len <<= 1) { double ang = 2 * PI / len * (invert ? -1 : 1); cd wlen(cos(ang), sin(ang)); for (int i = 0; i < n; i += len) { cd w(1); for (int j = 0; j < len / 2; j++) { cd u = a[i+j], v = a[i+j+len/2] * w; a[i+j] = u + v; a[i+j+len/2] = u - v; w *= wlen; } } } if (invert) { for (cd & x : a) x /= n; } } vector<cd> multiply(vector<int> &a) { int n = 1; while (n < 2 * (int)a.size()) { n <<= 1; } vector<cd> fa; fa.reserve(n); copy(a.begin(), a.end(), back_inserter(fa)); // while((int)fa.size() != n) { // fa.push_back(0); // } // fa.assign(a.begin(), a.end()); //cout << fa.size() << endl; //cout << n << endl; fa.resize(n); fft(fa, false); //exit(0); for (int i = 0; i < n; i++) { fa[i] *= fa[i]; } fft(fa, true); // vector<ll> result(n); // for (int i = 0; i < n; ++i) { // result[i] = round(fa[i].real()); // } return fa; } void dec (vector<cd> &x, vector<int> &y) { for (int i = 0; i < (int)y.size(); ++i) { x[2*i] -= y[i]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for (int i = 1; i <= n; ++i) { cin >> tab[i]; tab[i] += tab[i-1]; } int offset = 0, maxi=0; for (int i = 0; i < n; ++i) { for (int j = i + 1; j <= n; ++j) { offset = min(offset, tab[j]-tab[i]); maxi = max(maxi, tab[j]-tab[i]); } } offset *= -1; int offset_3 = 3 * offset; vector<int> one(offset + maxi + 3); for (int i = 0; i < n; ++i) { for (int j = i + 1; j <= n; ++j) { int sum = tab[j]-tab[i] + offset; one[sum]++; //one1[sum]++; } } auto two = multiply(one); dec(two, one); ll res = 0; for (int i = 0; i < (int)one.size(); ++i) { if (one[i] == 0) { continue; } int oposite = -i + offset_3; if (oposite < (int)two.size() && oposite >= 0) { res += (ll)one[i] * round(two[oposite].real()) / 2; int oposite_2 = -2 * i + offset_3; if (oposite_2 < (int)one.size() && oposite_2 >= 0) { if (oposite_2 != i) { res -= (ll)one[i] * (ll)one[oposite_2]; } else { res -= (ll)one[i] * (ll)(one[i]-1); } } } } cout << res / 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 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 | #include <bits/stdc++.h> using namespace std; #define ll long long #define maxn 510 #define maxa 30 #define maxv 20010 #define result_t pair<bool, pair<int,char>> #define cd complex<double> const double PI = acos(-1); int tab[maxn]; void fft(vector<cd> &a, bool invert) { int n = a.size(); for (int i = 1, j = 0; i < n; i++) { int bit = n >> 1; for (; j & bit; bit >>= 1) j ^= bit; j ^= bit; if (i < j) swap(a[i], a[j]); } for (int len = 2; len <= n; len <<= 1) { double ang = 2 * PI / len * (invert ? -1 : 1); cd wlen(cos(ang), sin(ang)); for (int i = 0; i < n; i += len) { cd w(1); for (int j = 0; j < len / 2; j++) { cd u = a[i+j], v = a[i+j+len/2] * w; a[i+j] = u + v; a[i+j+len/2] = u - v; w *= wlen; } } } if (invert) { for (cd & x : a) x /= n; } } vector<cd> multiply(vector<int> &a) { int n = 1; while (n < 2 * (int)a.size()) { n <<= 1; } vector<cd> fa; fa.reserve(n); copy(a.begin(), a.end(), back_inserter(fa)); // while((int)fa.size() != n) { // fa.push_back(0); // } // fa.assign(a.begin(), a.end()); //cout << fa.size() << endl; //cout << n << endl; fa.resize(n); fft(fa, false); //exit(0); for (int i = 0; i < n; i++) { fa[i] *= fa[i]; } fft(fa, true); // vector<ll> result(n); // for (int i = 0; i < n; ++i) { // result[i] = round(fa[i].real()); // } return fa; } void dec (vector<cd> &x, vector<int> &y) { for (int i = 0; i < (int)y.size(); ++i) { x[2*i] -= y[i]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for (int i = 1; i <= n; ++i) { cin >> tab[i]; tab[i] += tab[i-1]; } int offset = 0, maxi=0; for (int i = 0; i < n; ++i) { for (int j = i + 1; j <= n; ++j) { offset = min(offset, tab[j]-tab[i]); maxi = max(maxi, tab[j]-tab[i]); } } offset *= -1; int offset_3 = 3 * offset; vector<int> one(offset + maxi + 3); for (int i = 0; i < n; ++i) { for (int j = i + 1; j <= n; ++j) { int sum = tab[j]-tab[i] + offset; one[sum]++; //one1[sum]++; } } auto two = multiply(one); dec(two, one); ll res = 0; for (int i = 0; i < (int)one.size(); ++i) { if (one[i] == 0) { continue; } int oposite = -i + offset_3; if (oposite < (int)two.size() && oposite >= 0) { res += (ll)one[i] * round(two[oposite].real()) / 2; int oposite_2 = -2 * i + offset_3; if (oposite_2 < (int)one.size() && oposite_2 >= 0) { if (oposite_2 != i) { res -= (ll)one[i] * (ll)one[oposite_2]; } else { res -= (ll)one[i] * (ll)(one[i]-1); } } } } cout << res / 3; } |