Coins
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.
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
解析:
大意:用给出的面值和相应的数目,能拼凑出1~m的多少种面值
思路:二进制压缩法能记录,转化成背包问题
# include<stdio.h> # include<string.h> # define max(a,b) a>b?a:b int val[101],num[101],dp[100001]; int main() { int nNum,nVal,temp; int i,j,k,leap; while(scanf("%d %d",&nNum,&nVal)!=EOF) { if(nNum==0&&nVal==0)break; leap=0; memset(dp,0,sizeof(dp)); for(i=1;i<=nNum;i++) scanf("%d",&val[i]); for(i=1;i<=nNum;i++) scanf("%d",&num[i]); for(i=1;i<=nNum;i++)//用二进制压缩法记录能拼凑到面值 { for(k=1;k<=num[i];k*=2) { num[i]-=k; for(j=nVal;j>=k*val[i];j--) { dp[j]=max(dp[j],dp[j-k*val[i]]+k*val[i]); } } temp=num[i]; if(temp!=0) { for(j=nVal;j>=temp*val[i];j--) { dp[j]=max(dp[j],dp[j-temp*val[i]]+temp*val[i]); } } } for(i=1;i<=nVal;i++) { if(dp[i]==i)leap++; } printf("%d\n",leap); } return 0; }
浙公网安备 33010602011771号