题目链接:

题目

E. The Values You Can Make

time limit per test:2 seconds

memory limit per test:256 megabytes

问题描述

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 x using 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.

输入

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.

输出

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.

样例

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

题意

求原序列中子序和为k的子序列的子序列能构成的所有不同的子序和。

题解

由于数据<=500,所以可以n^3 dp。

设dp[i][j][k]表示前面i个数能构成的子序和为j的子序列能构造出自序和为k的数子序列。

然后类似01背包考虑选或不选的情况。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std; const int maxn = 1010;
int n, m; bool dp[2][maxn][maxn];
int arr[maxn], vis[maxn]; int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &arr[i]);
}
memset(dp, 0, sizeof(dp));
dp[0][0][0] = 1;
int cur = 0;
for (int i = 1; i <= n; i++) {
cur ^= 1;
memset(dp[cur], 0, sizeof(dp[cur]));
for (int j = 0; j <= m; j++) {
for (int k = 0; k <= m; k++) {
if (dp[cur^1][j][k]) {
dp[cur][j][k] = 1;
dp[cur][j + arr[i]][arr[i]] = 1;
dp[cur][j + arr[i]][k] = 1;
dp[cur][j + arr[i]][k + arr[i]] = 1;
}
}
}
}
vector<int> ans;
for (int k = 0; k <= m; k++) {
if (dp[cur][m][k]) ans.push_back(k);
}
printf("%d\n", ans.size());
for (int i = 0; i < ans.size() - 1; i++) printf("%d ", ans[i]);
printf("%d\n",ans[ans.size()-1]);
return 0;
}

总结

在数据范围允许情况下,考虑越高维的dp往往更能简化问题。

Codeforces Round #360 (Div. 2) E. The Values You Can Make 01背包的更多相关文章

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

  2. Codeforces Round #360 (Div. 2) D. Remainders Game 数学

    D. Remainders Game 题目连接: http://www.codeforces.com/contest/688/problem/D Description Today Pari and ...

  3. Codeforces Round #360 (Div. 2) D. Remainders Game 中国剩余定理

    题目链接: 题目 D. Remainders Game time limit per test 1 second memory limit per test 256 megabytes 问题描述 To ...

  4. Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 暴力并查集

    D. Dividing Kingdom II 题目连接: http://www.codeforces.com/contest/687/problem/D Description Long time a ...

  5. Codeforces Round #360 (Div. 2) C. NP-Hard Problem 水题

    C. NP-Hard Problem 题目连接: http://www.codeforces.com/contest/688/problem/C Description Recently, Pari ...

  6. Codeforces Round #360 (Div. 2) B. Lovely Palindromes 水题

    B. Lovely Palindromes 题目连接: http://www.codeforces.com/contest/688/problem/B Description Pari has a f ...

  7. Codeforces Round #360 (Div. 2) A. Opponents 水题

    A. Opponents 题目连接: http://www.codeforces.com/contest/688/problem/A Description Arya has n opponents ...

  8. Codeforces Round #360 (Div. 2) D. Remainders Game

    D. Remainders Game time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  9. Codeforces Round #360 (Div. 1)A (二分图&dfs染色)

    题目链接:http://codeforces.com/problemset/problem/687/A 题意:给出一个n个点m条边的图,分别将每条边连接的两个点放到两个集合中,输出两个集合中的点,若不 ...

随机推荐

  1. Android 图片开发内幕系列第一篇

    前言:本来我是做电视应用的,但是因为公司要出手机,人员紧张,所以就抽调我去支援一下,谁叫俺是雷锋呢!我做的一个功能就是处理手机中的应用ICON,处理无非就是美化一下,重新与底板进行合成和裁剪,用到了很 ...

  2. 集合框架学习之Guava Collection

    开源工具包: Guava : Google Collection Apache:Commons Collecton 1.1 Google Collections Guava:google的工程师利用传 ...

  3. HDU 1954 Subway tree systems (树的最小表示法)

    题意:用一个字符串表示树,0代表向下走,1代表往回走,求两棵树是否同构. 分析:同构的树经过最小表示会转化成两个相等的串. 方法:递归寻找每一棵子树,将根节点相同的子树的字符串按字典序排列,递归回去即 ...

  4. JavaScript 中undefined,null,NaN的区别

    1.类型分析: js中的数据类型有undefined,boolean,number,string,object等5种,前4种为原始类型,第5种为引用类型.var a1;var a2 = true;va ...

  5. JavaScript ES7 中使用 async/await 解决回调函数嵌套问题

    原文链接:http://aisk.me/using-async-await-to-avoid-callback-hell/ JavaScript 中最蛋疼的事情莫过于回调函数嵌套问题.以往在浏览器中, ...

  6. 对session和cookie的一些理解

    由于项目需要,最近用session容器比较多,传载的同时加上了自己的一些理解,不足之处还请大家补充和纠正.);                  response.addCookie(c1);   * ...

  7. 14.Apache配置

    环境:                                                      ↗  atl.example.com  (192.168.1.101) ↗ www.e ...

  8. 《Linux系统静态路由和火墙路由》

    本篇主要写的是关于静态路由表的添加,和如何让你不能上网的主机通过火墙路由表实现上网的功能. 静态路由表: 要是你的主机是2块网卡,并且做了网卡的绑定,依照我下面的方法是成功不了的,你可以去编辑: # ...

  9. 处理jquery版本之间冲突

    处理jquery版本之间冲突 前端开发们都知道jquery版本有好多,之间冲突很纠结.比如我刚来这公司的时候,后端的哥们用的是jQuery 1.3.2,我了个去,那哥们好久没更新了.我写的效果插件都是 ...

  10. 14种网页jQuery和css3特效插件代码演示

    1.网页table增删样式代码 演示和下载地址 2.jQuery左右滑动幻灯片插件 演示和下载地址 3.jQuery文字轮播焦点图 演示和下载地址 4.网页文字焦点图切换 演示和下载地址 5.jQue ...