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. UVa 232 Crossword Answers

     Crossword Answers  A crossword puzzle consists of a rectangular grid of black and white squares and ...

  2. HBase架构深度解析

    原文出处: DLevin(@雪地脚印_) 前记 公司内部使用的是MapR版本的Hadoop生态系统,因而从MapR的官网看到了这篇文文章:An In-Depth Look at the HBase A ...

  3. mongodb 基本语法

    成功启动MongoDB后,再打开一个命令行窗口输入mongo,就可以进行数据库的一些操作. 输入help可以看到基本操作命令: show dbs:显示数据库列表 show collections:显示 ...

  4. RMAN备份FORMAT格式中%的含义

    使用格式串先看例子:Configure channel 1 device type disk format 'd:/backup/orcl/backup_%U';在configure 命令中经常使用格 ...

  5. 启动Tomcat一闪而过解决

    打开apache-tomcat-6.0.32/bing/catalina.bat在首行添加: set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_07 就可以了.

  6. VS2010的调试参数/Zi /DEBUG

    /DEBUG只是是否要生成调试信息的开关.这个命令行选项在链接器页面那里 /Zi只是生成的调试信息的格式,这个格式是.pdb文件.当然还有好几种格式.这个命令行在编译页那里 如果上面的选项没有设置对, ...

  7. MLC固态硬盘,与入量是3000次P/E

    固态硬盘是什么,固态硬盘寿命有多长 SSD泛指使用闪存芯片组成的SSD固态硬盘,是使用FLASH闪存颗粒作为存储单元,不再使用传统的机械存储方法,使用模拟的方式虚拟出传统 硬盘存取方式和扇区等,也可以 ...

  8. linux会话命令screen详解

    Screen会话命令 我其实是把文件下载到 /home/zhang/temp/ 里面,当我N久以后再联上服务器,想看看文件下载情况怎样,那又要 cd 老半天,这时候 screen 就是好帮手了. 顾名 ...

  9. HDU 1576 A/B(数论)

    题目:求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1).数据给出n和b 推导过程 A/B = K K = 9973* ...

  10. Unity 异步加载场景

    效果图如下: 今天一直在纠结如何加载场景,中间有加载画面和加载完毕的效果动画! A 场景到 B ,  看见网上的做法都是 A –> C –> B.  C场景主要用于异步加载B 和 播放一些 ...