poj1011
2012-05-03 21:37
330 查看
Sticks
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
Sample Output
Source
Central Europe 1995
DFS,需要剪枝
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 95592 | Accepted: 21482 |
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
Central Europe 1995
DFS,需要剪枝
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int used[100],len[100],sum,n,Min; int find(int p,int rest,int trest) { int i; if (trest==Min) return 1; for (i=p;i<=n;i++) if (!used[i]&&len[i]<=rest) { used[i]=1; if (len[i]==rest) { if (find(1,Min,trest-len[i])) return 1; } else if (find(i+1,rest-len[i],trest-len[i])) return 1; used[i]=0; if (len[i]==rest) return 0; if (trest==sum) return 0; if (rest==Min) return 0; while (len[i+1]==len[i]) i++; } return 0; } bool cmp(int a,int b) {return a>b;} int main() { int i; while (scanf("%d",&n)&&n!=0) { for (i=1;i<=n;i++) scanf("%d",&len[i]); memset(used,0,sizeof(used)); sort(len+1,len+n+1,cmp); sum=0; for (i=1;i<=n;i++) sum+=len[i]; Min=len[1]; while (sum%Min!=0) Min++; while (find(1,Min,sum)==0) { Min++; while (sum%Min!=0) Min++; } printf("%d\n",Min); } return 0; }
相关文章推荐
- poj1011 深搜题代码解释
- POJ1011
- poj1011 Sticks---dfs
- poj1011 (Sticks)
- poj1011 dfs 剪枝
- G - Sticks HDU - 1455 POJ1011
- POJ1011
- POJ1011
- poj1011
- POJ1011 Sticks DFS+剪枝
- POJ1011 木棒
- POJ1011 Sticks
- poj1011
- poj1011
- POJ1011 stick
- 第九天:poj1011(又是深度搜索+剪枝,还是不会o(∩_∩)o )
- POJ1011
- poj1011:Sticks
- 【poj1011】STICKS
- 4.3.5 Sticks (POJ1011)