hdu 1709 母函数变形

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #define MAX 10010
  5. int c1[MAX], c2[MAX], num[110];
  6. /* 题目大意: 给你n种砝码,从1到n中砝码的重量和其中不能称出多少种重量,输出不能称出的总数和类别*/
  7. int main()
  8. {
  9. int n;
  10. while( scanf("%d", &n)!=EOF )
  11. {
  12. memset(num, 0, sizeof(num) );
  13. memset(c1, 0, sizeof(c1) );
  14. memset(c2, 0, sizeof(c2) );
  15. int total = 0;
  16. for(int i=0; i<n; i++) //重量输入
  17. {scanf("%d", &num[i]); total+=num[i];}
  18. //for(int i=0; i<=total; i++)
  19. //c1[i] = c2[i] = 0;
  20. c1[0] = 1; //第一个括号,重量为0的
  21. for(int i=0; i<n; i++)
  22. {
  23. for(int j=0; j<=total; j++)
  24. {
  25. c2[j] |= c1[j]; //否能称得,自己砝码的重量
  26. if(num[i] + j <=total) //重量和是否大于总重量
  27. c2[j+num[i]] |= c1[j]; //重量和
  28. c2[abs(j-num[i])] |= c1[j]; //重量差
  29. }
  30. for(int j=0; j<=total; j++) //一次循环赋值
  31. {c1[j] = c2[j]; c2[j] = 0;}
  32. }
  33. int count = 0;
  34. for(int i=0; i<=total; i++) //查找能否称得的重量
  35. {
  36. if(!c1[i])
  37. c2[count++] = i;
  38. }
  39. printf("%d\n", count);
  40. for(int i=0; i<count-1; i++)
  41. printf("%d ", c2[i]);
  42. if(count) printf("%d\n", c2[count-1]);
  43. }
  44. return 0;
  45. }
  46. /*
  47. Problem Description
  48. 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.
  49. Input
  50. 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.
  51. Output
  52. 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.
  53. Sample Input
  54. 3
  55. 1 2 4
  56. 3
  57. 9 2 1
  58. Sample Output
  59. 0
  60. 2
  61. 4 5
  62. */

来自为知笔记(Wiz)

附件列表

时间: 2024-10-13 15:17:51

hdu 1709 母函数变形的相关文章

*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 母函数天平问题 可出现减法的情况 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+

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 2082 找单词

找单词 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4567    Accepted Submission(s): 3271 Problem Description 假设有x1个字母A, x2个字母B,..... x26个字母Z,同时假设字母A的价值为1,字母B的价值为2,..... 字母Z的价值为26.那么,对于给定的字母,可以找

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