母函数 hdu 1709

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. 

Sample Input

3
1 2 4
3
9 2 1

Sample Output

0
2
4 5

你被要求称出一些质量的药品。现有一个天平和一些不同质量的砝码,每个质量的砝码只有一个,且令S为这些砝码质量的总和。现在,请你找出从1到S中,用这些砝码不能称出的药品的质量。

输入:

输入包括多组数据,每组数据有两行。第一行是一个正整数N(1<=N<=100),即砝码的个数,第二行有N个整数Ai(1<=i<=N),分别为这N个砝码的质量且1<=Ai<=100。

输出:

对于每组数据,请你首先输出一个整数,即不能称出的药品质量。如果第一行输出的质量不为0,第二行输出这些不能称出的质量的值。

输入样例:

3

1 2 4

3

9 2 1

输出样例:

0

2

4 5




思路,天平左右都能放砝码,所以要考虑减小的情况,依旧是套模版

转化为(1+x^a[1])*(1+x^a[2])*(1+x^a[3])……(1+x^a[n])

输出要注意一下,这题坑爹,不能循环带空格的那种输出,只有数之间有空格,最后一个数后面没空格,没空格,没空格



#include <iostream>
#include <string.h>
using namespace std;
int a[120],c1[10050],c2[10050];
int main()
{
    int n,i,j,k;
    while(cin>>n)
    {   int sum=0;
        for(int i=1;i<=n;i++)
            {cin>>a[i];sum+=a[i];}
            memset(c1,0,sizeof(c1));
            memset(c2,0,sizeof(c2));

        for(i=0;i<=a[1];i+=a[1])
            c1[i]=1;

        for(i=2;i<=n;i++)
        {
            for(j=0;j<=sum;j++)
                for(k=0;j+k<=sum&&k<=a[i];k+=a[i])
            {
                if(k>=j)//两种减法,一种加法
                    c2[k-j]+=c1[j];
                else
                    c2[j-k]+=c1[j];
                c2[j+k]+=c1[j];
            }
            for(j=0;j<=sum;j++)
            {
                c1[j]=c2[j];
                c2[j]=0;
            }
        }
        int f=0;
        for(i=1;i<=sum;i++)
            if(c1[i]==0)
                f=1;
        if(f==0)cout<<"0"<<endl;
        else
        {
            int ans=0;
            for(i=1;i<=sum;i++)
            if(c1[i]==0){j=i;break;}
            for(i=1;i<=sum;i++)
            if(c1[i]==0)ans++;
            cout<<ans<<endl;
            cout<<j;//先输出第一个
            for(i=j+1;i<=sum;i++)
            if(c1[i]==0)cout<<" "<<i;//最后一个没空格,坑爹!!!
            cout<<endl;
        }
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值