This is a typical problem of multiple backpacks. At the beginning of writing, I used the simplest solution of multiple backpacks, that is, converting multiple backpacks into 01 backpacks. The result was no more than once. It was then converted into a full backpack and A 01 backpack for solving the problem. However, the log algorithm was not used for optimization, but t. Finally, we had to use Log optimization to solve the problem of converting it into a 01 backpack.
If you know how to understand it, let's talk about the ideas first. The question is given to 6 items, the number of each item, and then asked if they can be evenly divided. We regard all items as items with the same value and cost, and then use the multi-backpack algorithm to solve the problem, to see if half of the total value of the backpack capacity can fit exactly half of the value. If yes, they can be evenly divided. Otherwise, they cannot be evenly divided.
The following is the AC code for your reference:
# Include <stdlib. h> # include <stdio. h> # include <string. h> # define Max 70010int DP [2 * max]; int A [10]; int inline max (int A, int B) {If (A> B) return; else return B;} int solve (int n, int v) {int I, J, K, amount; memset (DP, 0, sizeof (DP )); for (I = 1; I <= N; I ++) {if (a [I] * I> = V) // complete backpack {for (j = I; j <= V; j ++) DP [J] = max (DP [J], DP [J-I] + I );} else // 01 backpack {k = 1; amount = A [I]; while (k <amount) {for (j = V; j> = K * I; j --) DP [J] = max (DP [J], DP [J-K * I] + K * I); amount-= K; K * = 2;} For (j = V; j> = Amount * I; J --) DP [J] = max (DP [J], DP [J-Amount * I] + amount * I) ;}} return DP [v];} int main () {int CAS = 0, flag, I, sum; while (1) {CAS ++; sum = 0; for (I = 1; I <= 6; I ++) {scanf ("% d", & A [I]); sum + = A [I] * I;} flag = 0; for (I = 1; I <7; I ++) {if (a [I]! = 0) {flag = 1; break;} If (flag = 0) {break;} If (sum % 2 = 1) {printf ("collection # % d: \ ncan't be divided. \ n ", CAS); continue;} I = solve (6, sum/2); if (I = (sum/2 )) printf ("collection # % d: \ ncan be divided. \ n ", CAS); else printf (" collection # % d: \ ncan't be divided. \ n ", CAS);} return 0 ;}