Generate a given number x using combinations of a smaller given number

EDIT : I used 2.5 and 16 before. But these were just examples. But now I want to generalize for any 3 numbers.

I have a number that allows me to say N. Can this number be formed only by adding only x1, x2 and x3. We can use three numbers at any time and in any combination.

Can this be solved by dynamic programming, or is there some other simple method?

+4
source share
2 answers

Use Chicken McNugget Theor if you have only 2 reciprocal values. Otherwise, go to the solution Dynamic programming.

16 ( 2, ), 2 5 , , 2a + 5b 2*5 - 2 - 5 = 3. , , .

2 5 : , , 2. :

2k+1 = (2k - 4) + 5

, , , 5.

3 ., , .

, , :

dp[i] = true if we can reach sum i
dp[0] = true, false for the rest
for i = 0 to query_sum:
  dp[i] = dp[i] or dp[i - input1] or dp[i - input2] or dp[i - input3]
+3

, , .

a*2 + b*5 + c*16 = N

(a,b,c). -, .

+1

All Articles