#include <bits/stdc++.h> using namespace std; typedef pair<int,int> PII; typedef pair<long long,int> PLI; typedef pair<int,long long> PIL; const int mod = 1e9+7; int arr[505]; int pref[505]; const int M = 2e7+100; int cnt[2*M]; int main(){ ios_base::sync_with_stdio(0); int n; cin>>n; for(int i=1;i<=n;i++) cin>>arr[i]; for(int i=1;i<=n;i++) pref[i] = pref[i-1]+arr[i]; vector<int> segs; for(int i=0;i<n;i++){ for(int j=i+1;j<=n;j++){ segs.push_back(pref[j]-pref[i]); } } // for(int u: segs) cout<<u<<" "; cout<<endl; long long res = 0; int sz = segs.size(); // unordered_map<int,int> cnt; for(int i=n;i>=0;i--){ for(int u: segs){ // cout<<"qr "<<-(u-pref[i])<<endl; res += cnt[-(u-pref[i])+M]; } for(int u: segs){ cnt[pref[i]+u+M]++; // cout<<"add "<<pref[i]+u<<endl; } } unordered_map<int,int> cnt_segs; for(int u: segs) cnt_segs[u]++; for(int u:segs){ res -= 3*cnt_segs[-2*u]; } long long cnt0 = cnt_segs[0]; res += 2*cnt_segs[0]; res /= 6; cout<<res<<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 | #include <bits/stdc++.h> using namespace std; typedef pair<int,int> PII; typedef pair<long long,int> PLI; typedef pair<int,long long> PIL; const int mod = 1e9+7; int arr[505]; int pref[505]; const int M = 2e7+100; int cnt[2*M]; int main(){ ios_base::sync_with_stdio(0); int n; cin>>n; for(int i=1;i<=n;i++) cin>>arr[i]; for(int i=1;i<=n;i++) pref[i] = pref[i-1]+arr[i]; vector<int> segs; for(int i=0;i<n;i++){ for(int j=i+1;j<=n;j++){ segs.push_back(pref[j]-pref[i]); } } // for(int u: segs) cout<<u<<" "; cout<<endl; long long res = 0; int sz = segs.size(); // unordered_map<int,int> cnt; for(int i=n;i>=0;i--){ for(int u: segs){ // cout<<"qr "<<-(u-pref[i])<<endl; res += cnt[-(u-pref[i])+M]; } for(int u: segs){ cnt[pref[i]+u+M]++; // cout<<"add "<<pref[i]+u<<endl; } } unordered_map<int,int> cnt_segs; for(int u: segs) cnt_segs[u]++; for(int u:segs){ res -= 3*cnt_segs[-2*u]; } long long cnt0 = cnt_segs[0]; res += 2*cnt_segs[0]; res /= 6; cout<<res<<endl; } |