HDUOJ ----1709

 

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
 

 

母函数,有左右算法....很好的题目..
思路...先选着一个点作为‘0’点,然后对左右放置之后的数组(即幂)进行右移动.....最后统计.....很吊的一道母函数呀!!,题目非常新颖.....
 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 }
View Code

 

你可能感兴趣的:(HDU)