-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(poj 1011)Sticks.cpp
73 lines (64 loc) · 1.43 KB
/
(poj 1011)Sticks.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
int part[65], n, res, sum, sticks;
bool vis[65];
bool cmp(int a, int b)
{
return a>b;
}
bool dfs(int cnt, int remain, int pos)
{
if(cnt==sticks) return true;
for(int i = pos+1 ; i < n ; i++)
{
if(vis[i]) continue;
if(remain==part[i])//可以凑成一个组合
{
vis[i] = true;
if( dfs(cnt+1, res, -1) ) return true;
vis[i] = false;
return false;
}
else if(remain>part[i])//仍有剩余
{
vis[i] = true;
if( dfs(cnt,remain-part[i], i) ) return true;
vis[i] = false;
if(remain==res) return false;
while(part[i]==part[i+1]) i++;
}
}
return false;
}
void load()//输入并处理数据
{
sum = 0;
for(int i = 0 ; i < n;i ++)
{
int p;
cin>>p, part[i] = p, sum += p;
}
sort(part, part+n, cmp);
}
int main()
{
freopen("data.txt", "r",stdin);
while(cin>>n && n)
{
load();
for(res = part[0];res < sum;res++)
{
if(sum%res==0)
{
memset(vis, false, sizeof(vis));
sticks = sum/res;
if(dfs(1, res, -1)) break;
}
}
cout<<res<<endl;
}
return 0;
}