思路:多重背包
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;
int input[7];
vector<int> v;
int table[60010];
bool dp(int sum)
{
v.clear();
for (int i = 1; i <= 6; i++)
{
int j = 0;
while (input[i] >= (1<<j))
{
v.push_back((1<<j)*i);
input[i] -= (1 << j);
j++;
}
if (input[i] > 0)
v.push_back(input[i] * i);
}
memset(table, 0, sizeof(table));
for (int i = 0; i < v.size(); i++)
{
for (int j = sum; j >= v[i]; j--)
table[j] = max(table[j], table[j-v[i]] + v[i]);
}
if (table[sum] == sum)
return true;
else
return false;
}
int main()
{
int sum = 0;
int t = 1;
do
{
if (t > 1)
printf("\n");
sum = 0;
memset(input, 0, sizeof(input));
for (int i = 1; i <= 6; i++)
{
scanf("%d", &input[i]);
sum += input[i]*i;
}
if (sum==0)
break;
printf("Collection #%d:\n", t++);
if (sum % 2 == 0 && dp(sum / 2))
printf("Can be divided.\n");
else
printf("Can't be divided.\n");
} while (sum > 0);
return 0;
}