zoukankan      html  css  js  c++  java
  • Coins

    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
    #include<stdio.h>
    #include<string.h>
    int a[102],c[102],dp[100005];
    int max(int a,int b)
    {
        return a>b?a:b;
    }
    void CompletePack(int v,int w,int m) //完全背包
    {
         for(int j=v;j<=m;j++)
          dp[j]=max(dp[j],dp[j-v]+w);
    }
    void ZeroOnePack(int v,int w,int m) //01背包
    {
          for(int j=m;j>=v;j--)
           dp[j]=max(dp[j],dp[j-v]+w);
    }
    void MultiPack(int v,int w,int m,int c) //多重背包
    {
        if(v*c>=m) //体积乘以数量大于总体积,说明不能完全装完,相当于有无穷件,用完全背包
         CompletePack(v,w,m);
        else  //可以装完,用01背包
        {
            int k=1;
            while(k<c) //二进制优化
            {
                ZeroOnePack(k*v,k*w,m);
                 c-=k;
                 k*=2;
            }
            ZeroOnePack(c*v,c*w,m);
        }
    }
    int main()
    {
        int n,i,j,m,k;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            if(n==0&&m==0) break;
            for(i=0;i<n;i++)
             scanf("%d",&a[i]);  //a[i]既是物体的体积,又是物体的价值
            for(i=0;i<n;i++)
             scanf("%d",&c[i]); //c[i]是物体的数量
            memset(dp,0,sizeof(dp));
            for(i=0;i<n;i++)
            {
                MultiPack(a[i],a[i],m,c[i]);
            }
            int count=0; //计数
            for(i=1;i<=m;i++)
             if(dp[i]==i)  //可以组合且不用找钱
              count++;
            printf("%d
    ",count);
        }
        return 0;
    }
  • 相关阅读:
    window 7系统环境同时安装window xp系统,形成双系统
    工作感悟
    数据湖框架选型很纠结?一文了解Apache Hudi核心优势
    mysql数据库设计-规则
    maven中多个子模块的构建顺序
    EXTJS3.0 表单元素TextField datefield 设置只读并改背景颜色为灰色
    MySQL5.7的账号回收权限
    哲学王子-复旦博导王德峰教授:阅读与哲学思考
    abseil 的 cmake 方式编译
    [javascript] ie下audio不支持一些媒体类型
  • 原文地址:https://www.cnblogs.com/lsb666/p/5753204.html
Copyright © 2011-2022 走看看