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. PostgreSQL Replication之第七章 理解Linux高可用(5)

    7.5 高可用性是所有冗余 让我们从一个不同的角度看一下前面的混合超市的例子.为了处理大量顾客无需长排队,无需关闭超市,混合超市雇用更多的出纳员以及安装许多(甚至更多)的收银机. 这样,如果一个收银机 ...

  2. <Sicily>数字反转

    一.题目描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零(参见样例2). 二.输入 输入共 1 行, ...

  3. 使用freerdp远程连接Windows桌面(转载)

    使用freerdp远程连接Windows桌面 之前使用的是rdesktop,但是由于其不支持NLA认证,便不能登录公司的电脑.为此,现在使用freerdp——这是package的名字,实际的可执行程序 ...

  4. JS获取当前时间(YYYY-MM-DD ),element显示默认当前时间,显示默认昨天,显示默认上个月

    原文链接:点我 进来的随便看看,或许有帮助 vue+element-ui   datepicker 设置默认日期用的框架是vue+element-ui ,以下是时间控件 <el-form-ite ...

  5. 移动和PC的适配

    <script> //mode 移动端的适配方式 按需 传参 目前只有两种 px和rem (function(win, doc, mode) { var std = 750; if(/(i ...

  6. BZOJ 4241 历史研究(分块)

    题意 题解 #include<iostream> #include<cstring> #include<cstdio> #include<cmath> ...

  7. cookie 实现记住用户名演示 通过代码迅速理解cookie

    // 登录页 可直接 tomcat部署 测试 1 package com.itheima.login; import java.io.IOException; import java.io.Print ...

  8. Win7下JDK环境变量设置批处理(转)

    每次重装系统之后,都需要重新设置JDK环境变量 项目中有些入门小白看了网络上的设置环境变量的文章还是会设置错环境变量 提供一个批处理能够在Win7下运行(使用了setx命令),自动设置环境变量. cl ...

  9. 洛谷 P1542 包裹快递

    P1542 包裹快递 题目描述 小K成功地破解了密文.但是乘车到X国的时候,发现钱包被偷了,于是无奈之下只好作快递员来攒足路费去Orz教主…… 一个快递公司要将n个包裹分别送到n个地方,并分配给邮递员 ...

  10. 【转】CentOS下firefox安装flash说明

    http://www.cnblogs.com/lamper/archive/2013/01/16/2862254.htm CentOS下自带了firefox,但没有flash插件的,按它自己的提示安装 ...