多重背包即可
设dp[j] 表示是否可能分出价值为j的石头
最后判断dp[total/2]是否为真即可
贴代码
#include <string.h>
#include <iostream>
#include <stdio.h>
using namespace std;
int num[7];
bool dp[120001];
int main(void){
//freopen("","r",stdin);
int t = 1;
for(;;){
int total = 0;
for(int i = 0;i<6;i++){
scanf("%d",&num[i]);
total+= num[i]*(i+1);
}
if(!total) break;
if(total&1){
printf("Collection #%d:\nCan't be divided.\n\n",t++);
continue;
}
memset(dp,0,sizeof(dp));
dp[0] = true;
for(int i = 0;i<6;i++){
int k;
for(k = 1;k*2<=num[i];k*=2){
for(int j = total/2;j>=(i+1)*k;j--)
if(dp[j-(i+1)*k]) dp[j] = true;
}
k = num[i] - k + 1;
for(int j = total/2;j>=(i+1)*k;j--){
if(dp[j-(i+1)*k]) dp[j] = true;
}
}
if(!dp[total/2]) printf("Collection #%d:\nCan't be divided.\n",t++);
else printf("Collection #%d:\nCan be divided.\n",t++);
printf("\n");
}
}
本文探讨了如何通过多重背包算法解决特定问题,包括初始化、动态规划方程、边界条件和最终判断步骤。通过实例代码展示了解题过程,强调了算法在实际应用中的重要性。

2578

被折叠的 条评论
为什么被折叠?



