HDU 1455

来源:互联网 发布:js和php文件怎么运行 编辑:程序博客网 时间:2024/06/05 03:05

Sticks

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10645 Accepted Submission(s): 3206

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

Source
ACM暑期集训队练习赛(七)

Recommend
lcy

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#define maxn 100030using namespace std;int vis[maxn];int len[maxn];int n;int sum;int ans;bool cmp(int x,int r){    return x>r;}bool dfs(int l, int num,int pos,int  aim){    if(num==aim)return true;    if(l==sum/aim)return dfs(0,num+1,0,aim);    for(int i=pos;i<n;i++)    {        if(!vis[i]&&l+len[i]<=sum/aim)        {            vis[i]=1;            if(dfs(l+len[i],num,i+1,aim))return true;            vis[i]=0;            if(l==0)return false;            while(i+1<n&&len[i+1]==len[i])i++;        }    }    return false;}int main(){    while(scanf("%d",&n)!=EOF)    {        if(n==0)break;        sum=0;        int ans=0;        int maxnum=0;        memset(vis,0,sizeof(vis));        for(int i=0;i<n;i++)        {            scanf("%d",&len[i]);            sum+=len[i];            maxnum=max(maxnum,len[i]);        }        sort(len,len+n);        for(int i=1;i<=sum;i++)        {               if(sum%i==0)            {                memset(vis,0,sizeof(vis));                if(dfs(0,0,0,sum/i))                {                    ans=i;                    break;                }            }        }        printf("%d\n",ans);    }    return 0;}
原创粉丝点击