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. 集成 Apple Pay

    作者感言 在中秋过后终于把国内的三大支付平台SDK集成都搞定了, 现在我们终于可以来研究Apple自家的支付Apple Pay最后:如果你有更好的建议或者对这篇文章有不满的地方, 请联系我, 我会参考 ...

  2. Objective-C:Foundation框架-常用类-NSDate

    直接上代码吧: #import <Foundation/Foundation.h> #pragma mark 日期创建 void dateCreate() { // date方法返回的就是 ...

  3. Problem 2020 组合(FOJ)

    Problem 2020 组合 Accept: 714    Submit: 1724Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem ...

  4. plsql快速选中一行的快捷键

    实际工作中,经常用到pl/sql,在sql window中,经常性的用到选中一行然后按F8执行这条sql语句.用鼠标选中一行不是特别方便.用快捷键就快多了. 1.使用home键(不是windows键奥 ...

  5. java之进制转换

    [转载]晨风�0�5�0�2�0�1�6�6 2014年03月08日 于 爱Java 发表 众所周知.程序世界计算机中采用的是二进制,一个数字可以用任意进制表示.所以看一个数据值的同时.还要观察它的进 ...

  6. static详解

    static关键字用来修饰属性.方法,称这些属性.方法为静态属性.静态方法. static关键字声明一个属性或方法是和类相关的,而不是和类的某个特定的实例相关,因此,这类属性或方法也称为“类属性”或“ ...

  7. js基础之数组

    数组方法 添加: push arr.push();//尾部添加 unshift arr.unshift();//头部添加 删除: pop arr.pop();//尾部删除 shift arr.shif ...

  8. 二分搜索法(转载自vanezkw)

    二分查找算法java实现 今天看了一下JDK里面的二分法是实现,觉得有点小问题.二分法的实现有多种今天就给大家分享两种.一种是递归方式的,一种是非递归方式的.先来看看一些基础的东西. 1.算法概念. ...

  9. LibLinear(SVM包)使用说明之(一)README

    转自:http://blog.csdn.net/zouxy09/article/details/10947323/ LibLinear(SVM包)使用说明之(一)README zouxy09@qq.c ...

  10. 戴文的Linux内核专题:03驱动程序

    转自Linux中国 驱动程序是使内核能够沟通和操作硬件或协议(规则和标准)的小程序.没有驱动程序,内核不知道如何与硬件沟通或者处理协议(内核实际上先发送指令给BIOS,然后BIOS传给硬件). Lin ...