前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDUOJ ----1709

HDUOJ ----1709

作者头像
Gxjun
发布2018-03-21 11:03:33
5550
发布2018-03-21 11:03:33
举报
文章被收录于专栏:mlml

The Balance

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

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

http://acm.hdu.edu.cn/showproblem.php?pid=1709

母函数,有左右算法....很好的题目..

思路...先选着一个点作为‘0’点,然后对左右放置之后的数组(即幂)进行右移动.....最后统计.....很吊的一道母函数呀!!,题目非常新颖.....

代码语言:javascript
复制
 1 #include<iostream>
 2 #include<vector>
 3 #include<cstring>
 4 #define maxn 20003
 5 #define gong 10000
 6 using namespace std;
 7 int c1[maxn],c2[maxn];
 8 int save[maxn];
 9 int main()
10 {
11     int n,i,sum,j,k,count;
12     while(cin>>n)
13     {
14        vector<int>arr(n);
15        memset(c1,0,sizeof(c1));
16        memset(c2,0,sizeof c2);
17        memset(save,0,sizeof save);
18        count=sum=0;
19        for(i=0;i<n;i++)
20        {
21            cin>>arr[i];
22            sum+=arr[i];
23        }
24        for(i=-arr[0];i<=arr[0];i+=arr[0])
25        {
26            c1[i+gong]=1;
27        }
28      for(i=1;i<n;i++)
29      {
30          for(j=-sum;j<=sum;j++)
31          {
32            for(k=-arr[i];k<=arr[i]&&k+j<=sum;k+=arr[i])
33            {
34              c2[k+j+gong]+=c1[j+gong];
35            }
36          }
37          
38          for(j=-sum+gong;j<=sum+gong;j++)
39          {
40              c1[j]=c2[j];
41              c2[j]=0;
42          }
43      }
44        int ans=0;
45        for(j=0;j<=sum;j++)
46        {
47            if(c1[j+gong]==0||c1[gong-j]==0)
48            {   
49                save[ans++]=j;
50            }
51        }
52        cout<<ans<<endl;
53        if(ans)
54        {
55          for(i=0;i<ans;i++)
56          {
57              if(i==0)
58                  cout<<save[i];
59              else
60                  cout<<" "<<save[i];
61          }
62           cout<<endl;       
63        }
64     }
65     return 0;
66 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-08-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • The Balance
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档