The Balance hdu1709(母函数)

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值