#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <map>
int n;
int a[500];
std::vector<int>sums;
std::vector<int>p1;
std::vector<int>p2;
int inf=1e9;
long long result=0;
void te(bool en=false){
std::sort(p1.begin(),p1.end());
std::sort(p2.begin(),p2.end());
p1.emplace_back(inf);
p2.emplace_back(inf);
int i=0;
int j=0;
while(true){
while(p1[i]<p2[j])
i++;
while(p2[j]<p1[i])
j++;
if(p1[i]==inf)
break;
long long a=0;
long long b=0;
if(p1[i]==p2[j]){
while(p1[i]==p2[j]) {
i++;
a++;
}
i--;
while(p1[i]==p2[j]) {
j++;
b++;
}
i++;
}
if(!en)
result+=a*b;
else
result-=3*a*b;
//std::cout<<a<<" "<<b<<std::endl;
}
}
void test(int p,int k){
p1.clear();
p2.clear();
int sr=(k+p)/2;
for(auto x:sums){
int acc=a[sr];
p1.emplace_back(x+acc);
for(int i=sr-1;i>=p;i--){
acc+=a[i];
p1.emplace_back(x+acc);
}
acc=0;
p2.emplace_back(-x);
for(int i=sr+1;i<k;i++){
acc-=a[i];
p2.emplace_back(acc-x);
}
}
te();
if(sr>p)
test(p,sr);
if(sr+1<k)
test(sr+1,k);
}
int main() {
std::ios_base::sync_with_stdio(0);
std::cin.tie(NULL);
//std::cerr<<euklides(mod,2)<<std::endl;
std::cin >> n;
for (int i = 0; i < n; i++)
std::cin >> a[i];
for (int i = 0; i < n; i++) {
int acc = 0;
for (int j = i; j < n; j++) {
acc += a[j];
sums.emplace_back(acc);
//std::cout << acc << std::endl;
}
}
sort(sums.begin(), sums.end());
test(0, n);
p1.clear();
p2.clear();
for (auto x: sums) {
if (x == 0)
result+=2;
p1.emplace_back(-x);
p2.emplace_back(x*2);
}
//std::cout<<"fin"<<std::endl;
te(true);
std::cout<<result/6<<std::endl;
}
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 | #include <iostream> #include <vector> #include <set> #include <algorithm> #include <map> int n; int a[500]; std::vector<int>sums; std::vector<int>p1; std::vector<int>p2; int inf=1e9; long long result=0; void te(bool en=false){ std::sort(p1.begin(),p1.end()); std::sort(p2.begin(),p2.end()); p1.emplace_back(inf); p2.emplace_back(inf); int i=0; int j=0; while(true){ while(p1[i]<p2[j]) i++; while(p2[j]<p1[i]) j++; if(p1[i]==inf) break; long long a=0; long long b=0; if(p1[i]==p2[j]){ while(p1[i]==p2[j]) { i++; a++; } i--; while(p1[i]==p2[j]) { j++; b++; } i++; } if(!en) result+=a*b; else result-=3*a*b; //std::cout<<a<<" "<<b<<std::endl; } } void test(int p,int k){ p1.clear(); p2.clear(); int sr=(k+p)/2; for(auto x:sums){ int acc=a[sr]; p1.emplace_back(x+acc); for(int i=sr-1;i>=p;i--){ acc+=a[i]; p1.emplace_back(x+acc); } acc=0; p2.emplace_back(-x); for(int i=sr+1;i<k;i++){ acc-=a[i]; p2.emplace_back(acc-x); } } te(); if(sr>p) test(p,sr); if(sr+1<k) test(sr+1,k); } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); //std::cerr<<euklides(mod,2)<<std::endl; std::cin >> n; for (int i = 0; i < n; i++) std::cin >> a[i]; for (int i = 0; i < n; i++) { int acc = 0; for (int j = i; j < n; j++) { acc += a[j]; sums.emplace_back(acc); //std::cout << acc << std::endl; } } sort(sums.begin(), sums.end()); test(0, n); p1.clear(); p2.clear(); for (auto x: sums) { if (x == 0) result+=2; p1.emplace_back(-x); p2.emplace_back(x*2); } //std::cout<<"fin"<<std::endl; te(true); std::cout<<result/6<<std::endl; } |
English