Uva12325 暴力枚举

http://vjudge.net/contest/view.action?cid=50643#problem/A

Some brave warriors come to a lost village. They are very lucky and find a lot of treasures and a big treasure chest, but with angry zombies.

The warriors are so brave that they decide to defeat the zombies and then bring all the treasures back. A brutal long-drawn-out battle lasts from morning to night and the warriors find the zombies are undead and invincible.

Of course, the treasures should not be left here. Unfortunately, the warriors cannot carry all the treasures by the treasure chest due to the limitation of the capacity of the chest. Indeed, there are only two types of treasures: emerald and sapphire. All of the emeralds are equal in size and value, and with infinite quantities. So are sapphires.

Being the priest of the warriors with the magic artifact: computer, and given the size of the chest, the value and size of each types of gem, you should compute the maximum value of treasures our warriors could bring back.

Input

There are multiple test cases. The number of test cases T (T$ \le$200) is given in the first line of the input file. For each test case, there is only one line containing five integers NS1V1S2V2, denoting the size of the treasure chest is N and the size and value of an emerald isS1 and V1, size and value of a sapphire is S2V2All integers are positive and fit in 32-bit signed integers.

Output

For each test case, output a single line containing the case number and the maximum total value of all items that the warriors can carry with the chest.

Sample Input

2
100 1 1 2 2
100 34 34 5 3

Sample Output

Case #1: 100
Case #2: 86
题目大意:

                     知道背包的容量N,有两件物品占用空间分别为s1和s2,物品价值为v1和v2,,数量有无数个。求背包里能装的物品的最大价值是多少。

解题思路:

                       这道题和完全背包的问题很像,但是又有太大的差距,这里的N是一个很大的数,想利用完全背包的解法来解肯定是行不通的,必然会导致数组越界,我采用的方法是利用枚举的方法。首先确定s1和s2 的最小公倍数s,比较在这个容量下那种物品的价值更高一些,那么在装的时候我们把N/s*s部分都装这种物品,N%s部分枚举一种物品拼的数量求出在怎样的搭配下可以取到最大的价值。有一点值得注意我们在求N%s部分时,需要拿出一个s和N%s加起来做为枚举的对象(想想就明白)

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
long long Gcd(long long u, long long v)
{
    if(u<v)
    {
        long long t=u;
        u=v;
        v=t;
    }
    long long remainder;
    remainder = u % v;
    while(remainder)
    {
        u = v;
        v = remainder;
        remainder = u % v;
    }
    return v;
}
long long gcd(long long u, long long v)
{
    return u * v / Gcd(u, v);
}
int main()
{
    long long n,v1,s1,v2,s2,T,sum,s,sum1;
    scanf("%lld",&T);
    long long tt=0;
    while(T--)
    {
        scanf("%lld%lld%lld%lld%lld",&n,&s1,&v1,&s2,&v2);
        long long value=0,va=0;
        s=gcd(s1,s2);
        long long temp=n/s;
        n%=s;
        if(temp)
        {
            temp--;
            n+=s;
        }
        value=temp*max(s/s1*v1,s/s2*v2);
        if(s1<s2)
        swap(s1,s2),swap(v1,v2);
        for(int i=0; i<=n/s1; i++)
            va=max((n-i*s1)/s2*v2+i*v1,va);
        value+=va;
        printf("Case #%lld: %lld\n",++tt,value);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值