HDU 1059 完全背包

Dividing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22808    Accepted Submission(s): 6444


Problem Description
Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value.
Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.
 
Input
Each line in the input describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, ..., n6, where ni is the number of marbles of value i. So, the example from above would be described by the input-line ``1 0 1 2 0 0''. The maximum total number of marbles will be 20000.

The last line of the input file will be ``0 0 0 0 0 0''; do not process this line.
 
Output
For each colletcion, output ``Collection #k:'', where k is the number of the test case, and then either ``Can be divided.'' or ``Can't be divided.''.

Output a blank line after each test case.
 
Sample Input
1 0 1 2 0 0
1 0 0 0 1 1
0 0 0 0 0 0
 
Sample Output
Collection #1: Can't be divided.
Collection #2: Can be divided.
 
Source
 
题解:6种石头拥有不同价值  现在要求你等分
        输入 n1...ni....n6  代表 价值为i的石头有ni个  若能等分按要求输出 反之亦然
 
题意:背包容量为总价值的一半  若能满足f[m]==m则说明能够等分
        完全背包的二进制优化实现
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #define ll __int64
 6 using namespace std;
 7 int n[10];
 8 int m;
 9 int jishu;
10 int size[60005];
11 int value[60005];
12 int coun=1;
13 int f[60005];
14 void slove(int q)
15 {
16     coun=1;
17    for(int i=1;i<=q;i++) 
18   {  
19      int c=n[i],v=i;   
20     for (int k=1; k<=c; k<<=1) 
21     {  
22         value[coun] = k*v;  
23         size[coun++] = k*v;  
24         c -= k;  
25     }  
26     if (c > 0)  
27     {  
28        value[coun] = c*v;  
29        size[coun++] = c*v;  
30     }  
31 }  
32 }
33 int main()
34 {    jishu=0;
35     while(scanf("%d %d %d %d %d %d",&n[1],&n[2],&n[3],&n[4],&n[5],&n[6])!=EOF)
36     {   
37         if(n[1]==0&&n[2]==0&&n[3]==0&&n[4]==0&&n[5]==0&&n[6]==0)
38         break;
39         memset(f,0,sizeof(f));
40         memset(size,0,sizeof(size));
41         memset(value,0,sizeof(value));
42         m=n[1]+n[2]*2+n[3]*3+n[4]*4+n[5]*5+n[6]*6;
43         if(m%2)
44         {
45             printf("Collection #%d:
Can't be divided.

",++jishu);
46         }
47         else
48         { 
49           slove(6);
50             m=m/2;
51             for(int i=1;i<=coun-1;i++)
52             for(int k=m;k>=value[i];k--)
53             {
54                 f[k]=max(f[k],f[k-size[i]]+value[i]);
55             }
56          if(f[m]==m)
57         printf("Collection #%d:
Can be divided.

",++jishu);        
58             else
59          printf("Collection #%d:
Can't be divided.

",++jishu);
60                 
61         }
62         
63     }
64     return 0;
65 }
 
 
 
 
原文地址:https://www.cnblogs.com/hsd-/p/5460302.html