UVa - 10717 - Mint-(4个数的最大公约数)

The Royal Canadian Mint has commissioned a new series of designer coffee tables, with legs that are constructed from stacks of coins. Each table has four legs, each of which uses a different type of coin. For example, one leg might be a stack of quarters, another nickels, another loonies, and another twonies. Each leg must be exactly the same length.

Many coins are available for these tables, including foreign and special commemorative coins. Given an inventory of available coins and a desired table height, compute the lengths nearest to the desired height for which four legs of equal length may be constructed using a different coin for each leg.

Input consists of several test cases. Each case begins with an integers: 4 <= n<= 50 giving the number of types of coins available, and 1 <= t <= 10 giving the number of tables to be designed. n lines follow; each gives the thickness of a coin in hundredths of millimetres. t lines follow; each gives the height of a table to be designed (also in hundredths of millimetres). A line containing 0 0 follows the last test case.

For each table, output a line with two integers: the greatest leg length not exceeding the desired length, and the smallest leg length not less than the desired length.

Sample Input

4 2
50
100
200
400
1000
2000
0 0

Output for Sample Input

800 1200
2000 2000

题目给出n种硬币(每种给出厚度),让从中任选4种,同过叠加,每种硬币组成一个桌子的一条腿(四条长度相同),并且给出一个目标高度,找出组成的高度中从两侧最接近目标高度的值

那么直接枚举任意四种硬币,求其最小公倍数LCM即可

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 55

ll coin[M];
ll n,t,minv,maxv,table;

ll gcd(ll a,ll b)
{
    return b==0?a:gcd(b,a%b);
}
ll lcm(ll a,ll b)
{
    ll g=gcd(a,b);
    return a/g*b;
}

void solve()
{
    ll L1,L2,L3,L4;
    ll i,j,k,m;
    ll tdown,tup;
    for(i=1;i<=n-3;i++)
    {
        for(j=i+1;j<=n-2;j++)
        {
            L1=lcm(coin[i],coin[j]);
            for(k=j+1;k<=n-1;k++)
            {
                L2=lcm(L1,coin[k]);
                for(m=k+1;m<=n;m++)
                {
                    L3=lcm(L2,coin[m]);
                    tdown=floor(1.0*table/L3)*L3;
                    tup=ceil(1.0*table/L3)*L3;
                    if(minv<tdown)
                        minv=tdown;
                    if(maxv>tup)
                        maxv=tup;
                }
            }
        }
    }
}

int main()
{
    int i;
    while(scanf("%lld%lld",&n,&t)!=EOF)
    {
        if(n==0&&t==0) break;

        for(i=1;i<=n;i++)
            scanf("%lld",&coin[i]);
        while(t--)
        {
            scanf("%lld",&table);
            minv=-0x3f3f3f3f;
            maxv=0x3f3f3f3f;
            solve();
            printf("%lld %lld\n",minv,maxv);
        }
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值