题目链接:

题目

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往往更能简化问题。

最新文章

  1. Android Weekly Notes Issue #227
  2. GreenDao2.2升级GreenDao3.0的适配之路
  3. selenium2.0关于python的常用函数(一)
  4. Visual Studio Code 使用 Typings 实现智能提示功能
  5. 关于NS2安装的若干问题
  6. 轻松了解Spring中的控制反转和依赖注入(二)
  7. C#语法糖之Cookies操作类 asp.net
  8. Git学习记录
  9. [js]事件综合 整理
  10. Single Number II ——位操作
  11. AngularJS - Watch 监听对象
  12. Frank自动化测试
  13. PHP 读json文件并转php配置文件
  14. android 中对apache httpclient及httpurlconnection的选择
  15. [Cocos2d-x]布局与定位
  16. CoreCLR文档翻译 - GC的设计
  17. ZOJ Problem - 2588 Burning Bridges tarjan算法求割边
  18. Java源码学习 -- java.lang.StringBuilder,java.lang.StringBuffer,java.lang.AbstractStringBuilder
  19. 十个推荐使用的 Laravel 的辅助函数
  20. VS2017 + QT5 + C++开发环境搭建和计算器Demo测试

热门文章

  1. 原生js学习笔记2
  2. DataGridView 操作
  3. Cocos2d-x 3.0标签类Label
  4. 一次MVVM+ReactiveCocoa实践
  5. 三款精美的html5及css3的源码插件
  6. JS函数式编程【译】2.2 与函数共舞
  7. Windows 命令大全
  8. C++ Strings(字符串)
  9. Linux 输入子系统
  10. python——类