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. 161114、websocket实现心跳重连

    心跳重连缘由 在使用websocket过程中,可能会出现网络断开的情况,比如信号不好,或者网络临时性关闭,这时候websocket的连接已经断开, 而浏览器不会执行websocket 的 onclos ...

  2. Android 常用工具类之SPUtil,可以修改默认sp文件的路径

    参考: 1. 利用Java反射机制改变SharedPreferences存储路径    Singleton1900 2. Android快速开发系列 10个常用工具类 Hongyang import ...

  3. oracle 数据泵 每天自动备份

    @echo off echo 删除10天前的备分文件和日志 forfiles /p "e:\app\back" /m *.dmp /d -5 /c "cmd /c del ...

  4. Java锁的种类

    转载自:---->http://ifeve.com/java_lock_see/ Java锁的种类以及辨析锁作为并发共享数据,保证一致性的工具,在JAVA平台有多种实现(如 synchroniz ...

  5. office 2016 专业增强版 和 visio 2016 专业版 下载安装(附带激活工

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://babyshen.blog.51cto.com/8405584/1697910 o ...

  6. Entity Framework 第六篇 分页查询

    目前分页支持单表 , ) where TEntity : class { ) * size; var _reset = Get(filter, orderBy); total = _reset.Cou ...

  7. lhgdialog在打开的窗口里点击按钮关闭当前窗口

    lhgdialog在打开的窗口里点击按钮关闭当前窗口 var api = frameElement.api, W = api.opener; api.close();

  8. java的报表下载代码excel

    /** * 汇总报表数据下载 * */ private ModelAndView exportSummaryDatadown(HttpServletRequest request, HttpServl ...

  9. CentOS 配置solr中文分词器

    第一步:使用IK-Analyzer.把分析器的文件夹上传到服务器. 第二步:需要把分析器的jar包添加到solr工程中. [root@bogon IK Analyzer 2012FF_hf1]# cp ...

  10. placeholder 解决UITextField中placeholder和text文本同时显示的问题

    TextField都使用了placeholder属性,但在代码中又设置了text属性,因此ViewController会同时显示placeholder文本和text文本. 这个问题让我彻底崩溃.按道理 ...