UVa 10717 Mint (DFS枚举4个数的lcm)
来源:互联网 发布:windows search索引器 编辑:程序博客网 时间:2024/05/20 07:54
10717 - Mint
Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1658
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 250100200400100020000 0
Output for Sample Input
800 12002000 2000
用dfs更简单(懒得写4个for),注意枚举了4个数后把所有高度的桌腿都判断一遍,这样更快。
完整代码:
/*0.048s*/#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int n, t;int a[55], h[55], maxh[55], minh[55];int gcd(int a, int b){return b ? gcd(b, a % b) : a;}int lcm(int a, int b){return a / gcd(a, b) * b;}void dfs(int now, int cnt, int cur){if (cnt == 4){for (int i = 0; i < t; i++){int temp = h[i] / now * now;///小技巧if (temp == h[i])maxh[i] = minh[i] = h[i];else{if (maxh[i] == -1 || temp > maxh[i])maxh[i] = temp;temp += now;if (minh[i] == -1 || temp < minh[i])minh[i] = temp;}}return;}if (cur == n) return;dfs(now, cnt, cur + 1);int temp = lcm(now, a[cur]);dfs(temp, cnt + 1, cur + 1);}int main(){int i;while (scanf("%d%d", &n, &t), n){memset(maxh, -1, sizeof(maxh));memset(minh, -1, sizeof(minh));for (i = 0; i < n; ++i) scanf("%d", &a[i]);for (i = 0; i < t; ++i) scanf("%d", &h[i]);for (i = 0; i < n; ++i) dfs(a[i], 1, i + 1);///开始暴力!for (i = 0; i < t; ++i) printf("%d %d\n", maxh[i], minh[i]);}return 0;}
- UVa 10717 Mint (DFS枚举4个数的lcm)
- UVa 10717 - Mint (枚举状态求LCM更新结果)
- uva 10717 Mint(lcm)
- UVA - 10717 - Mint (GCD + LCM)
- UVA 10717 Mint(多个数最小公倍数)
- UVa 10892 LCM的个数 (GCD和LCM 质因数分解)
- LCM的个数 UVa10892
- LCM的个数
- LCM的个数
- UVA 10717 - Mint
- UVA 10717 Mint
- UVA - 10717 Mint
- UVa:10717 Mint
- UVA 10717 - Mint
- UVA 10717 Mint
- UVA 10717 MINT
- UVA - 10717 Mint
- UVa 10717 - Mint
- 才高行厚的hibernate(7)---hibernate的优化
- __int64
- [LeetCode] Clone Graph
- jQuery列表拖动排列
- 原子操作
- UVa 10717 Mint (DFS枚举4个数的lcm)
- Matlab画图 线型控制
- j2se之算数运算符
- 如何下载网页中背景音乐
- Python 与 C++ 的交互编程
- 素数环(nyoj488)
- Linux进程fork execu之数据和文件描述符的继承
- C#中BeginInvoke和EndInvoke的用法
- linux内核文件翻译- RPCSEC_GSS内核RPC服务器支持