WA.cpp's blog

By WA.cpp, history, 7 years ago, In English

I can't implement the State-space tree. How to implement it for subset sum problem?

What's the complexity of this?

Thanks in advance.

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Could you explain a little bit more on what you're trying to ask? Sorry if I'm sounding a bit rough

  • »
    »
    7 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    I have a set of positive integers. I have to tell if there any subset of S that all elements summation equal to given sum. I have done it with DP with O(sum*n) complexity. But here sum can be 10^9. So what's will be the approach?

    n>=30 and sum>=10^9

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      Knapsack problem with large weights can be done in O(2^n), but there is another algo. You can divide array into left and right parts. For the first part calc array f of all possible sums (i used bitmasks). For the second make map m, m[x] = 1 if it is possible to make sum x in the second part. Now go through array f and if m[sum — f[i]] == 1 answer is yes.

      My code