HDU 2844 Coins

本文介绍了一个关于多重背包问题的经典算法题,通过二进制优化来提高解决特定硬币组合问题的效率。问题要求计算在给定的硬币种类和数量下,能够支付的不超过特定金额的所有可能价格的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:传送门
题面:
C o i n s Coins Coins

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
1 2 4 2 1 1
2 5
1 4 2 1
0 0

Sample Output

8
4

题目大意:

你想要买一个东西,你有 n n n种硬币,每种硬币的面值为 w i w_i wi每种硬币的数量为 p i p_i pi要买的物品价值不超过 V V V,要输出 1 − m 1-m 1m之间能支付多少面额

对于 1 − V 1-V 1V的每一种价格都当成一个背包
要把它装满
做多重背包就好了
最后再统计一下能有多少装满的
唯一一点就是这个题要二进制优化

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <vector>
#include <iomanip>
#define A 1000010
#define B 2010
#define ll long long

using namespace std;
int n, m, V, w[A], f[A], p[A];

int main() {
    while (cin >> n >> V) {
        if (!n and !V) break;
        for (int i = 1; i <= n; i++) cin >> w[i];
        for (int i = 1; i <= n; i++) cin >> p[i];
        memset(f, -0x3f, sizeof f); f[0] = 0;
        for (int i = 1; i <= n; i++) {
            int xx = p[i], k = 1;
            while (k < xx) {
                for (int j = V; j >= k * w[i]; j--)
                  f[j] = max(f[j], f[j - k * w[i]] + k);
                xx -= k; k <<= 1;
            }
            for (int j = V; j >= xx * w[i]; j--)
              f[j] = max(f[j], f[j - xx * w[i]] + xx);
        }
        int ans = 0;
        for (int i = 1; i <= V; i++)
          if (f[i] > 0) ans++;
        cout << ans << endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

良月澪二

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值