Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
Submit Status

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 file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , . . . , 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 collection, 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.
多重背包和二进制优化,这个优化很重要,不会超时,
 1 #include<cstdio>
 2 #include<string.h>
 3 #include<math.h>
 4 using namespace std;
 5 int dp[100000];
 6 int main()
 7 {
 8     int a[11];
 9     int t=1;
10     int cnt;
11     while(scanf("%d %d %d %d %d %d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6])!=EOF)
12     {
13         if(a[1]+a[2]+a[3]+a[4]+a[5]+a[6]==0) break;
14         int sum,x;
15         sum=a[1]*1+a[2]*2+a[3]*3+a[4]*4+a[5]*5+a[6]*6;
16         printf("Collection #%d:\n",t);
17         t++;
18         if(sum%2)
19         {
20             printf("Can't be divided.\n\n");
21             continue;
22         }
23         x=sum/2;
24         memset(dp,0,sizeof(dp));
25         dp[0]=1;
26         for(int i=1;i<=6;i++)
27         {
28             if(!a[i])
29                 continue;
30             for(int j=1;j<=a[i];j*=2)//二进制优化,2,4,8,16,他把dp[3],dp[5],dp[7]等都算了,并且循环次数减少
31             {
32                cnt=j*i;
33                for(int k=x;k>=cnt;k--)
34                {
35                    if(dp[k-cnt])
36                     dp[k]=1;
37                }
38                a[i]-=j;//石头数目减少
39             }
40             cnt=a[i]*i;//最后再算一次上面循环完后的a[i]
41             if(cnt)//如果cnt不等于0
42             {
43                 for(int k=x;k>=cnt;k--)
44                 {
45                     if(dp[k-cnt])
46                         dp[k]=1;
47                 }
48             }
49         }
50         if(dp[x])//如果dp[x]不等于0,则说明能平分
51             printf("Can be divided.\n");
52         else printf("Can't be divided.\n");
53         printf("\n");
54     }
55     return 0;
56 }

 

posted on 2014-08-19 16:50  Damonll  阅读(187)  评论(0编辑  收藏  举报