Coins
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 32309   Accepted: 10986

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 题意:给出3种硬币的面额和数量,能拼成不大于m的多少种;
多重背包可解,因为只要求行或不行就可以了,所以就两种状态在01和完全背包的时候没必要求可行解,只要确定行或不行就ok了,所以直接与dp[j - a[i]] 或运算,
注意的是,位运算真的好快,把dp设成int,用关系运算||,是超时的,改成位运算的|直接3000ms卡过;
改成bool型直接2204ms;
 #include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAX = + ;
bool dp[MAX];
int a[ + ],c[ + ];
int n,m;
void ZeroOnePage(int cost)
{
for(int i = m; i >= cost; i--)
{
dp[i] |= dp[i - cost];
}
}
void CompletePage(int cost, int mount)
{
for(int i = cost; i <= m; i++)
dp[i] |= dp[i - cost];
}
void MultiplePage(int cost, int mount)
{
if(cost * mount >= m)
{
CompletePage(cost, mount);
return ;
}
int k = ;
while(k < mount)
{
ZeroOnePage(k * cost);
mount -= k;
k <<= ;
}
if(mount > )
ZeroOnePage(mount * cost);
return ;
}
int main()
{
while(scanf("%d%d", &n, &m) != EOF)
{
if(n == && m == )
break;
for(int i = ; i <= n; i++)
scanf("%d", &a[i]);
for(int i = ; i <= n; i++)
scanf("%d", &c[i]);
memset(dp, , sizeof(dp));
dp[] = ;
for(int i = ; i <= n; i++)
if(c[i])
MultiplePage(a[i], c[i]);
int sum = ;
for(int i = ; i <= m; i++)
if(dp[i])
sum++;
printf("%d\n",sum);
} return ;
}

多重背包好理解

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAX = + ;
bool dp[MAX];
int a[ + ],c[ + ];
int n,m;
void ZeroOnePage(int cost)
{
for(int i = m; i >= cost; i--)
{
dp[i] |= dp[i - cost];
}
}
void CompletePage(int cost, int mount)
{
for(int i = cost; i <= m; i++)
dp[i] |= dp[i - cost];
}
void MultiplePage(int cost, int mount)
{
if(cost * mount >= m)
{
CompletePage(cost, mount);
return ;
}
int k = ;
while(k < mount)
{
ZeroOnePage(k * cost);
mount -= k;
k <<= ;
}
//这里是还剩下的mount
if(mount > )
ZeroOnePage(mount * cost);
return ;
}
int main()
{
while(scanf("%d%d", &n, &m) != EOF)
{
if(n == && m == )
break;
for(int i = ; i <= n; i++)
scanf("%d", &a[i]);
for(int i = ; i <= n; i++)
scanf("%d", &c[i]);
memset(dp, , sizeof(dp));
dp[] = ;
for(int i = ; i <= n; i++)
if(c[i])
MultiplePage(a[i], c[i]);
int sum = ;
for(int i = ; i <= m; i++)
if(dp[i])
sum++;
printf("%d\n",sum);
} return ;
} 多重背包好理解
这种解法看不懂
 #include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAX = + ;
int dp[MAX],used[MAX],a[ + ],c[ + ];
int n,m;
int main()
{
while(scanf("%d%d", &n, &m) != EOF)
{
if(n == && m == )
break;
for(int i = ; i <= n; i++)
scanf("%d", &a[i]);
for(int i = ; i <= n; i++)
scanf("%d", &c[i]);
memset(dp, , sizeof(dp));
dp[] = ;
int sum = ;
for(int i = ; i <= n; i++)
{
memset(used, , sizeof(used));
for(int j = a[i]; j <= m; j++)
{
if(dp[j] == && dp[j - a[i]] && used[j - a[i]] < c[i])
{
sum++;
dp[j] = ;
used[j] = used[j - a[i]] + ;
}
}
}
printf("%d\n",sum);
} return ;
}

POJ1742Coins(多重背包)的更多相关文章

  1. 洛谷P1782 旅行商的背包[多重背包]

    题目描述 小S坚信任何问题都可以在多项式时间内解决,于是他准备亲自去当一回旅行商.在出发之前,他购进了一些物品.这些物品共有n种,第i种体积为Vi,价值为Wi,共有Di件.他的背包体积是C.怎样装才能 ...

  2. HDU 2082 找单词 (多重背包)

    题意:假设有x1个字母A, x2个字母B,..... x26个字母Z,同时假设字母A的价值为1,字母B的价值为2,..... 字母Z的价值为26.那么,对于给定的字母,可以找到多少价值<=50的 ...

  3. Poj 1276 Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26172   Accepted: 9238 Des ...

  4. poj 1276 Cash Machine(多重背包)

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33444   Accepted: 12106 De ...

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

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

  6. (多重背包+记录路径)Charlie's Change (poj 1787)

    http://poj.org/problem?id=1787   描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie dri ...

  7. 单调队列优化DP,多重背包

    单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...

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

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

  9. POJ1276Cash Machine[多重背包可行性]

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32971   Accepted: 11950 De ...

随机推荐

  1. linux内核分析第六周学习笔记

    LINUX内核分析第六周学习总结 标签(空格分隔): 20135328陈都 陈都 原创作品转载请注明出处 <Linux内核分析>MOOC课程 http://mooc.study.163.c ...

  2. Flask-论坛开发-4-知识点补充

    对Flask感兴趣的,可以看下这个视频教程:http://study.163.com/course/courseLearn.htm?courseId=1004091002 1. WTForms 表单使 ...

  3. RBAC权限管理及使用原生PHP实现

    关于RBAC的原理讲解在网上可以找到很多,推荐:编程浪子的RBAC讲解,本篇博客就不再累述RBAC的原理到底是什么样的. 传统的权限控制有ACL和RBAC方式,ACL的耦合度很高,扩展性不佳,RBAC ...

  4. Vert.x简介

    https://vertx.io/ https://vertx.io/download/ https://baike.baidu.com/item/Vert.x 近年来,移动网络.社交网络和电商的兴起 ...

  5. Linux 忘记root密码

    1 将系统重启,读秒的时候按下任意键就会出现如下图菜单界面 2 进入上图菜单界面之后,按e键就可以进入grub的编辑模式 3 选择第二行 kernel开头,再按 e 键进入该行的编辑界面中,然后在出现 ...

  6. Docker(十六)-Docker的daemon.json的作用

    docker安装后默认没有daemon.json这个配置文件,需要进行手动创建.配置文件的默认路径:/etc/docker/daemon.json 一般情况,配置文件 daemon.json中配置的项 ...

  7. 关于supervisor 的使用以及配置

    首先我个人认为,用python实现的supervisor使用了守护进程这个概念去实现一个包裹进程的概念. 他可以帮助你的进程完成失效重启,日志记录,确保在线,关机自启动等一系列的功能. 当使用supe ...

  8. sort和uniq的应用实例

    sort 排序 uniq 1.语法:sort [option]... [file]... 2.选项:-k key,关键子,指定以那个列来排序.如果不指定,默认将正行作为关键字排序-n 对数值排序.默认 ...

  9. 将关系型数据库抽取成redis的思路

    思路是 先把id抽取出来形成一个·list表示数量 然后再把表变成键值对形式把id当做成键

  10. c++ 为自定义类添加stl遍历器风格的遍历方式

    为仿照stl的遍历风格,实现对自定义类型的遍历. 1. 需要遍历的基础结构: struct ConnectionPtr { int id_; int port_; string addr_; //st ...