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

Source

HDU 2007-Spring Programming Contest

Recommend

lcy   |   We have carefully selected several similar problems for you:  2152 2069 2082 1721 1792

#include<stdio.h>
#include<string.h>
#include<math.h>
int num[10005];
int ans[10005];
int c[10005],temp[10005];
int main(){
   int n;
   while(scanf("%d",&n)!=EOF){
      memset(num,0,sizeof(num));
      memset(c,0,sizeof(c));
      memset(ans,0,sizeof(ans));
      memset(temp,0,sizeof(temp));
      int total=0;
      for(int i=0;i<n;i++){
        scanf("%d",&num[i]);
          total+=num[i];
        }
      for(int i=0;i<=num[0];i+=num[0])
        c[i]=1;
      for(int i=1;i<n;i++){
        for(int j=0;j<=total;j++){
            for(int k=0;k+j<=total&&k/num[i]<=1;k+=num[i]){///注意《=1
                temp[j+k]+=c[j];
                temp[(int)fabs(j-k)]+=c[j];//注意此刻应当考虑负值的情况
            }

        }
        for(int ii=0;ii<=total;ii++)
            c[ii]=temp[ii];

      }

    int cnt=0;
    for(int i=1;i<=total;i++){
        if(!c[i]){
            cnt++;
            ans[cnt]=i;
        }
    }
    printf("%d\n",cnt);
    for(int i=1;i<=cnt;i++){
        printf("%d%c",ans[i],i==cnt?‘\n‘:‘ ‘);
    }
   }
   return 0;
}
时间: 2025-01-04 02:32:22

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

*HDU 1709 母函数

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

hdu 1709 求天平不能称重的重量 动态规划

给定一个天平和n个砝码,每个砝码都有一个重量,求出从1到这些砝码的总和之间不能被天平表示的重量.天平可以两端放砝码. 动态规划做法,看网上都是母函数,真心不懂.. dp[i]如果是0表示这些砝码不能称重i重量,如果是1表示能称重i重量. 对于每个砝码,假设当前砝码重量是b[i],那么j从最大值开始,减小到b[i], 如果存在一个dp[j-b[i]]为1,说明当前的j能被组成. j逆着循环的原因是由于每个砝码只有 一个,类似于01背包与完全背包的样子, 如果正向循环会导致每个物品可能使用多次,逆向

hdu 1709 母函数变形

#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX 10010 int c1[MAX], c2[MAX], num[110]; /* 题目大意: 给你n种砝码,从1到n中砝码的重量和其中不能称出多少种重量,输出不能称出的总数和类别*/ int main() { int n; while( scanf("%d", &n)!=EOF ) { memset(n

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+

Square Coins (HDU 1398) ———母函数模板详解

Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7748    Accepted Submission(s): 5238 Problem Description People in Silverland use square coins. Not only they have square shapes but

HDU1171 Big Event in HDU 【母函数】

Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 22835    Accepted Submission(s): 8018 Problem Description Nowadays, we all know that Computer College is the biggest department

hdu 2082 母函数

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2082 找单词 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4035    Accepted Submission(s): 2887 Problem Description 假设有x1个字母A, x2个字母B,..... x26个字母Z,

hdu 1171 Big Event in HDU(母函数)

链接:hdu 1171 题意:这题可以理解为n种物品,每种物品的价值和数量已知,现要将总物品分为A,B两部分, 使得A,B的价值尽可能相等,且A>=B,求A,B的价值分别为多少 分析:这题可以用母函数的思想解,不过求的不是方案数,而是判断尽可能接近总价值的一半的方案是否存在. 也可以用背包思想,每种物品的价值和数量已知,可以将总价值的一半作为容量,求最大价值,也就最接近所求值了 注:数组要开的稍微大一点,否则可能WA #include<stdio.h> #include<strin

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){