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

Input
6 18
5 6 1 10 12 2
Output
16
0 1 2 3 5 6 7 8 10 11 12 13 15 16 17 18
Input
3 50
25 25 50
Output
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的更多相关文章

  1. [codeforces 360]A. Levko and Array Recovery

    [codeforces 360]A. Levko and Array Recovery 试题描述 Levko loves array a1, a2, ... , an, consisting of i ...

  2. codeforces 360 D - Remainders Game

    D - Remainders Game Description Today Pari and Arya are playing a game called Remainders. Pari choos ...

  3. 套题 codeforces 360

    A题:Opponents 直接模拟 #include <bits/stdc++.h> using namespace std; ]; int main() { int n,k; while ...

  4. 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 ...

  5. 【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 ...

  6. codeforces 688 E. The Values You Can Make(01背包+思维)

    题目链接:http://codeforces.com/contest/688/problem/E 题解:设dp[s1][s2]表示s1状态下出现s2是否合理.那么s1显然可以更具01背包来得到状态.首 ...

  7. codeforces 360 C

    C - NP-Hard Problem Description Recently, Pari and Arya did some research about NP-Hard problems and ...

  8. codeforces 360 C - NP-Hard Problem

    原题: Description Recently, Pari and Arya did some research about NP-Hard problems and they found the  ...

  9. codeforces 360 B

    B - Levko and Array 题目大意:给你你个长度为n的数列a,你最多改变k个值,max{ abs ( a[ i + 1] - a[ i ] ) } 的最小值为多少. 思路:这个题很难想到 ...

随机推荐

  1. Google Code Jam 2016 Round 1B B

    题意:给出两个数字位数相同,分别中间有若干位不知道,用问号表示.现在要求补全这两个数字,使得差值的绝对值最小,多解则取第一个数字的值最小的,再多解就取第二个数字最小的. 分析: 类似数位dp,但是很多 ...

  2. 使用Visual Leak Detector for Visual C++ 捕捉内存泄露

    什么是内存泄漏? 内存泄漏(memory leak),指由于疏忽或错误造成程序未能释放已经不再使用的内存的情况.内存泄漏并非指内存在物理上的消失,而是应用程序分配某段内存后,由于设计错误,失去了对该段 ...

  3. select样式美化(简单实用)

    美化select,可以实用一个a标签将select嵌套进去,然后将<a>相对定位,在将select绝对定位,美化<a>即可 文件下载链接:https://i.cnblogs.c ...

  4. mybatis 配置连接池

    <!-- 配置数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidData ...

  5. 建表过程-列名&列类型&修改表B

    怎么建表? 主键 名称 重量 价格 生产日期 保质期 产地 种类                       分析:我们只要把第一行的表头建好后,这张表也就完成了.  术语:建表的过程就是声明字段过程 ...

  6. Counter Mode ( CTR )

    Encryption Decryption

  7. node(Buffer缓存区)

    // 创建buffer类 var buf=new buffer(10); var buf=new buffer([10,20,30,40]); var buf=new buffer("www ...

  8. windows C++实现注销、重启、关机 logoff reboot shutdown

    实现这一功能很简单,主要需要调用一个系统API ExitWindowsEx 功能就是,注销当前用户,关闭系统,或者重新启动系统. 它会发送一个WM_QUERYENDSESSION消息给所有的应用程序, ...

  9. 搭建LAMP环境注意事项

    一:安装mysql 5.5以上版本需要使用cmake 和 bison 并且需要安装ncurses 在安装MySQL完毕之后,需要覆盖 掉 /etc/my.cnf centos默认会有一个my.cnf文 ...

  10. 【Java EE 学习 56】【酒店会员管理系统技术点总结】

    一.树状菜单加载 这是js的一个典型应用,使用zTree插件能够完成该项任务http://www.ztree.me/v3/main.php 我是用的版本:zTree2.5 使用方法: <%@ p ...