# 11067: 【原1067】Dividing

### 题目描述

author: Mid-Central European Regional Contest 1999 原OJ链接：https://acm.sjtu.edu.cn/OnlineJudge-old/problem/1067

## 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 Format

Each line in the input file describes one collection of marbles to be divided. The lines contain six non-negative integers \(n_1,...,n_6\), where \(n_i\) 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 Format

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

## FineArtz's solution

``````/* Dividing */
#include <iostream>
using namespace std;

bool work(int *a){
int v = 0;
bool ret;
for (int i = 1; i <= 6; ++i){
v += a[i] * i;
}
if (v % 2)
return false;
v /= 2;
int *f[7];
for (int i = 0; i <= 6; ++i)
f[i] = new int[v + 1];
f[0][0] = 0;
for (int i = 1; i <= v; ++i)
f[0][i] = -1;
for (int i = 1; i <= 6; ++i){
for (int j = 0; j <= v; ++j){
if (f[i - 1][j] >= 0)
f[i][j] = a[i];
else
f[i][j] = -1;
}
for (int j = 0; j <= v - i; ++j){
if (f[i][j] > 0){
f[i][j + i] = max(f[i][j + i], f[i][j] - 1);
}
}
}
if (f[6][v] != -1)
ret = true;
else
ret = false;
for (int i = 0; i <= 6; ++i)
delete [] f[i];
return ret;
}

int main(){
int n[7], t = 0;
bool eof = true;
for (int i = 1; i <= 6; ++i){
cin >> n[i];
if (n[i]) eof = false;
}
while (!eof){
++t;
bool flag = work(n);
cout << "Collection #" << t << ": \n";
if (flag)
cout << "Can be divided.\n\n";
else
cout << "Can't be divided.\n\n";
eof = true;
for (int i = 1; i <= 6; ++i){
cin >> n[i];
if (n[i]) eof = false;
}
}
return 0;
}
``````