博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
687C: The values you can make
阅读量:6676 次
发布时间:2019-06-25

本文共 2909 字,大约阅读时间需要 9 分钟。

Codeforces Round #360 Editorial [+ Challenges!]

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 isci. The price of the chocolate isk, so Pari will take a subset of her coins with sum equal tok 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 valuesx, such that Arya will be able to make x using some subset of coins with the sumk.

Formally, Pari wants to know the values x such that there exists a subset of coins with the sumk such that some subset of this subset has the sumx, i.e. there is exists some way to pay for the chocolate, such that Arya will be able to make the sumx using these coins.

Input

The first line contains two integers n andk (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 printq 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 185 6 1 10 12 2
Output
160 1 2 3 5 6 7 8 10 11 12 13 15 16 17 18
Input
3 5025 25 50
Output
30 25 50

Hint

Use dynamic programming.

Solution

Let dpi, j, k be true if and only if there exists a subset of the firsti coins with sumj, that has a subset with sumk. 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 makej, 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 todpi - 1, ji, kOR dpi - 1, j - ci, kOR dpi - 1, j - ci, k - ci.

The complexity is O(nk2).

C++ code

//	   - -- --- ---- -----be name khoda----- ---- --- -- -		\\#include 
using namespace std;inline int in() { int x; scanf("%d", &x); return x; }const int N = 505;bool dp[2][N][N];int main(){ int n, k; cin >> n >> k; dp[0][0][0] = 1; for(int i = 1; i <= n; i++) { int now = i % 2; int last = 1 - now; int x = in(); for(int j = 0; j <= k; j++) for(int y = 0; y <= j; y++) { dp[now][j][y] = dp[last][j][y]; if(j >= x) { dp[now][j][y] |= dp[last][j - x][y]; if(y >= x) dp[now][j][y] |= dp[last][j - x][y - x]; } } } vector
res; for(int i = 0; i <= k; i++) if(dp[n % 2][k][i]) res.push_back(i); cout << res.size() << endl; for(int x : res) cout << x << " "; cout << endl;}
原文链接:

转载于:https://www.cnblogs.com/tigerisland/p/7564666.html

你可能感兴趣的文章
新来的NB-IoT为什么这么NB?
查看>>
DoCoMo跳过HSPA+ 力争全球首家推出LTE商用服务
查看>>
Mozilla火狐发布最新计划:用户隐私和安全为发展核心
查看>>
金融行业解决方案
查看>>
[原创]分析解决lvs fullnat模式下后端服务器获取真实IP地址异常问题
查看>>
《Arduino开发实战指南:LabVIEW卷》——3.3 LabVIEW的常用工具及调试工具
查看>>
中国正式启动5G技术研发试验:力争2020年商用
查看>>
《精解 Windows 10》——第2章 Modern 2.0界面体验 2.1Modern 2.0界面
查看>>
《Android深度探索(卷1):HAL与驱动开发》——1.5节如何学习Linux驱动开发
查看>>
《Photoshop七大核心技术》—第2课Photoshop七大核心技术
查看>>
《动手玩转Arduino》——10.5 展望
查看>>
大数据开发套件-数据集成-云mongo跨区域如何同步到Maxcompute
查看>>
人工智能第三次黄金时代,藏在全球数亿摄像头里?
查看>>
RegularJS 0.2.12 发布,JavaScript MVC 框架
查看>>
根据《网络安全法》要求全面提升企业安全能力
查看>>
最新 Win10 测试版提供 Ubuntu 16.04 镜像
查看>>
云栖大会|新零售时代供应链的重“构”已经开始
查看>>
《Kali Linux渗透测试的艺术》—第2章2.6节道德准则
查看>>
《计算机科学导论》一1.9 练习
查看>>
《iOS应用软件设计之道》—— 第3章 熟悉iOS
查看>>