题目传送门

Coins

Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 41707   Accepted: 14125

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.

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


  分析:很显然的多重背包,由数据范围可知需要进行拆分优化或者用单调队列优化。这里蒟蒻还不会单调队列优化,所以用的是二进制拆分。

  Code:

//It is made by HolseLee on 17th May 2018
//POJ 1742
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<algorithm>
#define Fi(i,a,b) for(int i=a;i<=b;i++)
#define Fx(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=1e5+;bool dp[N];
int n,m,cnt,ans,k[N],a[],c[];
int main()
{
ios::sync_with_stdio(false);
while(cin>>n>>m){
if(!n||!m)break;
memset(dp,false,sizeof(dp));
dp[]=true;cnt=;ans=;
Fi(i,,n)cin>>a[i];Fi(i,,n)cin>>c[i];
Fi(i,,n){
for(int j=;j<=c[i];j<<=){
k[++cnt]=a[i]*j;c[i]-=j;}
if(c[i])k[++cnt]=a[i]*c[i];}
Fi(i,,cnt)Fx(j,m,k[i]){
dp[j]|=dp[j-k[i]];}
Fi(i,,m)if(dp[i])ans++;
cout<<ans<<"\n";}
return ;
}

最新文章

  1. 高级java必会系列一:多线程的简单使用
  2. CPU的内部架构和工作原理
  3. Spring依赖注入(IOC)那些事
  4. 边工作边刷题:70天一遍leetcode: day 88-5
  5. AngularJs $http.post 数据后台获取不到数据问题 的解决过程
  6. Java 类成员的初始化顺序
  7. OC - 22.隐式动画
  8. LeetCode OJ 104. Maximum Depth of Binary Tree
  9. python基础5 while循环
  10. 封装一个通用的正则,不再为test和replace所烦恼,eval很棒~
  11. struts校验
  12. 《算法》第四章部分程序 part 15
  13. SpringCloud报错:Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
  14. maven 不同环境打包命令
  15. QQ通讯录VS360通讯录对新建信息界面中草稿的处理
  16. 【cocos2d-x 3.0-Mac配置篇】
  17. protobuf--数据序列化及反序列化
  18. POJ2186:Popular Cows(tarjan+缩点)
  19. linux给一个文件夹开启权限
  20. 8.Python编写登录接口

热门文章

  1. mysql binlog日志手动清除
  2. Linux powercli 以及connect-viserver 连接问题
  3. bzoj 2730: [HNOI2012]矿场搭建——tarjan求点双
  4. 【CodeForces】582 C. Superior Periodic Subarrays
  5. 15、简述MySQL的执行计划?
  6. CSS 竖线颜色渐变
  7. bzoj 1030 ac自动机
  8. linux驱动基础系列--Linux mmc sd sdio驱动分析
  9. Linux 入门记录:十六、Linux 多命令协作:管道及重定向
  10. 81.Search in Rotated Sorted Array II---二分变形