Count number of ways to achieve a sum
You get the StackOverflowError because the solve method is called recursively too many times for large inputs such as 29194.

The first time you call solve(0, ...), nothing is already computed, so this will recursively call:
 * solve(2, ...) and then solve(4, ...), and so on until it reaches 29194
 *solve(3, ...)and thensolve(6, ...), and so on until it reaches 29194 

However, there's a simple trick to avoid all the recursion: iterate backwards. Call solve(n, ...) (yes, this will call solve(n+2) and solve(n+3), but those will finish fast without recurring). Then, call solve(n-1), then solve(n-2), and so on, until solve(0). Notice how this time all recursive calls will finish in one step, as the values will already be computed and available in dp.

So, before your call to solve(0, n), add the following code:

for (int i = n; i &gt;= 0; i--) {
    solve(i, n);
}


and you will avoid the StackOverflowError.

Here it is in action: http://ideone.com/7Ou8qq

Note that this is the smallest change to your code that I could make to get it to run, so it's more like a hack. Optimally, you would change your algorithm to run in reverse order by default.