Euler Numbers (Type 1)

Eulerian numbers of the first type answer the following question:

Consider n items (say, 1,2...n) arranged in order. We define an "ascent" as a total of the number of indices i with the following property:

x[i] > x[i-1]

How many series with k ascents exist?

Examples:

The series {1 3 2 4} has two ascents. 3 > 1, 4 > 2. The series {1,2,3,4} has 3 ascents. There are no series where n = 4 and with 4 ascents; that is, f(n,n) = 0.

The recurrence is: f(n,k) = (k+1)f(n-1,k) + (n-k)f(n-1,k-1)

Java is the best language to use for this because of the BigInteger class. f(400,200) has 868 digits.

Java
static BigInteger DP[][] ; public static BigInteger Eul(int n, int k)   { if (k == 0) return BigInteger.ONE ; if (n == 0 || n == k)           return BigInteger.ZERO ; if (k == 1) {           return BigInteger.valueOf(2).pow(n).subtract(BigInteger.valueOf(n)).subtract(BigInteger.ONE) ; }       if (DP[n][k] != null) {           return DP[n][k] ; }       BigInteger tem = BigInteger.valueOf(k+1) ; tem = tem.multiply(Eul(n-1,k)) ; tem = tem.add(BigInteger.valueOf(n-k).multiply(Eul(n-1,k-1))) ; return DP[n][k] = tem ; }   public static void main(String[] args) {       Scanner input = new Scanner(System.in) ; DP = new BigInteger[1000][1000] ; int N = input.nextInt ; int K = input.nextInt ; System.out.println(Eul(N,K)); }