Dividing 
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) 
Total Submission(s): 20691 Accepted Submission(s): 5827

Problem 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 describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, …, 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 colletcion, output Collection #k:'', where k is the number of the test case, and then eitherCan 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.

题解:

背包问题,二进制优化,对于价值为val有num个,可以将其分成1,2,4,8......这样的二进制价值,可以保证如果可以均分,是一定

可以的,如果可以均分,那么保证可以分出,如11 可以分出1,2,4,6,不可以均分,12分出1,2,4,5拿1,5或者2,4就均分了。

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
using namespace std; int Case=;
int cont=;
int a[],val[];
bool f[]; int main()
{
while(~scanf("%d%d%d%d%d%d",&a[],&a[],&a[],&a[],&a[],&a[])&&(a[]+a[]+a[]+a[]+a[]+a[]))
{
printf("Collection #%d:\n",++Case);
int sum=,mid=;
for (int i=;i<=;i++)
sum=sum+a[i]*i;
if (sum%)
{
printf("Can't be divided.\n\n");
continue;
}
mid=sum/;
memset(f,,sizeof(f));
f[]=;cont=;
for (int i=;i<=;i++)
{
int t=i;a[i]=a[i]*i;
while(a[i]>=t)
{
val[++cont]=t;
a[i]-=t;
t*=;
}
if (a[i]>) val[++cont]=a[i];
}
f[]=;
for (int i=;i<=cont;i++)
for (int j=mid;j>=val[i];j--)
if (f[j-val[i]]) f[j]=;
if (f[mid]) printf("Can be divided.\n\n");
else printf("Can't be divided.\n\n");
}
}

hdu1059(背包dp二进制优化)的更多相关文章

  1. luogu||P1776||宝物筛选||多重背包||dp||二进制优化

    题目描述 终于,破解了千年的难题.小FF找到了王室的宝物室,里面堆满了无数价值连城的宝物……这下小FF可发财了,嘎嘎.但是这里的宝物实在是太多了,小FF的采集车似乎装不下那么多宝物.看来小FF只能含泪 ...

  2. 动态规划:HDU2844-Coins(多重背包的二进制优化)

    Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  3. HDU 1059(多重背包加二进制优化)

    http://acm.hdu.edu.cn/showproblem.php?pid=1059 Dividing Time Limit: 2000/1000 MS (Java/Others)    Me ...

  4. CodeForces922E DP//多重背包的二进制优化

    https://cn.vjudge.net/problem/1365218/origin 题意 一条直线上有n棵树 每棵树上有ci只鸟 在一棵树底下召唤一只鸟的魔法代价是costi 每召唤一只鸟,魔法 ...

  5. dp之多重背包(二进制优化)

    void solve(int v,int w,int c){    int count=0;    for(int k=1;k<=c;k<<=1)    {        val[c ...

  6. HDU——2191悼念512汶川大地震遇难同胞(多重背包转化为01背包或二进制优化)

    悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  7. POJ 1276 Cash Machine(多重背包的二进制优化)

    题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...

  8. HDU-2844 Coins 多重背包 物品数量二进制优化

    题目链接:https://cn.vjudge.net/problem/HDU-2844 题意 给你一些不同价值和一定数量n的硬币. 求用这些硬币可以组合成价值在[1 , m]之间的有多少. 思路 多重 ...

  9. 多重背包的二进制优化——DP

    #include<cstdio> #include<cstring> #include<algorithm> #define LL long long using ...

随机推荐

  1. 06.NopCommerce配置邮箱账户

    NopCommerce如果配置让用户注册为通过邮箱注册,并且注册后激活邮箱才可登录,那么我们需要对NopCommerce的邮箱账户进行配置,用来发送邮件用.当然邮件还有很多其他用途,比如发送用户订阅的 ...

  2. PHP的加密方式

    1. MD5加密 string md5 ( string $str [, bool $raw_output = false ] ) 参数 str  --  原始字符串. raw_output  --  ...

  3. hihocoder1736 最大的K-偏差排列

    思路: 容易写错的贪心题. 实现: #include <bits/stdc++.h> using namespace std; int main() { int n, k; while ( ...

  4. python中的get函数

    >>> a={1:'a',2:'b'}>>> print a.get(1)a>>> print a.get(3)None

  5. chrome调试之Workspaces

    可通过workspaces来编辑本地文件 workspaces是Chrome DevTools的一个强大功能,这使DevTools变成了一个真正的IDE.Workspaces会将Sources选项卡中 ...

  6. Redis学习笔记(五)散列进阶

    HEXISTS key_name key(检查键key是否存在) HKEYS key_name(获得散列的所有键) HVALS key_name(获得散列的所有值) HINCRBY key_name ...

  7. Android天天数钱游戏项目源码

    Android天天数钱游戏源码,源码功能,天天数钱,这个游戏现在很多线上的小游戏都有这个了,游戏项目是在基于android游戏代码,大家可以参考一下. 源码下载:http://code.662p.co ...

  8. 第一周作业javaee strainmap

  9. sysUpload.vue上传组件 的时候 看进度的时候 不要mock 注释掉 // if (process.env.NODE_ENV !== 'production') require('@/mock')

    上传组件 的时候 看进度的时候 不要mock 注释掉 // if (process.env.NODE_ENV !== 'production') require('@/mock') <!-- * ...

  10. 把特征网络换成resnet-50

    从RFCN来看,Resnet-50和Resnet-101到最后一层卷积都是缩小到原来尺寸的16分之一,并且都用的7x7的格子去roi pooling. 看paper可以知道:resnet-50核心是由 ...