Sticks
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 113547 Accepted: 26078
问题描述
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.
输入格式
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.
输出格式
The output should contains the smallest possible length of original sticks, one per line.
样例输入
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
样例输出
6
5
首先 ,贴上超级大佬的此题代码网站大佬大佬大佬·
#include<bits/stdc++.h>
using namespace std;
int f=0,m,a[100],ss=0,l=0,len=0,s=0,visited[100];
int cmp(int x,int y)
{
return x>y;
}
int dfs(int nowlen,int nowget,int cnt)
{
if(cnt>=m) return 0;
if(nowget==l) return 1;
for(int i=cnt;i<m;i++)
{
if(visited[i]==0)
{
if(nowlen+a[i]==len)
{
// cout<<"p ";
visited[i]=1;
if(dfs(0,nowget+1,nowget)==1)
return 1;
// cout<<"q ";
visited[i]=0;
return 0;
}
else if(nowlen+a[i]<len)
{
visited[i]=1;
// cout<<"r ";
if(dfs(nowlen+a[i],nowget,i+1)==1)
return 1;
// cout<<"f ";
visited[i]=0;
if(nowlen==0)
return 0;
for(;a[i]==a[i+1]&&i+1<m;i++) cout<<"l ";
}
}
} return 0;
}
int main()
{
while(cin>>m)
{
memset(visited,0,sizeof(visited));
int i=0;
s=0;
if(!m)
break;
for( i=0;i<m;i++)
{
cin>>a[i];
s+=a[i];
}
sort(a,a+m,cmp);
for(len=a[0];len<=s;len++)
{
if(s%len!=0)
continue;
l=s/len;//共l棵
if(dfs(0,0,0)==1)
break;
}
cout<<len<<endl;
}
}