Chinaunix首页 | 论坛 | 博客
  • 博客访问: 232303
  • 博文数量: 75
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 848
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-08 10:27
文章分类
文章存档

2014年(9)

2013年(66)

我的朋友

分类: C/C++

2013-11-06 15:57:49

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
题意:给定一些硬币的高度,要求用4种硬币组成桌子4条腿,要一样高,然后给定一个high,求出最接近high的两个数字(向下取和向上取)
思路:暴力枚举每4个数的最小公倍数。答案即为这些最小公倍数*k中。
代码:
[cpp] view plaincopy
#include  
#include  
const int MAXN = 55;  
int n, t, i, j, Min, Max;  
int h[MAXN], high, save[MAXN][MAXN][MAXN][MAXN];  
  
int max(int a, int b) {return a > b ? a : b;}  
int min(int a, int b) {return a < b ? a : b;}  
  
int gcd(int a, int b) {  
    if (!b) return a;  
    return gcd(b, a % b);     
}  
  
void sav() {  
    for (int i = 0; i < n; i ++)  
        for (int j = i + 1; j < n; j ++)  
            for (int k = j + 1; k < n; k ++)  
                for (int l = k + 1; l < n; l ++)  
                    if (!save[i][j][k][l]) {  
                        int mul = h[i] * h[j] * h[k] * h[l];  
                        save[i][j][k][l] = mul / gcd(mul / h[i], gcd(mul / h[j], gcd(mul / h[k], mul / h[l])));  
                    }  
}  
  
void solve() {  
    for (int i = 0; i < n; i ++)  
        for (int j = i + 1; j < n; j ++)  
            for (int k = j + 1; k < n; k ++)  
                for (int l = k + 1; l < n; l ++) {  
                    if (!(high % save[i][j][k][l])) {  
                        Max = Min = high;  
                        return;  
                    }  
                    else {  
                        Max = min(Max, (high / save[i][j][k][l] + 1) * save[i][j][k][l]);  
                        Min = max(Min, high / save[i][j][k][l] * save[i][j][k][l]);  
                    }  
                }  
}  
  
int main() {  
    while (~scanf("%d%d", &n, &t) && n || t) {  
        memset(save, 0, sizeof(save));  
        for (i = 0; i < n; i ++)  
            scanf("%d", &h[i]);  
        sav();  
        while (t --) {  
            scanf("%d", &high);  
            Max = 999999999; Min = 0;  
            solve();  
            printf("%d %d\n", Min, Max);  
        }  
    }  
    return 0;  
}  
阅读(1028) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~