Coin Counting

Given a sequence of coins, how many ways are there to obtain a total sum of m?

Note that these numbers can get very large. Implementation with long or BigInteger may be necessary to get AC.

Runs O(n*m), where n is the length of the coin array (in the code below, n = 8) and m is the total sum.

Java
static int[][] DP ; static int[] coins = {1,5,10,25,50,100,200,500} ; public static int solve(int n, int m) { if (n &lt; 0 || m &lt; 0) return 0 ; if (m == 0) return 1 ; if (DP[n][m] != -1) return DP[n][m] ; int ans = 0 ; for (int i = 0 ; i &lt;= n ; i ++) ans += solve(i,m-coins[i]) ; DP[n][m] = ans ; return ans ; }