// #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") #include <bits/stdc++.h> using namespace std; #define PB emplace_back // #define int long long #define ll long long #define vi vector<int> #define siz(a) ((int) ((a).size())) #define rep(i, a, b) for (int i = (a); i <= (b); ++i) #define per(i, a, b) for (int i = (a); i >= (b); --i) void print(vi n) { rep(i, 0, siz(n) - 1) cerr << n[i] << " \n"[i == siz(n) - 1]; } const int N = 500, M = 3e7; int a, s[N + 5]; struct node { int s[M + M + 5]; int& operator[](const int &n) {return s[n + M];} } p; signed main() { // freopen(".in", "r", stdin); // freopen(".out", "w", stdout); ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> a; rep(i, 1, a) cin >> s[i], s[i] += s[i - 1]; rep(i, 1, a) rep(j, 1, a) rep(k, j, a) ++p[s[i] + s[k] - s[j - 1]]; ll ans = 0; rep(i, 1, a) { rep(j, 1, a) rep(k, j, a) { ans += p[-(-s[i - 1] + s[k] - s[j - 1])]; // if(p[-(-s[i - 1] + s[k] - s[j - 1])]) cout << i << ' ' << j << ' ' << k << ' ' << -s[i - 1] + s[k] - s[j - 1] << endl; } rep(j, 1, a) rep(k, j, a) --p[s[i] + s[k] - s[j - 1]]; } rep(i, 1, a) rep(j, i, a) if(s[j] - s[i - 1] == 0) --ans; ans /= 3; rep(i, 1, a) rep(j, i, a) { ans -= p[s[i - 1] - s[j]]; ++p[2 * (s[j] - s[i - 1])]; } memset(p.s, 0, sizeof(p.s)); rep(i, 1, a) rep(j, i, a) { ans -= p[2 * (s[i - 1] - s[j])]; ++p[s[j] - s[i - 1]]; } ans /= 2; cout << ans; return cerr << endl << 1.0 * clock() / CLOCKS_PER_SEC << endl, 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 | // #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") #include <bits/stdc++.h> using namespace std; #define PB emplace_back // #define int long long #define ll long long #define vi vector<int> #define siz(a) ((int) ((a).size())) #define rep(i, a, b) for (int i = (a); i <= (b); ++i) #define per(i, a, b) for (int i = (a); i >= (b); --i) void print(vi n) { rep(i, 0, siz(n) - 1) cerr << n[i] << " \n"[i == siz(n) - 1]; } const int N = 500, M = 3e7; int a, s[N + 5]; struct node { int s[M + M + 5]; int& operator[](const int &n) {return s[n + M];} } p; signed main() { // freopen(".in", "r", stdin); // freopen(".out", "w", stdout); ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> a; rep(i, 1, a) cin >> s[i], s[i] += s[i - 1]; rep(i, 1, a) rep(j, 1, a) rep(k, j, a) ++p[s[i] + s[k] - s[j - 1]]; ll ans = 0; rep(i, 1, a) { rep(j, 1, a) rep(k, j, a) { ans += p[-(-s[i - 1] + s[k] - s[j - 1])]; // if(p[-(-s[i - 1] + s[k] - s[j - 1])]) cout << i << ' ' << j << ' ' << k << ' ' << -s[i - 1] + s[k] - s[j - 1] << endl; } rep(j, 1, a) rep(k, j, a) --p[s[i] + s[k] - s[j - 1]]; } rep(i, 1, a) rep(j, i, a) if(s[j] - s[i - 1] == 0) --ans; ans /= 3; rep(i, 1, a) rep(j, i, a) { ans -= p[s[i - 1] - s[j]]; ++p[2 * (s[j] - s[i - 1])]; } memset(p.s, 0, sizeof(p.s)); rep(i, 1, a) rep(j, i, a) { ans -= p[2 * (s[i - 1] - s[j])]; ++p[s[j] - s[i - 1]]; } ans /= 2; cout << ans; return cerr << endl << 1.0 * clock() / CLOCKS_PER_SEC << endl, 0; } |