HDOJ-1709 The Balance(母函数)
2013-04-28 12:29
387 查看
The Balance
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4327 Accepted Submission(s): 1739
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
3
1 2 4
3
9 2 1
[align=left]Sample Output[/align]
0
2
4 5
[align=left]Source[/align]
HDU 2007-Spring Programming Contest
题意:
给出一些砝码,可以放在天秤的两边,问有[1,sum]中有哪些重量是不可称出来的
题解:
母函数,这里比较特殊的一点是砝码可以放在天枰的左右两端,我们可以在c2[j+k]+=c1[j]
后加多一句c2[abs(j-k)]+=c[j]...即可,假设原来的砝码都放在右端,则可以把新加的砝码放在左端,得到新重量。
#include <cstdio> #include <iostream> #include <cmath> using namespace std; int c1[10005], c2[10005]; int main() { int n, sum; int a[105]; while(~scanf("%d", &n)) { sum = 0; for(int i = 0; i < n; ++i) { scanf("%d", &a[i]); sum += a[i]; } memset(c1, 0, (sum+1)*sizeof(int)); memset(c2, 0, (sum+1)*sizeof(int)); c1[0] = c1[a[0]] = 1; int end = a[0]; for(int i = 2; i <= n; ++i) { for(int j = 0; j <= end; ++j) { for(int k = 0; k <= a[i-1] && j+k <= sum; k += a[i-1]) { c2[j+k] += c1[j]; c2[abs(j-k)] += c1[j]; } } end += a[i-1]; for(int j = 0; j <= end; ++j) { c1[j] = c2[j]; c2[j] = 0; } } int cnt = 0; for(int j = 0; j <= sum; ++j) { if(c1[j] == 0) c2[cnt++] = j; } if(cnt == 0) printf("0\n"); else { printf("%d\n", cnt); for(int i = 0; i < cnt-1; ++i) printf("%d ", c2[i]); printf("%d\n", c2[cnt-1]); } } return 0; }
相关文章推荐
- hdoj 1709 The Balance(母函数)
- 【母函数初学】The Balance hdoj 1709
- HDOJ 题目1709 The Balance(母函数)
- HDoj-1709-The Balance-母函数
- hdoj 1709 The Balance(母函数)
- HDU1709 The Balance,母函数套题
- HDU 1709 The Balance【负次方母函数】
- hdu 1709 The Balance (母函数)
- HDU1709_HDU 1709 The Balance(母函数/生成函数)
- 【HDOJ】1709 The Balance
- HDU - 1709 The Balance(母函数)
- HDU 1709 The Balance(母函数)
- HDOJ 1709-The Balance【模拟】
- 【HDU 1709】【母函数】The Balance【给出n个砝码,以及n个砝码的重量,求出(1~sum)中(sum为各砝码总和)不能被称出的重量】
- hdu 1709 The Balance(母函数)
- HDU 1709 The Balance [母函数]
- HDU1709 The Balance【母函数】
- HDU 1709 The Balance (母函数)
- HDOJ1709特殊的母函数
- HDU 1709 The Balance 母函数