Coins

来源:互联网 发布:linux shell usleep 编辑:程序博客网 时间:2024/05/19 17:50

Coins

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 866 Accepted Submission(s): 351 
Problem 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 101 2 4 2 1 12 51 4 2 10 0
 
Sample Output
84
给你一些钱的面值数和个数,给定一个m,问现有的钱有多少种方式表示
多重背包问题,每个物品数量不止一个,这个题中a即是体积又是价值,c是数量。
套用模板
#include<string.h>
#include<iostream>
#include<stdio.h>
using namespace std;
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(cin>>n>>m)    {        if(n==0&&m==0)break;        for(i=0;i<n;i++)         cin>>a[i];        for(i=0;i<n;i++)         cin>>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\n",count);    }    return 0;}

 
原创粉丝点击