codeforces 360 E - The Values You Can Make
E - The Values You Can Make
Description
Pari wants to buy an expensive chocolate from Arya. She has n coins,
the value of the i-th coin is ci. The price of the chocolate is k, so Pari
will take a subset of her coins with sum equal to k and give it to Arya.
Looking at her coins, a question came to her mind: after giving the
coins to Arya, what values does Arya can make with them? She is
jealous and she doesn't want Arya to make a lot of values. So she
wants to know all the values x, such that Arya will be able to make
xusing some subset of coins with the sum k.
Formally, Pari wants to know the values x such that there exists a
subset of coins with the sum k such that some subset of this subset
has the sum x, i.e. there is exists some way to pay for the chocolate,
such that Arya will be able to make the sum x using these coins.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 500) — the
number of coins and the price of the chocolate, respectively.
Next line will contain n integers c1, c2, ..., cn (1 ≤ ci ≤ 500) — the
values of Pari's coins.It's guaranteed that one can make value k
using these coins.
Output
First line of the output must contain a single integer q— the number
of suitable values x. Then print q integers in ascending order — the
values that Arya can make for some subset of coins of Pari that pays
for the chocolate.
Sample Input
6 18
5 6 1 10 12 2
16
0 1 2 3 5 6 7 8 10 11 12 13 15 16 17 18
3 50
25 25 50
3
0 25 50
题意:
给你n个数和k,问n个数所有能构成k的子集合中所有的可能的和是多少?
分析:
dp[i][j]表示当前和是i能否构成j,如果dp[i][j]是可以构成的话,
那由于是子集合的关系dp[i+m][j]和dp[i+m][j+m]也可以构成。
dp: dp[i][j]=dp[i+m][j]+dp[i+m][j+m];
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[][];
int a[];
int main()
{
int n,k,m;
scanf("%d%d",&n,&k);
memset(dp,,sizeof(dp));
dp[][]=;
for(int i=;i<n;i++)
{
scanf("%d",&m);
for(int a=k;a>=m;a--)
{
for(int b=;b+m<=k;b++)
{
if(dp[a-m][b])
{
dp[a][b]=;
dp[a][b+m]=;
}
}
} }
int len=;
for(int b=;b<=k;b++)
if(dp[k][b])
{
a[len]=b;
len++;
} sort(a,a+len);
printf("%d\n",len);
for(int i=;i<len-;i++)
printf("%d ",a[i]);
printf("%d\n",a[len-]);
return ;
}
codeforces 360 E - The Values You Can Make的更多相关文章
- [codeforces 360]A. Levko and Array Recovery
[codeforces 360]A. Levko and Array Recovery 试题描述 Levko loves array a1, a2, ... , an, consisting of i ...
- codeforces 360 D - Remainders Game
D - Remainders Game Description Today Pari and Arya are playing a game called Remainders. Pari choos ...
- 套题 codeforces 360
A题:Opponents 直接模拟 #include <bits/stdc++.h> using namespace std; ]; int main() { int n,k; while ...
- codeforces 688E E. The Values You Can Make(dp)
题目链接: E. The Values You Can Make time limit per test 2 seconds memory limit per test 256 megabytes i ...
- 【43.75%】【codeforces 688E】The Values You Can Make
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- codeforces 688 E. The Values You Can Make(01背包+思维)
题目链接:http://codeforces.com/contest/688/problem/E 题解:设dp[s1][s2]表示s1状态下出现s2是否合理.那么s1显然可以更具01背包来得到状态.首 ...
- codeforces 360 C
C - NP-Hard Problem Description Recently, Pari and Arya did some research about NP-Hard problems and ...
- codeforces 360 C - NP-Hard Problem
原题: Description Recently, Pari and Arya did some research about NP-Hard problems and they found the ...
- codeforces 360 B
B - Levko and Array 题目大意:给你你个长度为n的数列a,你最多改变k个值,max{ abs ( a[ i + 1] - a[ i ] ) } 的最小值为多少. 思路:这个题很难想到 ...
随机推荐
- JS客户端RSA加密,Java服务端解密
常用语网页客户端对密码加密,在后端java解密还原 java代码依赖 <dependency> <groupId>commons-codec</group ...
- Gitlab完美安装【CentOS6.5安装gitlab-6.9.2】
摘要: 拆腾了几天,终于在今天找到了快速安装Gitlab的方法.CentOS6.5安装gitlab-6.9.2 参考网址:https://gitlab.com/gitlab-org/omnibus-g ...
- input函数出现的问题(Python)
参考书<A Learner's Guide to Programming Using the Python Language>,写下如下的程序: # coding=utf-8 # 以上是为 ...
- dubbo升级spring4与cxf
阿里的dubbo项目是基于spring2.x的.但之后停止更新了.当当网有根据dubbo扩展成dubbox,基于spring3.x,但我的项目中用的是spring4.x,于是fork了dubbox,自 ...
- 学习CSS 笔记
1.动态修改div的大小 Html: <div> Hello </div> css: div { resize:both; overflow:auto; } 2. box-si ...
- 创建/发布cocoapods公共库
对于大多数iOS开发者而言,cocoapods都是一个非常便捷的第三方库引导工具,该工具可以帮助我们快速导入所需第三方库,并且进行相关配置. 本文即为描述如何发布一个第三方库,提供给所有的开发者使用. ...
- JPA快速入门(自用)
该案例为JPA的实体映射案例,将该项目解压导入到myeclipse中即可使用,然后直接使用hibernate即可! 文件地址:files.cnblogs.com/mrwangblog/JPA-Hibe ...
- request:getParameter getAttribute
转载自:http://www.cnblogs.com/shaohz2014/p/3804656.html 在浏览器地址输入,表示传入一个参数test,值为123 http://localhost:88 ...
- OSError: libcudart.so.7.5: cannot open shared object file: No such file or directory
在ubuntu14.04-64-bit,安装完cuda ,cudnn.opencv后, 配置完MXNet,运行demo 时出现错误,库路径环境变量问题,解决方法: sudo ldconfig /usr ...
- JAVA内部类有关
最近在看单例模式的实现,看到有一种利用JAVA静态内部类的特性来实现,对于内部类我还真是不了解,遂了解了一下,代码贴上. /** * 内部类分为:成员内部类.局部内部类.匿名内部类和静态内部类. */ ...