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[],c[],dp[];
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=;
while(k<c) //二进制优化
{
ZeroOnePack(k*v,k*w,m);
c-=k;
k*=;
}
ZeroOnePack(c*v,c*w,m);
}
}
int main()
{
int n,i,j,m,k;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==&&m==) break;
for(i=;i<n;i++)
scanf("%d",&a[i]); //a[i]既是物体的体积,又是物体的价值
for(i=;i<n;i++)
scanf("%d",&c[i]); //c[i]是物体的数量
memset(dp,,sizeof(dp));
for(i=;i<n;i++)
{
MultiPack(a[i],a[i],m,c[i]);
}
int count=; //计数
for(i=;i<=m;i++)
if(dp[i]==i) //可以组合且不用找钱
count++;
printf("%d\n",count);
}
return ;
}

Coins的更多相关文章

  1. [LeetCode] Arranging Coins 排列硬币

    You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...

  2. ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力

     Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Fo ...

  3. Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)

    传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...

  4. csuoj 1119: Collecting Coins

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1119 1119: Collecting Coins Time Limit: 3 Sec  Memo ...

  5. hdu 1398 Square Coins (母函数)

    Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  6. (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)

    http://poj.org/problem?id=3260   Description Farmer John has gone to town to buy some farm supplies. ...

  7. POJ3260The Fewest Coins[背包]

    The Fewest Coins Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6299   Accepted: 1922 ...

  8. POJ1742 Coins[多重背包可行性]

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 34814   Accepted: 11828 Descripti ...

  9. LeetCode 441 Arranging Coins

    Problem: You have a total of n coins that you want to form in a staircase shape, where every k-th ro ...

随机推荐

  1. 【TYVJ1864】[Poetize I]守卫者的挑战 概率与期望

    [TYVJ1864][Poetize I]守卫者的挑战 描述 打开了黑魔法师Vani的大门,队员们在迷宫般的路上漫无目的地搜寻着关押applepi的监狱的所在地.突然,眼前一道亮光闪过."我 ...

  2. Spring Autowired 注入失败总是Null

    报错:NullPointerException 分析:错误原因是注入失败? <context:annotation-config/> <context:component-scan ...

  3. ACM: FZU 2107 Hua Rong Dao - DFS - 暴力

    FZU 2107 Hua Rong Dao Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  4. Easyui的Dialog的toolbar的自定义添加

    最近一直在写快速定制Web表格,基于Easyui,整个过程使用了大量的Easyui的dialog,每个dialog的代码大部分都雷同,感觉代码出现了很大程度的重复,然后想写一个通用的dialog设置函 ...

  5. linux设备驱动

    http://blog.csdn.net/bob_fly1984/article/details/8820670 struct ov5640_data {    struct ov5640_platf ...

  6. 文件编码、charset、sublime编辑器支持GBK等问题

    问题一:如何让sublime3支持GBK 首先打开package control ,然后键入install package进入,搜索ConvertToUTF8安装成功后 打开要查看的GBK文件,点击菜 ...

  7. jQuery的封装方式与JS中new的实现原理

    function jQuery() { return new jQuery.fn.init(); } jQuery.fn = jQuery.prototype = { init: function() ...

  8. *HDU3172 并查集

    Virtual Friends Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  9. javascript学习之通过class获取元素

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  10. iOS开发之CocoaLumberjack

    Cocoa LumberJack是一个功能强大的NSlog,是通用的Cocoa日志框架之一.它可以提供更高级的log功能,比如记录log至文件或网络,并可根据log的级别(info.debug.war ...