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

很水........

Let dpi, j, k be true if and only if there exists a subset of the first i coins with sum j, that has a subset with sum k. There are 3 cases to handle:

  • The i-th coin is not used in the subsets.
  • The i-th coin is used in the subset to make j, but it's not used in the subset of this subset.
  • The i-th coin is used in both subsets.

So dpi, j, k is equal to dpi - 1, j, k OR dpi - 1, j - ci, k OR dpi - 1, j - ci, k - ci.

f[i][j][k]表示前i个coin能否凑成j价值再从凑成j价值的里面凑出k价值
f[0][0][0]=1
第一维可以滚掉
//
// main.cpp
// cf687c
//
// Created by Candy on 9/20/16.
// Copyright © 2016 Candy. All rights reserved.
// #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,v,c[N],cnt=;
int f[N][N];
void dp(){
f[][]=;
for(int i=;i<=n;i++){
for(int j=v;j>=;j--)
for(int k=v;k>=;k--)
if(j-c[i]>=){
f[j][k]|=f[j-c[i]][k];
if(k-c[i]>=) f[j][k]|=f[j-c[i]][k-c[i]];
}
}
}
int main(int argc, const char * argv[]) {
n=read();v=read();
for(int i=;i<=n;i++) c[i]=read();
dp();
for(int i=;i<=v;i++) if(f[v][i]) cnt++;
printf("%d\n",cnt);
for(int i=;i<=v;i++) if(f[v][i]) printf("%d ",i);
return ;
}

CF687C. The Values You Can Make[背包DP]的更多相关文章

  1. poj 2184 01背包变形【背包dp】

    POJ 2184 Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14657   Accepte ...

  2. 背包dp整理

    01背包 动态规划是一种高效的算法.在数学和计算机科学中,是一种将复杂问题的分成多个简单的小问题思想 ---- 分而治之.因此我们使用动态规划的时候,原问题必须是重叠的子问题.运用动态规划设计的算法比 ...

  3. hdu 5534 Partial Tree 背包DP

    Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  4. HDU 5501 The Highest Mark 背包dp

    The Highest Mark Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  5. Codeforces Codeforces Round #319 (Div. 2) B. Modulo Sum 背包dp

    B. Modulo Sum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/577/problem/ ...

  6. noj [1479] How many (01背包||DP||DFS)

    http://ac.nbutoj.com/Problem/view.xhtml?id=1479 [1479] How many 时间限制: 1000 ms 内存限制: 65535 K 问题描述 The ...

  7. HDU 1011 树形背包(DP) Starship Troopers

    题目链接:  HDU 1011 树形背包(DP) Starship Troopers 题意:  地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...

  8. BZOJ 1004: [HNOI2008]Cards( 置换群 + burnside引理 + 背包dp + 乘法逆元 )

    题意保证了是一个置换群. 根据burnside引理, 答案为Σc(f) / (M+1). c(f)表示置换f的不动点数, 而题目限制了颜色的数量, 所以还得满足题目, 用背包dp来计算.dp(x,i, ...

  9. G - Surf Gym - 100819S -逆向背包DP

    G - Surf Gym - 100819S 思路 :有点类似 逆向背包DP , 因为这些事件发生后是对后面的时间有影响. 所以,我们 进行逆向DP,具体 见代码实现. #include<bit ...

随机推荐

  1. go语言编辑器:liteide

    LiteIDE is a simple, open source, cross-platform Go IDE. LiteIDE是一款开源.跨平台的轻量级Go语言集成开发环境(IDE). 项目地址:h ...

  2. Autodesk 2013开发者日(DevDays)又要来了 -- 北京(2013年11月7日)和上海(2013年11月11日)

    各位, 一年一度的Autodesk 开发者日(Devdays)开始注册了,抓紧时间前排占座! 注册地址: www.autodesk.com.cn/adndevday2013 今年开发者日的主题:革命性 ...

  3. Android Animation学习(四) ApiDemos解析:多属性动画

    Android Animation学习(四) ApiDemos解析:多属性动画 如果想同时改变多个属性,根据前面所学的,比较显而易见的一种思路是构造多个对象Animator , ( Animator可 ...

  4. react native与现有的应用程序集成

    (1)通过cocopods 集成 ,以下内容 参考 http://wiki.jikexueyuan.com/project/react-native/integration-existing.html ...

  5. Android--ListView下拉刷新

    整理了下以前写的小项目,ListView的下拉刷新,虽然小但还是想纪念下..适合新手看,大神略过... 效果图:     代码:  实体类 package com.example.listviewre ...

  6. JavaSE 和 JavaEE 的关系

    1.javaSe看成是java基础   (Standard Edition(标准版) ) 2.J2EE看成是应用            (Enterprise Edition(企业版)) 3.java ...

  7. git 上的pull request 是什么意思?

    1.git 上有常见的pull request 功能 2.pull request 的含义 解释一:    有一个仓库,叫Repo A.你如果要往里贡献代码,首先要Fork这个Repo,于是在你的Gi ...

  8. android 最简单的自定义圆点view

    首先创建一个选择器,用来判断圆点状态,可以根本自己的需求改 <selector xmlns:android="http://schemas.android.com/apk/res/an ...

  9. 你真的了解UIButton、UILabel 吗?

    一:首先查看一下关于UIButton的定义 @class UIImage, UIFont, UIColor, UIImageView, UILabel; //设置UIButton的样式 typedef ...

  10. IOS整体项目层级构建

    在创建IOS项目时,若有一个比较明确的层级架构,将对于今后代码的维护或者功能的扩展很有帮助:本文将通过一个实例来展现我对于层级的一些观点:里面有一些零碎的知识点可能无法全部介绍,到时提供源代码进行下载 ...