HDU - 1709 The Balance(母函数)
2016-11-19 10:37
477 查看
题目:
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
Sample Output
母函数方法
代码:
值得注意的是,数组必须开这么大,少一点都不行,因为100个砝码可能都是100,那样的话sum就是10000
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
母函数方法
代码:
#include<stdio.h> int num[20001]; int main() { int n, sum, k, r; for (int i = 0; i < 20001; i++)num[i] = 0; while (scanf("%d",&n)!=EOF) { sum = 0; num[10000] = 1; while (n--) { scanf("%d", &k); for (int i = 10000 - sum; i <= 10000 + sum; i++)if (num[i])num[i - k] = 1; for (int i = 10000 + sum; i >= 10000 - sum; i--)if (num[i])num[i + k] = 1; sum += k; } r = 0; for (int i = 10000; i <= 10000 + sum; i++)r += (num[i] == 0); printf("%d\n", r); if (r == 0)for (int i = 10000; i <= 10000 + sum; i++)num[i] = 0; else for (int i = 10000; i <= 10000 + sum; i++) { if (num[i])num[i] = 0; else { printf("%d", i - 10000); r--; printf(r ? " " : "\n"); } } for (int i = 10000 - sum; i <= 10000; i++)num[i] = 0; } return 0; }
值得注意的是,数组必须开这么大,少一点都不行,因为100个砝码可能都是100,那样的话sum就是10000
相关文章推荐
- hdu 1709 The Balance(母函数)
- HDU 1709 The Balance【负次方母函数】
- 母函数 HDU 1709 The Balance
- 【HDU】1709 - The Balance(母函数)
- HDU 1709 The Balance 母函数
- HDU1709 The Balance【母函数】
- 组合数学 - 母函数的运用 --- hdu 1709 :The Balance
- HDU1709 The Balance,母函数套题
- HDU1709 The Balance(母函数,有限制的)
- hdu 1709 The Balance (母函数)
- HDU 1709 The Balance(母函数)
- HDU1709_HDU 1709 The Balance(母函数/生成函数)
- HDU 1709 The Balance【特殊母函数】
- hdu 1709 The Balance(母函数)
- HDU-1709-The Balance 母函数
- HDU 1709 The Balance (母函数)
- HDU 1709 The Balance [母函数]
- HDU 1709-The Balance(母函数的应用)
- [母函数]HDU 1709 The Balance
- HDU 1709 母函数天平问题 可出现减法的情况 The Balance