Time Limit: 1000MS              Memory Limit: 10000K
Total Submissions: Accepted:

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


Sample Output

Collection #:
Can't be divided. Collection #:
Can be divided.

解题思路

因为是第一道例题,所以只做一些大佬AC代码的笔记,以下是参考博客的讲解:

如果总价值为奇数,那么肯定是不能分的。如果总价值为偶数,也不一定能分,因为一个弹珠是不能被拆分的。

以总价值的1/2为背包容量,进行动态规划求解。还用了二进制优化的方法,可以说这道题目是简单的多重背包吧。

dp[x]=1表示这些弹珠可以凑出价值为x的部分,否则就是不能凑成价值为x的部分。

参考博客

https://blog.csdn.net/u011561033/article/details/39526897

 AC代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int n,sum,i,j,k,cs=;
int a[];
int dp[]; //大于6*20000
while(scanf("%d",&a[])!=-)
{
sum=a[];
for(i=; i<=; i++)
{
scanf("%d",&a[i]);//i为1,2,3,4,5,6,存储各类弹珠数量
sum+=a[i]*i;
}
if(sum<=)break; //没有弹珠,即最后一行的情况
memset(dp,,sizeof(dp));
printf("Collection #%d:\n",cs++);
if(sum%)printf("Can't be divided.\n");//奇数一定不可分
else
{
dp[]=; //初始化0,0肯定是可分出来的
sum/=; //背包容量
for(i=; i<=; i++)
{
if(a[i]==)continue;
for(k=; k<=a[i]; k*=)//先处理偶数情况,将偶数分值归到dp中,倒序同样是因为还原成了0-1背包问题,将大物品拆分成了各个小物品
{
for(j=sum; j>=; j--)
{
if(dp[j]==||j+i*k>sum)continue;
dp[j+i*k]=;
}
a[i]-=k;
}
if(a[i]>)//处理剩余的数的情况,比如10分为1,2,4之后还剩3
{
for(j=sum; j>=; j--)
{
if(dp[j]==||j+i*a[i]>sum)continue;
dp[j+i*a[i]]=;
}
}
}
if(dp[sum]==)
printf("Can be divided.\n");
else printf("Can't be divided.\n");
}
printf("\n");
}
return ;
}

POJ 1014 Dividing(入门例题一)的更多相关文章

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

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

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

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

  3. POJ 1014 Dividing

    Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 66032 Accepted: 17182 Descriptio ...

  4. POJ 1014 Dividing(多重背包)

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

  5. POJ 1014 Dividing 多重背包

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

  6. 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 ...

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

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

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

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

  9. POJ 1014 Dividing 背包

    二进制优化,事实上是物体的分解问题. 就是比方一个物体有数量限制,比方是13,那么就须要把这个物体分解为1. 2, 4, 6 假设这个物体有数量为25,那么就分解为1, 2, 4. 8. 10 看出规 ...

随机推荐

  1. 项目Alpha冲刺(团队)-第十天冲刺

    格式描述 课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(团队) 团队名称:为了交项目干杯 作业目标:描述第十天冲刺的项目进展.问题困难.心得体会 队员姓名与学号 队员学号 ...

  2. Nginx——报错汇总

    前言 记录NGINX的错误 错误 nginx: [emerg] unknown directive "erver" in /usr/local/nginx/conf/vhost/d ...

  3. intellij idea参数提示param hints

    https://jingyan.baidu.com/article/5225f26bae80f4e6fa0908b1.html

  4. python写入csv

    import xlwtimport csvnewfile=open("wu.csv","w",newline="")filewriter=c ...

  5. c#语言学习笔记(1)

    环境:VS Express 2013 for Desktop 也可以vs社区版,不过学习的话,Express本版做一些小的上位机工具应该是够用了 学习的网站:https://www.runoob.co ...

  6. solidworks 学习 (四)

    旋钮三维建模

  7. Chocolatey 方便的windows 包管理工具

    windows 在包管理上一般大家都是网上下载二进制文件或者就是通过软件管家进行安装,这些对于开发人员可能就有点不是 很专业了, Chocolatey 是一个不错的windows 软件包管理工具 安装 ...

  8. Linux 系统管理——磁盘管理及文件系统实例

    1.为主机新增两块30GB的SCSI硬盘 2.划分3个主分区,各5GB,剩余空间作为扩展分区 3.在扩展分区中建立2个逻辑分区,容量分别为2GB.10GB 4.将第一个逻辑分区的类型改为swap 5. ...

  9. 微信小程序地图组件

    index.wxml <map id="map" markers="{{markers}}" longitude="{{longitude}}& ...

  10. Cocos CreatorUI系统下

    若本号内容有做得不到位的地方(比如:涉及版权或其他问题),请及时联系我们进行整改即可,会在第一时间进行处理. 请点赞!因为你们的赞同/鼓励是我写作的最大动力! 欢迎关注达叔小生的简书! 这是一个有质量 ...