这题是时多重背包的入门题,好像bfs加上剪枝也是可以做的,不解释了吧,直接上代码吧
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=6*20010;
const int INF=0x3f3f3f3f;
bool f[maxn];
int cnt[7];
int main()
{
//freopen("int.txt","r",stdin);
int ct=0;
while(1){
int sum=0;
for(int i=1;i<7;i++){
scanf("%d",&cnt[i]);
sum+=cnt[i]*i;
}
if(sum==0)break;
printf("Collection #%d:\n",++ct);
if(sum%2){
printf("Can't be divided.\n\n");
continue;
}
memset(f,false,sizeof(f));
f[0]=true;
for(int i=1;i<=6;i++){
if(!cnt[i])continue;
int k;
for(k=1;k*2-1<cnt[i];k*=2){
for(int v=sum/2;v>=k*i;v--){
if(f[v-k*i])f[v]=true;
}
}
k=cnt[i]-(k-1);
for(int v=sum/2;v>=k*i;v--){
if(f[v-k*i])f[v]=true;
}
}
//for(int i=sum/2;i>=0;i--)printf("%d:%d\n",i,f[i]);
if(!f[sum/2])printf("Can't be divided.\n\n");
else printf("Can be divided.\n\n");
}
return 0;
}