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. ASP.NET使用jQuery AJAX实现MD5加密实例

    一个asp.net ajax例子,使用jquery,实现md5加密.在.NET 4.0,Visual Studio 2010上成功运行. 效果体验:http://tool.keleyi.com/t/m ...

  2. hybird之web动态换肤实现

    前言 最近在重构个hybird(原生的壳包着Web页面)的UI框架,进行到了做换肤功能的阶段,所以这里是我思考的解决的方法. 预想 目前实现换肤的功能无非就两种做法. 1.写几个皮肤文件,然后切换使用 ...

  3. JavaScript学习笔记-正则表达式(语法篇)

    正则表达式的模式规则是由一个字符系列组成的,包括所有字母和数字在内;大多数的字符(所有字母和数字)都是按字符的直接量来描述带匹配的字符;一些具有特殊语义的字符按照其特殊语义来进行匹配,有些字符需要通过 ...

  4. ie7下的javascript兼容

    <a href="javascript:;" onclick="functionone();"></a> <script> ...

  5. jQuery构造函数init参数分析(三)

    分析完了字符串情况剩下的就不多了. 5.参数selector是函数 这个就是很容易想到了,首先说一下dom加载.我们通常在head里面写脚本的时候需要等待文档加载在进行处理,js是这么写的 windo ...

  6. ae柱状图

  7. Office 365 - SharePoint 2013 Online 之应用程序开发

    1.给站点添加完Napa后,在网站内容里点击Napa,如下图: 2.创建一个新的app,如下图: 3.可以在Napa里添加新的项目,如下图: 4.添加新的文件,可以添加web页面.样式表.脚本,如下图 ...

  8. 2015年Java开发岗位面试题归类

    一.Java基础 1. String类为什么是final的. 2. HashMap的源码,实现原理,底层结构. 3. 说说你知道的几个Java集合类:list.set.queue.map实现类咯... ...

  9. iOS开发中常用的单例

    定义:一个类的对象,无论在何时创建.无论创建多少次,创建出来的对象都是同一个对象. 使用场景:当有一些数据需要共享给别的类的时候,就可以把这些数据保存在单例对象中.   关键代码: + (instan ...

  10. Double 数据保留两位小数一:五舍六入

    package com; public class T2 { public static void main(String[] args) { System.out.println(calculate ...