Coins

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10632    Accepted Submission(s): 4230

Problem 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        //n面值种类,m最大价格
1 2 4 2 1 1      // 3种面值,对应个数,求最多能凑出多少种小于等于m的钱数
2 5
1 4 2 1
0 0
 

Sample Output
8
4
一开始想到用母函数求解,数据量有点大,看了discuss然后用多重背包+二进制优化,二进制优化不能用pow函数,不然依然超时。。
 #include <stdio.h>
#define max(a,b) a>b?a:b
int main()
{
int n;
int a[],b[];
int f[];
int i,j,m,p,g;
int sum=;
freopen("in.txt","r",stdin);
int q;
while(scanf("%d%d",&n,&m)!=EOF)
{
sum=;
for(i=;i<=m;i++) f[i]=;
if(m==&&n==)
return ;
for(i=;i<=n;i++)
scanf("%d",&a[i]);
for(i=;i<=n;i++)
scanf("%d",&b[i]);
for(i=;i<=n;i++)
{
p=;
g=;
while(b[i]>g)
{
q=a[i]*g;
for(j=m;j>=q;j--)
{
f[j]=max(f[j],f[j-a[i]*g]+a[i]*g);
}
b[i]-=g;
g=g*;
}
q=a[i]*b[i];
for(j=m;j>=q;--j)
f[j]=max(f[j],f[j-a[i]*b[i]]+a[i]*b[i]);
}
for(i=;i<=m;i++)
{
if(f[i]==i)
sum++;
}
printf("%d\n",sum);
}
}

母函数超时代码:

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int val[+],num[+];
int c1[+],c2[+];
int m,n;
int solve()
{
int ans=,u;
int i,j,k,p=num[]*val[];
int t=min(val[]*num[],m);
for(i=;i<=t;i+=val[])
c1[i]=;
for(i=;i<=t;i++)
c2[i]=;
for(i=;i<=n;i++)
{
for(j=;j<=p;j++)
{
u=-;
for(k=;j+k<=m;k+=val[i])
{
u++;
if(u>num[i])
break;
c2[j+k]+=c1[j];
}
}
p=j+k;
for(j=;j<=p;j++)
{
c1[j]=c2[j];
c2[j]=;
}
}
for(i=;i<=m;i++)
{
if(c1[i]!=)
ans++;
}
return ans;
}
int main()
{
int i,j;
freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m))
{
if(m==&&n==)
return ;
for(i=;i<=n;i++)
scanf("%d",&val[i]);
for(i=;i<=n;i++)
scanf("%d",&num[i]);
cout<<solve()<<endl;
}
return ;
}

Coins(hdu 2844 多重背包)的更多相关文章

  1. hdu 2844 多重背包的转化问题 以及这个dp状态的确定

    在杭电上测试了下 这里的状态转移方程有两个.,. 现在有价值val[1],val[2],…val[n]的n种硬币, 它们的数量分别为num[i]个. 然后给你一个m, 问你区间[1,m]内的所有数目, ...

  2. hdu 2844 多重背包coins

    http://acm.hdu.edu.cn/showproblem.php?pid=2844 题意: 有n个硬币,知道其价值A1.....An.数量C1...Cn.问在1到m价值之间,最多能组成多少种 ...

  3. hdu 2844 多重背包+单调队列优化

    思路:把价值看做体积,而价值的大小还是其本身,那么只需判断1-m中的每个状态最大是否为自己,是就+1: #include<iostream> #include<algorithm&g ...

  4. hdu 2844 多重背包二进制优化

    //http://www.cnblogs.com/devil-91/archive/2012/05/16/2502710.html #include<stdio.h> #define N ...

  5. Coins HDU - 2844 POJ - 1742

    Coins HDU - 2844 POJ - 1742 多重背包可行性 当做一般多重背包,二进制优化 #include<cstdio> #include<cstring> in ...

  6. hdu 5445 多重背包

    Food Problem Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)To ...

  7. hdu 2844 混合背包【背包dp】

    http://acm.hdu.edu.cn/showproblem.php?pid=2844 题意:有n种纸币面额(a1,a2,...an),每种面额对应有(c1,c2,...cn)张.问这些钱能拼成 ...

  8. hdu 2191 多重背包 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活

    http://acm.hdu.edu.cn/showproblem.php?pid=2191 New~ 欢迎“热爱编程”的高考少年——报考杭州电子科技大学计算机学院关于2015年杭电ACM暑期集训队的 ...

  9. Big Event in HDU(HDU 1171 多重背包)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. 根据identifier从StoryBoard中获取对象,UIButton的图片文件位置

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  2. TypeScript开发Vue

    用TypeScript开发Vue——如何通过vue实例化对象访问实际ViewModel对象 目录 背景 解决方案 关于Vue中的计算属性类型 TypeScript的强制类型声明语法 强制类型声明的局限 ...

  3. DB_NAME、DB_UNIQUE_NAME、SERVICE_NAME和INSTANCE_NAME等的区别

    摘自:http://space.itpub.net/7922095/viewspace-715406 搭建DG时,突然想起oracle这些为数众多的name,以下是概念整理,仅代表个人观点 DB_NA ...

  4. cmake编译Debug和Release

    CMake 中有一个变量 CMAKE_BUILD_TYPE ,可以的取值是 Debug Release Rel WithDebInfo 和 MinSizeRel.当这个变量值为 Debug 的时候,C ...

  5. Tomcat启动load action异常

    近期将之前的项目移到另一个文件夹中(包的路径也更改了),启动Tomcat之后包错:无法加载action,看错误提示知道是路径错误,网上也有各种各样的解决方案,这里我的错误是因为项目移到了别的文件中,所 ...

  6. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅴ

    命题Q.对于一个含有N个元素的基于堆叠优先队列,插入元素操作只需要不超过(lgN + 1)次比较,删除最大元素的操作需要不超过2lgN次比较. 证明.由命题P可知,两种操作都需要在根节点和堆底之间移动 ...

  7. Pythoner | 你像从前一样的Python学习笔记

    Pythoner | 你像从前一样的Python学习笔记 Pythoner

  8. LVM(1)

    DM: DM: Device Mapper    逻辑设备        RAID, LVM2        DM: LVM2    快照    多路径

  9. android中的本地定时推送到通知栏

    一.使用系统定义的Notification 以下是使用示例代码: import android.app.Notification; import android.app.NotificationMan ...

  10. Linux 块设备驱动 (二)

    linux下Ramdisk驱动 1 什么是Ramdisk Ramdisk是一种模拟磁盘,其数据实际上是存储在RAM中,它使用一部分内存空间来模拟出一个磁盘设备,并以块设备的方式来组织和访问这片内存.对 ...