Problem Summary
Given M types of coins in infinite quantities where the value of each type of coin is given in array C, determine the number of ways to make change for N units using these coins.
Solution
This problem is very similiar to the unbounded knapsack problem (UKP).
Let f[i,j] be the number of ways to make change for j units using coins of types 1,2,…,i. Then we have
f[i,j] = f[i-1,j], if j < c[i]
f[i,j] = f[i-1,j] + f[i,j-c[i]], if j ≥ c[i]
Actually, it is not necessary to use 2-dimensional array. We can only use f[j] instead of f[i,j]. If we enumerate i from 1 to M, the formula is
f[j] = f[j] + f[j-c[i]], if j ≥ c[i]
Code
|
|