您的位置:首页 > 其它

poj 1014 Dividing

2013-02-27 16:04 267 查看
多重背包即可

设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");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: