View code
/* Give you n items, each of which has a certain volume, give you a certain total volume of a certain backpack, ask you to use this backpack to install these items, there are several different combinations of items in a backpack. One combination is legal: The remaining backpack capacity must be the minimum size of the remaining items. */ # Include <Cstdio> # Include <Cstring> # Include <Algorithm> Using Namespace STD; Int DP [ 1010 ]; Int V [ 50 ]; Int Max ( Int A, Int B ){ Return A> B? A: B ;} Int Main (){ Int T, I, J, K, V, D, CA = 1 ; Scanf ( " % D " ,& T ); While (T -- ) {Scanf ( " % D " , & V ,& D ); For (I = 1 ; I <= V; I ++) scanf ( " % D " ,& V [I]); sort (V + 1 , V + 1 ); Int Sum = 0 , ANS = 0 ; For (I = 1 ; I <= V; I ++ ){ // Enumerate the smallest item in the list, which is smaller than him. Memset (DP, 0 , Sizeof (DP); DP [ 0 ] = 1 ; DP [Sum] =1 ; For (J = I + 1 ; J <= V; j ++ ) For (K = D; k> = V [J] + sum; k -- ) DP [k] + = DP [k- V [J]; For (J = D; j> = max (D-V [I] + 1 , 1 ); J --) // D-J + 1 <= V [I]; If (J> = sum) ans + = DP [J]; sum + = V [I];} printf ( " % D \ n " , CA ++ , ANS );}}