题目1:Dividing HDU - 1059
分析:
1、多重背包——每种物品有有限件
2、物品二进制优化
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const ll mod = 1e9 + 7;
const int maxn = 2e6;
const int inf = 0x3f3f3f3f;
int dp[maxn];
int a[7];
int main()
{
int cases = 0;
while(1)
{
mem(dp, 0);
int sum = 0;
for(int i = 1; i <= 6; i++)
cin >> a[i], sum += a[i] * i;
if(!sum) return 0;
printf("Collection #%d:\n", ++cases);
if(sum % 2)
{
cout << "Can't be divided.\n" << endl;
continue;
}
int v = sum >> 1;
for(int i = 1; i <= 6; i++)
{
for(int k = 1; ; k <<= 1)
{
if(!a[i]) break;
if(k >= a[i]) k = a[i];
for(int j = v; j >= k * i; j--)
dp[j] = max(dp[j], dp[j - k * i] + k * i);
a[i] -= k;
}
}
if(dp[v]==v) printf("Can be divided.\n\n");
else printf("Can't be divided.\n\n");
}
}