#include
#include
/*
Gemeinheit: Ungerade Gesamtsumme
*/
int marbles[7], marsha_marbles[7];
int main() {
FILE * f = fopen("divide.in", "r");
//int i, j, k, x, y, s, xx, yy, x1, y1, x2, y2, _, ma;
//int t, n, geht, beinimm;
//int su, goal_value;
int cnt = 1;
int i, j;
int goal_value, actual_value;
int max_marbles;
int possible;
while (1) {
/* initialize variables, read test case */
goal_value = 0;
actual_value = 0;
for (i = 1; i <= 6; i++) {
fscanf(f, "%d", &marbles[i]);
goal_value += marbles[i] * i;
marsha_marbles[i] = 0;
}
/* End of input file if there are no marbles */
if (!goal_value) break;
printf("Collection #%d:\n", cnt++);
/* Can't be divided if the total value is odd */
if (goal_value % 2 == 1) {
possible = 0;
} else {
goal_value /= 2;
/* big loop to try "every" possibility */
while (1) {
/* found possibility? */
if (actual_value == goal_value) {
possible = 1;
break;
}
/* No? Then try next possibility:
First try to take more marbles of value 1; if that fails,
remove all marbles of value 1 and take more of value 2;
and so on*/
for (j = 1; j <= 6; j++) {
/* calculate maximum number of marbles of value j we want
to take */
/* Don't take more marbles than exist */
max_marbles = marbles[j];
/* Don't take so much marbles that marbles of greater value
could be taken instead */
for (i = j + 1; i <= 6 && i <= max_marbles; i++) {
/* If there are at least j marbles with value i left, we could
take j marbles with value i instead of i marbles with
value j; so in this case, we only want to take less than
i marbles */
if (marbles[i] - marsha_marbles[i] >= j) {
if (max_marbles > i - 1)
max_marbles = i - 1;
}
}
/* if we don't already have max_marbles marbles of value j,
take one more */
if (marsha_marbles[j] < max_marbles) {
actual_value += j;
marsha_marbles[j]++;
/* OK, this is the next possibility */
break;
}
/* We don't want to take more marbles of value j now; so
remove all marbles of value j (and try to take more marbles
of value j+1 in the next loop) */
actual_value -= j * marsha_marbles[j];
marsha_marbles[j] = 0;
} /* end: for (j = 1; j < 6; j++) */
/* If we did not leave the loop with break, this means that
we did not find any value where we could augment the number
of marbles; this means we are finished (and did not find a
possibility to divide the marbles) */
if (j > 6) {
possible = 0;
break;
}
} /* end: while (1) */
} /* end: even total value */
/* output the result */
if (possible)
printf("Can be divided.\n\n");
else
printf("Can't be divided.\n\n");
} /* end: while (1) */
fclose(f);
return 0;
}