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. django全文检索

    -------------------linux下配置操作1.在虚拟环境中依次安装包 1.pip install django-haystack haystack:django的一个包,可以方便地对m ...

  2. spring boot / cloud (十六) 分布式ID生成服务

    spring boot / cloud (十六) 分布式ID生成服务 在几乎所有的分布式系统或者采用了分库/分表设计的系统中,几乎都会需要生成数据的唯一标识ID的需求, 常规做法,是使用数据库中的自动 ...

  3. 配置HAProxy支持https协议

    author:JevonWei 版权声明:原创作品 实现http重定向到https HAProxy 创建CA证书 [root@HAProxy ~]# cd /etc/haproxy/ [root@HA ...

  4. 利用 VMWare 搭建随机拓扑网络

    这篇文章是计算机网络上机实验课的作业. 实验任务:利用 VMWare 搭建一个由 5 个主机组成的随机拓扑的网络.要求该网络中至少有 2 个子网,两个路由器 .实验的网络拓扑图如下: 网络中有两个路由 ...

  5. CSS3弹性盒模型 display:box

    刚开始做网页时就有一个困惑,为什么display:block只能垂直排列,如果要水平排列就要使用float:left等方式.这种方法最难受的当然是当子元素的数量改变时,需要去修改子元素的宽度使重新适应 ...

  6. 201521123065 《Java程序设计》第4周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容 类设计:属性设计类型为priate并初始化. 文档注释:以/*开始,以*/结束. 继承:存在的 ...

  7. 201521123015 《Java程序设计》第2周学习总结

    1.本章学习总结 (1)学习了枚举,数组等方法 (2)通过实验内容的讲解,解决了一些问题 (3)进一步运用和了解码云 书面作业 1.使用Eclipse关联jdk源代码,并查看String对象的源代码( ...

  8. 201521123073 《Java程序设计》第1周学习总结

    1.本章学习总结 你对于本章知识的学习总结 1.Java中使用Scanner处理输入,需要注意如下几个地方 程序开头必须import java.util.Scanner导入Scanner类. 使用Sc ...

  9. Java第十三周总结

    1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 书面作业 1. 网络基础 1.1 比较ping www.baidu.com与ping cec.jmu ...

  10. java课程设计---计算器(201521123020 邱伟达)

    1.团队课程设计博客链接 http://www.cnblogs.com/br0823/p/7064407.html 2.个人负责模板或任务说明 1.初始化按键 2.实现加减乘除开方乘方等运算 3.每个 ...