【POJ1014】【TYVJ1194】划分大理石

Dividing
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 55117   Accepted: 14112

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.

Source

Mid-Central European Regional Contest 1999

【题意】中文翻译参见TYVJ
【思路】记录总价值sum 为奇数就必定不能平分
                做容量为sum/2的多重背包
                一开始划分物品的时候num写成num-k ,RE了= = 
                注意输出格式
                以下给出二进制优化的做法

/*poj1014 AC*/
#include<cstdio>

long sum,v,tot,i;
bool f[100000];
long num[7];


void Zeroonepack(int weight)
{
     for (long j=v;j>=weight;j--)
       if (f[j-weight]) f[j]=true;
       }
       


void Multiplepack(int num,int weight)
{
     long k=1;
     if (num==1) { Zeroonepack(weight); return; }
     while (k<num)
     {
        Zeroonepack(k*weight);
        num-=k;
        k*=2;
        }
     if (num) Zeroonepack((num)*weight); //NUM!
     } 
            

int main()
{
    while (1) {
       tot++;
       bool b=false;
       sum=0;
       for (i=1;i<=6;i++) 
        {
          scanf("%ld",&num[i]);
          if (num[i]) b=true;
          sum+=i*num[i];
          }
       if (!b) break;
       if (sum%2) {
                  printf("Collection #%ld:\nCan't be divided.\n\n",tot);
                  continue;
                  }
       v=sum/2;
       for (i=1;i<=v;i++) f[i]=false;
       f[0]=true;
       for (i=1;i<=6;i++)
         Multiplepack(num[i],i);
       if (f[v]) printf("Collection #%ld:\nCan be divided.\n\n",tot);
         else   printf("Collection #%ld:\nCan't be divided.\n\n",tot);
         }
}    
      

你可能感兴趣的:(【POJ1014】【TYVJ1194】划分大理石)