Sticks
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 6035 accepted submission (s): 1704
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 the 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.
Inputthe input contains blocks of 2 lines. the first line contains the number of sticks parts after cutting, there are at most 64 sticks. the second line contains the lengths of those parts separated by the space. the last line of the file contains zero.
Outputthe output file contains the smallest possible length of original sticks, one per line.
Sample Input
95 2 1 5 2 1 5 2 141 2 3 40
Sample output
65Question: give multiple groups of data. Each group of data represents some sticks. Can I use all of them to form as many sticks of the same length as possible and output the length of the sticks?Code:# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace STD; int SS, T, D, M; int Q [99]; int vis [99]; int CMP (int A, int B) {return A> B;} void DFS (int K, int length, int num) // K indicates the number of sticks found. The length is the current stick length. Num indicates the number of splices. {If (t = 1) return; If (num = SS) {T = 1; return;} If (length = d) {DFS (0, 0, num + 1); // Add 1 If (t = 1) return;} For (INT I = K; I <m; I ++) {If (vis [I] = 0 & length + Q [I] <= D) // pruning 2 {vis [I] = 1; DFS (I + 1, Length + Q [I], num); vis [I] = 0; if (D-length = Q [I]) // pruning 3 satisfies the condition that the current fight is good but does not meet the condition that the return result does not need to be searched. If (length = 0) // pruning 4 the most important thing is not to write directly. TLE will not use return if the long stick is useless after recursion; if (Q [I] = Q [I + 1]) // It is difficult to directly skip I ++; }}} int main () if the same sticks of pruning 5 are hard to work () {int I; int s; int max1; while (~ Scanf ("% d", & M) {If (M = 0) break; S = 0; t = 0; for (I = 0; I <m; I ++) {scanf ("% d", & Q [I]); s = S + Q [I];} Sort (Q, q + M, CMP ); // sort max1 = Q [0] By size; for (I = max1; I <s; I ++) // start from the longest root length. Otherwise, WA {If (S % I = 0) // pruning 1: the wooden sticks should be used up and multiple long wooden sticks should have the same length {memset (VIS, 0, sizeof (VIS); SS = s/I; D = I; DFS (0, 0, 0);} If (t = 1) break;} printf ("% d \ n", I);} return 0 ;}