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. 【python cookbook】【数据结构与算法】5.实现优先级队列

    问题:要实现一个队列,它能够以给定的优先级对元素排序,且每次pop操作时都会返回优先级最高的那个元素: 解决方案:采用heapq模块实现一个简单的优先级队列 # example.py # # Exam ...

  2. 【翻译】了解ASP.NET MVC的HTML助手

    原文:Understanding HTML Helpers in ASP.NET MVC 作 者:Shailendra Chauhan works as Software Analyst at rep ...

  3. linux系统修改CST时区

    ================================================= [root@tzyyserveryg ~]# date -R Fri, 28 Nov 2014 08 ...

  4. JavaEE基础(十)

    1.面向对象(package关键字的概述及作用) A:为什么要有包 将字节码(.class)进行分类存放 包其实就是文件夹 B:包的概述 举例: 学生:增加,删除,修改,查询 老师:增加,删除,修改, ...

  5. Windows下打包Python的exe可执行文件

    参考:http://www.cnblogs.com/Lands-ljk/p/5447723.html

  6. mysql高可用之PXC(Percona XtraDB Cluster)

    简介 Percona XtraDB Cluster是MySQL高可用性和可扩展性的解决方案,Percona XtraDB Cluster提供的特性如下: 1).同步复制,事务要么在所有节点提交或不提交 ...

  7. Windows下Apache+mod_python+Django配置

    Windows下Apache+mod_python+Django配置 Apache 首先要安装Apache,我安装的版本是2.2.*: Python python肯定是要安装的,我安装的版本是2.5的 ...

  8. JDBC批量Insert深度优化(有事务)

    环境: MySQL 5.1 RedHat Linux AS 5 JavaSE 1.5 DbConnectionBroker 微型数据库连接池   测试的方案: 执行10万次Insert语句,使用不同方 ...

  9. Uva 12186 工人的请愿书

    题目链接:https://uva.onlinejudge.org/external/121/12186.pdf 题意: 给出一个树状关系图,公司里只有一个老板编号为0,其他人员从1开始编号.除了老板, ...

  10. Oracle OCCI学习之开篇

    官网:Oracle C++ Call Interface 一.OCCI介绍 Oracle C++ Call Interface(OCCI)是一个用于访问Oracle数据库的高性能且全面的API.基于标 ...