3 1 2 4 3 9 2 1
0 2 4 5
#include <stdio.h> #include <stdlib.h> #include <string.h> #define maxn 10002 int arr[102], store[maxn]; bool c1[maxn], c2[maxn]; int main() { int n, i, j, k, sum, count; while(scanf("%d", &n) != EOF){ memset(c1, 0, sizeof(c1)); memset(c2, 0, sizeof(c2)); for(i = sum = 0; i < n; ++i){ scanf("%d", arr + i); sum += arr[i]; } c1[0] = c1[arr[0]] = 1; for(i = 1; i < n; ++i){ for(j = 0; j <= sum; ++j) for(k = 0; k <= arr[i] && j + k <= sum; k += arr[i]){ c2[k + j] += c1[j]; c2[abs(k - j)] += c1[j]; } for(k = 0; k <= sum; ++k){ c1[k] = c2[k]; c2[k] = 0; } } for(count = 0, i = 1; i <= sum; ++i){ if(!c1[i]) store[count++] = i; } printf("%d\n", count); if(count) for(i = 0; i < count; ++i) if(i != count - 1) printf("%d ", store[i]); else printf("%d\n", store[i]); } return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> #define maxn 10002 int arr[102], store[maxn]; bool c1[maxn], c2[maxn]; int main() { int n, i, j, k, sum, count; while(scanf("%d", &n) != EOF){ memset(c1, 0, sizeof(c1)); memset(c2, 0, sizeof(c2)); for(i = sum = 0; i < n; ++i) scanf("%d", arr + i); c1[0] = c1[sum = arr[0]] = 1; for(i = 1; i < n; ++i){ for(j = 0; j <= sum; ++j) for(k = 0; k <= arr[i]; k += arr[i]){ c2[k + j] += c1[j]; c2[abs(k - j)] += c1[j]; } sum += arr[i]; for(k = 0; k <= sum; ++k){ c1[k] = c2[k]; c2[k] = 0; } } for(count = 0, i = 1; i <= sum; ++i){ if(!c1[i]) store[count++] = i; } printf("%d\n", count); if(count) for(i = 0; i < count; ++i) if(i != count - 1) printf("%d ", store[i]); else printf("%d\n", store[i]); } return 0; }
HDU1709 The Balance 【母函数】,布布扣,bubuko.com
原文:http://blog.csdn.net/chang_mu/article/details/38145895