与POJ 1276差不多,首先求总和,若为奇数则不可分,若为偶数,求出其一半.然后求出一半是否由这些相加可达. /*Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35222 Accepted: 8641 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. */ /*首先求总和,若为奇数则不可分,若为偶数,求出其一半.然后求出一半是否由这些相加可达.*/ #include <stdio.h> #include "stdlib.h" #include "string.h" #define MAX_TOTAL_MARBLE_VALUE 60001/*2W*6/2+1*/ #define MAX_MARBLE_VALUE_TYPE_NUM 50/*>>Log2W*/ int gaiMarbleValue[MAX_MARBLE_VALUE_TYPE_NUM]; int gaiTotalMarbleValue[MAX_TOTAL_MARBLE_VALUE]; int Dividingmain(void) { int iLoop; int iLoopN; int iLoopValue; int aiN[7]; int iValueTypeNum; int iTempNum; int iTotalValue; int iNo = 0; while(1) { scanf("%d %d %d %d %d %d",&aiN[1],&aiN[2],&aiN[3],&aiN[4],&aiN[5],&aiN[6]); if (0 == aiN[1]+aiN[2]+aiN[3]+aiN[4]+aiN[5]+aiN[6]) { break; } iValueTypeNum = 0; iTotalValue = 0; for (iLoopN = 1; iLoopN < 7; iLoopN++) { iTotalValue += iLoopN*aiN[iLoopN]; iTempNum = 1; for (iLoop = 0; 0 < aiN[iLoopN]; iLoop++) { if(aiN[iLoopN] >= iTempNum) { gaiMarbleValue[iValueTypeNum] = iTempNum*iLoopN; aiN[iLoopN] -= iTempNum; } else { gaiMarbleValue[iValueTypeNum] = aiN[iLoopN]*iLoopN; aiN[iLoopN] = 0; } iTempNum = iTempNum<<1; iValueTypeNum++; } } /************************************************************************/ /* */ /************************************************************************/ iNo++; if(1 == (iTotalValue&1)) { printf("Collection #%d:/nCan't be divided./n/n",iNo); continue; } iTotalValue = iTotalValue>>1; memset(gaiTotalMarbleValue,0,MAX_TOTAL_MARBLE_VALUE*sizeof(int)); gaiTotalMarbleValue[0] = 1; for (iLoop = 0; iLoop < iValueTypeNum; iLoop++) { for (iLoopValue = iTotalValue; iLoopValue >= gaiMarbleValue[iLoop]; iLoopValue--) { if (0 == gaiTotalMarbleValue[iLoopValue]) { gaiTotalMarbleValue[iLoopValue] = gaiTotalMarbleValue[iLoopValue - gaiMarbleValue[iLoop]]; } } } if (0 != gaiTotalMarbleValue[iTotalValue]) { printf("Collection #%d:/nCan be divided./n/n",iNo); } else { printf("Collection #%d:/nCan't be divided./n/n",iNo); } } return 0; }