#include <bits/stdc++.h>
using namespace std;
int T[510];
int Pre[510];
map<pair<int,int>, long long > Lew;
map<pair<int,int>, long long > Praw;
map<pair<int,int>, long long >::iterator it;
map<pair<int,int>, long long >::iterator cit;
map<pair<int,int>, long long >::iterator git;
map<int, int> M;
map<int, int>::iterator dit;
map<int, int>::iterator fit;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
int sum=0;
for(int i=1;i<=n;i++){
cin>>T[i];
sum+=T[i];
Pre[i]=sum;
}
int ma=0;
for(int i=1;i<=n;i++){
for(int j=i;j<=n;j++){
int dod = Pre[j] - Pre[i-1];
if(M.find(dod) == M.end()){
M[dod]=1;
}
else{
M[dod]++;
}
for(int k = 1; k<=n;k++){
int wyn = Pre[j]-Pre[i-1]- Pre[k-1];
if(Lew.find({wyn,k})==Lew.end()){
Lew[{wyn,k}]=1;
}
else{
Lew[{wyn,k}]++;
}
int wyn2 = Pre[j]-Pre[i-1] + Pre[k];
if(Praw.find({wyn2,k})==Praw.end()){
Praw[{wyn2,k}]=1;
}
else{
Praw[{wyn2,k}]++;
}
}
}
}
long long sum2 = 0;
for(cit = Praw.begin(); cit != Praw.end(); cit++){
sum2 += (*cit).second;
(*cit).second = sum2;
}
long long il = 0;
for(it = Lew.begin(); it!=Lew.end();it++){
int war = (*it).first.first;
int k = (*it).first.second;
cit = Praw.lower_bound({war*-1+1, 0});
git = Praw.lower_bound({war*-1, k});
if(git == Praw.end()){
continue;
}
long long dol;
if(git == Praw.begin()){
dol = 0;
}
else{
git--;
dol = (*git).second;
}
long long gor;
if(cit == Praw.begin()){
gor = 0;
}
else{
cit--;
gor = (*cit).second;
}
il += (*it).second*(gor-dol);
//cout<<gor-dol<<" "<<(*it).first.second<<"\n";
}
for(int i = 1;i<=n;i++){
for(int j=i; j<=n;j++){
int war = Pre[j]-Pre[i-1];
dit = M.find({war*-2});
if(dit!=M.end()){
il -= (*dit).second*3;
}
if(war == 0){
il += 2;
}
}
}
cout<<il/6<<"\n";
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | #include <bits/stdc++.h> using namespace std; int T[510]; int Pre[510]; map<pair<int,int>, long long > Lew; map<pair<int,int>, long long > Praw; map<pair<int,int>, long long >::iterator it; map<pair<int,int>, long long >::iterator cit; map<pair<int,int>, long long >::iterator git; map<int, int> M; map<int, int>::iterator dit; map<int, int>::iterator fit; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; int sum=0; for(int i=1;i<=n;i++){ cin>>T[i]; sum+=T[i]; Pre[i]=sum; } int ma=0; for(int i=1;i<=n;i++){ for(int j=i;j<=n;j++){ int dod = Pre[j] - Pre[i-1]; if(M.find(dod) == M.end()){ M[dod]=1; } else{ M[dod]++; } for(int k = 1; k<=n;k++){ int wyn = Pre[j]-Pre[i-1]- Pre[k-1]; if(Lew.find({wyn,k})==Lew.end()){ Lew[{wyn,k}]=1; } else{ Lew[{wyn,k}]++; } int wyn2 = Pre[j]-Pre[i-1] + Pre[k]; if(Praw.find({wyn2,k})==Praw.end()){ Praw[{wyn2,k}]=1; } else{ Praw[{wyn2,k}]++; } } } } long long sum2 = 0; for(cit = Praw.begin(); cit != Praw.end(); cit++){ sum2 += (*cit).second; (*cit).second = sum2; } long long il = 0; for(it = Lew.begin(); it!=Lew.end();it++){ int war = (*it).first.first; int k = (*it).first.second; cit = Praw.lower_bound({war*-1+1, 0}); git = Praw.lower_bound({war*-1, k}); if(git == Praw.end()){ continue; } long long dol; if(git == Praw.begin()){ dol = 0; } else{ git--; dol = (*git).second; } long long gor; if(cit == Praw.begin()){ gor = 0; } else{ cit--; gor = (*cit).second; } il += (*it).second*(gor-dol); //cout<<gor-dol<<" "<<(*it).first.second<<"\n"; } for(int i = 1;i<=n;i++){ for(int j=i; j<=n;j++){ int war = Pre[j]-Pre[i-1]; dit = M.find({war*-2}); if(dit!=M.end()){ il -= (*dit).second*3; } if(war == 0){ il += 2; } } } cout<<il/6<<"\n"; return 0; } |
English