Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

Submit Status

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.
多重背包和二进制优化,这个优化很重要,不会超时,
 #include<cstdio>
#include<string.h>
#include<math.h>
using namespace std;
int dp[];
int main()
{
int a[];
int t=;
int cnt;
while(scanf("%d %d %d %d %d %d",&a[],&a[],&a[],&a[],&a[],&a[])!=EOF)
{
if(a[]+a[]+a[]+a[]+a[]+a[]==) break;
int sum,x;
sum=a[]*+a[]*+a[]*+a[]*+a[]*+a[]*;
printf("Collection #%d:\n",t);
t++;
if(sum%)
{
printf("Can't be divided.\n\n");
continue;
}
x=sum/;
memset(dp,,sizeof(dp));
dp[]=;
for(int i=;i<=;i++)
{
if(!a[i])
continue;
for(int j=;j<=a[i];j*=)//二进制优化,2,4,8,16,他把dp[3],dp[5],dp[7]等都算了,并且循环次数减少
{
cnt=j*i;
for(int k=x;k>=cnt;k--)
{
if(dp[k-cnt])
dp[k]=;
}
a[i]-=j;//石头数目减少
}
cnt=a[i]*i;//最后再算一次上面循环完后的a[i]
if(cnt)//如果cnt不等于0
{
for(int k=x;k>=cnt;k--)
{
if(dp[k-cnt])
dp[k]=;
}
}
}
if(dp[x])//如果dp[x]不等于0,则说明能平分
printf("Can be divided.\n");
else printf("Can't be divided.\n");
printf("\n");
}
return ;
}

B - Dividing的更多相关文章

  1. POJ 1014 Dividing

    Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 66032 Accepted: 17182 Descriptio ...

  2. CF 371B Fox Dividing Cheese[数论]

    B. Fox Dividing Cheese time limit per test 1 second memory limit per test 256 megabytes input standa ...

  3. AC日记——Dividing poj 1014

    Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 69575   Accepted: 18138 Descri ...

  4. POJ 1014 Dividing(多重背包)

    Dividing   Description Marsha and Bill own a collection of marbles. They want to split the collectio ...

  5. Dividing a Chocolate(zoj 2705)

    Dividing a Chocolate zoj 2705 递推,找规律的题目: 具体思路见:http://blog.csdn.net/u010770930/article/details/97693 ...

  6. 动态规划--模板--hdu 1059 Dividing

    Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  7. Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环

    D. Dividing Kingdom II   Long time ago, there was a great kingdom and it was being ruled by The Grea ...

  8. Codeforces Round #218 (Div. 2) B. Fox Dividing Cheese

    B. Fox Dividing Cheese time limit per test 1 second memory limit per test 256 megabytes input standa ...

  9. Dividing 多重背包 倍增DP

    Dividing 给出n个物品的价值和数量,问是否能够平分.

  10. poj 2373 Dividing the Path

    Dividing the Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2858   Accepted: 1064 ...

随机推荐

  1. 安装 node-sass 的正确姿势

    SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/ npm install node-sass

  2. 高性能 Oracle JDBC 编程

    了解如何利用连接和语句池特性来提高 Oracle 驱动的 JDBC 程序的性能.作者:Yuli Vasiliev2009 年 4 月发布使用诸如连接池和语句池等池技术可以显著提高数据库密集型应用程序的 ...

  3. 深入理解git,从研究git目录开始

    转发学习的啦. 似乎很少有人在读某个git快速教程的时候会说:“这个关于git的快速教程太酷了!读完了用起git来超级舒服,并且我一点也不怕自己会破坏什么东西.” 对git的初学者来说,刚接触git时 ...

  4. Adapter 启动时报错——2

    在安装tibco adr3  7.00以前的版本,在designer中启动adr3 会报出“无法加载adr3.dll”文件的错误,这是因为在designer中的palettes默认是指向adapter ...

  5. 商品sku规格选择效果,没有商品的不能选中,选择顺序不影响展示结果

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

  6. (原创)如何使用selenium 驱动chrome浏览器并且打开方式为手机模式-转载请注明出处

    随着移动设备使用率的不断增加,移动页面的测试也变得越来越重要. 对于互联网公司M站的测试,如果不通过专用的appium等移动端测试工具是否还有方便快捷的办法呢?答案当然是有啊. 使用chrome dr ...

  7. eclipse 相同变量高亮显示

    windows-> preferences-> java-> Editor-> Mark Occurences

  8. Mybatis使用generator自动生成映射配置文件信息

     使用mybatis配置映射文件比较的麻烦,但是有自动生成jar工具,方便加速开发速度,下面主要是该工具的使用以及相关的配置. 1.下载相关的资源 我们需要下载mybatis-generator-co ...

  9. 《Pro Express.js》学习笔记——app.params中间件

    app.param中间件用于对URL中的参数进行获取.加工.输出,提供公有逻辑,以达到代码重构的目的. 以下示例采取三个步骤对代码进行重构,app.param中间件的作用非常明显: 不使用中间件 使用 ...

  10. MYSQL5.7无法启动服务原因及解决方案

    mysql5.7安装完成后,想要把它发布成windows服务: 首先,应该配置新的配置文件,然后将cmd打开到安装目录的bin文件,键入: mysqld --default-file="D: ...