Stirling Numbers (Type 2)

The Second form of the Stirling Numbers answer the following question:

Consider the set {1,2,3...n}. Into how many ways can we partition these numbers into k distinct, non-empty subsets?

The recurrence is given by:

f(n,k) = k*f(n-1,k) + f(n-1,k-1)

Java is the language of choice because of the BigInteger class. f(400,200) has 531 digits.

Java
static BigInteger[][] DP ; public static BigInteger St2(int n, int k)   { if (n == k)           return BigInteger.ONE ; if (k == 1) {           return BigInteger.ONE ; }       if (!DP[n][k].equals(BigInteger.valueOf(-1))) {           return DP[n][k] ; }       BigInteger tem = BigInteger.valueOf(k) ; tem = tem.multiply(St2(n-1,k)) ; tem = tem.add(St2(n-1,k-1)) ; DP[n][k] = tem ; return tem ; }   public static void main(String[] args) {       Scanner input = new Scanner(System.in) ; DP = new BigInteger[1000][1000] ; for (int i = 0 ; i < 1000 ; i ++) for (int j = 0 ; j < 1000 ; j ++) DP[i][j] = BigInteger.valueOf(-1) ; int N = input.nextInt ; int K = input.nextInt ; System.out.println(St2(N,K)); }