POJ1742:Coins
浅谈\(DP\):https://www.cnblogs.com/AKMer/p/10437525.html
题目传送门:http://poj.org/problem?id=1742
多重背包,每个物品可以使用若干次的背包,我们只需要多枚举一次当前要使用多少次然后把这么多次结合在一起,当做\(01\)背包做即可。复杂度\(O(\sum cnt_im)\),\(cnt_i\)表示第\(i\)个物品的个数
二进制分解优化:
因为每个数必然可以被分解成若干个二进制数相加,所以我们就把\(cnt_i\)提前分解为若干个二进制数,把这么多个物品先打包成一个物品,做一遍\(01\)背包必然包含了选择任意个该物品的决策。
时间复杂度:\(O(\sum log(cnt_i)m)\)
空间复杂度:\(O(m)\)
代码如下:
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=1e5+5;
bool f[maxn];
int n,m,tot,ans;
int a[maxn],c[maxn],coin[maxn];
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
int main() {
while(1) {
n=read(),m=read(),tot=ans=0;
if(!n&&!m)break;
memset(f,0,sizeof(f)),f[0]=1;
for(int i=1;i<=n;i++)a[i]=read();
for(int i=1;i<=n;i++)c[i]=read();
for(int i=1;i<=n;i++) {
for(int j=1;j<=512;j*=2)
if(c[i]>j)coin[++tot]=a[i]*j,c[i]-=j;
if(c[i])coin[++tot]=c[i]*a[i];
}
for(int i=1;i<=tot;i++)
for(int j=m;j>=coin[i];j--)
f[j]|=f[j-coin[i]];
for(int i=1;i<=m;i++)ans+=f[i];
printf("%d\n",ans);
}
return 0;
}
POJ1742:Coins的更多相关文章
- HDU-2844:Coins(多重背包+二进制优化)
链接:HDU-2844:Coins 题意:给你n个种类的钱和对应的数量,同统计一下从1到m能够凑成的钱有多少个. 题解:C[i] = 1 + 2 + 4 + ··· + 2^k + a (0 < ...
- 【poj1742】 Coins
http://poj.org/problem?id=1742 (题目链接) 题意 给出n钟纸币,每种纸币面值为a[i],数量为c[i],问能够成多少数值小于等于m的数. Solution 先想到了容斥 ...
- lintcode :Coins in Line II 硬币排成线 II
题目 硬币排成线 II 有 n 个不同价值的硬币排成一条线.两个参赛者轮流从左边依次拿走 1 或 2 个硬币,直到没有硬币为止.计算两个人分别拿到的硬币总价值,价值高的人获胜. 请判定 第一个玩家 是 ...
- lintcode:Coins in a Line 硬币排成线
题目 硬币排成线 有 n 个硬币排成一条线.两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止.拿到最后一枚硬币的人获胜. 请判定 第一个玩家 是输还是赢? 样例 n = 1, 返回 ...
- POJ 3210 : Coins
参考:https://blog.csdn.net/u010885899/article/details/46636523 http://kqwd.blog.163.com/blog/static/41 ...
- 多重背包问题的两种O(M*N)解法
多重背包的题目很多,最著名的是poj1742楼教主的男人八题之一. poj1742:coins 有几种面值的钱币和每种的数量,问能够组成m以内的多少种钱数 这个题大家都归为多重背包问题,不过跟实际意义 ...
- [LintCode] Coins in a Line II 一条线上的硬币之二
There are n coins with different value in a line. Two players take turns to take one or two coins fr ...
- 2017 ICPC Asia Urumqi A.coins (概率DP + 期望)
题目链接:Coins Description Alice and Bob are playing a simple game. They line up a row of nn identical c ...
- LeetCode:322. 零钱兑换
链接:https://leetcode-cn.com/problems/coin-change/ 标签:动态规划.完全背包问题.广度优先搜索 题目 给定不同面额的硬币 coins 和一个总金额 amo ...
随机推荐
- 《Maven实战》第5章 坐标和依赖
5.1 Maven坐标——项目唯一标识 groupId(必须定义):定义Mavan项目隶属的实际项目,如SpringFramework,一个实际项目可包含多个Maven项目 artifactId(必须 ...
- Registering Components-->Autofac registration(include constructor injection)
https://autofaccn.readthedocs.io/en/latest/register/registration.html Registration Concepts (有4种方式来 ...
- 简易的解决方式linker command failed with exit code 1 (use -v to see invocation)
linker command failed with exit code 1 (use -v to see invocation) 遇到这个问题先不要慌,不用纠结是不是自己改动了什么代码导致的. 长话 ...
- Spring3: 在Bean定义中使用EL-表达式语言
5.4.1 xml风格的配置 SpEL支持在Bean定义时注入,默认使用“#{SpEL表达式}”表示,其中“#root”根对象默认可以认为是ApplicationContext,只有Applicat ...
- c#实现验证某个IP地址是否能ping通
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net ...
- Win7 SP1 下安装 VS2015 Update 3
首先Win7必须是SP1,然后要求安装IE11,安装IE11前需要安装6个先决补丁,成功安装IE11后建议安装3个可选补丁.然后导入3个根证书(COMODO RSA Certification Aut ...
- tomcat学习篇
要求: 为Apache HTTP Server服务器添加JSP网页支持. 能够访问Tomcat容器的Web管理界面,以便管理各种JSP.Servelet应用. u 知识提示 在各种企业级网站应用系统中 ...
- oracle数据库查看用户相关语句
1.查看所有用户: select * from dba_users; select * from all_users; select * from user_users; 2.查看用户 ...
- Qt DLL总结
(转自:http://qimo601.iteye.com/blog/1397936) QT动态链接库的调用方法,主要包括: 1.显式链接DLL,调用DLL的全局函数,采用Qt的QLibrary方法 2 ...
- SQL正则表达式
(转自:http://blog.csdn.net/xu1314/article/details/10174067) 当我们要进行一些简单的糊涂查询时用百分号(%),通配符(_)就可以了.其中%表达任意 ...