题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1059
二进制优化
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
int num[7];
bool d[80000+5];
int main(int argc, char const *argv[])
{int kase=0;for(;;){int sum=0;for(int i=1;i<=6;i++){scanf("%d",&num[i]);sum+=num[i]*i;}if(sum==0) break;if(sum%2) {printf("Collection #%d:\nCan't be divided.\n\n",++kase);continue;}int half=sum/2;memset(d,false,sizeof(d));d[0]=true;for(int i=1;i<=6;i++){if(num[i]==0) continue;int n=num[i],k=1;if(n*i>=half){for(int j=i;j<=half;j++)if(d[j-i]) d[j]=true;continue;}while(n>k){for(int j=half;j>=k*i;j--)if(d[j-k*i]) d[j]=true;n-=k; k<<=1;}for(int j=half;j>=n*i;j--)if(d[j-n*i]) d[j]=true;n-=k; k<<=1;}printf("Collection #%d:\n%s\n\n",++kase,d[half]?"Can be divided.":"Can't be divided.");}return 0;
}