POJ 1011: Sticks
题目描述
输入
输出
样例输入
样例输出
#include <iostream> #include <string> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <map> #define range(i,a,b) for(int i=a;i<=b;++i) #define LL long long #define rerange(i,a,b) for(int i=a;i>=b;--i) #define fill(arr,tmp) memset(arr,tmp,sizeof(arr)) using namespace std; int n,len,stick[65]; bool flag,vis[65]; void init(){} bool cmp(int a,int b){ return a>b; } void dfs(int d,int now,int u){ if(flag)return; if(!now){ int pos=0; while(vis[pos])++pos; vis[pos]=true; dfs(d+1,stick[pos],pos+1); vis[pos]=false; return; } if(now==len){ if(d==n)flag=true; else dfs(d,0,0); return; } range(i,u,n-1) if(!vis[i]&&now+stick[i]<=len){ if(!vis[i-1]&&stick[i]==stick[i-1])continue; vis[i]=true; dfs(d+1,now+stick[i],i+1); vis[i]=false; } } void solve(){ while(cin>>n,n){ int sum=0;flag=false; range(i,0,n-1){ cin>>stick[i]; sum+=stick[i]; } sort(stick,stick+n,cmp); for(len=stick[0];len<sum;++len) if(!(sum%len)) { fill(vis, 0); dfs(0, 0, 0); if (flag)break; } cout<<len<<endl; } } int main() { init(); solve(); return 0; }