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. javaee 导航

    tomcate端口设定和服务器虚拟目录设定 静态web 应用和动态web应用 tomcat相关问题 web应用 http 响应 url uri 动态页面 servlet 一个简单的servlet的de ...

  2. windows 下搭建简易nginx+PHP环境

    2016年11月19日 14:40:16 星期六 官网下载 nginx, php windows下的源码包(windows下不用安装, 解压即可) 修改配置文件, (稍后补上) 路径如下: 启动脚本: ...

  3. CI 扩展 Service

    CI 扩展 Service 说明 CodeIgniter是一套典型的MVC框架,M负责数据,C负责交互,V负责视图,但是随着业务逻辑越来越复杂, 必然会涉及到一些列操作过程,例如用户下订单,就会存在校 ...

  4. 解除sql server数据库占用

    use master go ) set @dbName='dbname' --数据库名 ) declare cur_lock cursor for SELECT DISTINCT request_se ...

  5. Mybatis 总结

    1.如何传递多个参数 mybatis中,如果接口有多个参数,那么在mapper.xml中,可以通过#{0,VARCHAR},#{1,VARCHAR}或#{param1,VARCHAR},#{param ...

  6. STM32F412应用开发笔记之一:初识NUCLEO-F412ZG

    今天终于收到了期待已久的NUCLEO-F412ZG,感谢电子发烧友论坛! 近几年来基本都是在STM32平台上做一些设计开发工作.STM32F103.STM32F107.STM32F429等都应用过,但 ...

  7. c/c++的Soap应用

    1. 关于soap 在许多项目中团队中,我们常常会听到这样的话:我们这里是用webservice交互的.而说话的场景往往就是交互对象双方比较异构,所谓异构.即双方是不同的开发语言.不同的运行环境等.比 ...

  8. C++获取当前目录

    /* @author:CodingMengmeng @theme:C++获取当前目录 @time:2017-1-6 21:03:34 @blog:http://www.cnblogs.com/codi ...

  9. PHPCMS v9 安全防范教程

    一.目录权限设置很重要:可以有效防范黑客上传木马文件.如果通过 chmod 644 * -R 的话,php文件就没有权限访问了.如果通过chmod 755 * -R 的话,php文件的权限就高了. 所 ...

  10. jQuery简单倒计时插件

    一. 效果预览 二. 实现 1. 按照特定的类结构布局. 2. 需要先引入jQuery,再引入此文件. /** * Author: CC11001100 * * 简单倒计时 * * 1. 支持页面内同 ...