Catalan Numbers

''Catalan Numbers show up in odd places in Combinatorics. Among others, Catalan numbers are the solution to: ''

1) The number of distinct binary trees with n+1 verticies 

2) The number of ways to partition a polygon with n sides into n-2 triangles, if we consider rotations and reflections to be separate partitions. 

''This algorithm runs in O(1) plus a (fairly large) constant to generate the DP array. ''

Java
// import java.math.* ; BigDecimal[] arr = new BigDecimal[20001] ; arr[0] = new BigDecimal("1") ; for (int i = 1 ; i &lt;= 20000 ; i ++) {    // Catalan numbers are defined recursively as: // C(n+1) = C(n) * [4n^2 + 6n + 2]/[n^2 + 3n + 2] double v = 4.0*i*i + 6.0*i+2.0 ; double z = i*i + 3.0*i + 2.0 ; BigDecimal plz = new BigDecimal("" + v) ; arr[i] = arr[i-1].multiply(plz) ; arr[i] = arr[i].divide(new BigDecimal("" + z),2) ; } // Now, accessing the nth Catalan number is O(1) // Note that this array is ZERO-INDEXED. int N = 0 ; System.out.println(arr[N].toBigInteger) ; // Outputs: 1 int N = 1 ; System.out.println(arr[N].toBigInteger) ; // Outputs: 2 int N = 2 ; System.out.println(arr[N].toBigInteger) ; // Outputs: 5

UVA Problems:
991 Safe Salutations