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;
    }
  • 相关阅读:
    (总结)Nginx/LVS/HAProxy负载均衡软件的优缺点详解
    一个自动安装LNMP的简洁Shell脚本
    (总结)Nginx配置文件nginx.conf中文详解
    Linux 的启动流程
    Linux运维文档之nginx
    nginx索引目录配置
    nginx实现图片防盗链(referer指令)
    nginx记录分析网站响应慢的请求(ngx_http_log_request_speed)
    C# 使用WinRar命令压缩和解压缩
    localStorage存值取值以及存取JSON,以及基于html5 localStorage的购物车
  • 原文地址:https://www.cnblogs.com/fenhong/p/5675332.html
Copyright © 2011-2022 走看看