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

【题意】给出n个长度的短棒,是由原来等长的小棒任意剪切而来,问原来的小棒最短的长度是多少

【思路】用dfs搜索,重点是其中的几个剪枝,不减的话会超时。

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int N=;
int n;
int d,num;
int val[N];
int vis[];
bool flag;
int cmp(int x,int y)
{
return x>y;
}
void dfs(int k,int len,int num1)
{
if(flag==) return ;
if(len==d)
{
dfs(,,num1+);
if(flag==) return ;
}
if(num1==num)
{
flag=;
return ;
}
for(int i=k;i<=n;i++)
{
if(!vis[i]&&len+val[i]<=d)
{
vis[i]=;
dfs(i+,len+val[i],num1);
vis[i]=;
if(len==) return ;

//如果当前搜索时,前面的长度为0,而第一根没有使用,说明第一根始终要废弃,这种组合必然不会成功

            if(len+val[i]==d) return ;
if(val[i]==val[i+]) i++;

//如果当前和上一次搜到的木棒一样长,就没必要再搜了

        }
} }
int main()
{
while(~scanf("%d",&n),n)
{
int sum=;
for(int i=;i<=n;i++)
{
scanf("%d",&val[i]);
sum+=val[i];
}
sort(val+,val++n,cmp);
for(int i=val[];i<=sum;i++)
{
if(!(sum%i))
{
flag=;
memset(vis,,sizeof(vis));
d=i;
num=sum/d;
dfs(,,);
if(flag==) break;
}
}
printf("%d\n",d);
} return ;
}

Sticks_dfs的更多相关文章

随机推荐

  1. HDUOJ------3336 Count the string(kmp)

    D - Count the string Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  2. Word Search [LeetCode]

    Problem Description: http://oj.leetcode.com/problems/word-search/ Basic idea: recursively go forward ...

  3. 怎么实现form表单提交后不重新刷新当前页面

    怎么实现表单提交后不重新刷新当前页面     如何实现表单提交后不重新刷新当前页面 <form name='form1' id='form1' action='/xbcw/cw/xx_xx.ac ...

  4. JAVA读取EXCEL文件异常Unable to recognize OLE stream

    异常: jxl.read.biff.BiffException: Unable to recognize OLE stream at jxl.read.biff.CompoundFile.<in ...

  5. jQuery_pager.js分页

    在做前端项目中,总是需要自己手写类似于这样的分页效果: 这就需要使用jQuery.pager.js文件,其使用方法为:在html中引入三个文件,分别为: <link rel="styl ...

  6. 盘点:崛起中的九大HTML5开发工具

    HTML5被看做是Web开发者创建流行Web应用的利器,增加了对视频和Canvas 2D的支持.HTML5的诞生还让人们重新审视浏览器专用多媒体插件的未来,如Adobe的Flash和微软的Silver ...

  7. cookie与sessionID之间的关系实验

    上一篇介绍了cookie,这里来看看cookie与sessionID之间有什么关系. 一.编写测试用例代码 新建一个servlet如下: public class SessionServlet ext ...

  8. LZMA demo挑选使用备忘

    一.源码下载:http://sourceforge.net/projects/sevenzip/ 二.各种EXE及其功能 代码里面有很多的dsw工程,功能不一,size不一,需要挑选合适的加入到自己的 ...

  9. php solr 查询

    $options = array( 'hostname' => 'localhost', 'port' => 8080, 'path' => 'solr/test'); $clien ...

  10. sql删除多余重复的数据只保留一条

    delete from people where   peopleName in (select peopleName    from people group by peopleName      ...