Poke here: 2844
Review the knapsack problem
Test instructions: Tell you the value and quantity of the coins in N, how many different denominations can be composed, the denomination is limited in the interval [1, M]
Idea: Cover one side with a backpack, take the space of the coin for its own value, then when the dp[i] = = i, the space for I backpack back filled, that can constitute the case of denomination I
1#include"bits/stdc++.h"2 using namespacestd;3 intN, M;4 intval[ the], num[ the];5 intdp[100010];6 7 voidZopack (intSpaintval)8 {9 inti;Ten for(i = M; I >= spa;--i) { OneDp[i] = max (Dp[i-spa] +Val, dp[i]); A } - } - the voidCopack (intSpaintval) - { - inti; - for(i = spa; I <= M; + +)i) { +Dp[i] = max (Dp[i-spa] +Val, dp[i]); - } + } A at voidMultipack (intSpaintValintnum) - { - //can ignore the quantity when the full backpack - if(M <= Spa *num) { - Copack (Spa, Val); - return ; in } - //otherwise the binary divides the number of goods do, 01 backpack to intK =1; + while(K <num) { -num-=K; theZopack (Spa * k, Val *k); * $K <<=1;Panax Notoginseng } -Zopack (Spa * num, Val *num); the } + A intMain () the { + while(SCANF ("%d%d", &n, &m)! = EOF && (N | |M)) { - //Initialize $ //memset (DP, 0, sizeof (dp[0)) * (M + 1)); //Hey, 110, why don't you give me that? RE (╯‵-′) ╯︵┻━┻ $Memset (DP,0,sizeof(DP)); - inti; - //input the for(i =1; I <= N; ++i) { -scanf"%d", &val[i]);Wuyi } the for(i =1; I <= N; ++i) { -scanf"%d", &num[i]); Wu } - //do a full backpack, for each kind of item About for(i =1; I <= N; ++i) { $ //multipack (Val[i], val[i], num[i]); - multipack (Val[i], val[i], num[i]); - } - //Traverse DP, Statistical solution number A intres =0; + for(i =1; I <= M; ++i) { the if(Dp[i] = =i) { -++Res; $ } the } the //Output theprintf"%d\n", res); the } -}
HDU 2844 Coins Multiple backpack