When I first met this question, I had no idea at all. I did it only after reading the report on solving the problem .. No idea .. You can only learn from others ..
After reading this article, we should be able to thoroughly understand this question .. : Http://apps.hi.baidu.com/share/detail/17119728
Below is myCode:
# Include <iostream>
# Include <stdlib. h>
# Include <string. h>
Using namespace STD;
# Define n 70
Int sticks [N]; // used to store the length of a wooden stick
Int used [N]; // used to record whether the wood stick has been used
Int N, CNT, M, Len, flag;
Int CMP (const void * a, const void * B)
{
Return * (int *) B-* (int *);
}
Void DFS (INT POs, int current, int CNT) // POS is used to record the position in the current sticks [] array. Current is used to record the length of the currently matched wooden stick, CNT record to match the number of sticks
{
If (CNT = m)
Flag = true; // if the current length matches exactly all the wood sticks, the returned result is the completion task. The current length is the minimum length to be matched.
Else if (current = Len) // if current is exactly the same as Len, a match is completed, CNT + 1;
DFS (0, 0, CNT + 1 );
Else
{
Int pre =-1; // record the value previously searched. The value already searched in this matching process does not need to be matched if the length is the same.
For (INT I = Pos; I <n; I ++)
{
If (! Used [I] & sticks [I]! = Pre & Current + sticks [I] <= Len) // very important pruning conditions, which cannot be ignored. pruning techniques and art
{
Used [I] = 1;
Pre = sticks [I];
DFS (Pos + 1, current + sticks [I], CNT );
Used [I] = 0; // The search is unsuccessful and the use flag is restored.
If (Pos = 0 | flag) // returns if only one wooden stick is found or the search is successful.
Return;
}
}
}
}
Int main ()
{
While (CIN> N & n> 0)
{
Int sum = 0;
For (INT I = 0; I <n; I ++)
{
Cin> sticks [I];
Sum + = sticks [I];
}
Memset (used, 0, sizeof (used ));
Qsort (sticks, N, sizeof (sticks [0]), CMP); // The length of the wooden rod is sorted by the length from large to small, according to the meaning of the question, the minimum length of the original wooden rod is at least the length of the longest wooden rod after being truncated, and the maximum length is the length and
Flag = false; // indicates whether the operation is successful.
For (LEN = sticks [0]; Len <= sum; Len ++) // searches one by one from the possible length of the original wood stick. Once the loop is successfully stopped, the minimum result is obtained.
{
If (sum % Len = 0) // an important pruning condition that cannot be ignored can greatly improve the efficiency.
{
M = sum/Len;
DFS (0, 0 );
If (FLAG)
Break;
}
}
Cout <Len <Endl;
}
Return 0;
}