HDU_1709 The Balence (生成函数)

  /*用砝码秤重量,按照左物右码的话,砝码的值可以取负。因为砝码最多就一个,
所以不许要一般模板里的k 那一重循环,直接去砝码个数为0, 1的两种情况就行。开
始把重量取负时那种情况想错了,WA了好几次。。。T_T
*/

//My Code:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 10007;

int c1[N], c2[N];
int val[107];

int main() {
//freopen("data.in", "r", stdin);

int n, s, i, j, tmp, cnt;
while(~scanf("%d", &n)) {
memset(c1, 0, sizeof(c1));
memset(c2, 0, sizeof(c2));
memset(val, 0, sizeof(val));

for(s = 0, i = 1; i <= n; ++i) {
scanf("%d", &val[i]);
s += val[i];
}
c1[0] = 1; c1[val[1]] = 1;
for(i = 2; i <= n; ++i) {
for(j = 0; j + val[i] <= s; ++j) {
if(c1[j] != 0) {
c2[j] += c1[j];
c2[j+val[i]] += c1[j];
            tmp = j - val[i];   //这里WA了好几次。。。
tmp = tmp > 0 ? tmp : -1*tmp;
c2[tmp] += c1[j];
}
}
for(j = 0; j <= s; ++j) {
c1[j] = c2[j]; c2[j] = 0;
}
}
for(cnt = 0, i = 0; i <= s; ++i) {
if(!c1[i]) cnt++;
}
printf("%d\n", cnt);
for(j = 0, i = 0; i <= s && j <= cnt; ++i) {
if(!c1[i]) {
printf("%d", i); ++j;
if(j == cnt) putchar('\n');
else putchar('');
}
}
}
return 0;
}



posted @ 2011-11-25 20:13  AC_Von  阅读(263)  评论(0编辑  收藏  举报