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 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.
Sample Input
- 6 18
5 6 1 10 12 2
- 16
0 1 2 3 5 6 7 8 10 11 12 13 15 16 17 18
- 3 50
25 25 50
- 3
0 25 50
题意:
给你n个数和k,问n个数所有能构成k的子集合中所有的可能的和是多少?
分析:
dp[i][j]表示当前和是i能否构成j,如果dp[i][j]是可以构成的话,
那由于是子集合的关系dp[i+m][j]和dp[i+m][j+m]也可以构成。
dp: dp[i][j]=dp[i+m][j]+dp[i+m][j+m];
- #include <iostream>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- using namespace std;
- int dp[][];
- int a[];
- int main()
- {
- int n,k,m;
- scanf("%d%d",&n,&k);
- memset(dp,,sizeof(dp));
- dp[][]=;
- for(int i=;i<n;i++)
- {
- scanf("%d",&m);
- for(int a=k;a>=m;a--)
- {
- for(int b=;b+m<=k;b++)
- {
- if(dp[a-m][b])
- {
- dp[a][b]=;
- dp[a][b+m]=;
- }
- }
- }
- }
- int len=;
- for(int b=;b<=k;b++)
- if(dp[k][b])
- {
- a[len]=b;
- len++;
- }
- sort(a,a+len);
- printf("%d\n",len);
- for(int i=;i<len-;i++)
- printf("%d ",a[i]);
- printf("%d\n",a[len-]);
- return ;
- }
codeforces 360 E - The Values You Can Make的更多相关文章
- [codeforces 360]A. Levko and Array Recovery
[codeforces 360]A. Levko and Array Recovery 试题描述 Levko loves array a1, a2, ... , an, consisting of i ...
- codeforces 360 D - Remainders Game
D - Remainders Game Description Today Pari and Arya are playing a game called Remainders. Pari choos ...
- 套题 codeforces 360
A题:Opponents 直接模拟 #include <bits/stdc++.h> using namespace std; ]; int main() { int n,k; while ...
- codeforces 688E E. The Values You Can Make(dp)
题目链接: E. The Values You Can Make time limit per test 2 seconds memory limit per test 256 megabytes i ...
- 【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 ...
- codeforces 688 E. The Values You Can Make(01背包+思维)
题目链接:http://codeforces.com/contest/688/problem/E 题解:设dp[s1][s2]表示s1状态下出现s2是否合理.那么s1显然可以更具01背包来得到状态.首 ...
- codeforces 360 C
C - NP-Hard Problem Description Recently, Pari and Arya did some research about NP-Hard problems and ...
- codeforces 360 C - NP-Hard Problem
原题: Description Recently, Pari and Arya did some research about NP-Hard problems and they found the ...
- codeforces 360 B
B - Levko and Array 题目大意:给你你个长度为n的数列a,你最多改变k个值,max{ abs ( a[ i + 1] - a[ i ] ) } 的最小值为多少. 思路:这个题很难想到 ...
随机推荐
- ffmpeg-201612[01,08,10,17,21,27,30]-bin.7z
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 5 屏幕横向放大 20 像素 6 屏幕横向缩小 20 像素 S 下一帧 [ -2秒 ] +2 ...
- 浅析Java中的final关键字(转载)
自http://www.cnblogs.com/dolphin0520/p/3736238.html转载 一.final关键字的基本用法 在Java中,final关键字可以用来修饰类.方法和变量(包括 ...
- 关于C3翘边阴影的demo
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- Python列表去除重复元素
主要尝试了3种列表去除重复元素 #2.去除列表中的重复元素 #set方法 def removeDuplicates_set(nums): l2 = list(set(l1)) #用l1的顺序排序l2 ...
- JIRA FOR LINUX 安装过程
1.Download 官网下载地址:https://www.atlassian.com/software/jira/download,只看到window下的安装版本,这个时候需要点击? All JIR ...
- 基于ZooKeeper的分布式锁和队列
在分布式系统中,往往需要一些分布式同步原语来做一些协同工作,上一篇文章介绍了Zookeeper的基本原理,本文介绍下基于Zookeeper的Lock和Queue的实现,主要代码都来自Zookeeper ...
- 社区活动分享PPT:使用微软开源技术开发微服务
上周六在成都中生代技术社区线下活动进行了一个名为"微软爱开源-使用微软开源技术开发微服务"的技术分享. 也算是给很多不熟悉微软开源技术的朋友普及一下微软最近几年在开源方面所做的努力 ...
- 高性能的JavaScript--加载和执行
写在前面 JavaScript在浏览器中的性能,可认为是开发者所要面对的最重要的可用性的问题,此问题因JavaScript的阻塞特征而复杂,也就是说JavaScript运行时其他的事情不能被浏览器处理 ...
- 将 xunit.runner.dnx 的 xml 输出转换为 Nunit 格式
由于目前 DNX 缺乏 XSLT 的转换能力,因此只能使用变通方法.具体参考这个链接 主要内容复制过来是: From @eriklarko on July 14, 2015 7:38 As a wor ...
- 一个年轻的码农的一个C#项目
话不多少,今天要写一个小项目.我们写项目要做好准备.我们要做项目分析.要知道用户需求,然后在根据需求来规划自己的项目.我们要用自己所学,做最好的程序.尽自己所能完成项目需求.精简代码! 我们今天要写的 ...