HDU 4091 Zombie’s Treasure Chest 【杂题】%

Problem Description
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 <= 200) is given in the first line of the input file. For each test case, there is only one line containing five integers N, S1, V1, S2, V2, denoting the size of the treasure chest is N and the size and value of an emerald is S1 and V1, size and value of a sapphire is S2, V2. All 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表示总体积在给出两种物品1和2, S1表示1的体积, V1表示1的价格, S2表示2的体积, V2表示2的价格;
思路:暴力的解法,枚举0...N/s1..找出价格最大的,但是肯定超时;
设lcm为(s1, s2)的最小公倍数,那么1的个数a1=lcm/s1, 2的个数a2=lcm/s2; 如果lcm体积的空间应该存储max(a1*v1, a2*v2); 因此将N分成两部分,lcm的倍数与N%lcm;
但是要想全覆盖1和2, 应该N+=N%lcm;在枚举2部分的时候,该选择max(s1, s2)进行枚举;避免超时;
代码如下:
View Code
#include<stdio.h>

#include<iostream> 

#include<string.h>

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 Lcm(long long u, long long v)

{

    return u * v / Gcd(u, v);

}

int main()

{

    int i, j, T, cas=0;

    long long n, s1, v1, s2, v2, t; 

    scanf("%d", &T);

    while(T--)

    {

        //scanf("%lld%lld%lld%lld%lld", &n, &s1, &v1, &s2, &v2); //输入输出要%I64d否则WA 

        scanf("%I64d%I64d%I64d%I64d%I64d", &n, &s1, &v1, &s2, &v2);

        long long lcm=Lcm(s1, s2);

        long long temp=n/lcm;

        n=n%lcm;

        if(temp){ temp--; n+=lcm; } 

        long long sum=temp*(max(lcm/s1*v1, lcm/s2*v2));

        if(s1<s2){ t=s1, s1=s2, s2=t; t=v1, v1=v2, v2=t; } 

        long long maxnum=0; 

        for(i=0; i<=n/s1; i++)

        {

            t=i*v1+(n-i*s1)/s2*v2; 

            if(t>maxnum)

                maxnum=t;

        }

        maxnum+=sum;

        cas++; 

        printf("Case #%d: %I64d\n", cas, maxnum);

    }

}         

 

你可能感兴趣的:(HDU)