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. UVA297:Quadtrees(四分树)

    题目描述 四象树是每个内结点均有4个子结点的特殊四叉树,它可用于描述平面上黑白图像.平面上的黑白图像是32行×32列的正方形,每个格子称为1个象素,是最小的图像单位.正方形图像可分成四个相等的小正方形 ...

  2. hdu6313( 2018 Multi-University Training Contest 2)

    bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6313 参考dls的讲解: 以5*5的矩阵为例: 后一列分别对前一列+0+1+2+3+4操 ...

  3. go语言的特点

    Go语言保证了既能到达静态编译语言的安全和性能,又达到了动态语言开发维护的高效率 ,使用一个表达式来形容Go语言:Go = C + Python , 说明Go语言既有C静态语言程 序的运行速度,又能达 ...

  4. Azkaban的功能特点(二)

    Azkaban是什么?(一) 不多说,直接上干货! http://www.cnblogs.com/zlslch/category/938837.html Azkaban的功能特点 它具有如下功能特点: ...

  5. 05.Javascript——入门函数

    //定义函数的方法1 function abs(x) { if (x >= 0) { return x; } else { return -x; } } 上述abs()函数的定义如下: func ...

  6. mongodb 上限集合

    上限集合是固定大小的循环集合按照插入以支持高性能的创建,读取和删除操作.通过循环,这意味着,当分配给该集合中的固定大小要用尽时,它会开始删除集合中最旧的文件而不提供任何明确的命令. 上限集合限制更新, ...

  7. react项目构建中的坑—淘宝镜像安装后要设置

    基本的搭建步骤参考博客:https://blog.csdn.net/qq_27727251/article/details/86593415 这里要强调的坑是:安装完淘宝镜像要将其设置为默认Regis ...

  8. java.lang.ClassCastException android.widget.RelativeLayout LayoutParams 异常

    1.在xml布局文件如下所示: <RelativeLayout android:layout_width="match_parent" android:layout_heig ...

  9. 图片压缩(pc端和移动端都适用)

    最近在做移动端遇到了一个问题就是: 手机拍照后,图片过大如果上传到服务器务必会浪费带宽,最重要的是流量啊 别慌,好事儿来了,务必就会有人去研究研究图片的压缩: 鄙人结合前人的经验,结合自己实战,总结出 ...

  10. C语言中的fprintf函数详解

    fprintf 功能 传送格式化输出到一个文件中 用法 #include   stdio.h int fprintf( FILE *stream, const char *format,...); f ...