HDU1059 & amp; POJ1014: Dividing (multiple backpacks)

Source: Internet
Author: User

Problem 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 describes one collection of marbles to be divided. the lines consist of six non-negative integers n1, n2 ,..., 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 ''. the maximum total number of marbles will be 20000.

The last line of the input file will be '0 0 0 0 0 ''; do not process this line.

 


Output
For each colletcion, 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.

 

Question: there are 6 items with values: 1, 2, 3, 4, 5, and 6. Give the number of each item and judge that the master can divide the items equally.

Train of Thought: a typical multi-backpack that prevents timeout requires some necessary pruning and binary Optimization

 

# Include <stdio. h> # include <algorithm> # include <string. h> using namespace std; int dp [1, 100000]; int main () {int a [11], sum, v, I, j, k, cnt, cas = 1; while (~ Scanf ("% d", & a [1]) {sum = a [1]; for (I = 2; I <= 6; I ++) {scanf ("% d", & a [I]); sum + = I * a [I];} if (! Sum) break; printf ("Collection # % d: \ n", cas ++); if (sum % 2) // The sum is odd, {printf ("Can't be divided. \ n "); continue;} v = sum/2; memset (dp, 0, sizeof (dp); dp [0] = 1; for (I = 1; I <= 6; I ++) {if (! A [I]) continue; for (j = 1; j <= a [I]; j * = 2) // binary optimization {cnt = j * I; for (k = v; k> = cnt; k --) {if (dp [k-cnt]) // The front must be able to be placed in the backpack, now we can put the backpack dp [k] = 1;} a [I]-= j;} cnt = a [I] * I; // The remaining if (cnt) {for (k = v; k> = cnt; k --) {if (dp [k-cnt]) dp [k] = 1 ;}}} if (dp [v]) printf ("Can be divided. \ n "); else printf (" Can't be divided. \ n ");} return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.