(01 backpack) Buy The souvenirs (HDU 2126)

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=2126 Buy The SouvenirsTime limit:10000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1904 Accepted Submission (s): 711


problem Descriptionwhen The winter holiday comes, a lot of people would have a-trip. Generally, there is a lot of souvenirs to sell, and sometimes the travelers would buy some ones with pleasure. Not only can they give the souvenirs to their friends and families as gifts, but also can the souvenirs leave them good re Collections. All in all, the prices of souvenirs is not very dear, and the souvenirs is also very lovable and interesting. The money the people has is under the control. They can ' t buy a lot, but only a few. So after they admire all the souvenirs, they decide to buy some ones, and they has many combinations to select, but there is no ones with the same kind in any combination. Now there are a blank written by the names and prices of the souvenirs, as a top coder all around the world, you should CAL Culate How many selections you has, and any selection owns the most kinds of different souvenirs. For instance:



And you had only $7, this time you can select any combination with 3 kinds of souvenirs for most, so the selections of 3 kinds of souvenirs are ABC (6), ABD (7). But if you had 8 RMB, the selections with the most kinds of souvenirs is ABC (6), ABD (7), ACD (8), and if you had ten R MB, there is only one selection with the most kinds of souvenirs to YOU:ABCD (10).
 
InputFor the first line, there was a T means the number cases, then T cases follow.
In each case, in the first line there be, N and M, n is the number of the souvenirs and M was the money you hav E. The second line contains n integers; Each integer describes a kind of souvenir.
All the numbers and results is in the range of 32-signed Integer, and 0<=m<=500, 0<n<=30, t<=500, and the Prices is all positive integers. There is a blank line between the cases. 
OutputIf You can buy some souvenirs, you should print the result with the same formation as "you had s selection (s) to buy With K kind (s) of souvenirs ", where the K means the most kinds of souvenirs you can buy, and s means the numbers of the CO Mbinations you can buy with the K kinds of souvenirs combination. But sometimes can buy nothing, so you must print the result "Sorry, you can ' t buy anything." 
Sample Input2 3 2 3 4 
Sample OutputYou have 2 selection (s) to buy with 3 kind (s) of souvenirs. Sorry, you can ' t buy anything.Main topic:There are a total of n souvenirs, now you have M gold, tell you the price of n souvenirs, ask you how many souvenirs (max) you can buy, buy the most souvenirs how many combinations (sum) Ideas: Dp[i][k][j] = Dp[i-1][k][j] + dp[i-1][k-1][j-a[i]Dp[i][k][j] Represents a former I souvenir select K maximum value for the combination of JDimension ReductionDp[k][j] = Dp[k][j] + dp[k-1][j-a[i]Dp[k][j] Represents the number of combinations with K maximum value J
#include <iostream>#include<cstdio>#include<cstring>#include<queue>#include<vector>#include<map>#include<algorithm>using namespacestd;Const intN =550;Const intINF =0x3fffffff;Const Long LongMOD =1000000007; typedefLong LongLL;#defineMet (A, B) (Memset (A,b,sizeof (a)))inta[ +];intdp[ +][n];///Dp[k][j] Represents the selection of k items, of which the number of items with a value of JintMain () {intT; scanf ("%d", &u);  while(t--)    {        intI, J, K, N, M, max=0; scanf ("%d%d", &n, &m); Met (A,0); Met (DP,0);  for(i=1; i<=n; i++) scanf ("%d", &A[i]); dp[0][0] =1;  for(i=1; i<=n; i++)        {             for(K=i; k>=1; k--)            {                 for(J=a[i]; j<=m; J + +) {Dp[k][j]+ = dp[k-1][j-A[i]]; if(dp[k][j]&& (K>max))///if DP[K][J] has a value and K>max updates MaxMax =K; }            }        }        ///Max represents a maximum of max items from n items///sum represents the total number of combinations that have selected Max items        intsum =0;  for(i=0; i<=m; i++) Sum+=Dp[max][i]; if(!Max) printf ("Sorry, you can ' t buy anything.\n"); Elseprintf ("You have %d selection (s) to buy with%d kind (s) of souvenirs.\n", Sum, Max); }    return 0;}

(01 backpack) Buy The souvenirs (HDU 2126)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.