【HDOJ】1709 The Balance

时间:2021-04-27 15:17:18

母函数,指数可以为1也可以为-1,扩大指数加消减发现TLE,于是采用绝对值就过了。

 #include <stdio.h>
#include <string.h> #define MAXNUM 10001 int c1[MAXNUM], c2[MAXNUM];
int w[]; int myabs(int x) {
return x< ? -x:x;
} int main() {
int n, sum, tmp;
int i, j, k; while (scanf("%d", &n) != EOF) {
sum = ;
for (i=; i<=n; ++i) {
scanf("%d", &w[i]);
sum += w[i];
}
memset(c1, , sizeof(c1));
memset(c2, , sizeof(c2));
c1[] = ;
c1[w[]] = ;
tmp = w[];
for (i=; i<=n; ++i) {
for (j=; j<=tmp; ++j)
for (k=; k<=w[i]; k+=w[i]) {
c2[k+j] += c1[j];
c2[myabs(k-j)] += c1[j];
}
tmp += w[i];
for (j=; j<=tmp; ++j) {
c1[j] = c2[j];
c2[j] = ;
}
}
k = ;
for (i=; i<=sum; ++i) {
if (c1[i] == )
c2[k++] = i;
}
printf("%d\n", k);
for (i=; i<k; ++i)
if (i)
printf(" %d", c2[i]);
else
printf("%d", c2[i]);
if (k)
printf("\n");
} return ;
}