The Balance
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5741 Accepted Submission(s): 2330
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
/*
HDOJ 1709 母函数
砝码:(1+x)(1+x^2)(1+x^3)(1+x^4)=1+x+x^2+2x^3+2x^4+2x^5+2x^6+2x^7+x^8+x^9+x^10 ,系数即为方案数。
x表示砝码,x的指数表示重量(重量=单个重量*数量)
例称出重量为6的物品:①、1,2,3;②、2,4两种方案。
因为砝码可以两边放,所以加多一条代码temp[abs(j-k)]+=a[j];比如9和4可以称量出重量为5的物品
*/
//abs函数在头文件<stdlib.h>,不在<math.h>
//#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//using namespace std;
// c1是保存各项质量砝码可以组合的数目
// c2是中间量,保存没一次的情况
int c1[10002],c2[10002],a[101];
int main()
{
int i,j,k,n,sum,f;
while(scanf("%d",&n)!=EOF)
{
sum=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));
for(i=0;i<=a[0];i+=a[0])//第一个表达式1 每个只能取0 和 1
c1[i]=1;
for(i=1;i<n;i++)//第i个表达式
{
for(j=0;j<=sum;j++)//第j个变量,就是每个表达式的第j项
{
//k表示的是每个表达式的第j个指数
for(k=0;k<=a[i];k+=a[i]) //每个只能取0 和 1
{
c2[k+j]+=c1[j];
c2[abs(j-k)]+=c1[j];
}
}
for(j=0;j<=sum;j++)
{
c1[j]=c2[j];
c2[j]=0;
}
}
n=0;
for(i=1;i<=sum;i++)
{
if(c1[i]==0)
n++;
}
printf("%d\n",n);
if(n!=0)
{
f=1;
for(i=1;i<=sum;i++)
{
if(c1[i]==0)
{
if(!f)
printf(" ");
else
f=0;
printf("%d",i);
}
}
printf("\n");
}
}
return 0;
}