#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef long long LL; #define pb push_back const int INF = 2147483647; const int N = 10000000; const int N2 = 40000000; const int M = 200000; LL res; int i, j, tab[505], sum[N2 + 5], s, n, diff[M], up; VI v; int main() { scanf("%d", &n); for (i=0;i<n;i++) scanf("%d", &tab[i]); for (i=0;i<n;i++) { s = 0; for (j=i;j<n;j++) { s += tab[j]; v.pb(s); } } for (i=1;i<v.size();i++) diff[i] = v[i] - v[i - 1]; res = 0; up = v.size(); for (i=0;i<up;i++) { s = -2 * v[i] + N; for (j=i+1;j<up;j++) { s -= diff[j]; res += sum[s]; } if (v[i] + N >= 0 && v[i] + N <= N2 / 2) sum[v[i] + N]++; } printf("%lld\n", res); 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 | #include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef long long LL; #define pb push_back const int INF = 2147483647; const int N = 10000000; const int N2 = 40000000; const int M = 200000; LL res; int i, j, tab[505], sum[N2 + 5], s, n, diff[M], up; VI v; int main() { scanf("%d", &n); for (i=0;i<n;i++) scanf("%d", &tab[i]); for (i=0;i<n;i++) { s = 0; for (j=i;j<n;j++) { s += tab[j]; v.pb(s); } } for (i=1;i<v.size();i++) diff[i] = v[i] - v[i - 1]; res = 0; up = v.size(); for (i=0;i<up;i++) { s = -2 * v[i] + N; for (j=i+1;j<up;j++) { s -= diff[j]; res += sum[s]; } if (v[i] + N >= 0 && v[i] + N <= N2 / 2) sum[v[i] + N]++; } printf("%lld\n", res); return 0; } |