PKU 1014 Dividing

来源:互联网 发布:淘宝网纯皮钱包男士 编辑:程序博客网 时间:2024/04/29 06:17
初次编辑时间:2011-07-03
source page:http://poj.org/problem?id=1014
Dividing
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 62574 Accepted: 16205

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

source code:
#include<stdio.h>#include<string.h>int flag[120100];//flag[k]=1,表示能得到k,v-k的分配方法,v表示总价值int main(){    int i,j,k,sum,max,cnt=1,a[7];    while(scanf("%d",&a[1])!=EOF){        memset(flag,0,sizeof(flag));        sum=a[1]; flag[0]=1;        for(i=2;i<=6;i++){            scanf("%d",&a[i]);            sum+=a[i]*i;        }        if(a[1]==0&&a[2]==0&&a[3]==0&&a[4]==0&&a[5]==0&&a[6]==0)            break;        printf("Collection #%d:\n",cnt++);                //剪枝1:sum为总价值,如果sum不能被2整除,那么肯定不能分,则continue        if(sum%2!=0){            printf("Can't be divided.\n\n");            continue;        }        //核心部分        max=0;        for(i=1;i<=6;i++){            if(!a[i]) continue; //剪枝2:该重量如果没有,则continue            for(k=max;k>=0;k--)                if(flag[k])                    for(j=1;j<=a[i];j++)                        if(flag[k+i*j]||k+i*j>sum/2)                            break;                        /*非常重要的判断条件,是否超时的重要原因之一。                        因为flag[k+i*j]已经为1,接下去的都是重复操作。此外                        k+i*j>sum/2,因为只要判断flag[sum/2]是否为真即可 ,                        flag[k](k<=sum/2)和flag[sum-k]的情况是一样的*/                        else                            flag[k+i*j]=1;            max+=a[i]*i;            if(max>sum/2) max=sum/2;            //剪枝3:超时的重要原因之二        }        if(flag[sum/2]) printf("Can be divided.\n\n");        else printf("Can't be divided.\n\n");    }    return 0;}
0 0