poj 1855:Mint 搜索

1855:Mint

总时间限制: 

3000ms 

内存限制: 

65536kB

描述

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 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.

输出

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.

样例输入

4 2
50
100
200
400
1000
2000
0 0

样例输出

800 1200
2000 2000

来源

Waterloo local 2004.09.19

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
using namespace std;
int n,t,h,Max,Min;
int Coin[55];
int gcd(int a,int b)
{
	if(b==0)return a;
	else return gcd(b,a%b);
}
int lcm(int a,int b)
{
	return a/gcd(a,b)*b;
}
void dfs(int H,int num,int cur)//当前凑出高度为H,凑出了num只脚,目前搜索到第cur种硬币 
{
	if(num==4)
	{
		int tmp=h/H*H;
		if(tmp==h)
		{
			Max=Min=tmp;
		}
		else
		{
			if(Max==-1||Max<tmp)
				Max=tmp;
			if(Min==-1||Min>tmp+H)
				Min=tmp+H;
		}
		return;
	}
	if(cur==n)return;
	dfs(H,num,cur+1);
	int l;
	if(H==0)l=Coin[cur];
	else l=lcm(H,Coin[cur]);
	dfs(l,num+1,cur+1);
}
int main()
{
	while(cin>>n>>t)
	{ 
		if(n==0&&t==0)break;
		for(int i=0;i<n;++i)
		{
			cin>>Coin[i];
		}
		for(int i=0;i<t;++i) 
		{
			cin>>h;
			Max=-1;Min=-1;
			dfs(0,0,0);
			cout<<Max<<" "<<Min<<endl;
		}
	}
	return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值