Buy the souvenirs

Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1886    Accepted Submission(s): 699

Problem Description
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.
 
Author
wangye
 
Source
 
题意: n个物品对应不同的价值且只有一件 现在有m元 问 在最多能购买k种物品的情况下有s种购买方案。
输出 You have s selection(s) to buy with k kind(s) of souvenirs.
否则输出 Sorry, you can't buy anything.
 
题解:  不要写空行 有空行pe  坑
01背包 增加一维  f[j][k] 表示 j元钱 购买k种(件)物品有多少种方案
方程 f[j][k]=f[j][k]+f[j-val[i]][k-1];
 
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define ll __int64
using namespace std;
int t;
int n,m;
int val[];
int f[][];
int flag;
int main()
{
while(scanf("%d",&t)!=EOF)
{
for(int i=;i<=t;i++)
{
scanf("%d %d",&n,&m);
memset(f,,sizeof(f));
memset(val,,sizeof(val));
for(int j=;j<=m;j++)
f[j][]=;
flag=-;
for(int j=;j<=n;j++)
scanf("%d",&val[j]);
for(int j=;j<=n;j++)
for(int k=m;k>=val[j];k--)
{
for(int g=j;g>=;g--)
{
f[k][g]=f[k][g]+f[k-val[j]][g-];
if(f[k][g])
{
if(flag<g)
flag=g;
}
}
}
if(flag==-)
printf("Sorry, you can't buy anything.\n");
else
printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n",f[m][flag],flag);
}
}
return ;
}

HDU 2126 01背包(求方案数)的更多相关文章

  1. 洛谷P1164 小A点菜(01背包求方案数)

    P1164 小A点菜 题目背景 uim神犇拿到了uoi的ra(镭牌)后,立刻拉着基友小A到了一家……餐馆,很低端的那种. uim指着墙上的价目表(太低级了没有菜单),说:“随便点”. 题目描述 不过u ...

  2. HDU 1171 Big Event in HDU【01背包/求两堆数分别求和以后的差最小】

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...

  3. HDU 2639 01背包求第k大

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. HDU5119【dp背包求方案数】

    题意: 有n个数,问有多少方案满足取几个数的异或值>=m; 思路: 背包思想,每次就是取或不取,然后输出>=m的方案就好了. #include <bits/stdc++.h> ...

  5. Uva674 完全背包求方案数

    记忆化搜索.注意输入n的位置,否则Tle. dp[i][j]表示用前j种硬币组成i分钱时的种类数 那么状态转移方程是:dp[i][j]+=DP(i-k*v[j],j-1) #include<io ...

  6. openj 4004 01背包问题求方案数

    #include<iostream> #include<cstring> #include<cstdio> using namespace std; #define ...

  7. 关于01背包求第k优解

    引用:http://szy961124.blog.163.com/blog/static/132346674201092775320970/ 求次优解.第K优解 对于求次优解.第K优解类的问题,如果相 ...

  8. 背包DP 方案数

    题目 1 P1832 A+B Problem(再升级) 题面描述 给定一个正整数n,求将其分解成若干个素数之和的方案总数. 题解 我们可以考虑背包DP实现 背包DP方案数板子题 f[ i ] = f[ ...

  9. poj3254 Corn Fields 利用状态压缩求方案数;

    Corn Fields 2015-11-25 13:42:33 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10658   ...

随机推荐

  1. 官方yum源安装选择所需版本mysql数据库并初始化(yum默认安装的是最新版MySQL8.+)

    在官网是找不到5.x系列的域名源的,系统默认是安装的oracle数据库,在安装前需要删除默认的 以下教程来源于官网说明 先去官网下载yum源,地址 https://dev.mysql.com/down ...

  2. stm32+lwip(三):TCP测试

    我是卓波,很高兴你来看我的博客. 系列文章: stm32+lwip(一):使用STM32CubeMX生成项目 stm32+lwip(二):UDP测试 stm32+lwip(三):TCP测试 stm32 ...

  3. 使用maven插件生成grpc所需要的Java代码

    1.首先需要编写自己需要的.proto文件,本文重点不在这里,.proto可以参考grpc官方例子 https://grpc.io/docs/quickstart/java.html 2.创建自己的J ...

  4. MySQL数据库服务器逐渐变慢分析与解决

    一.检查系统的状态 通过操作系统的一些工具检查系统的状态,比如CPU.内存.交换.磁盘的利用率,根据经验或与系统正常时的状态相比对,有时系统表面上看起来看空闲,这也可能不是一个正常的状态,因为cpu可 ...

  5. Autofac小例子

    AutoFac是.net平台下的IOC容器产品.今天学习一下它的使用方法. 1.最简单的使用. public interface ITestDao { string SayHello(); } pub ...

  6. 正则表达式 Pattern和Matcher

    java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包. 1.简介:  java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包. ...

  7. 《python核心编程第二版》第5章习题

    5-1 整形 讲讲 Python 普通整型和长整型的区别 答:普通整型 32位,长整数类型能表达的 数值仅仅与你的机器支持的(虚拟)内存大小有关 5-2 运算符(a) 写一个函数,计算并返回两个数的乘 ...

  8. jmeter实例,如果有说明错误,请各位大神批评

    首先我们打开jmeter,今天录制的脚本的是获取QQ头像,找了好久才找到可以免费试用的接口,如果有什么错误的地方,欢迎大家提出来,我会及时修改,也给自己一次进步的机会,希望大家不吝赐教!!!如果有什么 ...

  9. 阿里云SLB漏选“健康检查正常的http状态码”导致url重定向失败问题处理

    背景:           一客户将线下电商网站迁移到阿里云上,公网出口使用阿里云SLB,SLB后端实例为ECS(webserver)web服务使用nginx.后端APP服务器使用了tomcat:to ...

  10. 1098 Insertion or Heap Sort (25 分)(堆)

    这里的第二序列相当于是排序还没拍好的序列 对于第二个样例的第二个序列其实已经是大顶堆了 然后才进行的堆排序 知道这个就好做了 #include<bits/stdc++.h> using n ...