题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1059
#include <stdio.h>
#include <string.h>
#define MAXN 60002
int dp[MAXN],V;
void ZeroOnePack(int val,int w)
{
int i;
for(i=V;i>=val;--i)
if(dp[i]<dp[i-val]+w)
dp[i]=dp[i-val]+w;
}
void CompletePack(int val,int w)
{
int i;
for(i=val;i<=V;++i)
if(dp[i]<dp[i-val]+w)
dp[i]=dp[i-val]+w;
}
void MultPack(int val,int w,int n)
{
int k;
if(val*n>=V)
CompletePack(val,w);
else
{
k=1;
while(k<n)
{
ZeroOnePack(k*val,k*w);
n-=k;
k*=2;
}
ZeroOnePack(n*val,n*w);
}
}
int main()
{
int n[6],i,cnt=1,vsum;
while(scanf("%d %d %d %d %d %d",&n[0],&n[1],&n[2],&n[3],&n[4],&n[5]))
{
vsum=0;
for(i=0;i<6;++i)
vsum+=(i+1)*n[i];
if(vsum==0)
break;
if(vsum%2==1)
{
printf("Collection #%d:\nCan't be divided.\n\n",cnt++);
continue;
}
V=vsum/2;
memset(dp,0,sizeof(dp));
for(i=0;i<6;++i)
{
if(n[i])
MultPack(i+1,i+1,n[i]);
}
if(V==dp[V])
printf("Collection #%d:\nCan be divided.\n\n",cnt++);
else
printf("Collection #%d:\nCan't be divided.\n\n",cnt++);
}
return 0;
}