原题链接:弹珠 - 洛谷
?看一个人能不能凑出sum / 2就可以了
多重背包
AC代码:
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define ll long long
#define PII pair<int,int>
#define rep(i, n) for (int i = 1; i <= (n); ++i)
#define rrep(i, n) for(int i = n; i >= 1; ++i)
using namespace std;
const double pi = acos(-1.0);
const int N = 6e4 + 10;
int a[7], f[N];
int main()
{
int cnt = 0;
f[0] = 1;
while(cin >> a[1] >> a[2] >> a[3] >> a[4] >> a[5] >> a[6])
{
cnt++;
int sum = 0;
rep(i, 6) sum += a[i] * i;
if(!sum) break;
cout<<"Collection #"<<cnt<<":\n";
if(sum % 2)
{
cout<<"Can't be divided.\n\n";
continue;
}
sum /= 2;
rep(i, sum) f[i] = 0;
for(int i = 1; i <= 6; i++)
for(int j = sum; j > 0; j--)
for(int k = 1; k <= a[i] && i * k <= j; k++)
f[j] |= f[j - k * i];
if(f[sum]) cout<<"Can be divided.\n";
else cout<<"Can't be divided.\n";
cout<<'\n';
}
return 0;
}
?参考:洛谷P1537 弹珠【多重背包DP】【绿】_一个老实的人的博客-CSDN博客_多重背包 洛谷
|