-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathExpressions.java
57 lines (48 loc) · 1.66 KB
/
Expressions.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
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
public class Expressions {
private static final BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
private static final BigInteger[][] memo = new BigInteger[151][151];
public static BigInteger c(int n, int d) {
if (d == 1 || n == 0) {
return BigInteger.ONE;
}
if (memo[n][d] != null) {
return memo[n][d];
}
BigInteger v = BigInteger.ZERO;
for (int i = 0; i <= n - 1; ++i) {
BigInteger v1 = c(i, d - 1);
BigInteger v2 = c(n - i - 1, d);
v = v1.multiply(v2).add(v);
}
memo[n][d] = v;
return v;
}
public static BigInteger solve(int n, int d) {
if (n % 2 != 0) {
return BigInteger.ZERO;
}
for (int i = 0; i < memo.length; ++i) {
Arrays.fill(memo[i], null);
}
return c(n / 2, d).subtract(c(n / 2, d - 1));
}
public static void main(String[] args) throws IOException {
String currentLine;
while ((currentLine = reader.readLine()) != null &&
!currentLine.trim().isEmpty()) {
List<Integer> input = stream(currentLine.trim().split(" "))
.filter(x -> !x.equals("")).map(Integer::parseInt)
.collect(toList());
System.out.println(solve(input.get(0), input.get(1)));
}
}
}