(solution version) your problem is still NP-completed. The idea is that if we can solve your problem, then (for each size of the subset, let’s say) we could ask how many sets sums less than V and how much sums less than V-1, and the difference between these two numbers will tell us are whether the subsets that make up exactly V - so we could solve the problem of summing the subsets. [This is not complete proof, because it is a Turing contraction , not a lot of one contraction .]
However, there is simple dynamic programming that runs in O (nLV) time. [The reason for this does not prove that P = NP means that V can be exponential in input size: with n bits, you can represent values up to 2 n . But assuming your V is not exponential, this is not a problem.]
Let num [v] [k] [i] denote the number of subsets of size-k from the first i elements of S that sum with v. They can be calculated as (for each i):
num[0][0][i] = 1 for v = 1 to V: for k = 1 to L: num[v][k][i] = num[v][k][i-1] + num[vS[i]][k-1][i-1]
where S [i] is the ith element in your sequence. (Any set of sizes k that sums with v either does not use S [i], so it is counted in num [v] [k] [i-1] or uses S [i], which means that the rest of the subset has k-1 elements, uses only the first numbers i-1 in the sequence and sums up with vS [i].) Finally, count num [v] [L] [| S |] for each v less than V; what is your answer.
In addition, you can omit the third index if you do this carefully (run your loop down for each self, etc.); I just turned it on for clarity.
ShreevatsaR Dec 17 '08 at 21:26 2008-12-17 21:26
source share