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. CSS入门

    CSS,层叠样式表,是对web页面显示效果进行控制的一套标准.当页面的内容受多种样式控制,将会按照一定的顺序处理.CSS的作用:(1)将网页的内容结构和格式控制分开.(2)可以精确控制页面的所有元素. ...

  2. C++11 变长模版和完美转发实例代码

    C++11 变长模版和完美转发实例代码 #include <memory>#include <iostream>#include <vector>#include ...

  3. 关于StatusStrip控件和StatusBar控件的小试

    今天,在网上查找资料,突然看到一个例子,但例子中提及的StatusBar控件,我发现在vs控件压根不存在,我就郁闷了,于是上网查找才知道,现在这个控件已经被StatusStrip控件给吞了,Statu ...

  4. Bootstrap <基础二十九>面板(Panels)

    Bootstrap 面板(Panels).面板组件用于把 DOM 组件插入到一个盒子中.创建一个基本的面板,只需要向 <div> 元素添加 class .panel 和 class .pa ...

  5. Bootstrap<基础六> 表单

    Bootstrap 通过一些简单的 HTML 标签和扩展的类即可创建出不同样式的表单. 表单布局 Bootstrap 提供了下列类型的表单布局: 垂直表单(默认) 内联表单 水平表单 垂直或基本表单 ...

  6. Eclipse中的一些快捷键的使用

    Eclipse是一款强大的编程工具,在使用的过程中,若能够有效的使用其快捷键,效率会得到很大的提升,下面是一些常用的eclipse快捷键,可谓是键键精彩. 1.成单词:Alt+/ 2 重构之重命名:S ...

  7. Go语言实现简单的一个静态WEB服务器

    package main import ( "net/http" ) func main() { http.Handle("/", http.FileServe ...

  8. UIButtonTypeSystem backBarButtonItem

    当UIButton是UIButtonTypeSystem类型时,改变UIButton的frame,系统会有一个动画改变效果,不想要这个效果,将类型改为UIButtonTypeCustom. backB ...

  9. Nginx+Tomcat构建动、静分离WEB架构

    一.简介 二.环境介绍 三.后端服务器安装配置 四.安装论坛 五.安装配置前端Nginx服务器 六.验证服务 一.Tomcat简介 Tomcat是Apache 软件基金会(Apache Softwar ...

  10. 例子:Database - Linq to sql

    DataContext类型(数据上下文)是System.Data.Linq命名空间下的重要类型,用于把查询句法翻译成SQL语句,以及把数据从数据库返回给调用方和把实体的修改写入数据库. DataCon ...