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 ] ) } 的最小值为多少. 思路:这个题很难想到 ...
随机推荐
- jquery插件封装成seajs模块
jquery直接在html中引入. jquery插件修改为: define(function (require, exports, moudles) { return function (jquery ...
- 什么时候用Model,什么时候用Entity?
在建立一个实体类的时候,究竟是用Model还是用Entity?比如MVC中,Model存了数据实体,但是他被称为Model,而在EF中,Entity也是存放数据实体,却被称作Entity,这两者有何区 ...
- 后台访问 JS解决跨域问题
今天看了看以前做的一个小项目(其实就是一个页面),分享一下当时解决跨域问题的: 背景:公司把项目部署在多台服务器上,防止一台服务器崩溃后,其他的可以继续访问,对应本公司来说,某台服务器出问题后,技术人 ...
- Oracle客户端工具出现“Cannot access NLS data files or invalid environment specified”错误的解决办法
Oracle客户端工具出现"Cannot access NLS data files or invalid environment specified"错误的解决办法 方法一:参考 ...
- java复杂枚举
枚举以红绿灯为例,红灯30s后为绿灯,绿灯45s后为黄灯,黄灯5s后为红灯.具体的枚举代码如下: public enum TrafficLamp{ RED(30){ public TrafficLam ...
- MongoDB 效率
写入: 插入100万条数据:用InsertMany,耗时16s左右. 读取: 读取300万条数据,耗时3600毫秒.
- js 字符串操作函数
concat() – 将两个或多个字符的文本组合起来,返回一个新的字符串. indexOf() – 返回字符串中一个子串第一处出现的索引.如果没有匹配项,返回 -1 . charAt() – 返回指定 ...
- 手把手教你玩转nginx负载均衡(五)----配置后端服务器组
引言 在前面几篇中,我们成功的搭建起了一台nginx服务器,所以我们要重复前面的步骤,把服务器的数量增加到3台以上,我这里已经建好了另外两台,分别是centos7-22,centos7-23,对应的i ...
- **SQL某一表中重复某一字段重复记录查询与处理
sql某一表中重复某一字段重复记录查询与处理 1.查询出重复记录 select 重复记录字段 form 数据表 group by houseno having count(重复记录字段)> ...
- PHP socket上传文件图片
最近了解了下下socket方面的东西,想做一个socket上传文件的例子. 在网上搜了搜代码执行后,图片数据传输了一半,图片的下半部分是灰色的.然后就自己仿着搜来的代码和php.net 中socket ...