这道题是个dfs加剪枝,不剪枝过不了
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define ull unsigned long long
#define uint unsigned int
#define l(x) x<<1
#define r(x) x<<1|1
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
int n;
int stick[100];
int sum;
int vis[100];
int len;
bool cmp(const int& a,const int& b){
return a > b;
}
bool dfs(int itr, int num, int nowLen) {
if (num == 0)
return true;
int knowNum = -1;
for (int i = itr; i < n; i++) {
if (vis[i] || stick[i] == knowNum)
continue;
vis[i] = 1;
if (nowLen + stick[i] < len) {
if (dfs(itr + 1, num, nowLen + stick[i]))
return true;
knowNum = stick[i];
}
else if (nowLen + stick[i] == len) {
if (dfs(0, num - 1, 0))
return true;
knowNum = stick[i];
}
vis[i] = 0;
if (nowLen == 0)
break;
}
return false;
}
int main() {
while (scanf("%d", &n), n != 0) {
sum = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &stick[i]);
sum += stick[i];
}
sort(stick, stick + n, cmp);
for (int i = stick[0]; i <= sum; i++) {
if (sum%i != 0) continue;
ms(vis, 0);
len = i;
if (dfs(0, sum / i, 0)) {
printf("%d\n", i);
break;
}
}
}
return 0;
}