UVa 10717 MINT:DFS enumerated 4-digit LCM

Source: Internet
Author: User
Tags gcd time limit

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; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.