-
Notifications
You must be signed in to change notification settings - Fork 2
/
Solution.java
64 lines (51 loc) Β· 1.77 KB
/
Solution.java
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
package BruteForce.swea3234;
import java.io.*;
import java.util.*;
public class Solution {
static int T, N, ans;
static int[] W;
static boolean[] select;
static int[] fact = new int[10];
static int[] pow = new int[10];
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/BruteForce/swea3234/sample_input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
fact[0] = 1; pow[0] = 1;
for (int i = 1; i <= 9; i++) {
fact[i] = i * fact[i-1];
pow[i] = 2 * pow[i-1];
}
T = Integer.parseInt(br.readLine());
for (int t = 1; t <= T; t++) {
N = Integer.parseInt(br.readLine());
W = new int[N];
int total = 0;
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
W[i] = Integer.parseInt(st.nextToken());
total += W[i];
}
ans = 0;
select = new boolean[N];
solve(0, 0, 0, total);
sb.append("#").append(t).append(" ").append(ans).append("\n");
}
System.out.println(sb.toString());
}
static void solve(int cnt, int left, int right, int remain) {
if (left < right) return;
if (left >= right + remain) {
ans += pow[N-cnt] * fact[N-cnt];
return;
}
for (int i = 0; i < N; i++) {
if (!select[i]) {
select[i] = true;
solve(cnt+1, left+W[i], right, remain-W[i]);
solve(cnt+1, left, right+W[i], remain-W[i]);
select[i] = false;
}
}
}
}