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]];
}
}