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 ...
随机推荐
- 搭建Hadoop2.6.4伪分布式
准备工作 操作系统 CentOS 7 软件环境 JDK 1.7.0_79 下载地址 SSH,正常来说是系统自带的,若没有请自行搜索安装方法 关闭防火墙 systemctl stop firewalld ...
- java 调用 scala
1 scala 方法的输入输出不能有 jdk 不可识别的类型(如:Int,Float,Any 等是不行的,Unit 对应到 void 是可以的.) http://rwh.readthedocs.org ...
- 使用centos引导内核错误:kernel: pnp 00:0b: can't evaluate _CRS: 8
CentOS系统在开机过程中,一直遇到黑屏提示:“kernel: pnp 00:0b: can't evaluate _CRS: 8”,不理会它仍能启动系统并正常工作,未知何故. 经查,这是内核引导的 ...
- 查询数据库表大小sql
SELECT a.name, b.rowsFROM sysobjects AS a INNER JOIN sysindexes AS b ON a.id = b.idWHERE (a.type = ' ...
- linux命令每日一练习 wc more
wc该命令统计文件行数,字数或者字节数 wc -l 统计行数 wc -w 统计字数 wc -c 统计字节数 more 显示文件内容,类似于cat more +n 从第n行开始显示 more -n 限定 ...
- Service Broker应用(1):简介、同server不同DB间的数据传输
简介:SQL Server Service Broker,以下简称SSB,是一种完全基于MSSQL数据库的数据处理技术,为短时间内处理大量数据提供了一种可靠.稳定.高效的解决方案.一次同步的数据最大可 ...
- javafx之登陆界面的跳转
界面布局用到的是fxml而非纯java代码,工具是javafx sence builder 账号:account 密码:password 登陆成功: 可以点击退出登陆返回到登陆页面 工程目录: pac ...
- Puppet安装及部署
本篇博客主要介绍Puppet的安装部署,后续会更新其他相关内容 一.简介 二.环境介绍 三.安装Puppet 四.配置Puppet-dashboard 五.配置Puppet Kick 一.简介 Pup ...
- rpc使用JUnit模块测试设计的方法及常见问题
RPC:Remote Procedure Call 远程过程调用 Wikipedia:http://en.wikipedia.org/wiki/Remote_Procedure_Call 百度百科:h ...
- hibernate 返回新插入数据的Id
例如 表明 studentInfo String sql="set set nocount on studentInfo(列名,列名) values('值','值');select @@id ...