POJ 1014 / HDU 1059 Dividing 多重背包+二进制分解

Problem Description
Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value. 
Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.
Input
Each line in the input describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, ..., n6, where ni is the number of marbles of value i. So, the example from above would be described by the input-line ``1 0 1 2 0 0''. The maximum total number of marbles will be 20000. 
The last line of the input file will be ``0 0 0 0 0 0''; do not process this line.
Output
For each colletcion, output ``Collection #k:'', where k is the number of the test case, and then either ``Can be divided.'' or ``Can't be divided.''. 
Output a blank line after each test case.
 
Sample Input
1 0 1 2 0 0
1 0 0 0 1 1
0 0 0 0 0 0
Sample Output
Collection #1:
Can't be divided.
 
Collection #2:
Can be divided.

题意:给出6种大小的石头,问能不能分成数值一样的两堆。

思路:背包方面是简单的,但主要的问题是,如果一个一个枚举物品,物品太多肯定会超时。所以就有将物品的数量进行二进制分解,即分成2的幂次,每个2的幂都作为一个新的物品,再进行01背包就行了。POJ上交不但不支持<bits/stdc++.h>而且数组还要开大一点/.\

/** @Date    : 2016-12-10-20.39
  * @Author  : Lweleth (SoungEarlf@gmail.com)
  * @Link    : https://github.com/
  * @Version :
  */
#include 
#include 
#include 
#include 
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;

int cnt = 0;
int a[10];
int b[N];

int dp[60010];

void binaDiv(int x, int v)
{
    int r = 1;
    while(r < x)
    {
        b[cnt++] = r * v;
        x -= r;
        r <<= 1;
    }
    b[cnt++] = x * v;
}

int main()
{
    int c = 0;
    while(true)
    {
        int sum = 0;
        for(int i = 1; i <= 6; i++)
            scanf("%d", a + i), sum += a[i]*i;
        if(sum == 0)
            break;
        printf("Collection #%d:\n", ++c);
        if(sum & 1)
        {
            printf("Can't be divided.\n\n");
            continue;
        }
        sum /= 2;

        cnt = 0;
        for(int i = 1; i <= 6; i++)
        {
            binaDiv(a[i], i);
        }
        MMF(dp);
        for(int i = 0; i < cnt; i++)
        {
            for(int j = sum; j >= 0; j--)
            {
                if(j >= b[i])
                    dp[j] = max(dp[j], dp[j - b[i]] + b[i]);
            }
        }
        if(dp[sum] == sum)
            printf("Can be divided.\n\n");
        else printf("Can't be divided.\n\n");
    }
    return 0;
}

转载于:https://www.cnblogs.com/Yumesenya/p/6158578.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值