zoukankan      html  css  js  c++  java
  • codeforces 360 E

       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

    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
    Input
    3 50
    25 25 50
    Output
    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[505][505];
    int a[505];
    int main()
    {
        int n,k,m;
        scanf("%d%d",&n,&k);
        memset(dp,0,sizeof(dp));
        dp[0][0]=1;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&m);
            for(int a=k;a>=m;a--)
            {
                    for(int b=0;b+m<=k;b++)
                     {
                         if(dp[a-m][b])
                         {
                             dp[a][b]=1;
                           dp[a][b+m]=1;
                         }
                     }
            }
    
        }
        int len=0;
       for(int b=0;b<=k;b++)
         if(dp[k][b])
       {
            a[len]=b;
            len++;
            }
    
        sort(a,a+len);
        printf("%d
    ",len);
        for(int i=0;i<len-1;i++)
            printf("%d ",a[i]);
        printf("%d
    ",a[len-1]);
        return 0;
    }
  • 相关阅读:
    散点图增加趋势线
    asp.net自动刷新获取数据
    jQuery在asp.net中实现图片自动滚动
    tornado 学习笔记一:模板
    node.js 学习笔记二:创建属于自己的模块
    mysql 5.6 innodb memcache 操作学习一
    unicode,str
    node.js 学习笔记四:读取文件
    gevent 学习笔记一
    while 循环居然可以用else
  • 原文地址:https://www.cnblogs.com/fenhong/p/5675332.html
Copyright © 2011-2022 走看看