#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
int dp[100000];
int main()
{
int a[11],sum,v,i,j,k,cnt,cas = 1;
while(~scanf("%d",&a[1]))
{
sum = a[1];
for(i = 2;i<=6;i++)
{
scanf("%d",&a[i]);
sum+=i*a[i];
}
if(!sum)
break;
printf("Collection #%d:\n",cas++);
if(sum%2)//总和为奇数,必定不能平分
{
printf("Can't be divided.\n\n");
continue;
}
v = sum/2;
memset(dp,0,sizeof(dp));
dp[0] = 1;
for(i = 1;i<=6;i++)
{
if(!a[i])
continue;
for(j = 1;j<=a[i];j*=2)//二进制优化
{
cnt = j*i;
for(k = v;k>=cnt;k--)
{
if(dp[k-cnt])//必须前面的能够放入背包,现在的才能放入背包
dp[k] = 1;
}
a[i]-=j;
}
cnt = a[i]*i;//剩下的
if(cnt)
{
for(k = v;k>=cnt;k--)
{
if(dp[k-cnt])
dp[k] = 1;
}
}
}
if(dp[v])
printf("Can be divided.\n\n");
else
printf("Can't be divided.\n\n");
}
return 0;
}
谢谢!
急求!!各位大佬能回答一下吗
慕码人2483693
一只萌萌小番薯
相关分类