Dividing 
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) 
Total Submission(s): 20691 Accepted Submission(s): 5827

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 eitherCan 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.

题解:

背包问题,二进制优化,对于价值为val有num个,可以将其分成1,2,4,8......这样的二进制价值,可以保证如果可以均分,是一定

可以的,如果可以均分,那么保证可以分出,如11 可以分出1,2,4,6,不可以均分,12分出1,2,4,5拿1,5或者2,4就均分了。

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
using namespace std; int Case=;
int cont=;
int a[],val[];
bool f[]; int main()
{
while(~scanf("%d%d%d%d%d%d",&a[],&a[],&a[],&a[],&a[],&a[])&&(a[]+a[]+a[]+a[]+a[]+a[]))
{
printf("Collection #%d:\n",++Case);
int sum=,mid=;
for (int i=;i<=;i++)
sum=sum+a[i]*i;
if (sum%)
{
printf("Can't be divided.\n\n");
continue;
}
mid=sum/;
memset(f,,sizeof(f));
f[]=;cont=;
for (int i=;i<=;i++)
{
int t=i;a[i]=a[i]*i;
while(a[i]>=t)
{
val[++cont]=t;
a[i]-=t;
t*=;
}
if (a[i]>) val[++cont]=a[i];
}
f[]=;
for (int i=;i<=cont;i++)
for (int j=mid;j>=val[i];j--)
if (f[j-val[i]]) f[j]=;
if (f[mid]) printf("Can be divided.\n\n");
else printf("Can't be divided.\n\n");
}
}

hdu1059(背包dp二进制优化)的更多相关文章

  1. luogu||P1776||宝物筛选||多重背包||dp||二进制优化

    题目描述 终于,破解了千年的难题.小FF找到了王室的宝物室,里面堆满了无数价值连城的宝物……这下小FF可发财了,嘎嘎.但是这里的宝物实在是太多了,小FF的采集车似乎装不下那么多宝物.看来小FF只能含泪 ...

  2. 动态规划:HDU2844-Coins(多重背包的二进制优化)

    Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  3. HDU 1059(多重背包加二进制优化)

    http://acm.hdu.edu.cn/showproblem.php?pid=1059 Dividing Time Limit: 2000/1000 MS (Java/Others)    Me ...

  4. CodeForces922E DP//多重背包的二进制优化

    https://cn.vjudge.net/problem/1365218/origin 题意 一条直线上有n棵树 每棵树上有ci只鸟 在一棵树底下召唤一只鸟的魔法代价是costi 每召唤一只鸟,魔法 ...

  5. dp之多重背包(二进制优化)

    void solve(int v,int w,int c){    int count=0;    for(int k=1;k<=c;k<<=1)    {        val[c ...

  6. HDU——2191悼念512汶川大地震遇难同胞(多重背包转化为01背包或二进制优化)

    悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  7. POJ 1276 Cash Machine(多重背包的二进制优化)

    题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...

  8. HDU-2844 Coins 多重背包 物品数量二进制优化

    题目链接:https://cn.vjudge.net/problem/HDU-2844 题意 给你一些不同价值和一定数量n的硬币. 求用这些硬币可以组合成价值在[1 , m]之间的有多少. 思路 多重 ...

  9. 多重背包的二进制优化——DP

    #include<cstdio> #include<cstring> #include<algorithm> #define LL long long using ...

随机推荐

  1. Xml文档数据提取到Excel表中

    近期,财务一位同事,吐槽:<某XX开票软件>导出数据文档只有Xml格式,竟然没有Excel文档,工作起来非常不方便,希望我想想办法.上图: 需求分析:Xml数据----> 提取到Da ...

  2. FPGA开发中的脚本语言

    多数FPGA开发者都习惯图形化界面(GUI).GUI方式简单易学,为小项目提供了一键式流程.然而,随着FPGA项目越来越复杂,在很多情况下GUI工具就阻碍了工作效率.因为GUI工具不能对整个开发过程提 ...

  3. python 字符与字节 json序列和反序列及支持的类型

    b = b"demo" s = "demo" # 字符串转字节 s = bytes(s, encoding = "utf8") s = st ...

  4. DELETE - 删除一个表中的行

    SYNOPSIS DELETE FROM [ ONLY ] table [ WHERE condition ] DESCRIPTION 描述 DELETE 从指明的表里删除满足 WHERE 子句的行. ...

  5. CAD交互绘制直线(网页版)

    用户可以在CAD控件视区任意位置绘制直线. 主要用到函数说明: _DMxDrawX::DrawLine 绘制一个直线.详细说明如下: 参数 说明 DOUBLE dX1 直线的开始点x坐标 DOUBLE ...

  6. MyBatis基本运行环境

    MyBatis基本运行环境 1. 创建项目 2.拷贝jar加入到项目中build path jar包 3.创建数据库的表及数据添加 USE [mybatis] CREATE TABLE [dbo].[ ...

  7. ajax从入门到深入精通

    前言 ajax全称Asynchronous Javascript and XML.其中Asynchronous代表异步.同步于异步是描述通信模式的概念,同步机制:发送方发生请求后,需要等待接收到接收方 ...

  8. adb 调试真机 wait for device 错误解决办法

    起因 真机测试,使用adb安装和卸载应用时,出现wait for device. 解决办法 拔掉USB重新插入即可.

  9. <Spring Data JPA>一 JPA原生

    1.pom依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  10. windows环境下Robot Framework的安装步骤

    Robot Framework是由python编写的开源的用来做功能性测试的自动化测试框架.本文介绍Robot Framework在windows环境下的安装步骤. 安装python从python官网 ...