The total price is cash now. There is a currency with a version value. The number of each coin is amount and the nominal value is denomination, the maximum value sum that can be composed of less than or equal to the total price.
Question: multiply the nominal value of each currency by the factor 1, 2, 4 ,..., 2 ^ (k-1), amount-2 ^ k + 1, and K is the maximum integer that satisfies amount-2 ^ k + 1> 0. For example, if amout is 13, multiply the nominal value by the coefficients 1, 2, 4, and 6 to get four values. Then convert multiple backpacks into 01 backpacks.
# Include <iostream> using namespace STD; int DP [100005], C [10001]; int main () {int cash, version, denomination, amount; while (CIN> cash) {int I, j, temp, K = 0; CIN> version; for (I = 0; I <version; I ++) {CIN> amount> denomination; j = 1; while (j <= Amount) {C [k] = J * denomination; amount-= J; j = J * 2; k ++;} If (amount> 0) C [k ++] = Amount * denomination;} memset (DP, 0, sizeof (DP); for (I = 0; I <K; I ++) {for (j = cash; j> = C [I]; j --) {temp = DP [J-C [I] + C [I]; If (temp> DP [J]) DP [J] = temp ;}} cout <DP [Cash] <Endl;} return 0 ;}