点击打开题目
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7474 Accepted Submission(s): 3101
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
Source
HDU 2007-Spring Programming Contest
同样是天平用母函数的问题,思路同上一篇博客。
代码如下:
#include <cstdio>
#include <cstring>
#define CLR(a) memset(a,0,sizeof(a))
int abs(int x)
{
return x < 0 ? -x : x;
}
int main()
{
int n;
int sum;
int w[111];
int c[10011];
int t[10011];
int ant;
int num[10011];
while (~scanf ("%d",&n))
{
sum = 0;
ant = 0;
CLR(c);
CLR(t);
for (int i = 1 ; i <= n ; i++)
{
scanf ("%d",&w[i]);
sum += w[i];
}
c[0] = c[w[1]] = 1;
for (int i = 2 ; i <= n ; i++)
{
for (int j = 0 ; j <= sum ; j++)
for (int k = 0 ; k <= w[i] && k+j <= sum ; k += w[i])
{
t[j+k] += c[j];
t[abs(j-k)] += c[j];
}
for (int j = 0 ; j <= sum ; j++)
{
c[j] = t[j];
t[j] = 0;
}
}
for (int i = 1 ; i <= sum ; i++)
{
if (c[i] == 0)
num[ant++] = i;
}
if (ant)
{
printf ("%d\n",ant);
for (int i = 0 ; i < ant-1 ; i++)
printf ("%d ",num[i]);
printf ("%d\n",num[ant-1]);
}
else
printf ("0\n");
}
return 0;
}