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.

 

输入样例

3
1 2 4
3
9 2 1

输出样例

0
2
4 5

天平既可以承j+k又可以称abs(j-k)
附带ac代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;
int p[N],a[110],c1[N],c2[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n;
    while(cin>>n)
    {
        int sum=0;
        for(int i=1;i<=n;++i)
        {cin>>a[i];sum+=a[i];
        }
        for(int i=0;i<=sum;++i)
        {c1[i]=0,c2[i]=0;}
        c1[0]=1;c1[a[1]]=1;
        for(int i=2;i<=n;++i)
        {
            for(int j=0;j<=sum;++j)
              for(int k=0;k<=a[i]&&k+j<=sum;k+=a[i])
             //k+j<=sum的限制仍成立因为实际情况不存在>sum
            {
                c2[j+k]+=c1[j];
                c2[abs(j-k)]+=c1[j];
                //此处应该取绝对值
            }
            for(int j=0;j<=sum;++j)
            c1[j]=c2[j],c2[j]=0;
        }
        int cnt=0;
        for(int i=1;i<=sum;++i)
        {
            if(!c1[i]) p[cnt++]=i;
        }
        cout<<cnt<<'\n';
        if(cnt)
        {
            for(int i=0;i<cnt;++i)
            cout<<p[i]<<' ';
            cout<<'\n';
            ////注意换行QAQ
        }
    }
    return 0;
}

 

 posted on 2023-01-26 19:21  ruoye123456  阅读(46)  评论(0)    收藏  举报