#include <bits/stdc++.h>
#define int long long
using namespace std;
int Ccc(int n, int k) {
double res = 1;
for (int i = 1; i <= k; ++i)
res = res * (n - k + i) / i;
return (int)(res + 0.01);
}
int cc3(int n){
return Ccc(n, 3);
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector <int> a(n);
for(int i = 0; i < n; i++)
cin >> a[i];
vector <int> presum(n+2, 0);
presum[0];
for(int i = 1; i <= n; i++)
presum[i] = presum[i-1] + a[i-1];
vector <int> sums;
for(int i = 0; i < n; i++)
for(int j = i+1; j <= n; j++){
sums.push_back(presum[j] - presum [i]);
}
sort(sums.begin(), sums.end());
vector <int> sum2;
vector <int> mp(20000010);
for(auto x: sums){
mp[10000000+x]++;
if(mp[10000000+x] == 1)
sum2.push_back(x);
}
int c1 = 0, c2 = 0, c3 = 0;
for(auto x: sum2)
{
for(auto y: sum2)
{
int z = - x - y;
if(mp[10000000+z] > 0)
{
if(x == y and mp[10000000+x] < 2)
{}
else if(x == z and mp[10000000+x] < 2)
{}
else if(z == y and mp[10000000+z] < 2)
{}
else if(z == y and x == z and mp[10000000+z] < 3)
{}
else if(x != y and y != z and x != z)
{
c3 += mp[10000000+x]*mp[10000000+y]*mp[10000000+z];
}
else if(x == y and y == z){
c1 += cc3(mp[10000000+x]);
}
else if(x == y)
{
c2 += Ccc(mp[10000000+x], 2)*mp[10000000+z];
}
else if(x == z)
{
c2 += Ccc(mp[10000000+x], 2)*mp[10000000+y];
}
else if(z == y)
{
c2 += Ccc(mp[10000000+z], 2)*mp[10000000+x];
}
}
}
}
cout << c1 + c2/3 + c3/6;
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 | #include <bits/stdc++.h> #define int long long using namespace std; int Ccc(int n, int k) { double res = 1; for (int i = 1; i <= k; ++i) res = res * (n - k + i) / i; return (int)(res + 0.01); } int cc3(int n){ return Ccc(n, 3); } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector <int> a(n); for(int i = 0; i < n; i++) cin >> a[i]; vector <int> presum(n+2, 0); presum[0]; for(int i = 1; i <= n; i++) presum[i] = presum[i-1] + a[i-1]; vector <int> sums; for(int i = 0; i < n; i++) for(int j = i+1; j <= n; j++){ sums.push_back(presum[j] - presum [i]); } sort(sums.begin(), sums.end()); vector <int> sum2; vector <int> mp(20000010); for(auto x: sums){ mp[10000000+x]++; if(mp[10000000+x] == 1) sum2.push_back(x); } int c1 = 0, c2 = 0, c3 = 0; for(auto x: sum2) { for(auto y: sum2) { int z = - x - y; if(mp[10000000+z] > 0) { if(x == y and mp[10000000+x] < 2) {} else if(x == z and mp[10000000+x] < 2) {} else if(z == y and mp[10000000+z] < 2) {} else if(z == y and x == z and mp[10000000+z] < 3) {} else if(x != y and y != z and x != z) { c3 += mp[10000000+x]*mp[10000000+y]*mp[10000000+z]; } else if(x == y and y == z){ c1 += cc3(mp[10000000+x]); } else if(x == y) { c2 += Ccc(mp[10000000+x], 2)*mp[10000000+z]; } else if(x == z) { c2 += Ccc(mp[10000000+x], 2)*mp[10000000+y]; } else if(z == y) { c2 += Ccc(mp[10000000+z], 2)*mp[10000000+x]; } } } } cout << c1 + c2/3 + c3/6; return 0; } |
English