#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define PB push_back
typedef long long ll;
typedef long double ld;
#define REP(x,y) for(int x=0;x<(y);x++)
#define ROF(x,y) for(int x=(y);x>0;x--)
#define FOR(x,z,y) for(int x=(z);x<=(y);x++)
#define INT(x) int x;scanf("%d ",&x)
#define LL(x) ll x;scanf("%lld",&x)
#define CZ(x) char x;scanf(" %c",&x)
typedef pair<int,bool> pib;
int t[505];
map<int,ll> m;
int main(){
INT(n);
FOR(i,1,n){
scanf("%d",t+i);
}
FOR(i,1,n){
t[i] += t[i-1];
}
FOR(i,0,n){
FOR(j,i+1,n){
m[t[j]-t[i]]++;
}
}
ll res = 0;
for(auto it=m.begin();it!=m.end();it++){
if(it->f == 0 && it->s >= 3){
res += it->s * (it->s-1) * (it->s-2) / 6;
}
else if(it->s >=2 && m.find(-2*it->f) != m.end()){
res += it->s * (it->s-1) / 2 * m[-2*it->f];
}
for(auto it2=next(it,1);it2!=m.end();it2++){
auto it3 = m.find(-(it2->f + it->f));
if(it3!= m.end() && it3->f>it2->f) {
res += it->s * it2->s * it3->s;
}
}
}
printf("%lld\n",res);
}
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 | #include<bits/stdc++.h> using namespace std; #define f first #define s second #define PB push_back typedef long long ll; typedef long double ld; #define REP(x,y) for(int x=0;x<(y);x++) #define ROF(x,y) for(int x=(y);x>0;x--) #define FOR(x,z,y) for(int x=(z);x<=(y);x++) #define INT(x) int x;scanf("%d ",&x) #define LL(x) ll x;scanf("%lld",&x) #define CZ(x) char x;scanf(" %c",&x) typedef pair<int,bool> pib; int t[505]; map<int,ll> m; int main(){ INT(n); FOR(i,1,n){ scanf("%d",t+i); } FOR(i,1,n){ t[i] += t[i-1]; } FOR(i,0,n){ FOR(j,i+1,n){ m[t[j]-t[i]]++; } } ll res = 0; for(auto it=m.begin();it!=m.end();it++){ if(it->f == 0 && it->s >= 3){ res += it->s * (it->s-1) * (it->s-2) / 6; } else if(it->s >=2 && m.find(-2*it->f) != m.end()){ res += it->s * (it->s-1) / 2 * m[-2*it->f]; } for(auto it2=next(it,1);it2!=m.end();it2++){ auto it3 = m.find(-(it2->f + it->f)); if(it3!= m.end() && it3->f>it2->f) { res += it->s * it2->s * it3->s; } } } printf("%lld\n",res); } |
English