首页 技术 正文
技术 2022年11月12日
0 收藏 818 点赞 2,385 浏览 1843 个字

Coins

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

Problem DescriptionWhuacmers 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.

 InputThe 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. OutputFor each test case output the answer on a single line. Sample Input3 101 2 4 2 1 12 51 4 2 10 0 Sample Output84

多重背包的模板题

 #include <iostream>
#include <map>
#include <math.h>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
int n,m,a[],c[],dp[];
void comdp(int w,int v)
{
int i;
for(i=w; i<=m; i++)
dp[i]=max(dp[i],dp[i-w]+v);
}
void zeroone(int w,int v)
{
int i;
for(i=m; i>=w; i--)
dp[i]=max(dp[i],dp[i-w]+v);
}
void multidp(int w,int v,int cnt)//此时开始多重背包,dp[i]表示背包中重量为i时所包含的最大价值
{
if(cnt*w>=m)//此时相当于物品数量无限进行完全背包
{
comdp(w,v);
return;
}
int k=;//否则进行01背包转化,具体由代码下数学定理可得
while(k<=cnt)
{
zeroone(k*w,k*v);
cnt-=k;
k*=;
}
zeroone(cnt*w,cnt*v);
return ;
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
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));
int ans=;
for(int i=;i<=n;i++)
{
multidp(a[i],a[i],c[i]);//重量和价值都设为a[i],硬币的价值
}
for(int i=;i<=m;i++)//因为重量和价值都是a[i],所以dp[i]就表示在重量为i时的价值,这题的答案就是数一下有多少个价值是在1到m之间的
{
if(dp[i]==i)ans++; }
printf("%d\n",ans);
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893