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
| #include<bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int a[N]; int main() { int n; cin >> n; int sum = 0; for(int i = 1; i <= n; i ++) { cin >> a[i]; sum +=a[i]; a[i] += a[i - 1]; } if(n < 3) { cout << 0; return 0; } if(sum % 3 != 0) { cout << 0; return 0; } if(sum == 0) {
int num = 0; for(int i = 1; i <= n; i ++) { if(a[i] == sum)num ++; } cout << (long long)(1 + (num - 2)) * (num - 2) / 2; return 0; } sum /= 3; int num1 = 0, num2 = 0; for(int i = 1; i <= n; i ++) { if(a[i] == sum)num1 ++; if(a[i] == sum * 2)num2 ++; } long long res = 0; for(int i = 1; i <= n; i ++) { if(a[i] == sum) { res += num2; } if(a[i] == sum * 2){ num2 --; } } cout << res; return 0; }
|