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. Hibernate缓存简介和对比、一级缓存、二级缓存详解

    一.hibernate缓存简介 缓存的范围分为3类:  1.事务范围(单Session即一级缓存)     事务范围的缓存只能被当前事务访问,每个事务都有各自的缓存,缓存内的数据通常采用相互关联的对象 ...

  2. Intellij IDEA 与 Gitlab 实现代码上传与下载

    整体流程:1.download project2.deposit project structure and set default server3.configure tomcat 2 steps4 ...

  3. AtCoder Beginner Contest 125 解题报告

    那天晚上刚好有事就咕了. 最近的那一场E题还不会写.F题全场又只过了三个?留坑吧... A - Biscuit Generator #include <cstdio> using name ...

  4. 学习Spring-Data-Jpa(十三)---动态查询接口JpaSpecificationExecutor

    1.JpaSpecificationExecutor JPA2引入了一个criteria API,我们可以使用它以编程的形式构建查询.通过编写criteria,动态生成query语句.JpaSpeci ...

  5. LeetCode 1215. Stepping Numbers

    原题链接在这里:https://leetcode.com/problems/stepping-numbers/ 题目: A Stepping Number is an integer such tha ...

  6. (尚022)Vue案例_初始化显示(十分详细!!!)

    项目结构目录 所需资料: comment_page文件夹: ====================================================================== ...

  7. 使用vault pki engine 方便的管理证书

    vault 是一个很方便的secret .敏感数据管理工具,当前的版本已经包含了UI,使用起来很方便 以下演示一个简单的pki 管理 项目使用docker-compose 运行,为了简单使用单机开发模 ...

  8. luoguP1576 最小花费

    LOL新英雄皮肤弹丸天使点击就送 两种做法: 1.边的权值为手续费z,从b向a跑最短路,边跑边处理答案 2.边的权值为汇率,从a向b跑最短路,边跑边处理答案 #include<cstdio> ...

  9. 洛谷P1902 刺杀大使

    题目 二分加广搜 #include <bits/stdc++.h> using namespace std; int n, m, l, r, p[1001][1001], vis[1001 ...

  10. 阿里云部署自己的web服务器

    阿里云部署自己的web服务器 [外链图片转存失败(img-GIKNTPPx-1564287221547)(https://upload-images.jianshu.io/upload_images/ ...