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. 3.Vue 实例

    创建一个 Vue 实例 每个 Vue 应用都是通过用 Vue 函数创建一个新的 Vue 实例开始的: var vm = new Vue({ // 选项 }) 虽然没有完全遵循 MVVM 模型,但是 V ...

  2. Django API接口FK ManyTo Many 模板

    Url from django.contrib import admin from django.urls import path, re_path from django.urls import i ...

  3. 多项式求逆入门 板题(Luogu P4238)

    下面是代码,推导详见 传送门 模板Code #include <cstdio> #include <cstring> #include <algorithm> us ...

  4. php+tcpdf如何把生成的pdf文件保存在服务端

    tcpdf组件目前应用得非常广泛,但是对于如何把生成的pdf文件自动保存在服务端却很少有人提及.让我们先来看看标准输出代码:   //服务器存档模式 $pdf->Output('output.p ...

  5. Codeforces Round #493 (Div. 2) 【A,B,C】

    简单思维题 #include<bits/stdc++.h> using namespace std; #define int long long #define inf 0x3f3f3f3 ...

  6. pgloader 学习(五)pgloader 参考手册

    pgloader将各种来源的数据加载到PostgreSQL中.它可以转换动态读取的数据,并在加载前后提交原始SQL. 它使用COPY PostgreSQL协议将数据流式传输到服务器,并通过填充一对re ...

  7. callbag js callback 标准-支持轻量级观测以及迭代

    callbag 是一个js 的回调标准,方便开发支持观测以及迭代的代码 类似已经有好多的实现了 callbag-basics 比rxjs 以及xstream 还快 wonka 说明 基于标准的开发,对 ...

  8. 【loj2339】【WC2018】通道

    题目 三棵带边权的树,求 \[ dis1(u,v) + dis2(u,v) + dis3(u,v) \] 的最大值 \(1 \le n \le 10^5\) 题解 对\(T_1\)做边分治,把分治边的 ...

  9. C语言第一篇博客

    你对网络专业或者计算机专业了解是怎样? 进行网络安全,防止信息泄露. 你了解C语言么?C语言主要应用有哪些? C语言简言之就是一门计算机的编程语言. C语言主要应用于应用软件,服务器端开发,系统软件和 ...

  10. Java两个数的和

    给你一个整数数组, 返回两个数的指数,使他们加起来等于给定的数. 你可以假设只有一个解, 并且相同的元素不会用两次. 比如: 给定数组 = [2, 7, 11, 15], 目标数 = 9, 因为[0] ...