Change coins with a limited number of coins

I wrote a program to generate the sum of a subset that can be used in this task, which states:

Suppose you have 3 $ 1-coins, 2 $ 2-coins, 3 $ 5-coins, 1 $ 10-coins, there are 4 ways to get 10 US dollars from these coins. If there are n1 $ X1 coins, n2 $ X2 coins .... nm $ Xm coins, how many ways to get $ X from this limited number of coins?

If we create the set {X1, X1 ..... X1, X2, X2 .......... X2, ..., ..., ............ , Xm, Xm ... Xm}, and then execute a subset of the subset on it, we can get the result for $ X. But I could not find a way to use the sets {n1, n2, n3 .... nm}, {X1, X2, X3 .... Xm}. A friend told me that this is a variation of the problem with the backpack, but I'm not sure how.

This is a partial code of what I wrote:

ways[0]=1, mylim=0;
for(i=0;i<count;i++){
    if(mylim+coins[i]<=LIMIT) mylim+=coins[i];
    else mylim=LIMIT;

    for(j=mylim; j>=coins[i];j--){
        ways[j]=(ways[j]+ways[j-coins[i]])%MOD;
    }
}

It would be great if you were kind enough to explain a little.

EDIT : This question is more suitable for stackexchange for computer science, but since this is an old question, I rather edit it.

This problem can be solved using the principle of Exception Exception , and it will come in handy when we fix the values โ€‹โ€‹of coins, but the amount of each coin varies with each request.

, [v] - $v $x1, $x2,.. $xm, , . , n1 $x1, , , , (n1 + 1) $x1 ( [v - (n1 + 1) x1]). , n2 $x2, [v - (n2 + 1) x2] ..

, , , (n1 + 1) $x1 (n2 + 1) $x2, [v - (n1 + 1) x1 - (n2 + 1) x2] ..

, ,

N= , ,

Ai= , ni + 1 $xi, 1 <= <= m,

, = | N | - | A1 | - | A2 |.. - | Am | + | A1 A2 | + | A1 A3 | +... - | A1 A2 A3 |.....

, , :

ways[0]=1;
for( int i = 0 ; i < count ; i++){
    for( int j = coins[i] ; j < ways.size() ; j++ ){
        ways[j] += ways[j-coins[i]];
    }
}
+5
3

, ni - 1.

ways[j] = number of ways of obtaining sum j.

( , , , primes).

ways[0] = 1
for i = 1 to m do
    for j = myLim downto X[i] do
        ways[j] += ways[j - X[i]];

, Xi . , ni :

ways[0] = 1
for i = 1 to m do
    for times = 1 to n[i] do // use Xi one time, then two times, then three, ..., then ni
        for j = myLim downto times*X[i] do
            ways[j] += ways[j - times*X[i]];

, .

+3

, , i -th, , , i -th, .

V(1...n) , V[i] i -th 0 < <= n, Q(1...n) m .

, :

1) , i -th

2) , i -th

t[0...m][0...n] , j, 1 < j <= m, i -th 1 < <= n. , :

t[j][i] = 
          t[j - v[i]][i], if j - v[i] >= 0, otherwise 0
          +
          t[j][i - 1], if i > 1, otherwise 0

1 , t[0][1...n] = 1 , , :

Coin-limited-change(V, Q, n, m):
  Let t[0...m][0...n] be a new table
  for i = 1 to n, do:
    t[0][i] = 1
  for j = 1 to m, do:
    for i = 1 to n, do:
      if j - V[i] >= 0 and Q[i] > 0, then: // also check if a coin is available 
        x = t[j - V[i]][i]
        Q[i] = Q[i] - 1 // update the amount of coin
      else:
        x = 0
      if j > 1, then:
        y = t[j][i - 1]
      else:
        y = 0
      t[j][i] = x + y
  return t[m][n]          

: ,

0

" " , , NP-hard.

.

-2

All Articles