Dividing
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 53029   Accepted: 13506

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 file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , . . . , 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 collection, 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. 用二进制优化多重背包,使它变成01背包
 #include <iostream>
#include <stdio.h>
#include <queue>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
#include <vector>
int main()
{
int a[];
int ttt=;
while(scanf("%d%d%d%d%d%d",&a[],&a[],&a[],&a[],&a[],&a[]))
{
if(ttt!=)
cout<<endl;
if(a[]==&&a[]==&&a[]==&&a[]==&&a[]==&&a[]==)
break;
a[]*=;a[]*=;a[]*=;a[]*=;a[]*=;
int sum=a[]+a[]+a[]+a[]+a[]+a[];
if(sum%==)
{
cout<<"Collection #"<<ttt<<":"<<endl;
cout<<"Can't be divided."<<endl;
ttt++;
continue;
}
sum=sum/;
int b[];
memset(b,,sizeof(b));
int i,j;
int t=;
for(i=;i<;i++)
{
if(a[i]==)
continue;
int temp=i+;
while(temp<a[i])
{
b[t++]=temp;
a[i]-=temp;
temp*=;
}
b[t++]=a[i];
}
int dp[sum+];
memset(dp,,sizeof(dp));
for(i=;i<t;i++)
{
for(j=sum;j>;j--)
{
if(j-b[i]>=)
if(dp[j]<dp[j-b[i]]+b[i])
dp[j]=dp[j-b[i]]+b[i];
}
}
cout<<"Collection #"<<ttt<<":"<<endl;
if(dp[sum]==sum)
cout<<"Can be divided."<<endl;
else cout<<"Can't be divided."<<endl;
ttt++;
}
}

poj1014二进制优化多重背包的更多相关文章

  1. 51nod 1086 背包问题 V2(二进制优化多重背包)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1086 题解:怎么用二进制优化多重背包,举一个例子就明白了. ...

  2. POJ - 1276 二进制优化多重背包为01背包

    题意:直接说数据,735是目标值,然后3是后面有三种钱币,四张125的,六张五块的和三张350的. 思路:能够轻易的看出这是一个多重背包问题,735是背包的容量,那些钱币是物品,而且有一定的数量,是多 ...

  3. [Bzoj 1192][HNOI2006]鬼谷子的钱袋(二进制优化多重背包)

    (人生第一篇bzoj题解有点激动 首先介绍一下题目: 看它题目那么长,其实意思就是给定一个数a,求将其拆分成n个数,通过这n个数可以表示出1~a中所有数的方案中,求最小的n. 您看懂了嘛?不懂咱来举个 ...

  4. BZOJ 1531 二进制优化多重背包

    思路: 讲道理我应该写单调队列优化多重背包的 但是我不会啊 但是我现在! 还不会啊 我就写了个二进制优化的.. 过了 //By SiriusRen #include <cstdio> #i ...

  5. 【二进制优化-多重背包】zznu-oj-2120 : 安详--如何用尽钱币打赏主播获得最大好感度

    2120 : 安详 题目描述 spring最近喜欢上了B站新秀主播,身为顿顿吃黄焖鸡的土豪,当然要过去打赏一番,但是spring还是喜欢精打细算,所以在打赏的时候,想要掏出有限的钱,获得主播的最大好感 ...

  6. poj 1742 Coins(二进制优化多重背包)

    传送门 解题思路 多重背包,二进制优化.就是把每个物品拆分成一堆连续的\(2\)的幂加起来的形式,然后把最后剩下的也当成一个元素.直接类似\(0/1\)背包的跑就行了,时间复杂度\(O(nmlogc) ...

  7. Codeforces 755F PolandBall and Gifts bitset + 二进制优化多重背包

    PolandBall and Gifts 转换成置换群后, 对于最大值我们很好处理. 对于最小值, 只跟若干个圈能否刚好组能 k 有关. 最直观的想法就是bitset优化背包, 直接搞肯定T掉. 我们 ...

  8. [POJ1014]Dividing(二进制优化多重背包)

    #include <cstdio> #include <algorithm> #include <cstring> using namespace std; int ...

  9. [tyvj-1194]划分大理石 二进制优化多重背包

    突然发现这个自己还不会... 其实也不难,就和快速幂感觉很像,把物品数量二进制拆分一下,01背包即可 我是咸鱼 #include <cstdio> #include <cstring ...

随机推荐

  1. [2014-08-18]初尝 AspNet vNext On Mac

    网上关于AspNet vNext的介绍已经非常多,本文不再赘述,仅记录下Mac环境的几点注意事项. 环境 OSX 10.9.4 Mono 3.6.1 Kvm 1.0.0-alpha4-10285 mo ...

  2. KVM+Qemu+Libvirt实战

    上一篇的文章是为了给这一篇文件提供理论的基础,在这篇文章中我将带大家一起来实现在linux中虚拟出ubuntu的server版来 我们需要用KVM+Qemu+Libvirt来进行kvm全虚拟化,创建虚 ...

  3. javascript实现jsp页面的打印预览

    1.加载WebBrowser打印预览控件 <OBJECT classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" heigh ...

  4. Selenium 定位网页元素

    第一 定位元素辅助工具 IE中在元素上右击 ->  “检查元素”,或按F12键打开开发者工具: Chrome中在元素上右击 -> “审查元素”,或按F12键打开开发者工具: Firefox ...

  5. 我的Markdown的利器——Markdown Here、有道云笔记、iPic

    Markdown逐渐成为大家文章编辑的首选,这里推荐两个比较冷门的Markdown工具. 用什么当做Markdown的主力工具? 网上有很多人推荐的Markdown的工具包括专业的Markdown工具 ...

  6. 【转载】makefile经典教程

    该篇文章为转载,是对原作者系列文章的总汇加上标注. 支持原创,请移步陈浩大神博客: http://blog.csdn.net/haoel/article/details/2886 makefile很重 ...

  7. 比较三个 CSS 预处理器:Sass、LESS 和 Stylus(上)

    前沿 : 第一次写不够成熟,可能描述有所错误,还请大家修改指正,我会对已完成的文章进行修改. 一.什么是CSS预处理器 CSS预处理器定义了一种新的语言,基本的思想是用一种专门的编程语言,开发者只需要 ...

  8. NHibernate教程(18)--对象状态

    本节内容 引入 对象状态 对象状态转换 结语 引入 在程序运行过程中使用对象的方式对数据库进行操作,这必然会产生一系列的持久化类的实例对象.这些对象可能是刚刚创建并准备存储的,也可能是从数据库中查询的 ...

  9. 【1414软工助教】团队作业7——Alpha冲刺之事后诸葛亮 得分榜

    题目 团队作业7--Alpha冲刺之事后诸葛亮 往期成绩 个人作业1:四则运算控制台 结对项目1:GUI 个人作业2:案例分析 结对项目2:单元测试 团队作业1:团队展示 团队作业2:需求分析& ...

  10. Emacs操作指南