Dividing
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 68769   Accepted: 17955

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.

Source


裸题
回顾一下两种做法
然而O(nv)反而被二进制拆分虐了
//
// main.cpp
// poj1014
//
// Created by Candy on 9/21/16.
// Copyright © 2016 Candy. All rights reserved.
// #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=2e4*+;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n[],sum=,cnt=;
int f[N];
inline void zp(int v){
for(int i=sum;i>=v;i--) f[i]|=f[i-v];
}
inline void cp(int v){
for(int i=v;i<=sum;i++) f[i]|=f[i-v];
}
void mp(int v,int c){
if(c*v>sum) {cp(v);return;}
int k=;
while(k<c){
zp(k*v);
c-=k;
k*=;
}
zp(c*v);
}
int main(int argc, const char * argv[]) {
while(true){
++cnt; sum=; memset(f,,sizeof(f)); f[]=;
int flag=;
for(int i=;i<=;i++) {n[i]=read();sum+=i*n[i];if(n[i]) flag=;}
if(flag) break;
if(sum%) {
printf("Collection #%d:\nCan't be divided.\n\n",cnt);
continue;
}
sum/=;
for(int i=;i<=;i++) mp(i,n[i]);
if(!f[sum]) printf("Collection #%d:\nCan't be divided.\n\n",cnt);
else printf("Collection #%d:\nCan be divided.\n\n",cnt);
} return ;
}
//
// main.cpp
// poj1014
//
// Created by Candy on 9/21/16.
// Copyright © 2016 Candy. All rights reserved.
// #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=2e4*+;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n[],v[],sum=,cnt=;
int f[][N];
void mpAble(){
memset(f,-,sizeof(f));
f[][]=;
for(int i=;i<=;i++){
for(int j=;j<=sum;j++){
if(f[i-][j]>=) f[i][j]=n[i];
else f[i][j]=-;
}
for(int j=v[i];j<=sum;j++)
if(f[i][j-v[i]]>) f[i][j]=max(f[i][j],f[i][j-v[i]]-);
}
}
int main(int argc, const char * argv[]) {
while(true){
++cnt; sum=; memset(f,,sizeof(f));
int flag=;
for(int i=;i<=;i++){
n[i]=read();v[i]=i;sum+=i*n[i];
if(n[i]) flag=;
}
if(flag) break;
if(sum%) {
printf("Collection #%d:\nCan't be divided.\n\n",cnt);
continue;
}
sum/=;
mpAble();
if(f[][sum]==-) printf("Collection #%d:\nCan't be divided.\n\n",cnt);
else printf("Collection #%d:\nCan be divided.\n\n",cnt);
} return ;
}

POJ1014Dividing[多重背包可行性]的更多相关文章

  1. POJ1742 Coins[多重背包可行性]

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 34814   Accepted: 11828 Descripti ...

  2. POJ1276Cash Machine[多重背包可行性]

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32971   Accepted: 11950 De ...

  3. 【DP|多重背包可行性】POJ-1014 Dividing

    Dividing Time Limit: 1000MS Memory Limit: 10000K Description Marsha and Bill own a collection of mar ...

  4. poj1742硬币——多重背包可行性

    题目:http://poj.org/problem?id=1742 贪心地想,1.如果一种面值已经可以被组成,则不再对它更新: 2.对于同一种面值的硬币,尽量用较少硬币(一个)更新,使后面可以用更多此 ...

  5. POJ 1742 Coins 【多重背包DP】

    题意:有n种面额的硬币.面额.个数分别为A_i.C_i,求最多能搭配出几种不超过m的金额? 思路:dp[j]就是总数为j的价值是否已经有了这种方法,如果现在没有,那么我们就一个个硬币去尝试直到有,这种 ...

  6. poj1742 多重背包的可行性问题

    http://poj.org/problem? id=1742 Description People in Silverland use coins.They have coins of value ...

  7. 背包问题(01背包,完全背包,多重背包(朴素算法&&二进制优化))

    写在前面:我是一只蒟蒻~~~ 今天我们要讲讲动态规划中~~最最最最最~~~~简单~~的背包问题 1. 首先,我们先介绍一下  01背包 大家先看一下这道01背包的问题  题目  有m件物品和一个容量为 ...

  8. BZOJ.3425.[POI2013]Polarization(DP 多重背包 二进制优化)

    BZOJ 洛谷 最小可到达点对数自然是把一条路径上的边不断反向,也就是黑白染色后都由黑点指向白点.这样答案就是\(n-1\). 最大可到达点对数,容易想到找一个点\(a\),然后将其子树分为两部分\( ...

  9. $POJ1742\ Coins$ 多重背包+贪心

    Vjudge传送门 $Sol$ 首先发现这是一个多重背包,所以可以用多重背包的一般解法(直接拆分法,二进制拆分法...) 但事实是会TLE,只能另寻出路 本题仅关注“可行性”(面值能否拼成)而不是“最 ...

随机推荐

  1. CSS盒子模型

    2016-10-22 <css入门经典>第6章 1.每个HTML元素对应于一个显示盒子,但不是所有的元素都显示在屏幕上. 2.HTML元素显示为CSS显示盒子的真正方法称为"可视 ...

  2. yii 项目根目录下需要有assets目录

    yii 项目根目录下需要有assets目录,如果没有,会导致gii失效

  3. Android 采用HttpClient提交数据到服务器

    在前几篇文章中<Android 采用get方式提交数据到服务器><Android 采用post方式提交数据到服务器>介绍了android的两种提交数据到服务器的方法 本文继续介 ...

  4. Java你可能不知道的事系列1

    概述 本类文章会不段更新分析学习到的经典面试题目,在此记录下来便于自己理解.如果有不对的地方还请各位观众拍砖. 今天主要分享一下常用的字符串的几个题目,相信学习java的小伙伴们对String类是再熟 ...

  5. .NET 多语言支持解决方案(转)

    asp.net 2.0中的App_GlobalResources可以用来解决本地化的问题,程序会根据浏览器的语言首选项自动判断显示出本地化的界面. 首先在App_GlobalResources新建re ...

  6. Ajax最详细的参数解析和场景应用

    4.1.定义和用法 AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJAX = 异步 J ...

  7. 十五天精通WCF——第二天 告别烦恼的config配置

    经常搞wcf的基友们肯定会知道,当你的应用程序有很多的“服务引用”的时候,是不是有一种疯狂的感觉...从一个环境迁移到另外一个环境,你需要改变的 endpoint会超级tmd的多,简直就是搞死了人.. ...

  8. R语言中的循环函数(Grouping Function)

    R语言中有几个常用的函数,可以按组对数据进行处理,apply, lapply, sapply, tapply, mapply,等.这几个函数功能有些类似,下面介绍下这几个函数的用法. Apply 这是 ...

  9. 按要求编写Java应用程序。 (1)建立一个名叫Cat的类: 属性:姓名、毛色、年龄 行为:显示姓名、喊叫 (2)编写主类: 创建一个对象猫,姓名为“妮妮”,毛色为“灰色”,年龄为2岁,在屏幕上输 出该对象的毛色和年龄,让该对象调用显示姓名和喊叫两个方法。

    package zuoye; public class Cat { String name="妮妮"; String color="灰色"; int age=1 ...

  10. 这些web前端特效你造吗?

    友情提示:所有特效效果均是GIF图片演示(均有源码下载),所以这个博文可能加载的比较慢,请谅解. 凛冬将至(Winter Is Coming),在上一篇博客凛冬将至,用几款特效暖暖身得到了比较好的响应 ...