The Balance - http://acm.hdu.edu.cn/showproblem.php?pid=1709
代码
#include<bits/stdc++.h>
using namespace std;
#define MXN 20001
int main(){
int N, S, c1[MXN], c2[MXN], ws[100];
// 指针类型的常量——指针常量
int * const p1 = c1 + (MXN+1)/2, * const p2 = c2 + (MXN+1)/2;
while(scanf("%d", &N) == 1){
memset(c1, 0, sizeof c1);
memset(c2, 0, sizeof c2);
S = 0;
for(int i = 0; i < N; i++) scanf("%d", ws+i);
p1[0] = 1, p1[ws[0]] = p1[-ws[0]] = 1;
S = ws[0];
for(int i = 1; i < N; i++){
for(int j = -S; j <= S; j++){
for(int k = -ws[i]; k <= ws[i]; k += ws[i]){
p2[j+k] += p1[j];
}
}
S += ws[i];
for(int j = -S; j <= S; j++) p1[j] = p2[j], p2[j] = 0;
}
int ans = S;
for(int i = -S; i < 0; i++) if(p1[i] || p1[-i]) ans--;
printf("%d\n", ans);
if(ans == 0) continue;
for(int i = 0; i <= S; i++){
if(p1[i]==0 && p1[-i]==0){
printf("%d", i);
ans--;
if(ans == 0){
printf("\n");
break;
}
printf(" ");
}
}
}
return 0;
}