UVaOJ10717 - Mint

Problem E: Mint

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

Gordon V. Cormack


题目大意:

    输入n,m,表示有n种不同的硬币,m张桌子。下面n行是n种硬币的数值,接下来m行是每个桌子高度。每个桌子都固定有4个脚而且高度一样。用硬币来组成桌脚,要求一只脚要用一样的硬币。对于每个桌子输出两个数字,一个是小于等于该桌子高度的最大值,一个是大于等于桌子高度的最小值。

解题方法:

    就是枚举n个硬币中的4个,然后求4个硬币的最小公倍数,然后将这个lcm翻倍,只要满足 “一个是小于等于该桌子高度的最大值,一个是大于等于桌子高度的最小值”

源代码:

#include <iostream>
#include <cstdio>
using namespace std;

int n, t, goal, L, R;
int high[55], num[5];
int gcd (int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}
int lcm (int a, int b) {
    if (a < b) {
        int temp = a;
        a = b;
        b = temp;
    }
    return a / gcd(a, b) * b;
}
void solve () {
    int l, r, i;
    int LCM = num[0];
    for (i = 0; i < 4; i ++) {
        LCM = lcm(LCM, num[i]);
    }
    for (i = 0; LCM * (i + 1) < goal; i ++);
    if (LCM * (i + 1) == goal) {
        l = r = LCM * (i + 1);
    } else {
        l = LCM * i;
        r = LCM * (i + 1);
    }
    if (l > L) {
        L = l;
    }
    if (r < R) {
        R = r;
    }
}
void dfs(int p, int c) {
    if (c == 4) {
        solve();
        return;
    }
    for (int i = p + 1; i <= n - 4 + c; i ++) {
        num[c] = high[i];
        dfs(i, c + 1);
    }
    return;
}
int main() {
    while (scanf("%d%d", &n, &t) != EOF && n && t) {
        for (int i = 0; i < n; i ++) {
            scanf("%d", &high[i]);
        }
        while (t --) {
            scanf("%d", &goal);
            L = -0x3f3f3f3f;
            R = 0x3f3f3f3f;
            for (int i = 0; i <= n - 4; i ++) {
                num[0] = high[i];
                dfs(i, 1);
            }
            printf("%d %d\n", L, R);
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值