题目链接:

E. The Values You Can Make

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
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的数,用这些数能组成哪些数; 思路: dp[i][j]表示和为i的那些数能否组成j,dp[i][j]=1表示可以,0表示不行, 现在面临第c[x],当把c[x]加到已有的集合中当dp[i][j]==1时dp[i+c[x]][j]=dp[i+c[x]][j+c[x]]=1;
转移的时候枚举i要从大到小枚举,否则当前转移的c[x]较小的和会对当前c[x]较大的和产生影响; AC代码:
//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio> using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=+;
const int maxn=;
const double eps=1e-; int n,k;
int c[N],dp[][*N]; int main()
{
read(n);read(k);
for(int i=;i<=n;i++)
read(c[i]);
// freopen("out.txt","w",stdout);
dp[][]=;
for(int i=;i<=n;i++)
{
for(int j=k;j>=;j--)
{
if(j+c[i]<=k)
{
for(int x=;x<=k;x++)
{
if(dp[j][x])dp[j+c[i]][x]=dp[j+c[i]][x+c[i]]=;
}
}
}
}
int ans=;
for(int i=;i<=k;i++)
{
if(dp[k][i])ans++;
}
cout<<ans<<"\n";
for(int i=;i<=k;i++)
{
if(dp[k][i])printf("%d ",i);
} return ;
}

codeforces 688E E. The Values You Can Make(dp)的更多相关文章

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

  2. Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)

    题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...

  3. Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题

    除非特别忙,我接下来会尽可能翻译我做的每道CF题的题面! Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题 题面 胡小兔和司公子都认为对方是垃圾. 为了决出谁才是垃 ...

  4. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  5. Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)

    Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...

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

  7. Codeforces Round #360 (Div. 2) E. The Values You Can Make DP

    E. The Values You Can Make     Pari wants to buy an expensive chocolate from Arya. She has n coins, ...

  8. Codeforces 687C. The Values You Can Make (dp)

    题目链接:http://codeforces.com/problemset/problem/687/C 题目大概说给n个各有价值的硬币,要从它们中选出若干个组合成面值k,而要求的是各个方案里这些选出的 ...

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

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

随机推荐

  1. jmeter-如何进行参数化-循环读取参数

    在进行测试的时候,测试数据是一项重要的准备工作,每次迭代的数据当不一样的时候,需要进行参数化,从参数化的文件中来读取测试数据. 本经验主要介绍的是用Csv Data配置元件来进行参数化. 方法/步骤 ...

  2. 性能测试工具 - ab 简单应用

    之前知道一般网站性能可以通过 LoadRunner, JMeter, QTP 等相应的软件进行测试, 印象中本科学习 “软件测试” 这门课程时安装并使用过, LoadRunner等不是一个小软件, 安 ...

  3. rac的不完全恢复

    模拟rac的不完全恢复,虽然小鱼对常规的完全和不完全恢复已经轻车熟路了,还是记录一个不完全恢复完整过程记录下来. 1 首先小鱼做了一个完全备份. RMAN> backup database in ...

  4. CSS属性操作二

    9.float属性 基本浮动规则 先来了解一下block元素和inline元素在文档流中的排列方式. block元素通常被现实为独立的一块,独占一行,多个block元素会各自新起一行,默认block元 ...

  5. .NET Core 3.0之深入源码理解Configuration(一)

    Configuration总体介绍 微软在.NET Core里设计出了全新的配置体系,并以非常灵活.可扩展的方式实现.从其源码来看,其运行机制大致是,根据其Source,创建一个Builder实例,并 ...

  6. Palindrome--poj 1159(最长公共子字符串+滚动数字)

    http://poj.org/problem?id=1159 题目大意:  给你一个n  代表n个字符   第二行给你一个字符串  求使这个字符串变成回文字符串 最少需要添加多少个字符 分析:   原 ...

  7. [转]linux下tar.gz、tar、bz2、zip等解压缩、压缩命令小结

    原文:http://www.jb51.net/LINUXjishu/43356.html ------------------------------------------------------- ...

  8. Java编程50题

    [程序1]    题目:古典问题:有一对兔子.从出生后第3个月起每一个月都生一对兔子,小兔子长到第三个月后每一个月又生一对兔子,假如兔子都不死.问每一个月的兔子总数为多少?    //这是一个菲波拉契 ...

  9. springMVC4(16)拦截器解析与登陆拦截模拟

    在SpringMVC中,我们会常常使用到拦截器,尽管SpringAOP也能帮我们实现强大的拦截器功能,但在Web资源供给上.却没有SpringMVC来得方便快捷. 使用SpringMVC拦截器的核心应 ...

  10. android JNI 资料大全

    AndroidJNI 通过C++调用JAVA 1. JNIEnv对象    对于本地函数    JNIEXPORT void JNICALL Java_video1_TestNative_sayHel ...