Dividing
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 61130 | Accepted: 15794 |
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.
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.
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.
计算总价值如果是奇数则肯定不能平分,如果是偶数继续往下判断,
计算总价值的一半为指数的系数是多少,如果为0,则说明总价值的一半没有方法去构成,不为0,则说明有方法将该石头的总价值分平均分为两部分。
发现一个定理:对于任意一种珠宝的个数n,如果n大于等于8,可以将n改写为11(n为奇数)或12(n为偶数).
#include <iostream> #include <stdio.h> #include <memory.h> int a[200005],b[200005],c[10]; using namespace std; void mother(int i,int num,int n) {//i代表第i个元素,第i个元素的数量有num个,需要计算的最大指数n for(int j=0;j<=n;j++) for(int k=0;k<=num&&k*i+j<=n;k++) b[k*i+j]+=a[j]; for(int j=0;j<=n;j++) { a[j]=b[j]; b[j]=0; } } int main(void) { // freopen("1014.txt","r",stdin); int l=1; while(scanf("%d%d%d%d%d%d",&c[1],&c[2],&c[3],&c[4],&c[5],&c[6])&&(c[1]!=0||c[2]!=0||c[3]!=0||c[4]!=0||c[5]!=0||c[6]!=0)) { printf("Collection #%d:\n",l++); memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); a[0]=1; int value=0; for(int i=1;i<=6;i++) { if(c[i]>=8) { if(c[i]%2) c[i]=11; else c[i]=12; } value+=i*c[i]; } if(value%2) { printf("Can't be divided.\n\n"); continue; } for(int i=1;i<=6;i++) { mother(i,c[i],value/2); } if(a[value/2]) printf("Can be divided.\n\n"); else printf("Can't be divided.\n\n"); } return 0; }