HDU1709 The Balance 【母函数】

The Balance

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 5463    Accepted Submission(s): 2214

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.

Sample Input

3
1 2 4
3
9 2 1

Sample Output

0
2
4 5

题意:给定n个砝码的重量,总质量为sum,问在1~sum中有多少个重量不能被称出来。

题解:母函数的应用,需要注意sum的值。

版本一:171ms

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxn 10002

int arr[102], store[maxn];
bool c1[maxn], c2[maxn];

int main()
{
    int n, i, j, k, sum, count;
    while(scanf("%d", &n) != EOF){
        memset(c1, 0, sizeof(c1));
        memset(c2, 0, sizeof(c2));
        for(i = sum = 0; i < n; ++i){
            scanf("%d", arr + i);
            sum += arr[i];
        }
        c1[0] = c1[arr[0]] = 1;
        for(i = 1; i < n; ++i){
            for(j = 0; j <= sum; ++j)
                for(k = 0; k <= arr[i] && j + k <= sum; k += arr[i]){
                    c2[k + j] += c1[j];
                    c2[abs(k - j)] += c1[j];
                }
            for(k = 0; k <= sum; ++k){
                c1[k] = c2[k]; c2[k] = 0;
            }
        }
        for(count = 0, i = 1; i <= sum; ++i){
            if(!c1[i]) store[count++] = i;
        }
        printf("%d\n", count);
        if(count)
            for(i = 0; i < count; ++i)
                if(i != count - 1) printf("%d ", store[i]);
                else printf("%d\n", store[i]);
    }
    return 0;
}

版本二:78ms

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxn 10002

int arr[102], store[maxn];
bool c1[maxn], c2[maxn];

int main()
{
    int n, i, j, k, sum, count;
    while(scanf("%d", &n) != EOF){
        memset(c1, 0, sizeof(c1));
        memset(c2, 0, sizeof(c2));
        for(i = sum = 0; i < n; ++i)
            scanf("%d", arr + i);
        c1[0] = c1[sum = arr[0]] = 1;
        for(i = 1; i < n; ++i){
            for(j = 0; j <= sum; ++j)
                for(k = 0; k <= arr[i]; k += arr[i]){
                    c2[k + j] += c1[j];
                    c2[abs(k - j)] += c1[j];
                }
            sum += arr[i];
            for(k = 0; k <= sum; ++k){
                c1[k] = c2[k]; c2[k] = 0;
            }
        }
        for(count = 0, i = 1; i <= sum; ++i){
            if(!c1[i]) store[count++] = i;
        }
        printf("%d\n", count);
        if(count)
            for(i = 0; i < count; ++i)
                if(i != count - 1) printf("%d ", store[i]);
                else printf("%d\n", store[i]);
    }
    return 0;
}

HDU1709 The Balance 【母函数】,布布扣,bubuko.com

时间: 2024-10-13 08:43:34

HDU1709 The Balance 【母函数】的相关文章

HDU1709 The Balance【母函数】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1709 题目大意: 现有一个天平和不同质量的砝码,每个质量的砝码只有一个,且令S为这些砝码质量的总和. 现在,从1~S中,找到用这些砝码不能称出的物品质量. 思路: 每种砝码既可以放在右盘,又可以放在左盘,如果视作左物右码的话,砝码放在左边取减号, 放在右边取加号.最终将指数看为正数,求得的系数要求绝对值.放在同一端c2[j+k] += c1[j]: 放在不同端c2[abs(k-j)] += c1[

使用母函数方法解答几个问题

由于在货币组合的题目中使用了母函数的方法,就顺便搜索一些资料和练习,加深自己的理解. 杭电ACM课件中简要介绍了母函数的方法,并给出了几道相关的题目. 1. HDU1398 Square Coins 使用指定的货币来组合一个给定的目标值,问一共有多少种可能.这道题和我们以前的货币组合题目大同小异,代码如下: 1 #include <stdio.h> 2 3 #define MAXITEMS 2048 4 5 int v[17] = {0}; 6 int c1[MAXITEMS]; 7 int

组合数学 - 母函数的运用 --- hdu 1709 :The Balance

The Balance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5706    Accepted Submission(s): 2311 Problem Description Now you are asked to measure a dose of medicine with a balance and a number o

hdu1709(母函数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1709 题意: 给你一个n,表示n个物品,下面有n个数,表示n个物品的重量,然后进行称量,每个物品只有一件,看不能称出的价值有几个. 分析:母函数裸题,再处理一下相减的情况. #include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <algorith

hdu 1709 The Balance(母函数)

题意: 有一个天平.有N个砝码.重量分别是A1...AN. 问重量[1..S]中有多少种重量是无法利用这个天平和这些砝码称出来的. S是N个砝码的重量总和. 思路: 对于每一个砝码来说,有三种:不放,放左盘,放右盘. 额,,母函数和DP其实核心一样,,,, 看代码,, 代码: int n; int aa[105]; bool a[10005], b[10005]; int temp[10005]; int main(){ while(scanf("%d",&n)!=EOF){

HDU 1709 母函数天平问题 可出现减法的情况 The Balance

The Balance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6652    Accepted Submission(s): 2730 Problem Description Now you are asked to measure a dose of medicine with a balance and a number o

Hdu 1709 The Balance

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1709 题意: 给N个整数,每个数只能使用一次.将他们组合起来,最后看在1~sum(a[1]..a[N])这些数里有多少数是这N个数组合不出来的. 先输出这些数的个数,再将这些数输出来.如果个数是0,那么不需要输出数. 案例分析: input: 3 1 2 4 output: 0 -->1(||4-1-2) , 2(||4-2) , 1+2(||4-1) , 4  , 1+4 , 2+4 , 1+2+

母函数专题

1.Hdu 2082 找单词 题意:单词A-Z具有1-26的价值,现有字母A-Z的个数num[i],求问在不超过价值为五十的情况下,有多少种字母的组合数. 思路:用指数代表价值,价值又为数组的下标:用系数代表组成该价值的方案数,方案数为数组中存的值. 1 #include<iostream> 2 #include<memory.h> 3 using namespace std; 4 int re[55]; 5 int p1[55]; 6 int p2[55]; 7 int xx[2

杭电 HDU ACM 1709 The Balance

The Balance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6304    Accepted Submission(s): 2592 Problem Description Now you are asked to measure a dose of medicine with a balance and a number