# HDU - 1709 The Balance (母函数)

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.
InputThe 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.
OutputFor 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

1： 1

2:  2

3 ： 1+2

4  ：不行

5 ：不行

6:   9-1-2

7:   9-2

8：  9-1

10： 9+1

11 ：9+2

12： 9+1+2

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<math.h>
#include<queue>
#include<map>
#include<vector>
#include<set>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
map<int,int>mapp;//用map存，看是否有相同质量砝码已经出现过
int c1[11330],c2[11330];
int num[110],a[110];//num是砝码个数，a是砝码质量
int main()
{
int n,b;
while(~scanf("%d",&n))
{
int tot=0,ans;
mapp.clear();
memset(num,0,sizeof(num));
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++)
{
scanf("%d",&b);
if(!mapp[b])//如果这个质量的砝码第一次出现，把他存入
{
mapp[b]=++tot;
a[tot]=b;
num[tot]++;
}
else//第二次出现，个数增加
{
int u=mapp[b];
num[u]++;
}
}
memset(c2,0,sizeof(c2));
memset(c1,0,sizeof(c1));
for(int i=0;i<=num[1];i++) //第1个砝码能称出的所有质量
{
c1[i*a[1]]=1;
}
ans=num[1]*a[1];//当前能得到的最大质量
for(int i=2;i<=tot;i++)//1共tot种砝码
{
for(int j=0;j<=ans;j++)//从 0到上一种砝码为止能得到的最大值
{
for(int k=0;k<=num[i];k++) //第i种砝码的个数
{
c2[j+(k*a[i])]+=c1[j]; // 原来砝码+增加的砝码 =物
if(j>(k*a[i]))
c2[j-(k*a[i])]+=c1[j];//原来砝码= 物+增加砝码
else
c2[(k*a[i])-j]+=c1[j]; //增加砝码 =物+原来砝码

}
}
ans+=num[i]*a[i]; //最大值增加
for(int j=0;j<=ans;j++) //c2值赋给c1
c1[j]=c2[j];
memset(c2,0,sizeof(c2));//c2 清空
}
int kk[11000];
int uu=0;
for(int i=0;i<=ans;i++)
{
if(!c1[i]) kk[uu++]=i; //如果为0代表不能称得
}
printf("%d\n",uu);
for(int i=0;i<uu;i++)
{
if(i!=0) printf(" ");
printf("%d",kk[i]);
}
if(uu)
printf("\n");
}
}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120