The idea of this topic is clever. Under what circumstances can all the remaining items be put down? That is, the smallest of the remaining items cannot be placed. Therefore, first sort items by capacity from small to large, and enumerate the situations where the current backpack is the smallest item that cannot be placed.
For the current item I, there must be 1 to all the items of the I-1 put in, this time than I large items who put who don't put is not sure. Convert to 0-1 backpack problem: Put the previous items in the I-1, get space for tsum-sum [I-1] (prefix and) package, as long as you fill out a scheme from the I + 1 to the N items so that the remaining volume is smaller than the volume of the I items, the total number of solutions is the result!
Note: In special cases, when the smallest item after sorting cannot be placed, 0 is returned directly, because DP [0] is initialized to 1,
Code:
/* Poj 3093232k0ms */# include <cstdio> # include <algorithm> # include <iostream> # define maxn 1005 using namespace STD; int n, m, wei [35], DP [maxn], sum [maxn] ;__ int64 bag () {memset (sum, 0, sizeof (SUM); sort (Wei + 1, wei + n + 1); If (wei [1]> m) // because there may be a minimum number of records, DP [0] is initialized to 1, 1 return 0; For (INT I = 1; I <= N; I ++) sum [I] = sum [I-1] + wei [I]; __int64 ans = 0; For (INT I = 1; I <= N; I ++) {If (sum [I-1]> m) break; memset (DP, 0, sizeof (DP); DP [sum [I-1] = 1; for (Int J = I + 1; j <= N; j ++) for (int K = m; k> = sum [I-1] + wei [J]; k --) DP [k] + = DP [k-wei [J]; for (Int J = m-wei [I] + 1; j <= m; j ++) ans + = DP [J];} return ans;} int main () {INT cases; CIN> cases; for (int cnt = 1; CNT <= cases; CNT ++) {CIN> N> m; For (INT I = 1; I <= N; I ++) CIN> wei [I]; cout <CNT <"" <bag () <Endl;} return 0 ;}