HDU1455Sticks(dfs剪枝)
2015-12-28 23:13
357 查看
Sticks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Problem 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
/********************************************************************* > File Name: aa.cpp > Author: Tailless > Mail: xihuanjin1@gmail.com ********************************************************************/ #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <string> #include <cstdlib> #include <iomanip> #include <vector> #include <ctime> #include <cmath> #include <stack> #include <queue> #include <list> #include <map> #include <set> #define C 0.57721566490153286060651209 #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; typedef long long LL; const int INF=0x3f3f3f3f; const double eps=1e-10; const double PI=acos(-1.0); const int maxn=1000009; const int maxm=1000009; int stick[100],vis[100]; int n; int cmp(int a,int b) { return a>b; } int dfs(int len,int s,int num) { int i; if(s==0&&num==0) return len; if(s==0) s=len; for(i=0;i<n;i++) { if(vis[i]==1) continue; if(s>=stick[i]) { vis[i]=1; if(dfs(len,s-stick[i],num-1)) { return len; } vis[i]=0; if(stick[i]==s||s==len) break; while(stick[i]==stick[i+1]) i++; } } return 0; } int main() { while(~scanf("%d",&n),n) { int sum,len,ans; sum=0; for(int i=0;i<n;i++) { scanf("%d",&stick[i]); sum+=stick[i]; } sort(stick,stick+n,cmp); for(len=stick[0];len<=sum;len++) { memset(vis,0,sizeof(vis)); if(sum%len==0) { if(dfs(len,0,n)) break; } } printf("%d\n",len); } return 0; }
相关文章推荐
- Android获取屏幕宽高信息
- 又是一周-AJAX(三)
- 对大盘的一些看法
- EditText 代码设置光标颜色(反射)
- 20150916谱聚类.md(图片版)
- 数学中的术语与常用表达
- BZOJ 2190 - 欧拉函数的应用(数据范围不同 -> 做法不同 -> 启示)
- Linux下USB转串口的驱动
- Python笔记-几种取整方式
- (转)(contant的一些用法)
- AngularJS中Directive间交互实现合成
- Mac & Xcode 技巧
- linux:shell:tree
- 在线生成简单二维码图片
- Android之SwipeRefreshLayout
- js简单对象
- 线性拟合——从最大似然估计到平方误差到huber loss
- Python笔记-均值列表
- 马士兵Java课堂笔记-6-容器Collection接口remove方法中判断是否相等并删除问题
- 南京大数据技术Meetup第四次活动