码迷,mamicode.com
首页 > 其他好文 > 详细

UVA - 10717 - Mint (GCD + LCM)

时间:2015-04-29 11:51:17      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:acm   gcd   uva   lcm   


题目传送:UVA - 10717


思路:思路很明确,就是找出所有的四种硬币的组合,然后求出他们的最小公倍数,再去找出该最小公倍数的倍数中与desired length最接近的,昨晚不知道啥错误,一直WA,今天重写一下就过了


AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <cctype>
#define LL long long
#define INF 0x7fffffff
using namespace std;

int n, t; 
int a[105];

int L, R;

int gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a % b);
}

int lcm(int a, int b) {
	return a / gcd(a, b) * b;
}

void fun(int des) {
	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 ++) {
					int Lcm = lcm(a[i], lcm(a[j], lcm(a[k], a[l])));
					int num = des / Lcm;
					if(Lcm * num == des) {
						L = des; R = des; return;
					}
					else {
						L = max(L, num * Lcm);
						R = min(R, (num + 1) * Lcm);
					}
				}
			}
		}
	}
}

int main() {
	while(scanf("%d %d", &n, &t), n || t) {
		for(int i = 0; i < n; i ++) {
			scanf("%d", &a[i]);
		}
		for(int i = 0; i < t; i ++) {
			int des;
			scanf("%d", &des);
			L = - INF;
			R = INF;
			fun(des);
			printf("%d %d\n", L, R);
		}
	}
	return 0;
}
















UVA - 10717 - Mint (GCD + LCM)

标签:acm   gcd   uva   lcm   

原文地址:http://blog.csdn.net/u014355480/article/details/45362793

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!