Dividing
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 63980   Accepted: 16591

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.

Source

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; int sum;
int num[7], dp[60000 + 60];
template<class T > inline T getMax(const T &a, const T &b)
{
return a > b ? a : b;
} void ZeroOnePack(int cost, int weight, int V)
{
for (int i = V; i >= cost; i--) {
dp[i] = getMax(dp[i], dp[i - cost] + weight);
}
} void CompletePack(int cost, int weight, int V)
{
for (int i = cost; i <= V; i++) {
dp[i] = getMax(dp[i], dp[i - cost] + weight);
}
} void MultiPack(int cost, int weight, int V, int amount)
{
if (cost * amount >= V) {
CompletePack(cost, weight, V);
return;
}
int k = 1;
while (k < amount) {
ZeroOnePack(cost * k, weight * k, V);
amount -= k;
k *= 2;
}
ZeroOnePack(cost * amount, weight * amount, V);
} int main()
{
int t = 1;
while (~scanf("%d", &num[1])) {
sum = num[1];
for (int i = 2; i <= 6; i++) {
scanf("%d", &num[i]);
sum += num[i] * i;
}
if (num[1] + num[2] + num[3] + num[4] + num[5] + num[6] == 0) break;
printf("Collection #%d:\n", t++);
if (sum % 2) {
puts("Can't be divided.\n");
continue;
}
sum >>= 1;
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= 6; i++) {
MultiPack(i, i, sum, num[i]);
}
if (dp[sum] != sum) {
puts("Can't be divided.\n");
} else
puts("Can be divided.\n");
}
return 0;
}

  

POJ 1014 Dividing 多重背包的更多相关文章

  1. Hdu 1059 Dividing & Zoj 1149 & poj 1014 Dividing(多重背包)

    多重背包模板- #include <stdio.h> #include <string.h> int a[7]; int f[100005]; int v, k; void Z ...

  2. POJ 1014 Dividing(多重背包, 倍增优化)

    Q: 倍增优化后, 还是有重复的元素, 怎么办 A: 假定重复的元素比较少, 不用考虑 Description Marsha and Bill own a collection of marbles. ...

  3. POJ 1014 Dividing (多重可行性背包)

    题意 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两份的总价值相等,其中一个物品不能切开,只能分给其中的某一方,当输入六个0是( ...

  4. POJ 1014 Dividing(多重背包+二进制优化)

    http://poj.org/problem?id=1014 题意:6个物品,每个物品都有其价值和数量,判断是否能价值平分. 思路: 多重背包.利用二进制来转化成0-1背包求解. #include&l ...

  5. POJ 1014 Dividing(多重背包)

    Dividing   Description Marsha and Bill own a collection of marbles. They want to split the collectio ...

  6. DFS(DP)---POJ 1014(Dividing)

    原题目:http://poj.org/problem?id=1014 题目大意: 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两 ...

  7. hdu 1059 Dividing(多重背包优化)

    Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  8. POJ 1742 Coins(多重背包, 单调队列)

    Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...

  9. POJ 2392【多重背包】

    题意: k个块,给出每个块的高度hi,数量ci,不能超过的高度: 求这些块可以组成的最大高度一个. 思路: 大致可看这个题是一个背包,背包的承重是高度. 对于每个物品,有他的价值是高度,还有限定的数量 ...

随机推荐

  1. 电源管理之pmu驱动分析

    电源管理芯片可以为多设备供电,且这些设备电压电流有所不同.为这些设备提供的稳压器代码模型即为regulator. 说白了regulator就是稳压器,它提供电源供给.简单的可以gpio操作,高电平开电 ...

  2. Java--创建线程及常用方法

    继承java.lang.Thread类--Thread类代表线程类  它的常用方法如下: static Thread currentThread():返回当前正在运行的线程对象的引用. static ...

  3. Python默认编码错误SyntaxError: Non-ASCII character '\xe5'之解决方法

    在编写Python时,当使用中文输出或注释时运行脚本,会提示错误信息: SyntaxError: Non-ASCII character '\xe5' in file ******* 解决方法: py ...

  4. listview加载图片显示

    Adapter:   ---- //adapter的构造方法:   参数1 为url数组: public static String[] mList;// 讲url保村在静态的String[] 中 在 ...

  5. AX2012服务器配置--Windows Server 2012 配置远程桌面同一帐户允许多session同时登录

    网上找了很多关于设置远程桌面最大连接数的文章,大都是说先要到控制面板的管理工具中设置远程桌面会话主机等,大体和我之前的文章<设置WINDOWS SERVER 2008修改远程桌面连接数>里 ...

  6. Java工作队列和线程池

    背景    最近的需要做一个与设备通信的web项目.当然,我们需要写好与设备之间的通信协议(socket).大致的时序逻辑时:当用户用浏览器点击页面某一控件后,它就向后台发送一个post请求,后台解析 ...

  7. 如何解决因为找不到Notepad++的安装路径而导致的不能更新CS-Script的问题

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:如何解决因为找不到Notepad++的安装路径而导致的不能更新CS-Script的问题.

  8. mvc分层的原理

    首先这是现在最基本的分层方式,结合了SSH架构.1.modle层就是对应的数据库表的实体类.2.Dao层是使用了hibernate连接数据库.操作数据库(增删改查).3.Service层:引用对应的D ...

  9. mybatis update语句参数正常, 数据没有更新

    昨天做的一个功能, 更新每天支付宝账号的所有订单收益 ,有一个update 语句:

  10. 用高德地图API 通过详细地址获得经纬度

    http://cloud.sinyway.com/Service/amap.html http://restapi.amap.com/v3/geocode/geo?key=xxxxxxxxxxxxxx ...