Dividing
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 71453   Accepted: 18631

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. 一件物品有a[i]件,总价值为i*a[i],为能否平分,多重背包问题
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
ll dp[];
ll a[],b;
ll ans,pos,n,m;
int main()
{
int count=;
while(scanf("%d",&a[]))
{
ans=a[];
mem(dp);
dp[]=;
for(int i=;i<=;i++)
{
scanf("%d",&a[i]);
ans+=i*a[i];
}
if(!ans) break;
printf("Collection #%d:\n",count++);
if(ans%)
{
printf("Can't be divided.\n\n");
continue;
}
pos=ans/;
for(int i=;i<=;i++)
{
if(!a[i]) continue;
for(int t=;a[i];t*=)
{
if(a[i]<t) t=a[i];
for(int k=pos;k>=t*i;k--)
{
if(dp[k-t*i]) dp[k]=;
}
a[i]-=t;
}
}
if(dp[pos]) printf("Can be divided.\n\n");
else printf("Can't be divided.\n\n");
}
return ;
}

POj 2159 Dividing的更多相关文章

  1. POJ 2373 Dividing the Path(DP + 单调队列)

    POJ 2373 Dividing the Path 描述 农夫约翰的牛发现,在他的田里沿着山脊生长的三叶草是特别好的.为了给三叶草浇水,农夫约翰在山脊上安装了喷水器. 为了使安装更容易,每个喷头必须 ...

  2. POJ 2159 Ancient Cipher 难度:0

    题目链接:http://poj.org/problem?id=2159 #include <cstring> #include <cstdio> #include <cc ...

  3. Poj 2159 / OpenJudge 2159 Ancient Cipher

    1.链接地址: http://poj.org/problem?id=2159 http://bailian.openjudge.cn/practice/2159 2.题目: Ancient Ciphe ...

  4. POJ 1014 Dividing

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

  5. POJ 1014 Dividing(多重背包)

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

  6. poj 2373 Dividing the Path

    Dividing the Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2858   Accepted: 1064 ...

  7. POJ 1014 Dividing 多重背包

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

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

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

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

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

随机推荐

  1. js实现图片上传后即时预览

    //关于FileReader对象 http://blog.csdn.net/zk437092645/article/details/8745647 <!DOCTYPE html> < ...

  2. 为什么使用Nginx & Nginx的使用

    Nginx在Windows平台的配置: 什么是Nginx? 根据前面的对比,我们可以了解到Nginx是一个http服务器.是一个使用c语言开发的高性能的http服务器及反向代理服务器.Nginx是一款 ...

  3. 使用let's encrypt为你的Ubuntu14+nginx网站保驾护航!

    finch最近正在研究一个新的网站系统,闲的没事想搞搞ssl,结果搞了两天,遇到很多问题,现在记录并分享一下经验. 环境之前搭建好了是Ubuntu14+nginx+php5+mysql 现在开始使用l ...

  4. NET Core微服务之路:实战SkyWalking+Exceptionless体验生产下追踪系统

    原文:NET Core微服务之路:实战SkyWalking+Exceptionless体验生产下追踪系统 前言 当一个APM或一个日志中心实际部署在生产环境中时,是有点力不从心的. 比如如下场景分析的 ...

  5. BZOJ——T 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛

    http://www.lydsy.com/JudgeOnline/problem.php?id=1612 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1 ...

  6. SpringMVC拓展

    ### 原生SpringMVC有如下缺陷 参数的JSON反序列化只支持@RequestBody注解,这意味着不能在controller方法中写多个参数,如下代码是不对的 public Map test ...

  7. Effective JavaScript Item 40 避免继承标准类型

    本系列作为Effective JavaScript的读书笔记. ECMAScript标准库不大.可是提供了一些重要的类型如Array,Function和Date.在一些场合下.你或许会考虑继承当中的某 ...

  8. vim 基础学习之文件跳转

    1. ''-当前文件上次跳转之前的位置2. '.-当前文件上次修改的位置,只要是发生了可能导致变化的命令操作就会被标记,哪怕实际结果没有变化3. '^-当前文件上次插入的位置,只要是发生了插入操作命令 ...

  9. Linux 内核源码(kernel source)

    查看内核的发行版:uname -r(--kernel-release) $ uname -r 4.4.0-78-generic 内核源码所在的位置:/usr/src $ cd /usr/src $ l ...

  10. CentOS7 启动[root@localhost ~]# systemctl start docker Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for de

    1).在linux虚拟机上安装docker步骤:1.检查内核版本,必须是3.10及以上uname ‐r2.安装dockeryum install docker3.输入y确认安装4.启动docker[r ...