B - Dividing
Description
Input
The last line of the input file will be "0 0 0 0 0 0"; do not process this line.
Output
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的更多相关文章
- POJ 1014 Dividing
Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 66032 Accepted: 17182 Descriptio ...
- CF 371B Fox Dividing Cheese[数论]
B. Fox Dividing Cheese time limit per test 1 second memory limit per test 256 megabytes input standa ...
- AC日记——Dividing poj 1014
Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 69575 Accepted: 18138 Descri ...
- POJ 1014 Dividing(多重背包)
Dividing Description Marsha and Bill own a collection of marbles. They want to split the collectio ...
- Dividing a Chocolate(zoj 2705)
Dividing a Chocolate zoj 2705 递推,找规律的题目: 具体思路见:http://blog.csdn.net/u010770930/article/details/97693 ...
- 动态规划--模板--hdu 1059 Dividing
Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- 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 ...
- 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 ...
- Dividing 多重背包 倍增DP
Dividing 给出n个物品的价值和数量,问是否能够平分.
- poj 2373 Dividing the Path
Dividing the Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2858 Accepted: 1064 ...
随机推荐
- what is blade and soul Soul Shields
Soul Shields are sets of 8 fragments which serve as the main source of attributes to a character. Ad ...
- winform学习笔记02
Hashtable 数据遍历的几种方式 ---Hashtable 在集合中称为键值对,它的每一个元素的类型是 DictionaryEntry,由于Hashtable对象的键和值都是Object类型,决 ...
- 关于Xcode6创建的工程在Xcode5打开
Xcode6创建的工程在Xcode5打开- 4.0只显示3.5大小的问题 只需要在工程里添加Default-568h@2x.png,即可以解决
- 使用git删除远程仓库文件
git rm -r -f --cached 文件或文件夹 git commit -m "移除文件或文件夹" git push origin master 注意:要删除的文件或文件夹 ...
- ASP.NET DAY1
<!-- AutoEventWireup,CodeBehind,Inherits等属性可省略, Language属性为必须项 --> <%@ Page Language=" ...
- tesseract 编译与使用(windows)
tesseract是google的一个开源OCR项目,项目地址已经迁移到github(现在 2016/09),地址 https://github.com/tesseract-ocr/tesseract ...
- Struts2中动态方法的调用
Struts2中动态方法调用就是为了解决一个action对应多个请求的处理,以免action太多. 主要有一下三种方法:指定method属性.感叹号方式和通配符方式.推荐使用第三种方式. 1.指定me ...
- C#基础知识汇总
过了一遍基础视频,发现有一些最基本的知识点还掌握的不够,汇总如下: 1) 占位符 string name = "张三"; ; decimal salary = 7600.33M; ...
- CMT learning
一个 GMT 命令由"gmt + 模块 + 选项 + 参数"构成,写成如下形式: gmt module -Axx+bxxxx -Bxx+axxxx • gmt 是 GMT 中&qu ...
- Spark MLlib Data Type
MLlib 支持存放在单机上的本地向量和矩阵,也支持通过多个RDD实现的分布式矩阵.因此MLlib的数据类型主要分为两大类:一个是本地单机向量:另一个是分布式矩阵.下面分别介绍一下这两大类都有哪些类型 ...