Sticks
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 129606   Accepted: 30388

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
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int a[], n;
bool vis[];
int Cmp(int a, int b)
{
return a>b;
} //len 是要构造的每段的长度。 cur是构造每段
//长度的过程中还需要的。 num是剩余的木棒数。
int dfs(int len, int cur, int num)
{
if(cur==&&num==) return true;
if(cur==) cur = len;
for(int i=; i<n; i++)
{
if(vis[i]) continue;
if(cur-a[i]>=)
{
vis[i] = ;
if(dfs(len, cur-a[i], num-)) return true;
vis[i] = ;
if(a[i]==cur||cur==len) return false;//a[i]==cur
//满足一个木棒长度, 但是不满足全部的 cur==len是进行循环之后还是不满足
while(a[i]==a[i+]&&i+<n) ++i;
}
}
return false;
} int main()
{
while(scanf("%d", &n), n)
{
int sum = ;
for(int i=; i<n; i++)
{
scanf("%d", &a[i]);
sum+=a[i];
}
sort(a, a+n, Cmp);
for(int i=a[]; i<=sum; i++)
{
if(sum%i==)
{
memset(vis, , sizeof(vis));
if(dfs(i, , n))
{
printf("%d\n", i);
break;
}
}
}
}
return ;
}

POJ1011 (DFS+剪枝)的更多相关文章

  1. poj1011(DFS+剪枝)

    题目链接:https://vjudge.net/problem/POJ-1011 题意:给定n(<=64)条木棍的长度(<=50),将这些木棍刚好拼成长度一样的若干条木棍,求拼出的可能的最 ...

  2. *HDU1455 DFS剪枝

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  3. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  4. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  5. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  6. DFS+剪枝 HDOJ 5323 Solve this interesting problem

    题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...

  7. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  8. HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))

    Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  9. LA 6476 Outpost Navigation (DFS+剪枝)

    题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...

  10. poj 1011 Sticks (DFS+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127771   Accepted: 29926 Descrip ...

随机推荐

  1. linux设备驱动归纳总结(三):2.字符型设备的操作open、close、read、write【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-59417.html linux设备驱动归纳总结(三):2.字符型设备的操作open.close.rea ...

  2. fedora 20 yum出错

    需要利用linux做项目,所以在win10装了vmvare 以及 fedora,据说这个linux比较稳定.. 1.系统装好以后,需要先把terminal调处理,这才符合程序猿的习惯嘛,具体方法如下 ...

  3. Docker Centos安装Mysql5.6

    之前一篇随笔<Docker Centos安装Openssh> 写的是如何在基础的centos镜像中搭建ssh服务,在此基础上再搭建其他服务.本文继续介绍在centos_ssh基础上搭建my ...

  4. 在ECSHOP后台的订单列表中显示配送方式

    熟悉ECSHOP后台的人都知道,只有点击某个具体的订单,进入订单详细页面才能看到该订单的配送方式,最模板修改的目的,是想让管理者在订单列表页面 就能看到该订单的配送方式. 下面是修改方法:首先来修改 ...

  5. 20145227 《Java程序设计》第7周学习总结

    20145227 <Java程序设计>第7周学习总结 教材学习内容总结 第十二章 Lambda 如果使用JDK8的话,可以使用Lambda特性去除重复的信息. 在只有Lambda表达式的情 ...

  6. JAVA 值传递

    Java里方法的参数传递方式只有一种:值传递 值传递:当系统开始执行方法时,系统为形参执行初始化,就是把实参变量的值赋给方法的形参变量,方法的操作的并不是实际的实参变量 引用型变量:系统复制的是变量, ...

  7. AngularJS 用 Interceptors 来统一处理 HTTP 请求和响应

    Web 开发中,除了数据操作之外,最频繁的就是发起和处理各种 HTTP 请求了,加上 HTTP 请求又是异步的,如果在每个请求中来单独捕获各种常规错误,处理各类自定义错误,那将会有大量的功能类似的代码 ...

  8. 如何在plSql查询数据查出的数据可编辑

    最近开发项目时要经常自己造数据,遇到好多查询出数据时要进行修改.上网查询资料 总结如下: plSql允许查询数据可以编辑的条件是必须查询出rowid 在某个表上点击query data 出现的sql语 ...

  9. [ios][opengles]GLKit如何搭一个app的框架

    一个外文对GLKit的讲解: Beginning OpenGL ES 2.0 with GLKit Part 1    英文原文链接:http://www.raywenderlich.com/5223 ...

  10. 理解css中的position-static\relative\fixed\absolute

    position属性有四个值: static(静态定位):是默认值,不会被特殊的定位,遵循正常的文档流对象,对象占用文档空间,该方式下,top.right.bottom.left.z-index等属性 ...