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. string长度问题

    原文地址: https://toutiao.io/shares/2029578/url String是Java中很重要的一个数据类型,除了基本数据类型以外,String是被使用的最广泛的了,但是,关于 ...

  2. MAT022 Foundations of Statistics

    MAT022 Foundations of Statistics and Data Science Summative Assessment 2019/20MAT022 Foundations of ...

  3. How Open Source Became The Default Business Model For Software

    https://www.forbes.com/sites/forbestechcouncil/2018/07/16/how-open-source-became-the-default-busines ...

  4. SpringMVC_原理(转)

    在整个Spring MVC框架中,DispatcherServlet处于核心位置,它负责协调和组织不同组件完成请求处理并返回响应的工作.具体流程为:1)客户端发送http请求,web应用服务器接收到这 ...

  5. Centos7安装Spark2.4

    准备 1.hadoop已部署(若没有可以参考:Centos7安装Hadoop2.7),集群情况如下(IP地址与之前文章有变动): hostname IP地址 部署规划 node1 172.20.0.2 ...

  6. [CSS] prefers-reduced-motion

    The prefers-reduced-motion CSS media feature is used to detect if the user has requested that the sy ...

  7. .pdb 文件的内部结构

    粗略察看一 下.pdb 文件,会发现在其起始位置存放的是这样一个字符串“Microsoft C/C++ program database 2.00”.可以看出 PDB 是 Program Databa ...

  8. oracle的一些状态查询

  9. python 报can't subtract offset-naive and offset-aware datetimes错误

    两个时间一个含时区,一个不含时区

  10. Phalcon框架的编译安装 内存不足的解决办法

    对症解决 有两种解决方法,一种是提升ECS系统内存.但是却要真金白银跟阿里云去购买的.另一种,则是手动创建swap交换文件.下面来介绍第二种方法. 第一步:首先确定系统是否已经开启swap交换分区: ...