#include <bits/stdc++.h> #define int long long #define pii pair<int,int> #define all(x) x.begin(),x.end() #define vi vector<int> #define vii vector<pii> #define vb vector<bool> #define siz(x) (int)x.size() #define pb push_back #define nd second #define st first #define rep(i,a,b) for(int i=a; i<=b; i++) using namespace std; const int maxn = 1e6, inf = 1e9; int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; vi V(n); for(auto &w : V)cin>>w; map<int,int> ciag1; vii ciag; int ans = 0; rep(i,0,n-1){ int aktsum=0; rep(j,i,n-1){ aktsum+=V[j]; ciag1[aktsum]++; } } for(auto [a,b] : ciag1)ciag.pb({a,b});// ciag nie ma duplikatow for(auto [a,b] : ciag){ if(a == 0 && b>=3){ ans+= ((b*(b-1)*(b-2))/6); // gdy zero to dodaje do wyniku b po 3 } } rep(i,0,siz(ciag)-1){ int a = i; int b = i+1; int c = siz(ciag)-1; while(b<c){ if(ciag[a].st+ciag[b].st+ciag[c].st == 0){ ans+=(ciag[a].nd*ciag[b].nd*ciag[c].nd); b++; c--; }else if(ciag[a].st+ciag[b].st+ciag[c].st > 0)c--; else b++; } } int ans2=0; rep(i,0,siz(ciag)-1){ if(ciag[i].nd>=2){ rep(j,0,siz(ciag)-1){ if(i!=j){ if(ciag[i].st+ciag[i].st+ciag[j].st == 0){ ans2+=((ciag[i].nd*(ciag[i].nd+1))/2)*ciag[j].nd; } } } } } ans+=(ans2/2); cout<<ans; }
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 | #include <bits/stdc++.h> #define int long long #define pii pair<int,int> #define all(x) x.begin(),x.end() #define vi vector<int> #define vii vector<pii> #define vb vector<bool> #define siz(x) (int)x.size() #define pb push_back #define nd second #define st first #define rep(i,a,b) for(int i=a; i<=b; i++) using namespace std; const int maxn = 1e6, inf = 1e9; int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; vi V(n); for(auto &w : V)cin>>w; map<int,int> ciag1; vii ciag; int ans = 0; rep(i,0,n-1){ int aktsum=0; rep(j,i,n-1){ aktsum+=V[j]; ciag1[aktsum]++; } } for(auto [a,b] : ciag1)ciag.pb({a,b});// ciag nie ma duplikatow for(auto [a,b] : ciag){ if(a == 0 && b>=3){ ans+= ((b*(b-1)*(b-2))/6); // gdy zero to dodaje do wyniku b po 3 } } rep(i,0,siz(ciag)-1){ int a = i; int b = i+1; int c = siz(ciag)-1; while(b<c){ if(ciag[a].st+ciag[b].st+ciag[c].st == 0){ ans+=(ciag[a].nd*ciag[b].nd*ciag[c].nd); b++; c--; }else if(ciag[a].st+ciag[b].st+ciag[c].st > 0)c--; else b++; } } int ans2=0; rep(i,0,siz(ciag)-1){ if(ciag[i].nd>=2){ rep(j,0,siz(ciag)-1){ if(i!=j){ if(ciag[i].st+ciag[i].st+ciag[j].st == 0){ ans2+=((ciag[i].nd*(ciag[i].nd+1))/2)*ciag[j].nd; } } } } } ans+=(ans2/2); cout<<ans; } |