*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 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
 
题意:
n个砝码称重,天平的两端都可以放砝码,问1~sum之间的重量中有多少是这些砝码称不出来的,sum是这些砝码的总重量。
代码:
 1 //a克砝码有三种状态,放在天平的左端可以认为是x^-a,不放是1,放在右边是x^a,这样(x^-a,1,x^a)。但是负的重量没法用数组存,所以
 2 //可以用abs(k-j)表示左右砝码的差得到c2[abs(k-j)]+=c1[j];
 3 #include<bits\stdc++.h>
 4 using namespace std;
 5 int n,sum,a[102],c1[10004],c2[10004];
 6 void solve()
 7 {
 8     memset(c1,0,sizeof(c1));
 9     memset(c2,0,sizeof(c2));
10     c1[0]=1;c1[a[1]]=1;   //第一个表达式的系数
11     for(int i=2;i<=n;i++)
12     {
13         for(int j=0;j<=sum;j++)
14         {
15             int k=0;
16             if(k+j<=sum)
17             c2[k+j]+=c1[j];
18             k=a[i];
19             if(k+j<=sum)
20             c2[k+j]+=c1[j];
21             int tem=fabs(k-j);
22             if(tem<=sum)
23             c2[tem]+=c1[j];
24         }
25         for(int j=0;j<=sum;j++)
26         {
27             c1[j]=c2[j];
28             c2[j]=0;
29         }
30     }
31 }
32 int main()
33 {
34     while(scanf("%d",&n)!=EOF)
35     {
36         sum=0;
37         for(int i=1;i<=n;i++)
38         {
39             scanf("%d",&a[i]);
40             sum+=a[i];
41         }
42         solve();
43         int t=0,ans[10000];
44         for(int i=1;i<=sum;i++)
45         if(c1[i]==0)
46         {
47             t++;
48             ans[t]=i;
49         }
50         printf("%d\n",t);
51         if(t){
52         for(int i=1;i<t;i++)
53         printf("%d ",ans[i]);
54         printf("%d\n",ans[t]);
55         }
56     }
57     return 0;
58 }

 

转载于:https://www.cnblogs.com/--ZHIYUAN/p/6103211.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值