HDU 1455 sticks (Classic Deep Search + pruning)

Source: Internet
Author: User

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 3582 accepted submission (s): 903

Problem descriptiongeorge
Took sticks of the same length and cut them randomly until all parts
Became at most 50 units long. Now he wants to return sticks to
Original state, but he forgot how many sticks he had originally and how
Long they were originally. Please help him and design a program which
Computes the smallest possible original length of those sticks. All
Lengths expressed in units are integers greater than zero.


Input contains blocks of 2 lines. The first line contains the number
Sticks parts after cutting, there are at most 64 sticks. The second
Line contains the lengths of those parts separated by the space.
Last line of the file contains zero.


Outputthe output file contains the smallest possible length of original sticks, one per line.


Sample input95 2 1 5 2 5 2 141 2 3 40


Sample output65: This is a previous question poj (1011). At that time, I had been reading it from the Internet for a long time. Although it had passed, it had been so long, it is found that it is still necessary to write it again. This time, it is much easier to write it again. It is similar to the previous one. This time, a struct is used to store the information of the wooden stick, but there is still little pruning, here is the classic application of pruning, so you must learn it well .... Kabad...
# Include <iostream> # Include <Algorithm>Using   Namespace  STD;  //  Number of groups of wood rods that can be composed of total, L: the length of a wood rod.  Int  Total, L;  //  Num: the input integer. Sum: the total length.  Int  Num, sum; typedef  Struct  {  Int Length; //  Length of wood stick     Bool Mark; //  Has the wooden stick been used?  } Sticks; sticks [  70  ];  Bool  CMP (sticks a, sticks B ){  Return A. length> B. length ;}  //  S: Number of wood sticks that have been composed, the length of Len, and the position of the wood sticks searched by POS  Int DFS ( Int S, Int Len, Int  Pos ){  If (S = total) Return   1  ;  For ( Int I = POS + 1 ; I <num; I ++ ){  //  If this wooden stick has been used, continue with the next one.          If (Sticks [I]. Mark) Continue  ; If (LEN + sticks [I]. Length = L) {sticks [I]. Mark = True  ;  If (DFS (S + 1 , 0 ,- 1 )) Return   True  ; Sticks [I]. Mark = False  ;  Return  False  ;}  Else   If (Sticks [I]. Length + Len < L) {sticks [I]. Mark = True  ;  If (DFS (S, Len + Sticks [I]. length, I ))  Return   True  ; Sticks [I]. Mark = False  ; //  Pruning: if the length of the front edge is 0 during the current search, and the first root is not successfully used,  //  It indicates that the first root is always to be discarded, so this combination will not succeed.  //  Pruning here is required, because pruning here saves a lot of useless searches,  //  I tried it. The pruning will time out if it is omitted ....              If (LEN = 0 ) Return   False  ;  // Pruning: if the current and last found sticks are the same length, you do not need to search them again.              While (Sticks [I]. Length = sticks [I + 1 ]. Length) I ++ ;}}  Return   False  ;}  Int  Main (){  While (CIN> num & num! = 0  ) {Sum = 0 ; // Mark as 0          For ( Int I = 0 ; I <num; I ++ ) {CIN > Sticks [I]. length; sum + = Sticks [I]. length; sticks [I]. Mark = False  ;}  //  Sort the sticks in the order of length to short Sort (sticks, sticks + Num, CMP ); //  Search from the longest root of a wooden stick, because the smallest combination will be greater than or equal to the longest root.          For (L = sticks [ 0 ]. Length; L <= sum; l ++ ){  //  Pruning 1: if it cannot be divisible, it means it cannot constitute an integer wooden stick. Search for the next one.              If (Sum % L! = 0 ) Continue  ; Total = Sum/L; //  Obtain the total number of sticks.              If (DFS (1 , 0 ,- 1  ) {Cout <L < Endl;  Break  ;}}}  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.