The Balance
Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 14 Accepted Submission(s) : 11
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Now you are asked to measure a dose of medicine with a balance and a number of weights. Certainly it is not always achievable. So you should find out the qualities which cannot be measured from the range [1,S]. S is the total quality of all the weights.
Input
The input consists of multiple test cases, and each case begins with a single positive integer N (1<=N<=100) on a line by itself indicating the number of weights you have. Followed by N integers Ai (1<=i<=N), indicating the quality of each weight where 1<=Ai<=100.
Output
For each input set, you should first print a line specifying the number of qualities which cannot be measured. Then print another line which consists all the irrealizable qualities if the number is not zero.
Sample Input
3 1 2 4 3 9 2 1
Sample Output
0 2 4 5
#include<cstdio> #include<cstring> #include<cmath> #include<string> #include<vector> #include<queue> #include<stack> #include<list> #include<set> #include<map> #include<algorithm> using namespace std; #define inf 1<<29 int c[10010], c1[10010], c2[10010]; int main() { int n,Max,Nmax; //freopen("F:\\1.txt", "r", stdin); while (scanf("%d", &n) != EOF) { int a[105]; Max = 0; Nmax = 0; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); Max += a[i]; } for (int i = 0; i < 10010; i++) c[i] =c1[i] =c2[i] =0; c2[0] = 1; c2[a[0]] = 1; for (int i = 1; i < n; i++) { for (int j = 0; j <= Max; j++) { for (int k = 0; k + j <= Max&&k <= a[i]; k += a[i]) { c1[k + j] += c2[j]; } } for (int j = 0; j <= Max; j++) { c2[j] = c1[j]; c1[j] = 0; } } for (int i = 0; i <= Max; i++) c1[i] = c2[i]; for (int i = Max; i >= 0; i--) { if (c1[i]) { for (int j = i; j >= 1; j--) { if (c1[j]) c2[i - j]=1; } } } int cnt = 0; for (int i = 0; i <= Max; i++) { if (!c2[i])c[cnt++] = i; } if (cnt == 0) printf("0\n"); else { printf("%d\n", cnt); for (int i = 0; i < cnt-1; i++) printf("%d ", c[i]); printf("%d\n", c[cnt - 1]); } } return 0; }