#include <bits/stdc++.h> using namespace std; vector<int> nums; vector<long long> presum, newNums; const int mid = 1e7+10; int ex[2*mid]; int main(){ int n; cin>>n; int a; presum.push_back(0); for(int i = 0; i<n; i++){ cin>>a; nums.push_back(a); presum.push_back(presum.back() + a); for(int j = 0; j<=i; j++){ newNums.push_back(presum.back() - presum[j]); } } int nn = newNums.size(); long long out = 0; for(int i = 0; i<nn; i++){ for(int j = i+1; j<nn; j++){ long long tmp = newNums[i] + newNums[j]; out += ex[-tmp + mid]; } ex[newNums[i] + mid]++; } cout<<out<<endl; 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 | #include <bits/stdc++.h> using namespace std; vector<int> nums; vector<long long> presum, newNums; const int mid = 1e7+10; int ex[2*mid]; int main(){ int n; cin>>n; int a; presum.push_back(0); for(int i = 0; i<n; i++){ cin>>a; nums.push_back(a); presum.push_back(presum.back() + a); for(int j = 0; j<=i; j++){ newNums.push_back(presum.back() - presum[j]); } } int nn = newNums.size(); long long out = 0; for(int i = 0; i<nn; i++){ for(int j = i+1; j<nn; j++){ long long tmp = newNums[i] + newNums[j]; out += ex[-tmp + mid]; } ex[newNums[i] + mid]++; } cout<<out<<endl; return 0; } |