Dividing
Time limit:1000 ms |
|
Memory limit:10000 K |
Total submissions:59666 |
|
Accepted:15417 |
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 wowould be easy if all the marbles had the same value, because then they coshould 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 wocould 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 |
Each row provides six numbers, and the number I represents the number of stones with the value of I. Ask if the two stones can be evenly divided.
Is a simple primary function. First, you can calculate the sum of the total values of the stone. If sum is an odd number, the average score cannot be obtained. If sum is an even number, the knowledge of the primary function is used, you only need to judge whether the coefficient with an index of sum/2 is zero. If it is 0, there is no solution that can be evenly divided. If it is not 0, you can.
It should be noted that because the number of each valuable stone may be large, there is a theorem: For the number n of any stone, if n is greater than or equal to 8, you can rewrite n to 11 (n is an odd number) or 12 (N is an even number ). Otherwise, timeout occurs.
#include <stdio.h>#include <string.h>#include <math.h>int c[200005],d[200005];int main(){int i,j,sum,k,cas=1,f[8];while(1){sum=0;for(i=1;i<=6;i++){scanf("%d",&f[i]);if(f[i]>=8){if(f[i]%2)f[i]=11;elsef[i]=12;}sum+=f[i]*i;}if(sum==0)break;if(sum%2){printf("Collection #%d:\nCan't be divided.\n\n",cas++);continue;}memset(c,0,sizeof(c));memset(d,0,sizeof(d));c[0]=1;sum=sum/2;for(i=1;i<=6;i++){for(j=0;j<=sum;j++)for(k=0;k<=f[i] && k*i+j<=sum;k++)d[k*i+j]+=c[j];memcpy(c,d,sizeof(d));memset(d,0,sizeof(d));}if(c[sum])printf("Collection #%d:\nCan be divided.\n\n",cas++);else printf("Collection #%d:\nCan't be divided.\n\n",cas++);}return 0;}
Pay attention to the output format, and finally empty rows.
Poj1014 dividing primary function