很simple,背包容量为总价值数/2,如果能凑出来就OK,不行就不ok。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int c[20005],cnt=0;
bool v[120005];
void get(int x,int cost)
{
for(int i=0;x>=1<<i;i++){
c[++cnt]=(1<<i)*cost;x-=(1<<i);
}
if(x)c[++cnt]=x*cost;//不要漏了这个
}
int main()
{
int num[7];int t=0;
while(cin>>num[1]>>num[2]>>num[3]>>num[4]>>num[5]>>num[6]){
if(!num[1]&&!num[2]&&!num[3]&&!num[4]&&!num[5]&&!num[6]){
return 0;
}
cout<<"Collection #"<<++t<<':'<<endl;
int total=0;
memset(c,0,sizeof(c));memset(v,0,sizeof(v));
for(int i=1;i<=6;i++)get(num[i],i),total+=num[i]*i;
//for(int i=1;i<=cnt;i++)cout<<c[i]<<' ';
if(total&1){//特判奇数
cout<<"Can't be divided."<<endl<<endl;continue;
}
total/=2;v[0]=true;
for(int i=1;i<=cnt;i++){
for(int V=total;V>=c[i];V--){
if(v[V-c[i]])v[V]=true;
}
}
if(v[total])
cout<<"Can be divided."<<endl<<endl;
else
cout<<"Can't be divided."<<endl<<endl;
}
return 0;
}