POJ Coins

来源:互联网 发布:用php写一个登录页面 编辑:程序博客网 时间:2024/06/05 15:20

Description

People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box 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.

题目大意

有n种硬币,这n种硬币的价值为a[i],第i种硬币的个数为c[i]个,问有多少种方案,使这些硬币加起来和的值小于等于m。
有多组测试数据。
第一行为n,m,第二行2*n个数表示a[1]~a[n],c[1]~c[n]。
输入以n=0,m=0结束。

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

题解

记忆化搜索,也可看成背包问题。
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>using namespace std;int n,m,a[102],c[102],ans;void dp(){int pd[100002],usd[100002];memset(pd,0,sizeof(pd));pd[0]=1;ans=0;for(int i=1;i<=n;i++)   {memset(usd,0,sizeof(usd));    for(int j=a[i];j<=m;j++)       {if(!pd[j]&&pd[j-a[i]]&&usd[j-a[i]]+1<=c[i])           {pd[j]=1;    usd[j]=usd[j-a[i]]+1;    ans++;       }       }   }}int main(){while(scanf("%d%d",&n,&m)&&n&&m)   {for(int i=1;i<=n;i++) scanf("%d",&a[i]);    for(int i=1;i<=n;i++) scanf("%d",&c[i]);    dp(); printf("%d\n",ans);   }return 0;}

0 0