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. 软件工程(五)UML

    UML 统一建模语言,又称标准建模语言.是用来对软件密集系统进行可视化建模的一种语言.包括UML语义和UML表示法两个元素. UMl图由事物和关系组成,事物:UML模型中最基本的构成元素,是具有代表性 ...

  2. FMDB数据库升级

    FMDBMigrationManager 是与FMDB结合使用的一个第三方,可以记录数据库版本号并对数据库进行数据库升级等操作.首先要集成FMDB和FMDBMigrationManager,建议使用c ...

  3. ppm\℃是什么意思/

    转自http://www.zybang.com/question/b158a106b4e39d8fdb2b93fd3777a00f.html 在基准电压的数据手册里,我们会找到一个描述基准性能的直流参 ...

  4. Protobuf一例

    Developer Guide  |  Protocol Buffers  |  Google Developershttps://developers.google.com/protocol-buf ...

  5. [转载]以及部分总结--Linux下创建单机ASM存储的Oracle实例的过程---感谢方总

    Linux下单机安装ASM流程总结 一.安装Linux ESXi上传iso镜像至存储目录 创建虚拟机,并且选择主机设备的ISO启动 选择完成时编辑虚拟机设置 配置镜像文件如下: 打开控制台: 并且选择 ...

  6. java面向对象的核心思想

    java面向对象的特征之一:封装 1.封装性的使用 package edu.tongji.classdemo; /* 封装性 1.封装的目的:保护某些属性和方法不被外部所见 2.封装的实现 为属性和方 ...

  7. 使用DataContext和ItemsSource将数据源绑定到ListView上的区别

    在最近的一个项目中,将DataView类型的数据源绑定到ListView控件时,发现当DataView的内容发生变化时,前台的ListView控件的内容并没有发生改变,在这里我先贴出前台要绑定数据源的 ...

  8. mysql学习笔记四 —— AB复制

    要点:ab复制 mysql集群架构流程: ABB(主从复制)-->MHA(实现mysql高可用.读写分离.脚本控制vip飘逸)-->haproxy(对slave集群实现分发,负载均衡)-- ...

  9. 跪求delphi 中的bitbutton 中的bmp图标

    C:\Program Files (x86)\Embarcadero\RAD Studio\11.0\Images\GlyFX

  10. BZOJ2588Count on a tree——LCA+主席树

    题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...