When the winter holiday comes, a lot of people will have a trip. Generally, there are a lot of souvenirs to sell, and sometimes the travelers will 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 recollections. All in all, the prices of souvenirs are not very dear, and the souvenirs are also very lovable and interesting. But the money the people have 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 have many combinations to select, but there are no two ones with the same kind in any combination. Now there is a blank written by the names and prices of the souvenirs, as a top coder all around the world, you should calculate how many selections you have, and any selection owns the most kinds of different souvenirs. For instance:

And you have only 7 RMB, this time you can select any combination with 3 kinds of souvenirs at most, so the selections of 3 kinds of souvenirs are ABC (6), ABD (7). But if you have 8 RMB, the selections with the most kinds of souvenirs are ABC (6), ABD (7), ACD (8), and if you have 10 RMB, there is only one selection with the most kinds of souvenirs to you: ABCD (10).

Input


For the first line, there is a T means the number cases, then T cases follow.

In each case, in the first line there are two integer n and m, n is the number of the souvenirs and m is the money you have. The second line contains n integers; each integer describes a kind of souvenir.

All the numbers and results are in the range of 32-signed integer, and 0<=m<=500, 0<n<=30, t<=500, and the prices are all positive integers. There is a blank line between two cases.

Output


If you can buy some souvenirs, you should print the result with the same formation as “You have 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 combinations you can buy with the K kinds of souvenirs combination. But sometimes you can buy nothing, so you must print the result “Sorry, you can't buy anything.”

Sample Input

2
4 7
1 2 3 4
4 0
1 2 3 4

Sample Output

You have 2 selection(s) to buy with 3 kind(s) of souvenirs.
Sorry, you can't buy anything.

题解

求最大取得值显然是01背包,需要求方案数,只要再开一个数组在转移时记录即可

01背包转移方程为

dp[j]代表花费为j时的最大选择数
For i:1~n
For j:m~a[i]
dp[j]=max(dp[j],d[j-a[i]]+1);
f[j]代表花费为j时最大选择数的方案数
if(dp[j]==dp[j-a[i]]+1)
f[j]+=f[j-a[i]]
if(dp[j]<dp[j-a[i]]+1)
f[j]=f[j-a[i]];
#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
#define inf 1000000000
#define PI acos(-1)
#define REP(i,x,n) for(int i=x;i<=n;i++)
#define DEP(i,n,x) for(int i=n;i>=x;i--)
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int N=500+5;
int dp[N],f[N];
int a[35];
int main(){
int T=read();
while(T--){
int n=read(),m=read();
REP(i,1,n) a[i]=read();
mem(dp,0);mem(f,0);
REP(i,0,m) f[i]=1;
REP(i,1,n) DEP(j,m,a[i]){
if(dp[j]==dp[j-a[i]]+1)
f[j]=f[j-a[i]]+f[j];
else if(dp[j]<dp[j-a[i]]+1){
dp[j]=dp[j-a[i]]+1;
f[j]=f[j-a[i]];
}
}
if (dp[m]!=0)
printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n"
, f[m], dp[m]);
else
printf("Sorry, you can't buy anything.\n");
}
return 0;
}

【HDU 2126】Buy the souvenirs(01背包)的更多相关文章

  1. HDU 2126 Buy the souvenirs (01背包,输出方案数)

    题意:给出t组数据 每组数据给出n和m,n代表商品个数,m代表你所拥有的钱,然后给出n个商品的价值 问你所能买到的最大件数,和对应的方案数.思路: 如果将物品的价格看做容量,将它的件数1看做价值的话, ...

  2. hdu 2126 Buy the souvenirs 二维01背包方案总数

    Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  3. hdu 2126 Buy the souvenirs(记录总方案数的01背包)

    Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. hdu 2126 Buy the souvenirs 买纪念品(01背包,略变形)

    题意: 给出一些纪念品的价格,先算出手上的钱最多能买多少种东西k,然后求手上的钱能买k种东西的方案数.也就是你想要买最多种东西,而最多种又有多少种组合可选择. 思路: 01背包.显然要先算出手上的钱m ...

  5. hdu 2126 Buy the souvenirs 【输出方案数】【01背包】(经典)

    题目链接:https://vjudge.net/contest/103424#problem/K 转载于:https://blog.csdn.net/acm_davidcn/article/detai ...

  6. [HDU 2126] Buy the souvenirs (动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2126 题意:给你n个物品,m元钱,问你最多能买个多少物品,并且有多少种解决方案. 一开始想到的是,先解 ...

  7. HDU 5234 Happy birthday --- 三维01背包

    HDU 5234 题目大意:给定n,m,k,以及n*m(n行m列)个数,k为背包容量,从(1,1)开始只能往下走或往右走,求到达(m,n)时能获得的最大价值 解题思路:dp[i][j][k]表示在位置 ...

  8. HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

  9. HDOJ(HDU).2546 饭卡(DP 01背包)

    HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...

  10. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

随机推荐

  1. Guard Duty (hard) Codeforces - 958E3 || uva 1411

    https://codeforces.com/contest/958/problem/E3 当没有三点共线时,任意一个这样的点集都是保证可以找到答案的,(考虑任意一种有相交的连线方案,一定可以将其中两 ...

  2. python tickle模块与json模块

    #! /usr/bin/env python# -*- coding:utf-8 -*-#JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式 ...

  3. 用注解@DelcareParents实现引用增强

    引用增强,是一个比较特殊的增强,不同于其他方法级别的增强. 引用增强可以实现:一个Java类,没有实现A接口,在不修改Java类的的情况下,使其具备A接口的功能. 先看看背景,我们有个Love接口: ...

  4. Hadoop工作流引擎之Azkaban与Oozie对比(四)

    Azkaban是什么?(一) Azkaban的功能特点(二) Azkaban的架构(三) 不多说,直接上干货! http://www.cnblogs.com/zlslch/category/93883 ...

  5. python flask学习(2)

    本文主要整理下几个Flask扩展: 0.Flask-Bootstrap:集成Twitter开发的一个开源框架Bootstrap.1.Flask-Script:为Flask程序添加一个命令行解析器2.F ...

  6. [转]依赖注入框架Autofac的简单使用

    本文转自:http://www.nopchina.net/post/autofac.html 话说nopcommerce底层用到了autofac框架,这里转了一篇文章简单说明一下: Autofac是一 ...

  7. 安卓linux真机调试

    原文链接:https://www.zhihu.com/question/35517675 你使用的是Linux,请遵以下步骤执行. 以root用户执行adb kill-server 以root用户执行 ...

  8. vue2.0:(二)、mock数据

    什么是mock数据呢?很多情况下,后台的搭建比起前端来说要麻烦的多,所以,常常是前端写好了页面以后后台接口却没有写好,但是在一个项目中,接口调试确实是最浪费时间的,所以,往往前端需要自己模拟数据. 第 ...

  9. vscode设置html默认浏览器

    Vscode版本:1.30.2,设置方法:file→preference→settings,剩余设置如下图.

  10. 从零开始利用vue-cli搭建简单音乐网站(二)

    1.利用vue-router实现页面跳转 程序可以正常运行之后,下面我们需要配置路由实现页面的局部刷新,这一功能将用来实现网站页面的跳转. 打开程序目录,进入"src\router\inde ...