#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> tab(n*(n+1)/2+1, 0); for(int i = 1; i <= n; i++) { int tmp; cin >> tmp; tab[i] = tab[i-1]+tmp; } int p = n+1; ll ans = 0; for(int i = 1; i < n; i++) { for(int j = i+1; j <= n; j++) { tab[p++] = tab[j] - tab[i]; } } for(int i = 1; i < n*(n+1)/2-1; i++) { for(int j = i+1; j < n*(n+1)/2; j++) { for(int k = j+1; k < n*(n+1)/2+1; k++) { if(tab[i] + tab[j] + tab[k] == 0) ans++; } } } cout << ans << "\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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> tab(n*(n+1)/2+1, 0); for(int i = 1; i <= n; i++) { int tmp; cin >> tmp; tab[i] = tab[i-1]+tmp; } int p = n+1; ll ans = 0; for(int i = 1; i < n; i++) { for(int j = i+1; j <= n; j++) { tab[p++] = tab[j] - tab[i]; } } for(int i = 1; i < n*(n+1)/2-1; i++) { for(int j = i+1; j < n*(n+1)/2; j++) { for(int k = j+1; k < n*(n+1)/2+1; k++) { if(tab[i] + tab[j] + tab[k] == 0) ans++; } } } cout << ans << "\n"; return 0; } |