This is my first thought of as a mother function, but it is said that when the mother function uses TLE, multiple backpacks are decisive. Here we use multiple backpacks, obviously we cannot directly output DP [N, in this case, the result is wrong. How can we find the type. If (DP [I]) ++ C ;?????? Can this be done ???? No, this is the meaning of DP [I]. DP [I] represents the greatest value that can be found in weight I, and here the weight and value are both money, so there is a very convenient place. If DP [I] is the most capable of loading I, then I will certainly be able to choose, therefore, the judgment condition is if (DP [I] = I) ++ C;
Run the Code directly.
# Include <stdio. h> # include <string. h> // # define max (a, B) (a)> (B )? (A): bint n, m, V [105], W [105], num [105], DP [100005]; inline int max (int A, int B) // remember to add inline, which is much faster {return A> B? A: B;} void _ 01 (INT V, int W) {for (Int J = m; j> = V; -- J) DP [J] = max (DP [J-v] + W, DP [J]);} void complete (int v, int W) {for (Int J = V; j <= m; ++ J) DP [J] = max (DP [J-v] + W, DP [J]);} void multiply (int v, int W, int c) {If (V * C> m) complete (V, W); else {int K = 1; while (C> K) {_ 01 (K * v, k * w); C-= K; k <= 1;} _ 01 (C * V, C * w );}} int main () {While (scanf ("% d", & N, & M), N | M) {for (INT I = 1; I <= N; ++ I) scanf ("% d", & V [I]); For (INT I = 1; I <= N; ++ I) scanf ("% d", & num [I]); memset (DP, 0, sizeof (DP); For (INT I = 1; I <= N; ++ I) Multiply (V [I], V [I], num [I]); int C = 0; For (INT I = 1; I <= m; ++ I) if (DP [I] = I) ++ C; printf ("% d \ n", c);} return 0 ;}