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. Largest Rectangle in a Histogram(HDU 1506 动态规划)

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  2. [CSAPP]并发与并行

    学了这么久的计算机,并发与并行的概念理解的一直不够透彻.考研复习那会儿,以为自己懂了,然而直到看了CSAPP才算是真正明白了这俩个概念. 并发(concurrency) 流X和流Y并发运行是指,流X在 ...

  3. Win8 X64下JDK版本切换问题

    1:现在笔记本上安装的是,执行java -version: 2:现在又安装了,想通过修改环境变量,使JDK切换到1.6版本上 JDK的安装目录:D:\Java\ 修改环境变量: (1)JAVA_HOM ...

  4. Qt String 与char* char int之间的转换

    下面CSDN的博客已经描述的很好了.不写了 references: http://blog.csdn.net/ei__nino/article/details/7297791 http://blog. ...

  5. mysql命令行的基本用法

    基础介绍:1.在linux下使用下列命令,请确认mysql的bin目录是否已经加入到PATH路径中,或者是已经进入到mysql安装路径下的bin目录查看PATHshell> echo $PATH ...

  6. kibana 统计每天注册数

  7. centos 6.5 hadoop 2.3 初配置

    为了安装hadoop废了好大的劲才把esxi5.5给装好. 同时装了centos6.5,由于hadoop里面有个免密码登陆所以这里讲的就是免密码登陆. 看了大家的博客文章发现转发的一部分,写ubunt ...

  8. 【转】android cts failed items

    原文网址:http://blog.csdn.net/linsa0517/article/details/19031479 Fail的一些修改   1.直接设置问题 estUnknownSourcesO ...

  9. 第03讲- 第一个Android项目

    第03讲第一个Android项目 Android项目目录结构: 重要文件: src res AndroidManifest.xml 包含内容: MainActivity.java (程序主视图) 存放 ...

  10. FFmpeg缩放swscale详解 <转>

    利用ffmpeg进行图像数据格式的转换以及图片的缩放应用中,主要用到了swscale.h文件中的三个函数,分别是: struct SwsContext *sws_getContext(int srcW ...