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 this are constructed from stack s of coins. Each table has four legs, each of which uses a different type of coin. For example, one leg might is a stack of quarters, another nickels, another loonies, and another twonies. Each leg must is exactly the same length.
Many coins are available for this tables, including foreign and special commemorative. Given an inventory of available coins and a desired table height, compute the lengths nearest to the desired height for WH Ich four legs of equal length may is constructed using a different for each coin.
Input consists of several test cases. Each case begins with a integers:4 <= n <= giving the number of types of coins available, and 1 <= t <= 1 0 giving the number of tables to be DESIGNED.N lines follow; Each gives the thickness of a coin in hundredths of millimetres.t lines; 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 Smalles T leg length not less than the desired length.
Sample Input
4
1000 2
mb
0 0
Output for Sample Input
1200
2000 2000
It's simpler to use DFS (lazy to write 4 for), and note that the 4-digit enumeration is used to determine all the height of the table leg, which is faster.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
Full code:
/*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;///Tips 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);///started the violence!
for (i = 0; i < T; ++i) printf ("%d%d\n", Maxh[i], minh[i);
return 0; }