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 would be easy if all the marbles had the same value, because then they could 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 would be described by the input-line ``1 0 1 2 0 0''. The maximum total number of marbles will be 20000.
The last line of the input file will be ``0 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.
题目大意:输入价值为1-6的玻璃球的数量,然后让你判断能否将它们平分成价值相等的两份
思路:刚开始想到的是普通的0-1背包,遍历每个玻璃球然后刷新状态就可以的。结果交上去TLE了
然后网上找到了半天说是要用二进制优化。然后让我来解释一下我理解的二进制优化吧
用mar[i]表示价值为i的玻璃球的总数量。二进制优化就是把你的等价值的玻璃球分成系数为1,2,4,8,...,2^(k-1),mar[i]-2^k+1.其中k 为满足 mar[i]-2^k+1 > 0 的最大值.
这样就可以分别组合成1,2,3,4,5,6,7,...,mar[i]了。玻璃球就被分为1*i,2*i,4*i,8*i,...,2^(k-1)*i,(mar[i]-2^k+1)*i
然后就可以根据0-1背包的原理做了。这样就不会超时了

Dividing
#include <iostream>
#include <cstdio>
#include <memory.h>
#include <algorithm>
using namespace std;
#define MAXN 120000 + 10
int DP[MAXN];
int mar[7];
int items[MAXN];
int main(){
int T = 1;
while(scanf("%d",&mar[1]) != EOF){
int sum = mar[1];
for(int i = 2;i <= 6;i++){
scanf("%d",&mar[i]);
sum += mar[i] * i;
}
if (sum == 0)
break;
printf("Collection #%d:\n",T++);
if (sum % 2){
printf("Can't be divided.\n");
printf("\n");
continue;
}
int temp = sum / 2;
int tot = 0;
for(int i = 1;i <= 6;i++){
int j;
for(j = 1;j * 2 - 1 < mar[i];j *= 2){
items[tot++] = i * j;
}
items[tot++] = (mar[i] - j + 1) * i;
}
memset(DP,0,sizeof(DP));
//for(int i = 0;i <= mar[1] && i <= temp;i++) DP[i] = 1;
/*
for(int i = 2;i <= 6;i++){
for(int j = 0;j < mar[i];j++){
for(int k = temp;k >= 0;k--){
if (DP[k] <= temp && k >= i){
if (DP[k-i] + i <= temp)
DP[k] = max(DP[k-i]+i,DP[k]);
}
}
}
}*/
for(int i = 0;i < tot;i++){
for(int k = temp;k >= 0;k--){
if (DP[k] <= temp && k >= items[i]){
if (DP[k-items[i]] + items[i] <= temp)
DP[k] = max(DP[k-items[i]] + items[i],DP[k]);
}
}
}
if (DP[temp] == temp) printf("Can be divided.\n");
else printf("Can't be divided.\n");
printf("\n");
}
return 0;
}