Poj 1014 Dividing (multiple backpacks)

Source: Internet
Author: User

There are 6 kinds of valuable marble (1 ~ 6) First, give a number of marble values for each type of value, and wonder if they can be evenly divided.

Idea: the template in section 9 of the backpack is set to OK. dp [j] indicates the value when the weight is j. Here the weight is equal to the value.
This question is very similar to the previous question (...). You can also use f [j] to record whether it has occurred when the weight is j.
Code:
# Include <stdio. h>
# Include <string. h>
Int sum = 0, c [12], dp [20002*6];
Void zero_one (int weight, int value)
{
Int j = 0;
For (j = sum; j> = weight; j --)
Dp [j] = dp [j]> dp [j-weight] + value? Dp [j]: dp [j-weight] + value;
}
Int main ()
{
Int I = 0, j = 0, k = 0, count = 0;
While (1)
{
Sum = 0;
For (I = 1; I <= 6; I ++)
{
Scanf ("% d", & c [I]);
Sum + = I * c [I];
}
If (! Sum)
Break;
Printf ("Collection # % d: \ n", ++ count );
If (sum % 2 = 1)
Printf ("Can't be divided. \ n ");
Else
{
Sum/= 2;
Memset (dp, 0, sizeof (dp ));
For (I = 1; I <= 6; I ++)
{
If (I * c [I]> sum) // full backpack
{
For (j = I; j <= sum; j ++)
Dp [j] = dp [j]> dp [j-I] + I? Dp [j]: dp [j-I] + I;
}
Else
{
K = 1;
While (k <c [I]) // binary splitting
{
Zero_one (k * I, k * I );
C [I]-= k;
K * = 2;
}
Zero_one (c [I] * I, c [I] * I );
}
If (dp [sum] = sum)
Break;
}
If (dp [sum] = sum)
Printf ("Can be divided. \ n ");
Else
Printf ("Can't be divided. \ n ");
}
Printf ("\ n ");
}
Return 0;
}

 

Code:
# Include <stdio. h>
# Include <string. h>
Int sum = 0, c [12], used [20002*6], f [20002*6];
Int main ()
{
Int I = 0, j = 0, k = 0, count = 0;
While (1)
{
Sum = 0;
For (I = 1; I <= 6; I ++)
{
Scanf ("% d", & c [I]);
Sum + = I * c [I];
}
If (! Sum)
Break;
Printf ("Collection # % d: \ n", ++ count );
If (sum % 2 = 1)
Printf ("Can't be divided. \ n ");
Else www.2cto.com
{
Sum/= 2;
Memset (f, 0, sizeof (f ));
F [0] = 1;
For (I = 1; I <= 6; I ++)
{
Memset (used, 0, sizeof (used ));
For (j = I; j <= sum; j ++)
{
If (! F [j] & f [j-I] & used [j-I] <c [I])
{
F [j] = 1;
Used [j] = used [j-I] + 1;
}
}
If (f [sum])
Break;
}
If (f [sum])
Printf ("Can be divided. \ n ");
Else
Printf ("Can't be divided. \ n ");
}
Printf ("\ n ");
}
Return 0;
}

 

Author: ulquiorra0cifer

Related Article

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.