-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy path10007.java
65 lines (53 loc) · 1.23 KB
/
10007.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
65
import java.io.*;
import java.util.*;
import java.math.*;
/*
* Number of binary trees which has N elements:
*
* (2N)!
* ---------------
* (N + 1)! * N!
*
* http://en.wikipedia.org/wiki/Catalan_number
*
* The problem asks us to calculate the number
* of binary trees which has N elements times
* number of permutations of N
*
*/
class Main
{
BigDecimal fact[] = new BigDecimal[601];
BigDecimal result[] = new BigDecimal[301];
void precalc_factorial()
{
fact[0] = new BigDecimal(1);
fact[1] = new BigDecimal(1);
for (int i = 2; i < 601; i++) {
fact[i] = fact[i-1].multiply(new BigDecimal(i));
}
}
void precalc()
{
precalc_factorial();
for (int i = 1; i < 301; i++) {
result[i] = fact[2*i].divide(fact[i+1]);
}
}
void solve()
{
precalc();
Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
while (true) {
int n = in.nextInt();
if (n == 0)
break;
System.out.println(result[n]);
}
}
public static void main(String args[])
{
Main problem = new Main();
problem.solve();
}
}