HDU 1059 dividing

Source: Internet
Author: User

It means that two people collect the weights of stones together. There are six types of stones, each of which has a certain number.

Now they want to divide all the stones equally by weight. Can this be done?

 

This problem can be solved with multiple backpacks

Convert multiple backpacks into 01 backpacks. If DP [TOT/2] = 1, they can be evenly divided.

Otherwise, no

A large number of binary optimizations are required.

 

# Include <cstdio> # include <cstring> # include <cmath> # include <cstdlib> # include <iostream> # include <queue> # include <algorithm> typedef long ll; using namespace STD; int DP [1, 121000]; int main () {int A [10], CA = 1, E = 0; while (scanf ("% d ", & A [1]) {int I, j; for (I = 2; I <= 6; I ++) scanf ("% d ", & A [I]); int CNT = 0; for (I = 1; I <7; I ++) {if (a [I] = 0) CNT ++;} If (CNT = 6) break; If (E ++) printf ("\ n"); CNT = 0; For (I = 1; I <= 6; I ++) CNT + = I * A [I]; printf ("collection # % d: \ n ", CA ++); If (CNT % 2 = 1) {cout <"can't be divided. "<Endl; continue;} int ans = CNT/2; memset (DP,-1, sizeof (DP); DP [0] = 1; int K; for (I = 1; I <7; I ++) // The Rock weight is I {int tot = A [I]; // I-weight stones have tot for (j = 1; j <= tot; j * = 2) // J stones {tot-= J; for (k = ans; k> = I * j; k --) // current state {int W = I * j; If (DP [k-W]! =-1) DP [k] = 1 ;}} if (Tot! = 0) {for (k = ans; k> = I * tot; k --) {int W = I * tot; If (DP [k-W]! =-1) DP [k] = 1 ;}} if (DP [ANS] = 1) cout <"can be divided. "<Endl; else cout <" can't be divided. "<Endl;} 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.