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. 初学tiny4412

    1.解压友善之臂提供的uboot make tiny4412_config make 然后将sd卡插到电脑上,编辑虚拟机,选择对应的usb口(usb3.0兼容),如果没有usb3.0,可能是虚拟机版本 ...

  2. Python爬虫爬取百度翻译之数据提取方法json

    工具:Python 3.6.5.PyCharm开发工具.Windows 10 操作系统 说明:本例为实现输入中文翻译为英文的小程序,适合Python爬虫的初学者一起学习,感兴趣的可以做英文翻译为中文的 ...

  3. 017---Django的中间件解决跨域

    跨域 跨域是什么? 浏览器从一个域名的网页去请求另一个域名的资源的时候,如果不同源.请求的响应结果就会被浏览器的同源策略所拦截 同源策略是什么? 同源:协议 + 域名 + 端口 特点:阻止ajax请求 ...

  4. python pip ,安装,卸载,查看等命令,不同版本

    pycharm及python的使用说明   Python和 pycharm的使用 1. pycharm和Python 下载 安装后需要激活码.判断Python是否安装好了,cmd下跑: python ...

  5. 【Consul】关于健康检查的一点思考

    健康检查是Consul提供的一项主要功能,其配置格式如下: { "check": { "id": "redis", "name&q ...

  6. 【Luogu P4644】Cleaning Shifts

    题目 给定 \(n\) 个区间 \([a_i, b_i]\), 花费为 \(c_i\), 求覆盖 \([L, R]\) 区间的所有整数的最小花费. \(0\le n \le 10^4, 0\le L, ...

  7. Qt BarChart实践

    按照帮助文档编写 运行截图 上代码 #include "widget.h" #include "ui_widget.h" Widget::Widget(QWid ...

  8. Linux-Shell脚本编程-学习-3-Shell编程-shell脚本基本格式

    前面两篇文章基本介绍了一部分linux下的基本命令,后面还需要大家自行了解下linux的文件系统的磁盘管理部分,这里就不在写了. 什么是shell编程,我也解释不来,什么是shell脚本了,我理解就是 ...

  9. 51单片机实现外部中断0-F

    #include< reg51.h> #define uint unsigned int #define uchar unsigned char sfr P0M0 = 0x94; sfr ...

  10. Spring Boot 学习随记

    微架构的思想在各大互联网公司越来越普及,特此记录Spring Boot的一些细节问题! 网上spring-boot的教程一堆一堆,就没有必要再详细记录了 1:建议通过Idea 来创建spring-bo ...