POJ1011

Sticks
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 100068   Accepted: 22747

Description

George 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.

Input

The 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.

Output

The output should contains the smallest possible length of original sticks, one per line.

Sample Input

9

5 2 1 5 2 1 5 2 1

4

1 2 3 4

0

Sample Output

6

5

Source

 
 1 #include <stdio.h>

 2 #include <string.h>

 3 #include <algorithm>

 4 

 5 using namespace std;

 6 

 7 int used[100],len[100],sum,n,Min;

 8 

 9 int find(int p,int rest,int trest)

10 {

11     int i;

12 

13     if (trest==Min)

14         return 1;

15     for (i=p;i<=n;i++)

16        if (!used[i]&&len[i]<=rest)

17        {

18           used[i]=1;

19           if (len[i]==rest)

20           {

21              if (find(1,Min,trest-len[i]))

22                 return 1;

23           } else if (find(i+1,rest-len[i],trest-len[i]))

24              return 1;

25           used[i]=0;

26           if (len[i]==rest)

27              return 0;

28           if (trest==sum)

29              return 0;

30           if (rest==Min)

31              return 0;

32           while (len[i+1]==len[i])

33               i++;

34        }

35     return 0;

36 }

37 

38 bool cmp(int a,int b) {return a>b;}

39 int main()

40 {

41     int i;

42     while (scanf("%d",&n)&&n!=0)

43     {

44        for (i=1;i<=n;i++)

45            scanf("%d",&len[i]);

46        memset(used,0,sizeof(used));

47        sort(len+1,len+n+1,cmp);

48        sum=0;

49        for (i=1;i<=n;i++)

50            sum+=len[i];

51        Min=len[1];

52        while (sum%Min!=0)

53            Min++;

54        while (find(1,Min,sum)==0)

55        {

56           Min++;

57           while (sum%Min!=0)

58              Min++;

59        }

60        printf("%d\n",Min);

61     }

62     return 0;

63 }

 

你可能感兴趣的:(poj)