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. python之线程进程协成

    线程与进程 什么是线程 线程是进程一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位,线程自己基本上不拥有系统资源,只拥有一点在运行中必不可少的资源,但是它可与同属一个线程的 ...

  2. UML大战需求分析——阅读笔记06

    状态机图和活动图在样子比较相似,但状态机图是用来为对象的状态及造成状态改变的事件建模.我们大二学习UML统一建模语言状态机图模块时了解到,UML的状态机图主要用于建立对象类或对象的动态行为模型,描述系 ...

  3. JavaScript通过id获取不到元素是什么原因阿?

    s代码 JavaScript code   ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 function show_more_mess() {     $(&qu ...

  4. 【Java EE 学习 71 下】【数据采集系统第三天】【分析答案实体】【删除问题】【删除页面】【删除调查】【清除调查】【打开/关闭调查】

    一.分析答案实体 分析答案实体主要涉及到的还是设计上的问题,技术点几乎是没有的.首先需要确定一下答案的格式才能最终确定答案实体中需要有哪些属性. 答案格式的设计是十分重要的,现设计格式如下: 在表单中 ...

  5. C++ 系列:多线程编程基础知识

    Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...

  6. [转]序列化悍将Protobuf-Net,入门动手实录

    最近在研究web api 2,看了一篇文章,讲解如何提升性能的, 在序列化速度的跑分中,Protobuf一骑绝尘,序列化速度快,性能强,体积小,所以打算了解下这个利器 1:安装篇 谷歌官方没有提供.n ...

  7. Linux 使用Crontab设置定时调用Shell文件

    定时执行步骤: 利用crontab来定时执行任务大致有如下两步: 1.编写shell脚本 2.利用crontab加入到定时任务队列 一.如何建立shell脚本 程序必须以下面的行开始(必须方在文件的第 ...

  8. android 查看当前正在运行的进程

    转载至 https://github.com/wenmingvs/AndroidProcess 因为Android5.0以上的权限封锁,无法直接获取正在运行的进程,此文总共介绍6中方法获取, 详细介绍 ...

  9. mysql查询练习题-2016.12.16

    >>>>>>>>>> 练习时间:2016.12.16 编辑时间:2016-12-20-->22:12:08 题: 涉及:多表查询.ex ...

  10. NoSQL 数据库分类

    类型 部分代表 特点 列存储 Hbase Cassandra Hypertable 顾名思义,是按列存储数据的.最大的特点是方便存储结构化和半结构化数据,方便做数据压缩,对针对某一列或者某几列的查询有 ...