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 ] ) } 的最小值为多少. 思路:这个题很难想到 ...
随机推荐
- 【ASP.NET】复制单个文件同时到多个目录
有时候,当我们更新了一个dll文件后,需要将该dll文件复制到到不同的文件夹中,手动操作会很麻烦,因此可以考虑利用程序实现. 利用powershell批量复制 示例代码如下: $source=&quo ...
- mysql character set exception
问题: 插入数据时,报了这样一个错误:“_mysql_exceptions.Warning: Incorrect string value: ‘\xE6\xB5\x81\xE8\xA1\x8C…’ f ...
- sql server 2008 R2配置管理
安装vs2013后,sql server 2008R2配置管理提示“远程过程调用失败” 这是因为vs2013自带的Microsoft SQL Server 2012Local DB与之冲突. 通过升级 ...
- 手机开发中的AP与BP的概念
转自:http://blog.csdn.net/macong01/article/details/15504611 手机的AP和BP: AP:ApplicationProcessor,即应用芯片 BP ...
- 名词含义阅读 todolist
1.node webkit 2.C#设计模式 3.算法导论 4.SQLSERVER RowNum() 5.图片文字识别 6.tuple 7.yield 8.Web语义化 (多用 p ul ol li ...
- 《Python数据分析》环境搭建之安装Jupyter工具(一)
(免责声明:本文档是针对Python有经验的用户,如果您对Python了解很少,或者从未使用,建议官方教程用Anaconda安装) 前期准备:Python环境 虽然Jupyter可以运行多种编程语言, ...
- Win10 UI入门 RenderTransform属性分析之Translate 平移变形
对齐方式是中心底部对齐: HorizontalAlignment="Center" VerticalAlignment="Bottom" 以底部边为起始线,向上 ...
- 开源Unity小插件CheatConsole
我们在开发游戏的过程中,通常都需要一些快捷的方式来进行一些非常规的测试,这些功能一般被称作控制台或者GM指令,比如虚幻竞技场中,可以使用~键呼出控制台,输入一些指令即可进行快捷设置,比如设置分辨率,全 ...
- Java集合框架练习-计算表达式的值
最近在看<算法>这本书,正好看到一个计算表达式的问题,于是就打算写一下,也正好熟悉一下Java集合框架的使用,大致测试了一下,没啥问题. import java.util.*; /* * ...
- 一些linux命令
1. more 慢慢查看文件2. mkdir -p 递归的创建目录3. tree 4. ls -lh 人性化显示