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. Collections.synchronizedList与CopyOnWriteArrayList比较

    1.单线程方式 2.多线程版本,不安全的 ArrayList 3.多线程版本,线程安全,CopyOnWriteArrayList()方式 4.多线程版本,线程安全,Collections.synchr ...

  2. SpringBoot之使用Druid连接池,SQL监控和spring监控

    项目结构 1.引入maven依赖 <dependencies> <dependency> <groupId>org.springframework.boot< ...

  3. 12-ESP8266 SDK开发基础入门篇--PWM,呼吸灯

    https://www.cnblogs.com/yangfengwu/p/11094085.html PWM其实没有什么,就是看着官方给的API,,,然后就是用呗 对了,其实对于RTOS SDK版本的 ...

  4. JMeter学习2

    JMeter学习(四)参数化 参数化:录制脚本中有登录操作,需要输入用户名和密码,假如系统不允许相同的用户名和密码同时登录,或者想更好的模拟多个用户来登录系统. 这个时候就需要对用户名和密码进行参数化 ...

  5. vue-cli 中的 eslint 规则说明

    "no-alert": 0,//禁止使用alert confirm prompt "no-array-constructor": 2,//禁止使用数组构造器 & ...

  6. 微信小程序地图组件

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

  7. C博客作业02—循环结构

    0.展示PTA总分(0----2) 截图展示2次题目集:单循环和嵌套循环题目集,排名分数截图. 1.本章学习总结(2分) 1.1 学习内容总结 整理这两周学习主要知识点,并能对每个知识点介绍简单案例或 ...

  8. Eclipse安装jbpm插件

    1.1   eclipse mar 和neon有什么区别? Eclipse 是一个开放源代码的.基于Java的可扩展开发平台.就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境. . ...

  9. 【Qt开发】vs2017+qt5.x编译32位应用

    概述 最近有同学私信我,问如何使用vs2017+qt5.10编译出32位的应用,需要使用msvc2017_x86的插件,然而qt官网并没有提供,只能使用源码编译生成msvc2017_x86插件,使用n ...

  10. 公网IP地址就一定是A类地址和B类地址吗?那C类地址就一定是私有地址吗?

    A,B,C三类中既有公网地址,也有私网地址:在A类地址中,10.0.0.0-10.255.255.255是私有地址.在B类地址中,172.16.0.0-172.31.255.255是私有地址.在C类地 ...