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
给你一个天平和一些砝码,砝码都只有一个,问你在1到S之间有没有不能用这些砝码称出来的重量,S是这些砝码加起来的重量。如果有,先输出不能称的数量,再输出是哪些重量,如果没有,就直接输出0。
因为天平有左右之分,如果规定左天平是正值,那么右天平就是负值。相减取绝对值。
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<math.h>
#include<iostream>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<queue>
typedef long long ll;
using namespace std;
const int N=1e5+5;
const int INF=0x3f3f3f3f3f;
int v[N],n[N],a[N],b[N],p;
int sa[N];
int main()
{
int k;
while(scanf("%d",&k)!=EOF)
{
p=0;
for(int i=0;i<k;i++)
{
scanf("%d",&v[i]);
n[i]=1;
p+=v[i];
}
memset(a,0,sizeof(a));
a[0]=1;
for(int i=0;i<k;i++)
{
memset(b,0,sizeof(b));
for(int j=0;j<=n[i]&&j*v[i]<=p;j++)
{
for(int h=0;h+j*v[i]<=p;h++)
{
b[h+j*v[i]]+=a[h];
b[abs(h-j*v[i])]+=a[h];
}
}
memcpy(a,b,sizeof(b));
}
int g=0;
for(int i=1;i<=p;i++)
{
if(!a[i])
sa[g++]=i;
}
printf("%d\n",g);
if(g==0)
continue;
for(int i=0;i<g-1;i++)
{
printf("%d ",sa[i]);
}
printf("%d\n",sa[g-1]);
}
return 0;
}