首页 技术 正文
技术 2022年11月15日
0 收藏 321 点赞 3,883 浏览 1634 个字

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.

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 Input

3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0看到英问题就头疼!!!
这个题目看懂题目,理解题意是我觉得最难的一步!(英语贼烂)
其实这个题目意思就是在这个[1,m]这个区间里面有多少个背包可以装满。
(题意浓缩起来只有这么点,提炼出来真心费力)
这个题目第二难的一部就是进行二进制优化。
(由于本菜鸟刚学DP确实这个对我而言是一个长期理解的过程)
二进制优化:
任何一个数都可以由2的N次方来表示,例如4可以有1,2来表示;
8可以有1,2,4来表示;11可以有1,2,4,8来表示。
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 100010
int dp[maxn],a[],b[];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
if (n== && m==) break;
memset(dp,,sizeof(dp));
for (int i= ;i<=n ;i++)
scanf("%d",&a[i]);
for (int i= ;i<=n ;i++)
scanf("%d",&b[i]);
for (int i= ;i<=n ;i++){
if (a[i]*b[i]>m) {
for (int k=;k<=m ;k++){
if (k>=a[i]) dp[k]=max(dp[k],dp[k-a[i]]+a[i]);
}
}else {
for (int j= ;j<=b[i] ;j=j*){
for (int k=m ; k>=a[i]*j ;k--){
dp[k]=max(dp[k],dp[k-a[i]*j]+a[i]*j);
}
b[i]-=j;
}
if (b[i]>) {
for (int k=m ;k>=a[i]*b[i] ;k--) {
dp[k]=max(dp[k],dp[k-a[i]*b[i]]+a[i]*b[i]);
}
}
}
}
int sum=;
for (int i= ;i<=m ;i++){
if (dp[i]==i) sum++;
}
printf("%d\n",sum);
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,028
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,367
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,858