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. Eclipse spket插件 内置js文件

    这一篇将怎么在spket内置js文件,而不用用户自己去添加.    1. 在开发的Eclipse的 运行配置将下面几个插件勾选上.     2. 在org.eclipse.ui.startup拓展里执 ...

  2. Delphi 时间耗时统计

    处理事情: 数据处理过程中,速度很慢,无法准确定位分析是DB问题还是客户端处理问题,所以增加计时统计日志: Delphi计时首次使用,查阅资料,予以记录: var BgPoint, EdPoind: ...

  3. 使用js实现带有停顿效果的图片滚动(按钮控制)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 27款后台管理页面设计 DIV+CSS+JS

    -----------. 演示: http://www.websjy.com/club/websj ... _61040268/index.htm http://www.websjy.com/club ...

  5. Autodesk Vault: 获取授权失败

    在登录Vault Explorer时弹出对话框,获取授权失败,不能登录. 1.首先Autodesk Vault Professional采用网络版授权方式,在安装之前之前你需要首先配置网络授权服务器, ...

  6. Wix安装程序中判断是否安装的.net framwork 4.5

    <PropertyRef Id="NETFRAMEWORK40FULL"/> <PropertyRef Id="NETFRAMEWORK45" ...

  7. Creating Custom Connector Sending Claims with SharePoint 2013

    from:http://blogs.msdn.com/b/security_trimming_in_sharepoint_2013/archive/2012/10/29/creating-custom ...

  8. AVAudioSession初探

    根据文档,AudioSession规定了app和系统音频行为交互的规范,一个app只有一个AudioSession的单例. app通过设置自己AudioSession的单例的属性来告诉系统自身想达到的 ...

  9. Android时区及语言代码

    1. 设置默认时区   PRODUCT_PROPERTY_OVERRIDES += \         persist.sys.timezone=Asia/Shanghai\ 注:搜索“persist ...

  10. Android 异步Http框架简介和实现原理

    在前几篇文章中<Android 采用get方式提交数据到服务器><Android 采用post方式提交数据到服务器><Android 采用HttpClient提交数据到服 ...