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. android开发学习 ------- git - 将代码回滚到任意版本

    不小心将一个东西错误提交到git - 远程仓库上 参考  https://www.cnblogs.com/wancy86/p/5848024.html 你的git可能关联了多个远程仓库,每个关联的代码 ...

  2. Lambda表达式的一些常用形式

    1.调用一个方法 prod=>EvaluteProduct(prod); 2.lambad表达式来表示一个多参数的委托,则必须把参数封装在括号内.语句如下: (prod,count)=>p ...

  3. Spring的校验(Validator)

    使用Spring校验的大体流程: 最首先要有配置文件xml的支持(spring_validate.xml).(当然在web.xml中要有对该xml的体现) <beans xmlns=" ...

  4. 小程序setData,视图层没有跟新

    如果你完全符合微信介绍的setData使用说明的情况下,发现视图层没有更新,你可以看看我的这种情况. 使用setData的时候,修改的是data中一个对象的值,然后这个对象里面第一层不能含有 numb ...

  5. 微软将于12月起开始推送Windows 10 Mobile

    [环球科技报道 记者 陈薇]据瘾科技网站10月8日消息,根据微软Lumia官方Faceboo发布的消息,新版系统Windows 10 Mobile 将会12月起陆续开始推送. 推送的具体时程根据地区. ...

  6. TCP socket如何判断连接断开

    http://blog.csdn.net/zzhongcy/article/details/21992123 SO_KEEPALIVE是系统底层的机制,用于系统维护每一个tcp连接的. 心跳线程属于应 ...

  7. js将时间戳装换成日期格式

    13位时间戳改为yyyy-MM-dd HH-mm-ss 格式 目标时间戳:1516324500000 formatDateTime (unix) { // 转换时间戳 var date = new D ...

  8. js 根据指定个数切割数组

    Part.1 问题 写项目时,遇到需要前端做 假分页 的问题:后端会将数据全部返回,前端自己做分页 Part.2 思路 拿到后端全部返回的数据后,按照 产品需求  进行分页,如每页显示 10 条数据为 ...

  9. vc++创建多线程应用

    构建线程参数结构体: typedef struct { int nIndex; HANDLE hThread; int param1; ... }ThreadParam; 创建线程数组: Thread ...

  10. python爬虫---从零开始(五)pyQuery库

    什么是pyQuery: 强大又灵活的网页解析库.如果你觉得正则写起来太麻烦(我不会写正则),如果你觉得BeautifulSoup的语法太难记,如果你熟悉JQuery的语法,那么PyQuery就是你最佳 ...