hdu2126--Backpack DP (on state)--buy the Souvenirs

Source: Internet
Author: User

Description

When the winter holiday is comes, a lot of people'll has 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).

Input

For 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.

Output

If 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 Combin Ations 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 Input

24 71 2 3 44 01 2 3 4

Sample Output

You have 2 selection (s) to buy with 3 kind (s) of souvenirs. Sorry, you can ' t buy anything. The main idea: to give you n items you have m dollars, ask the most can buy several items, and this is the most of this kind of situation can choose,
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;intMain () {intdp[550][2]; intT; intw[550]; intn,m; scanf ("%d",&T);  while(t--) {scanf ("%d%d",&n,&m);  for(inti =1; I <= N; i++) scanf ("%d",&W[i]); Memset (DP,0,sizeof(DP));  for(inti =0; I <= m; i++) dp[i][1] =1;  for(inti =1; I <= N; i++){             for(intj = m; J >= W[i]; j--){                if(dp[j][0] = = dp[j-w[i]][0] +1){//Dp[j][0] refers to the number of gifts bought with J money, if the number of gifts bought with J money and the amount of money used to buy I items, the number of gifts to buy 1, indicating that the type of gift bought with J is the sum of the twodp[j][1] = dp[j][1] + dp[j-w[i]][1];//                }                Else if(dp[j][0] < dp[j-w[i]][0] +1){//if not greater than, description dp[j-w[i]][0] better than dp[j][0]dp[j][1] = dp[j-w[i]][1]; dp[j][0] = dp[j-w[i]][0] +1; }            }        }        if(dp[m][0]) printf ("You have %d selection (s) to buy with%d kind (s) of souvenirs.\n", dp[m][1],dp[m][0]); Elseprintf"Sorry, you can ' t buy anything.\n"); }    return 0;}
View Code

hdu2126--Backpack DP (on state)--buy the Souvenirs

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.