题目链接:

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. ul标签中,li标签的移除、属性值获取

  2. python017 Python3 模块

    Python3 模块在前面的几个章节中我们脚本上是用 python 解释器来编程,如果你从 Python 解释器退出再进入,那么你定义的所有的方法和变量就都消失了.为此 Python 提供了一个办法, ...

  3. Tao Tao要吃鸡(01背包)

    题目描述 Taotao的电脑带不动绝地求生,所以taotao只能去玩pc版的荒野行动了,和绝地求生一样,游戏人物本身可以携带一定重量m的物品,装备背包之后可以多携带h(h为0代表没有装备背包)重量的东 ...

  4. VS Code 列编辑功能说明

    新版本v1.13.1或者附近的版本中的列编辑功能已经调整. 一.多光标插入功能 Alt+鼠标左键,添加多光标输入 二.自由多行选择 Alt键+鼠标左键拖动选择各行的部分内容 三.列选择 Shift+A ...

  5. apache + DSO -动态共享对象(DSO)

    http://www.jinbuguo.com/apache/menu22/dso.html

  6. Free Web Application Firewall相关资料

    http://www.freewaf.org/solution/#1 http://baike.soso.com/v60659982.htm

  7. 在jinja2的页面中使用javascript对页面元素进行删除

    以对用户注册信息的审核为例. 后端的代码为: #encoding=utf-8 class RegisterCheck(system.page): '''注册信息审核''' path = "/ ...

  8. Django的form,model自定制

    一.Form组件原理: django框架提供了一个form类,来处理web开发中的表单相关事项.众所周知,form最常做的是对用户输入的内容进行验证,为此django的forms类提供了全面的内容验证 ...

  9. Java并发编程,3分分钟深入分析volatile的实现原理

    volatile原理 volatile简介 Java内存模型告诉我们,各个线程会将共享变量从主内存中拷贝到工作内存,然后执行引擎会基于工作内存中的数据进行操作处理. 线程在工作内存进行操作后何时会写到 ...

  10. flask-admin的学习使用

    参考: 1.https://blog.igevin.info/wechats/wechat-flask-admin/ 2.http://flask-admin.readthedocs.io/en/la ...