https://www.acwing.com/problem/content/description/169/
剪枝剪枝还是剪枝
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 100;
int n, len;
int a[maxn], used[maxn];
bool dfs(int d, int now, int num, int goal){
if(d == len / goal){
return true;
}
if(now == goal) return dfs(d + 1, 0, 0, goal);
int fail = 0;
for(int i = num + 1; i <= n; ++i){
if(used[i] || now + a[i] > goal || fail == a[i]) continue;
used[i] = 1;
if(dfs(d, now + a[i], i, goal)) return true;
fail = a[i];
used[i] = 0;
if(!now || now + a[i] == goal) return false;
}
return false;
}
bool cmp(int x, int y){
return x > y;
}
ll read(){ ll s=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*f; }
int main(){
while(1){
len = 0;
n = read();
if(!n) break;
for(int i=1;i<=n;++i) a[i] = read(), len += a[i];
sort(a + 1, a + 1 + n, cmp);
for(int i=a[1]; i<=len; ++i){
if(len % i == 0){
memset(used, 0, sizeof(used));
if(dfs(0, 0, 0, i)){
printf("%d
",i);
break;
}
}
}
}
return 0;
}