-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy path466C.cpp
More file actions
executable file
·43 lines (34 loc) · 728 Bytes
/
Copy path466C.cpp
File metadata and controls
executable file
·43 lines (34 loc) · 728 Bytes
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
// Problem Code: 466C
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int64_t number_of_ways(int n, vector<int>& a) {
int64_t ways = 0, total_sum = accumulate(a.begin(), a.end(), static_cast<int64_t>(0));
if (total_sum % 3 != 0)
return 0;
total_sum /= 3;
int64_t sum = 0;
vector<int64_t> prefix(n + 1);
for (int i = n - 1; i >= 0; i--) {
sum += a[i];
prefix[i] = (sum == total_sum);
prefix[i] += prefix[i + 1];
}
sum = 0;
for (int i = 0; i + 1 < n; i++) {
sum += a[i];
if (sum == total_sum)
ways += prefix[i + 2];
}
return ways;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int& x: a)
cin >> x;
cout << number_of_ways(n, a);
return 0;
}