题目传送: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)
原文地址:http://blog.csdn.net/u014355480/article/details/45362793