hdu1790(母函数的简单应用)

题意:给n个砝码,每个砝码的质量为Hi,[1,S]内无法用这些砝码称出的质量
这里砝码是有限的,母函数为(1 + x^h1)(1 + x^h2)(1 + x^h3).........
还有一点要注意的是砝码可以放在天平的两边,比如2,3可以称得1和5

代码如下:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define inf 0x7fffffff
#define eps 1e-9
#define N 10005
#define pi acos(-1.0)
#define P system("pause")
using namespace std;
int c1[N],c2[N];
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
    int n;
    int a[105];
    while(scanf("%d",&n) != EOF)
    {
        int i, j,sum = 0;
        for(i = 0 ; i < n;i++)
        {
            scanf("%d",&a[i]);
            sum += a[i];
        }
        memset(c1,0,sizeof(c1));
        memset(c2,0,sizeof(c2));
        c1[0] = 1;
        for(i = 0; i < n; i++)
        {
            for(j = 0; j <= sum; j++)
            {
                   c2[j] += c1[j];
                   if(j+a[i] <= sum)
                        c2[j+a[i]] += c1[j];
                    c2[abs(j-a[i])] += c1[j];
            }
            for(j = 0; j <= sum; j++)
            {
                c1[j] = c2[j];
                c2[j] = 0;
            }
        }
        int res = 0;
        for(i = 1;i <= sum; i++)
            if(c1[i] == 0)
                  res++;
        printf("%d\n",res);
        if(!res) continue;
        int flag = 1;
        for(i = 1; i <= sum; i++)
            if(c1[i] == 0)
            {
                if(flag){
                    printf("%d",i);
                    flag = 0;
                }
                else printf(" %d",i);

            }
        printf("\n");
    }
    return 0;
}


你可能感兴趣的:(acm之母函数)