forked from daniCh8/eth-algolab-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplanks.cpp
82 lines (65 loc) · 1.88 KB
/
planks.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* Master solution - Planks
**/
#include <iostream>
#include <vector>
#include <algorithm>
typedef std::vector<int> VI;
typedef std::vector<VI> VVI;
void back_track(int id, int ubound, VVI &F, VVI &assignment, const VI &planks) {
if (id >= ubound) {
VI tuple(4, 0);
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < (int)assignment[i].size(); ++j)
tuple[i] += planks[assignment[i][j]];
}
F.push_back(tuple);
return;
}
for (int i = 0; i < 4; ++i) {
assignment[i].push_back(id);
back_track(id + 1, ubound, F, assignment, planks);
assignment[i].pop_back();
}
}
void testcase() {
int n;
std::cin >> n;
std::vector<int> planks;
for (int i = 0; i < n; ++i) {
int plank;
std::cin >> plank;
planks.push_back(plank);
}
int sum = 0;
long long result = 0;
for (int i = 0 ; i < (int)planks.size(); ++i)
sum += planks[i];
if (sum % 4 != 0) {
std::cout << 0 << std::endl;
return;
}
VVI F1, assignment(4);
back_track(0, n/2, F1, assignment, planks);
VVI F2, assignment2(4);
back_track(n/2, n, F2, assignment2, planks);
std::sort(F2.begin(), F2.end());
for (int idx = 0; idx < (int)F1.size(); ++idx) {
std::vector<int> member = F1[idx];
for (int i = 0; i < 4; ++i)
member[i] = sum/4 - member[i];
std::pair<VVI::iterator, VVI::iterator> bounds;
bounds = std::equal_range(F2.begin(), F2.end(), member);
long long counter = std::distance(bounds.first, bounds.second);
result += counter;
}
std::cout << result / 24 << std::endl;
}
int main() {
std::ios_base::sync_with_stdio(false);
int t;
std::cin >> t;
for (int i = 0; i < t; i++) {
testcase();
}
}