HDU

来源:互联网 发布:颐和园结局 知乎 编辑:程序博客网 时间:2024/06/05 05:19

The Balance

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8348    Accepted Submission(s): 3502


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
31 2 439 2 1
   

Sample Output
024 5


题意:给你一些砝码,告诉你它们的质量,问你用天平和这些砝码在[1,S]中有哪些质量是称不出来的,其中S是所有砝码的总质量。


思路:这是一道母函数题,因为天平的原理,这些砝码的质量可以加可以减。我一开始考虑让起始系数等于-1来表示减的情况,然后把数组开大了两倍,从中间开始,但是一直RE。后来发现其实根本不用这样,就像普通的母函数那样写,然后在b[k + j*v[i]] += a[k];的时候,再加上一个b[abs(k - j*v[i])] += a[k];就行了,这就可以表示减的情况了。


#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cstdlib>#include <cmath>#include <vector>#include <queue>#include <map>#include <algorithm>#include <set>#include <functional>using namespace std;typedef long long LL;typedef unsigned long long ULL;const int INF = 1e9 + 5;const int MAXN = 30007;const int MOD = 1e9+7;const double eps = 1e-8;const double PI = acos(-1.0);int Num;//因子个数  int n1[MAXN];//n1[i]表示该乘积表达式第i个因子的起始系数  int n2[MAXN];//n2[i]表示该乘积表达式第i个因子的终止系数  int v[MAXN];//v[i]表示该乘积表达式第i个因子的权重  int P;//P是可能的最大指数  int a[MAXN], b[MAXN];//a为计算结果,b为中间结果。  int last;//最大的指数位置  void solve()  {        a[0] = 1;      last = 0;      for (int i = 1; i<=Num; i++)      {          int last2 = min(last + n2[i] * v[i], P);           memset(b, 0, sizeof(int)*(last2 + 1));          for (int j = n1[i]; j <= n2[i] && j*v[i] <= last2; j++)  for (int k = 0; k <= last&&k + j*v[i] <= last2; k++){b[k + j*v[i]] += a[k];b[abs(k - j*v[i])] += a[k];}        memcpy(a, b, sizeof(int)*(last2 + 1));           last = last2;      }  } int main(){int ans,j;while (scanf("%d",&Num)!=EOF){P = 0;ans = 0;for (int i = 1; i <= Num; i++){n1[i] = 0;n2[i] = 1;scanf("%d", &v[i]);P += v[i];}solve();for (int i = 1; i <= last; i++)if (a[i] == 0)ans++;if (ans == 0)printf("0\n");else{printf("%d\n", ans);for (j = 1; j <= last; j++)if (a[j] == 0){printf("%d", j);break;}for (j = j+1; j <= last; j++)if (a[j] == 0)printf(" %d", j);printf("\n");}}}