Sticks
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 122771 |
|
Accepted: 28441 |
Description
George took sticks of the same length and cut them randomly until all parts became at most units long. Now he wants to return sticks to the original state, but he forgot what many sticks he had originally and how long they wer E originally. Him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units is integers greater than zero.
Input
The input contains blocks of 2 lines. The first line contains the number of sticks parts after cutting, there is at most sticks. The second line contains the lengths of those parts separated by the space. The last line of the file contains zero.
Output
The output should 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
65
Source
Central Europe 1995
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm>using namespace Std;int len[66], Tarlen, tarcnt, N, Sum, Cnt;bool vis[66];bool DFS (int k, int leftlen) {int i;if (Leftlen = 0) {if (++cnt = = tarcnt) return true;for (i = 0; i < N; ++i) if (!vis[i]) {Vis[i] = 1;if (DFS (i + 1, tarlen-len[i]) return True;else {VI S[i] = 0;--cnt;return false;}}} for (i = k; i < N; ++i) {if (!vis[i] && len[i] <= leftlen) {if (len[i] = = Len[i-1] &&!vis[i-1]) continu E;vis[i] = 1;if (DFS (i + 1, leftlen-len[i])) return true;vis[i] = 0;}} return false;} int main () {int I, J;while (scanf ("%d", &n), N) {for (i = sum = 0; i < N; ++i) {scanf ("%d", &len[i]); sum + = Len[i ];} Sort (len, Len + N, greater<int> ()); for (Tarlen = len[0]; tarlen < sum; ++tarlen) {if (sum% tarlen) continue;tarcnt = Sum/tarlen;memset (Vis, 0, sizeof (VIS)); vis[0] = 1; CNT = 0;if (DFS (1, tarlen-len[0])) break; printf ("%d\n", Tarlen);} return 0;}
POJ1011 sticks "pruning"