UVa 1213 - Sum of Different Primes

Problem Number - Problem Name
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=24&problem=3654&mosmsg=Submission+received+with+ID+15292026

Summary
You are given two integers n and k. 1<=n<=1120 and 1<=k<=14. n=0 and k=0 will terminate the program. You should count the number of ways to express n as a sum of k different primes.

Explanation
This problem can be solved by using dynamic programming technique.At each step,we can either take a prime number,or we can leave it.This is 0-1 knapsack variant,in this case we need to find the ways instead of maximizing the sum.First we have to store some prime numbers on an array,and then we can run knapsack on the array keeping 3 states, one is the given sum,the taken number count,and the index of the prime number array.

if the prime number stored array is called arr[],we can write, dp[id][sum][taken]=knapsack(id+1,sum,taken)+knapsack(id+1,sum-arr[id],taken+1);

Gotchas
Everyone should be careful about the base cases.

Implementations
http://paste.ubuntu.com/10775680/

Optimizations
Recursive DP gives AC at approximately 2.00 seconds. Iterative DP will be somehow faster.

Input
24 3 24 2 2 1 1 1 4 2 18 3 17 1 17 3 17 4 100 5 1000 10 1120 14 0 0

Output
2 3 1 0 0 2 1 0 1 55 200102899 2079324314