Sticks

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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 file 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<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int st[];
bool vis[];
int cnt;
int n;
int l;
bool cmp(int a,int b)
{
return a>=b;
}
bool dfs(int num,int len)
{
if(num==cnt) return true;
for(int i=;i<n;i++)
{
if(!vis[i])
{
if(len+st[i]==l)
{
vis[i]=true;
if(dfs(num+,)) return true;
vis[i]=false; }
if(len+st[i]<l)
{
vis[i]=true;
if(dfs(num,len+st[i])) return true;
vis[i]=false;
if(len==) return ;//这里剪枝很重要,而且很经典。如果当前为0,即本次搜索失败,则一定会废弃一个static,则当前长度的分法肯定不成立。
int last=st[i];
while(last==st[i]) i++;
}
}
}
return false;
}
int main()
{ while((scanf("%d",&n))&&(n!=))
{
for(int i=; i<=n; i++) vis[i]=false;
int sum=;
cnt=;
for(int i=; i<n; i++)
{
scanf("%d",&st[i]);
sum+=st[i];
}
sort(st,st+n,cmp);
for(int i=st[];i<=sum;i++)
{ if(sum%i==)
{
//cout<<"fuck"<<endl;
cnt=sum/i;
l=i;
if(dfs(,))
{
printf("%d\n",l);
break;
}
}
}
}
}

hdu 1455 Sticks的更多相关文章

  1. hdu 1455 Sticks(dfs+剪枝)

    题目大意: George有许多长度相同的木棍,随机的将这些木棍砍成小木条,每个小木条的长度都是整数单位(长度区间[1, 50]).现在George又想把这些小木棒拼接成原始的状态,但是他忘记了原来他有 ...

  2. HDU 1455 Sticks(经典剪枝)

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

  3. uva 215 hdu 1455 uvalive5522 poj 1011 sticks

    //这题又折腾了两天 心好累 //poj.hdu数据极弱,找虐请上uvalive 题意:给出n个数,将其分为任意份,每份里的数字和为同一个值.求每份里数字和可能的最小值. 解法:dfs+剪枝 1.按降 ...

  4. Sticks HDU - 1455 (未完成)

    George took sticks of the same length and cut them randomly until all parts became at most 50 units ...

  5. hdoj 1455 Sticks 【dfs】

    题意:找最短的木棍可以组成的长度, hdoj  1518 的加强版 代码: #include <stdio.h> #include <string.h> #include &l ...

  6. hdu Wooden Sticks

    这题是暴力加贪心,算是一道水题吧!只要把l和w从小到大排个序就行了... #include"iostream" #include"stdio.h" #inclu ...

  7. hdu 1455 N个短木棒 拼成长度相等的几根长木棒 (DFS)

    N根短木棒 能够拼成几根长度相等的长木棒 求长木棒的长度 如果答案不止一种 输出最小的 Sample Input95 2 1 5 2 1 5 2 141 2 3 40 Sample Output65 ...

  8. hdu 1455(DFS+好题+经典)

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

  9. hdu 1145(Sticks) DFS剪枝

    Sticks Problem Description George took sticks of the same length and cut them randomly until all par ...

随机推荐

  1. Session超时处理

    1.web.xml 添加配置: <!-- session超时 --> <filter> <filter-name>sessionFilter</filter- ...

  2. IE8 松散耦合进程框架(Loosely-Coupled IE (LCIE)--特性介绍

    官方介绍:http://blogs.msdn.com/b/ie/archive/2008/03/11/ie8-and-loosely-coupled-ie-lcie.aspx 参考文档:http:// ...

  3. tolua.cast的实用方法

    local name = (tolua.cast(sender, "ccui.Button")):getTitleText()

  4. 教程Xcode 4下编译发布与提交App到AppStore

    地址:http://www.cocoachina.com/bbs/simple/?t55825.html 教程Xcode 4下编译发布与提交App到AppStore 先说一下这个是我在网上看到的一个帖 ...

  5. HDU 1069&&HDU 1087 (DP 最长序列之和)

    H - Super Jumping! Jumping! Jumping! Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format: ...

  6. MySQL关闭过程详解和安全关闭MySQL的方法

    MySQL关闭过程详解和安全关闭MySQL的方法 www.hongkevip.com 时间: -- : 阅读: 整理: 红客VIP 分享到: 红客VIP(http://www.hongkevip.co ...

  7. MYSQL随机抽取查询 MySQL Order By Rand()效率问题

    MYSQL随机抽取查询:MySQL Order By Rand()效率问题一直是开发人员的常见问题,俺们不是DBA,没有那么牛B,所只能慢慢研究咯,最近由于项目问题,需要大概研究了一下MYSQL的随机 ...

  8. js 去掉input标签中的百分号【%】

    parseInt("100%") --100 parseFloat("17%")     --17

  9. svn 切换默认用户名

    2015年1月19日 15:37:30\ 原文: http://www.2cto.com/os/201307/229325.html linux svn切换用户   1. 临时切换   在所有命令下强 ...

  10. 13.python笔记之pyyaml模块

    Date:2016-03-25 Title:13.Python笔记之Pyymal模块使用 Tags:Python Category:Python 博客地址:www.liuyao.me 作者:刘耀 YA ...