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;
    }
  • 相关阅读:
    uva 1391(2-SAT)
    uva 1146(2-SAT+二分判断)
    uva 12167(强连通分支)
    uva 11324(强连通分支+DAG)
    Codeforces Round #227 (Div. 2) 解题报告
    uva 10319(2-SAT)
    uva 610(割边)
    uva 11504(强连通分支)
    测试:网页测试,入门面试题
    测试:安装包的测试
  • 原文地址:https://www.cnblogs.com/lsb666/p/5753204.html
Copyright © 2011-2022 走看看