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. 1 0 1 2 0 0
  2. 1 0 0 0 1 1
  3. 0 0 0 0 0 0

Sample Output

  1. Collection #1:
  2. Can't be divided.
  3.  
  4. Collection #2:
  5. Can be divided.

Source

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. int sum;
  8. int num[7], dp[60000 + 60];
  9. template<class T > inline T getMax(const T &a, const T &b)
  10. {
  11. return a > b ? a : b;
  12. }
  13.  
  14. void ZeroOnePack(int cost, int weight, int V)
  15. {
  16. for (int i = V; i >= cost; i--) {
  17. dp[i] = getMax(dp[i], dp[i - cost] + weight);
  18. }
  19. }
  20.  
  21. void CompletePack(int cost, int weight, int V)
  22. {
  23. for (int i = cost; i <= V; i++) {
  24. dp[i] = getMax(dp[i], dp[i - cost] + weight);
  25. }
  26. }
  27.  
  28. void MultiPack(int cost, int weight, int V, int amount)
  29. {
  30. if (cost * amount >= V) {
  31. CompletePack(cost, weight, V);
  32. return;
  33. }
  34. int k = 1;
  35. while (k < amount) {
  36. ZeroOnePack(cost * k, weight * k, V);
  37. amount -= k;
  38. k *= 2;
  39. }
  40. ZeroOnePack(cost * amount, weight * amount, V);
  41. }
  42.  
  43. int main()
  44. {
  45. int t = 1;
  46. while (~scanf("%d", &num[1])) {
  47. sum = num[1];
  48. for (int i = 2; i <= 6; i++) {
  49. scanf("%d", &num[i]);
  50. sum += num[i] * i;
  51. }
  52. if (num[1] + num[2] + num[3] + num[4] + num[5] + num[6] == 0) break;
  53. printf("Collection #%d:\n", t++);
  54. if (sum % 2) {
  55. puts("Can't be divided.\n");
  56. continue;
  57. }
  58. sum >>= 1;
  59. memset(dp, 0, sizeof(dp));
  60. for (int i = 1; i <= 6; i++) {
  61. MultiPack(i, i, sum, num[i]);
  62. }
  63. if (dp[sum] != sum) {
  64. puts("Can't be divided.\n");
  65. } else
  66. puts("Can be divided.\n");
  67. }
  68. return 0;
  69. }

  

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. 基于CodeBlocks上的wxWidgets开发环境配置

    本文摘自http://www.cnzui.com/?p=962 在原文的基础上根据自己的实践做了一些修改 我的IDE为C::B 12.11,wxWidgets版本为wxWidgets-2.8.12 在 ...

  2. jOOQ

    jOOQ http://www.jooq.org/ jOOQ是个更不错的SQL解决方案. 你可以在Java中以一种类型安全的方式来书写SQL语句: // Typesafely execute the ...

  3. web dynpro message(备忘用)

    DATA lo_api_controller TYPE REF TO if_wd_controller. DATA lo_message_manager TYPE REF TO if_wd_messa ...

  4. Lucene入门的基本知识(四)

    刚才在写创建索引和搜索类的时候发现非常多类的概念还不是非常清楚,这里我总结了一下. 1 lucene简单介绍  1.1 什么是lucene  Lucene是一个全文搜索框架,而不是应用产品.因此它并不 ...

  5. 探索WebKit内核(一)------ 菜鸟起步

    为什么搞WebKit 如今研究WebKit的人越来越多,俺不能免俗,也增加当中.WebKit的火爆也是得益于浏览器和WebOS的混战,随着Palm WebOS, Chrome OS, Firefox ...

  6. BZOJ 3265 志愿者招募增强版 单

    标题效果:同1061 只是间隔为每种类型的志愿工作是多级 这是卡网络流量?未知 所有在所有的1061您将可以更改为在稍微改变- - #include <cmath> #include &l ...

  7. 一些as的配置

    打开Android Studio首选项对话框(Mac用户选择Android Studio菜单,Windows用户选择File → Settings菜单).分别展开Editor和Code Style选项 ...

  8. oracle口令管理之允许某个用户最多尝试三次登录

    如果一个用户连续三次登录失败,则锁定该用户两天,两天之后该用户才能重新登录. 创建profile文件: 更新账户: 三次登录失败后用户就会被锁定: 用户锁住之后要怎么给他解锁: 解锁之后就可以正常登录 ...

  9. Android(java)学习笔记168:Java异常分类

    Java异常可分为3种: (1)编译时异常:Java.lang.Exception (2)运行期异常:Java.lang.RuntimeException (3)错误:Java.lang.Error

  10. xshell连接本地Linux虚拟机!

    终端输入ifconfig获取本地虚拟机的IP地址; 安装openssh-server sudo apt-get install openssh-server 查看server是否启动: ps -ef ...