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