TOJ 2446: Mint

2446: Mint 分享至QQ空间

Time Limit(Common/Java):2000MS/20000MS     Memory Limit:65536KByte
Total Submit: 4            Accepted:3

Description

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

Input consists of several test cases. Each case begins with two 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.

Output

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

Sample Output

给你n个数,给你个定义,问你最大和最小的距离

其实就是求下全部的四个数的最小公倍数,因为n不大,50^4还是比较小的,直接遍历一下就行的

#include<stdio.h>
#include<algorithm>
using namespace std;
const int N=3e6;
int f[N],a[62];
int gcd(int a,int b)
{
    return b?gcd(b,a%b):a;
}
int main()
{
    int n,t;
    while(scanf("%d%d",&n,&t),n||t)
    {
        for(int i=0; i<n; i++)
            scanf("%d",&a[i]);
        int b=0;
        for(int i=0; i<n-3; i++)
            for(int j=i+1; j<n-2; j++)
            {
                int m=a[i]/gcd(a[i],a[j])*a[j];
                for(int k=j+1; k<n-1; k++)
                {
                    int mm=m/gcd(m,a[k])*a[k];
                    for(int l=k+1; l<n; l++)
                        f[b++]=mm/gcd(mm,a[l])*a[l];
                }
            }
        while(t--)
        {
            int q;
            scanf("%d",&q);
            int ma=0,mi=0x3f3f3f3f;
            for(int i=0; i<b; i++)
                if(q%f[i]==0)
                {
                    ma=mi=q;
                    break;
                }
                else
                {
                    int x=q/f[i]*f[i];
                    int y=(q/f[i]+1)*f[i];
                    ma=max(ma,x);
                    mi=min(mi,y);
                }
           printf("%d %d\n",ma,mi);
        }
    }
    return 0;
}

 

posted @ 2017-10-23 19:15  暴力都不会的蒟蒻  阅读(200)  评论(0编辑  收藏  举报