Analysis:
If you want to share the ball fairly, the sum of the value of the ball must be an even number. You can first determine the sum of the value of the ball. If it is an odd number, you do not need to make the following judgment.
If it is an even number, we can map this problem to a problem of multiple backpacks, and this backpack is required to be fully filled. The total capacity V of the backpack is half of the total value of all fl and sum. The cost and weight of the fl are their numbers. The number is input by the outside world. Final judgment: If the obtained F [V] is equal to sum/2, it indicates that the ball can be scored fairly.
Note that because the maximum number of balls is 20000, in extreme cases, 20000 balls are placed at a position worth 6, the maximum value of V is 6*20000/2 + 1 = 60001.
Okay, Cuihua, go to the Code:
[Cpp]
# Include <iostream>
Using namespace std;
# Define LEN 7
# Define INIT 0 xffffffff
Int F [60001];
Int max (int x, int y)
{
Return x> y? X: y;
}
Void ZeroOnePack (int cost, int weight, int V)
{
For (int v = V; v> = cost; -- v ){
F [v] = max (F [v], F [v-cost] + weight );
}
}
Void CompletePack (int cost, int weight, int V)
{
For (int v = cost; v <= V; ++ v ){
F [v] = max (F [v], F [v-cost] + weight );
}
}
Void MultiPack (int cost, int weight, int V, int amount)
{
If (cost * amount> = V ){
CompletePack (cost, weight, V );
Return;
}
Int k = 1;
While (k <amount ){
ZeroOnePack (cost * k, weight * k, V );
Amount-= k;
K * = 2;
}
ZeroOnePack (cost * amount, weight * amount, V );
}
Int main (int argc, char ** argv)
{
Int index = 0;
Int num [LEN];
While (1 ){
++ Index;
Int sum = 0;
Int bin = 0;
For (int I = 1; I <LEN; ++ I ){
Cin> num [I];
Sum + = I * num [I];
Bin | = num [I];
}
If (! Bin)
Break;
If (sum & 0x01)
Cout <"Collection #" <index <": \ n" <"Can't be divided." <endl;
Else {
Int V = sum> 1;
F [0] = 0;
For (int I = 1; I <= V; ++ I)
F [I] = INIT;
For (int I = 1; I <LEN; ++ I)
MultiPack (I, I, V, num [I]);
If (F [V]! = V)
Cout <"Collection #" <index <": \ n" <"Can't be divided." <endl;
Else
Cout <"Collection #" <index <": \ n" <"Can be divided." <endl;
}
}
System ("pause ");
Return 0;
}