-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCombinatorics.java
More file actions
61 lines (58 loc) · 1.99 KB
/
Copy pathCombinatorics.java
File metadata and controls
61 lines (58 loc) · 1.99 KB
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BinomialTheorm {
static long[][] C = new long[55][55];
public static long C(int n, int r) {
if (n < 0 || r < 0)
return 0;
else if (r > n)
return C[n][r] = 0;
else if (C[n][r] != -1)
return C[n][r];
else
return C[n][r] = C(n - 1, r - 1) + C(n - 1, r);
}
public static void main(String[] args) throws IOException {
int cc = 1;
for (int i = 0; i < 55; i++)
for (int j = 0; j < 55; j++)
C[i][j] = -1;
C[0][0] = 1;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
while (n-- > 0) {
String input = in.readLine();
String a = input.substring(input.indexOf('(') + 1, input
.indexOf('+'));
String b = input.substring(input.indexOf('+') + 1, input
.indexOf(')'));
int c = Integer.parseInt(input.substring(input.indexOf('^') + 1,
input.length()));
StringBuilder res = new StringBuilder("");
for (int i = 0; i <= c; i++) {
int ea = c - i;
if (C(c, i) > 1) {
res.append(C(c, i));
if (ea >= 1)
res.append('*');
}
if (ea >= 1) {
res.append(a);
if (ea > 1)
res.append("^" + ea);
if (i >= 1)
res.append('*');
}
if (i >= 1) {
res.append(b);
if (i > 1)
res.append("^" + i);
}
if (i != c)
res.append('+');
}
System.out.printf("Case %d: %s\n", cc++, res);
}
}
}