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. Core Data (二)备

    序 上次只是说了三个Core Data栈基本类.这次准备介绍一下常用的类. NSManagedObject Core Data是一次底层数据封装成面向对象的技术.最直接的表现就是在SQLite里面的一 ...

  2. Oracle多行记录合并处理

    1:效果如下图所示: 表T1: CREATE TABLE T1 ( WEEKWORKID VARCHAR2(20) , DD VARCHAR2(20) ) 表T2 CREATE TABLE T2 ( ...

  3. BZOJ 2879 NOI2012美食节

    链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2879 CZ市为了欢迎全国各地的同学,特地举办了一场盛大的美食节.作为一个喜欢尝鲜的美食客,小M ...

  4. ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase

    原文地址:http://www.51csharp.com/MVC/882.html   ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL 引言-- 在初级篇中,我们 ...

  5. Zigzag Iterator 解答

    Question Given two 1d vectors, implement an iterator to return their elements alternately. For examp ...

  6. UITableView系列(1)---Apple缓存池机制

    一.概述 关于UITableView的基本使用, 其实十分简单.但是做App最重要的之一就是细致,技术方面要做到细致, 必须深入了解底层, 才能做出优化让程序跑得更快.那么这一系列文章从我实际项目中获 ...

  7. tabbar 嵌套 navigation

    -------------- 源代码:点击打开链接 ------------------------ AppDelegate.m - (BOOL)application:(UIApplication ...

  8. input里面check 状态检测

    if($("#check_status").attr('checked')) //检测checkbox状态 { //checked状态 }else { //不是checked状态 ...

  9. handsontable常规配置的中文API

    常规配置: 1.固定行列位置 fixedRowsTop:行数 //固定顶部多少行不能垂直滚动 fixedColumnsLeft:列数 //固定左侧多少列不能水平滚动 2.拖拽行头或列头改变行或列的大小 ...

  10. 黑马程序猿 IO流 ByteArrayInputStream与ByteArrayOutputStream

    ---------------------- ASP.Net+Unity开发..Net培训.期待与您交流! ---------------------- package cn.itcast.IO; i ...