http://poj.org/problem?id=1014

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.

大致题意:

有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,使两份的总价值相等,其中一个物品不能切开,只能分给其中的某一方,当输入六个0是(即没有物品了),这程序结束,总物品的总个数不超过20000

输出:每个测试用例占三行:

第一行: Collection #k: k为第几组测试用例

第二行:是否能分(具体形式见用例)

第三行:空白(必须注意,否则PE)

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int dp[],w[],v[];
int V,K=;
void wpack(int w)
{
for(int i=w; i<=V; i++)
{
if(dp[i-w]+w>dp[i])
dp[i]=dp[i-w]+w;
}
}
void pack1(int w)
{
for(int i=V; i>=w; i--)
{
if(dp[i-w]+w>dp[i])
dp[i]=dp[i-w]+w;
}
}
void Mul(int w,int num)
{
if(w*num>=V)
{
wpack(w);
return ;
}
int k=;
while(k<num)
{
pack1(k*w);
num-=k;
k=k*;
}
pack1(num*w);
}
int main()
{
while(scanf("%d%d%d%d%d%d",&w[],&w[],&w[],&w[],&w[],&w[])!=EOF)
{
K++;
V=w[]+w[]+w[]+w[]+w[]+w[];
if(V==) break;
V=w[]*+w[]*+w[]*+w[]*+w[]*+w[]*;
if(V%==)
{
printf("Collection #%d:\n",K);
printf("Can't be divided.\n\n");
}
else
{
V=V/;
memset(dp,,sizeof(dp));
for(int i=; i<=; i++)
{
Mul(i,w[i]);
}
if(dp[V]==V)
{
printf("Collection #%d:\n",K);
printf("Can be divided.\n\n");
}
else
{
printf("Collection #%d:\n",K);
printf("Can't be divided.\n\n");
}
}
}
return ;
}

POJ1014:Dividing(多重背包)的更多相关文章

  1. POJ1014(多重背包)

    Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65044   Accepted: 16884 Descri ...

  2. hdu 1059 Dividing(多重背包优化)

    Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  3. poj1014 dp 多重背包

    //Accepted 624 KB 16 ms //dp 背包 多重背包 #include <cstdio> #include <cstring> #include <i ...

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

  5. poj1014 hdu1059 Dividing 多重背包

    有价值为1~6的宝物各num[i]个,求能否分成价值相等的两部分. #include <iostream> #include <cstring> #include <st ...

  6. hdu 1059 Dividing 多重背包

    点击打开链接链接 Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  7. Dividing 多重背包 倍增DP

    Dividing 给出n个物品的价值和数量,问是否能够平分.

  8. POJ 1014 Dividing 多重背包

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

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

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

  10. POJ 1014 / HDU 1059 Dividing 多重背包+二进制分解

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

随机推荐

  1. <转>特征工程(二)

    出处: http://blog.csdn.net/longxinchen_ml/article/details/50493845, http://blog.csdn.net/han_xiaoyang/ ...

  2. 【十大算法实现之naive bayes】朴素贝叶斯算法之文本分类算法的理解与实现

    关于bayes的基础知识,请参考: 基于朴素贝叶斯分类器的文本聚类算法 (上) http://www.cnblogs.com/phinecos/archive/2008/10/21/1315948.h ...

  3. 【大数据系列】hive修改默认的derby数据库

    <?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml- ...

  4. intellij2016.03激活

    激活的时候采用server的方式 :http://jetbrains.tech

  5. Python GUI编程之WxPython

    https://en.wikipedia.org/wiki/WxPython 官网: Overview of wxPython: https://wxpython.org/pages/overview ...

  6. java(1) 编程基础

    1.classpath 环境变量 * 当java虚拟机需要运行一个类时,会在classpath 环境变量中所定义的路径下寻找所需的class文件 2.java 的基本语法 * java 语言是严格区分 ...

  7. C语言位操作--不用中间变量交换两数值

    1.使用加法与减法交换两数值: #define SWAP(a, b) ((&(a) == &(b)) || \ (((a) -= (b)), ((b) += (a)), ((a) = ...

  8. [通信] C#多线程Socket-文件传输

    FileSendClient : Form1.cs using System; using System.IO; using System.Net; using System.Net.Sockets; ...

  9. Android 获取View中的组件

    可以把这个view强转成ViewGroup对象,再通过getChildAt(0),getChildAt(1) 获取之后AddView可能会报错:IllegalStateException: The s ...

  10. Office2007 每次打开斗需要检查 【配置进度】

    打开C:\Program Files\Common Files\microsoft shared\OFFICE12\Office Setup Controller目录下将SETUP.EXE 重命名为 ...