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

题意:你有n个砝码,然后给你每个砝码的质量,s=每个砝码质量之和,让你求【1,s】范围内你不能通过天平称出来的重量

比如  3     9     2    1    

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

解 :首先我们得明白母函数的基础用法  https://blog.csdn.net/dsaghjkye/article/details/79965837

然后我们发现他除了要求所有的和以外,得求出差

#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");
    }
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值