I - The Values You Can Make (背包求具体方案)
题目大意
给你n个数,让你用这n个数在组成k的情况下,找到所有的value,这些value也由这n个数组成,且这些value组合在一起能够组成k
解法
看到题目我的想法就是母函数= =不过wa了,后来发现因为母函数能找到这n个数所能形成的所有情况,但是可能两种情况是包含关系的。比如3,3,6这个数据可以形成6和9但是如果k是15的时候,你就不能得到因为9是由6生成的
dp[i][j]表示在和为i的情况下,能否得到j
那么当dp[i][j] = 1时,dp[i][j + c[k]]比然能得到,dp[i+c[k]][j+c[k]]也能得到。
C++代码
/**
6 18
5 6 1 10 12 2
dp[i-c[i]][k] -> dp[i][k],dp[i][k+c[i]]
*/ #include<bits/stdc++.h>
using namespace std; int a[];
int res[];
int dp[][];
int main(){
int n , m ;
cin >> n >> m;
for(int i = ; i <= n ; i++){
cin >> a[i];
}
sort(a+,a++n);
dp[][] = ;
for(int i = ;i <= n ; i++){
for(int j = m;j >= a[i];j --){
for(int k = ;k + a[i]<= m;k ++){
if(dp[j - a[i]][k]) dp[j][k] = dp[j][k+a[i]] = ;
}
}
}
int tot = ;
for(int i = ;i <= m; i ++) if(dp[m][i]) res[tot++] = i;
printf("%d\n",tot);
for(int i = ;i < tot;i ++) printf("%d ",res[i]);
}
I - The Values You Can Make (背包求具体方案)的更多相关文章
- P1474 货币系统 Money Systems(完全背包求填充方案数)
题目链接:https://www.luogu.org/problemnew/show/1474 题目大意:有V种货币,求用V种货币凑出面值N有多少种方案. 解题思路:就是完全背包问题,只是将求最大价值 ...
- P1466 集合 Subset Sums(01背包求填充方案数)
题目链接:https://www.luogu.org/problem/show?pid=1466 题目大意:对于从1到N (1 <= N <= 39) 的连续整数集合,能划分成两个子集合, ...
- HDU 2639(01背包求第K大值)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2639 Bone Collector II Time Limit: 5000/2000 MS (Jav ...
- 关于01背包求第k优解
引用:http://szy961124.blog.163.com/blog/static/132346674201092775320970/ 求次优解.第K优解 对于求次优解.第K优解类的问题,如果相 ...
- 518-零钱兑换 II(完全背包-求方案总数)
518-零钱兑换 II(完全背包-求方案总数) 给定不同面额的硬币和一个总金额.写出函数来计算可以凑成总金额的硬币组合数.假设每一种面额的硬币有无限个. 示例 1: 输入: amount = 5, c ...
- poj3683(2-SAT 求任意方案)
基础的2-SAT求任意方案的题目. Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- HDU 2639 01背包求第k大
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- Bone Collector II---hdu2639(01背包求第k优解)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2639求01背包的第k大解.合并两个有序序列 选取物品i,或不选.最终的结果,是我们能在O(1)的时间内 ...
- 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 ...
随机推荐
- mybatis config 配置设置说明
<!– 配置设置 –> 2. <settings> 3. <!– 配置全局性 cache 的 ( 开 / 关) defau ...
- ExcelUtils
本ExcelUtils工具类是用poi写的,仅用于线下从excel文件中读取数据.如果生产环境要用的话,建议切换到阿里的easyexcel. 引入poi.jar: <!-- https://mv ...
- luoguP1025+codevs 1039 数的划分 x
luoguP1025 + codevs1039 数的划分 2001年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Des ...
- 目标检测Object Detection概述(Tensorflow&Pytorch实现)
1999:SIFT 2001:Cascades 2003:Bag of Words 2005:HOG 2006:SPM/SURF/Region Covariance 2007:PASCAL VOC 2 ...
- socket编程相关阐述
一.socket初识 ①服务端 import socket server = socket.socket() server.bind(('127.0.0.1', 8080)) server.liste ...
- Python3学习笔记(八):集合
集合(set)是一种可变的无序的不重复的数据类型 要创建集合,需要将所有项(元素)放在花括号({})内,以逗号(,)分隔. >>> s = {'p','y','t','h','o', ...
- [LeetCode]-algorithms-Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- mysql补0操作有什么意义?
比如我们在创建int的时候会使用int(10)这样的方式来定义某一个列,但是这样定义是没有任何意义的. Create Table showzerofill(Val1 INT(5) ZEROFILL, ...
- JavaScript中this的一些坑
我们经常在回调函数里面会遇到一些坑: var obj = { name: 'qiutc', foo: function() { console.log(this); }, foo2: function ...
- Mysql基本管理知识
数据库的启动 [root@node80 ~]# /etc/init.d/mysqld start #mysqld是从安装包拷贝的mysql.server Starting MySQL. SUCCESS ...