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. 使用nodejs+express+socketio+mysql搭建聊天室

    使用nodejs+express+socketio+mysql搭建聊天室 nodejs相关的资料已经很多了,我也是学习中吧,于是把socket的教程看了下,学着做了个聊天室,然后加入简单的操作mysq ...

  2. [deviceone开发]-do_QRCode的简单示例

    一.简介 do_QRCode组件可以用来生成二维码,识别二维码图片文件,这个示例直观的展示组件基本的使用方式. 二.效果图 三.相关下载 https://github.com/do-project/c ...

  3. Window对象

    Window对象:         Window 对象表示浏览器中打开的窗口,如果文档包含框架(frame 或 iframe 标签),浏览器会为 HTML 文档创建一个 window 对象,并为每个框 ...

  4. JavaScript学习笔记-实现枚举类型,扑克牌应用

    //实现枚举类型,扑克牌应用 function creatEnum(p){     //构造函数     var Enumeration = function(){throw 'can not Ins ...

  5. 简单代码在ABAP中实现声音的播放

    这段代码的功能是在SAP里面实现声音的播放,可以用作程序提醒功能,和SAP里面’噹噹噹’那个声音的意思差不多.将来在项目中遇到客户想要SAP ABAP发出一点声音的时候就可以参考一下这个程序. REP ...

  6. Android基础面试题

    1. 请描述一下Activity 生命周期. 答: 如下图所示.共有七个周期函数,按顺序分别是: onCreate(), onStart(), onRestart(), onResume(), onP ...

  7. 首届Autodesk编程马拉松(Hackathon)开始报名啦 -- 6.14~15 上海

    欢迎报名参加Autodesk 首届编程马拉松 ( Hackathon ) 活动   首届Autodesk编程马拉松(Hackathon)活动即将在Autodesk公司中国研究院(上海)举办.本次编程马 ...

  8. java使用动态代理来实现AOP(日志记录)

    以下内容为原创,转载时请注明链接地址:http://www.cnblogs.com/tiantianbyconan/p/3336627.html AOP(面向方面)的思想,就是把项目共同的那部分功能分 ...

  9. android加固系列—6.仿爱加密等第三方加固平台之动态加载dex防止apk被反编译

    [版权所有,转载请注明出处.出处:http://www.cnblogs.com/joey-hua/p/5402599.html ] 此方案的目的是隐藏源码防止直接性的反编译查看源码,原理是加密编译好的 ...

  10. ASP和ASP.NET发送邮件笔记

    这两天因公司网站邮件发不出去,然后研究了在asp网站发送邮件和在asp.net网站发送邮件的代码,把碰到的问题这里记录一下. 1.先说在asp.net中发送邮件吧, 刚开始只有126邮箱可以发出邮件, ...