• H


    H - Coins

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    Description

    Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch. 

    You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.

    Input

    The input contains several test cases. The first line of each test case contains two integers n(1 ≤ n ≤ 100),m(m ≤ 100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1 ≤ Ai ≤ 100000,1 ≤ Ci ≤ 1000). The last test case is followed by two zeros.

    Output

    For each test case output the answer on a single line.

    Sample Input

    3 10
    1 2 4 2 1 1
    2 5
    1 4 2 1
    0 0

    Sample Output

    8
    4


    //题意是 第一行两个整数 n 和 m (1 <= n <= 100)(m <= 100000) 然后第二行是 n 个硬币的价值,再是 n 个硬币的数量,
    问这些硬币能组成多少个小于等于 m 的价值。
    多重背包,有一点难吧,要用到二进制优化,直接当 0 1 背包处理要超时

    一维多重背包二进制优化的思考:先看这个问题,100=1+2+4+8+16+32+37,观察可以得出100以内任何一个数都可以由以上7个数选择组合得到,所以对物品数目就不是从0都100遍历,而是0,1,2,4,5,16,32,37遍历,时间大大优化。

    hud 340ms 另一个oj 187 ms


     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int a[105],c[105],b[105];
     5 bool dp[100005];
     6 
     7 int main()
     8 {
     9     int n,m,i,j;
    10     while (scanf("%d%d",&n,&m)&&n+m)
    11     {
    12         memset(dp,0,sizeof(dp));
    13         dp[0]=1;
    14         for (i=1;i<=n;i++)
    15             scanf("%d",&a[i]);
    16         for (i=1;i<=n;i++)
    17             scanf("%d",&c[i]);
    18         for (i=1;i<=n;i++)
    19         {
    20             if (a[i]*c[i]>m)
    21             {
    22                 for (j=a[i];j<=m;j++)
    23                     if (!dp[j])
    24                         dp[j]=dp[j-a[i]];
    25             }
    26             else
    27             {
    28                 if (c[i]==0)
    29                     continue;
    30 
    31                 int num=c[i];
    32                 int p=1,t=0;
    33                 while (p<num)//二进制优化成 01 背包问题
    34                 {
    35                     num-=p;
    36                     b[t++]=p*a[i];
    37                     p*=2;
    38                 }
    39                 b[t++]=num*a[i];
    40 
    41                 for (p=0;p<t;p++)
    42                     for (j=m;j>=b[p];j--)
    43                         if (!dp[j])
    44                             dp[j]=dp[j-b[p]];
    45             }
    46         }
    47         j=0;
    48         for (i=1;i<=m;i++)
    49             if (dp[i])
    50                 j++;
    51             printf("%d
    ",j);
    52     }
    53     return 0;
    54 }
    View Code
    
    

     

     
    
    

     



  • 相关阅读:
    阿里云 Serverless 异步任务处理系统在数据分析领域的应用
    主流定时任务解决方案全横评
    传统单节点网站的 Serverless 上云
    从函数计算到 Serverless 架构
    阿里云丁宇:以领先的云原生技术,激活应用构建新范式
    [Django] 创建python virtual environment 虚拟环境 并激活 | 安装配置Django
    在PPT里“露脸”
    Excel VBA中的“复制”和“粘帖”(Excel代码集团)
    提取学校名
    190010显示成
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/5747408.html
Copyright © 2020-2023  润新知