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<cmath>
#include<cstring>
#include<algorithm>
#define W 10005
using namespace std;
int b[W],c[W];
int main()
{
int a[],d[];
int N,i,t,n,j,len,r,p,k;
while(scanf("%d",&N)!=EOF)
{
for(i=;i<N;i++)
scanf("%d",&a[i]);
sort(a,a+N);
d[]=t=a[];n=;j=;
for(i=;i<N;i++)
{
if(a[i]==t){n++;}
else {d[j]=n;n=;a[j++]=t;t=a[i];}
}
a[j]=t;
d[j]=n;
memset(b,,sizeof(b));
memset(c,,sizeof(c));
for(i=;i<=d[]*a[];i+=a[])
c[i]=;len=d[]*a[];
for(i=;i<=j;i++)
{
for(p=;p<=len;p++)
for(k=;k<=d[i]*a[i];k+=a[i])
{
b[p+k]+=c[p];
if(k!=&&p!=)
{r=fabs(p-k);
b[r]+=c[p];}
}
len+=d[i]*a[i];
for(k=;k<=len;k++)
{c[k]=b[k];b[k]=;}
}
n=;
for(i=,k=;i<=len;i++)
if(c[i]==) b[k++]=i;
if(k==)printf("0\n");
else {
printf("%d\n",k);
for(i=;i<k;i++)
{ if(i!=)printf(" ");
printf("%d",b[i]);
}
printf("\n");
}
}
return ; }