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 ...
随机推荐
- RMAN 命令-删除过期
手动删除日志文件后的处理,不然rman备份会出错 rman target / crosscheck archivelog all; delete expried archivelog all; 删除所 ...
- C语言编程风格(转发)
- php开发必备小工具
/*递归删除目录及目录下的文件*/ function del_dir($dir){ $files = new DirectoryIterator($dir); foreach ($files as $ ...
- 【图像处理】【SEED-VPM】3.外设信息
———————————————————————————————————————————————————————————————————————— 摄像头接口 具有AV端子输出的彩色摄像头,摄像头广角8 ...
- Imagick 缩放图片和实现模糊
Imagick功能相当的多,只是还不稳定,我下面的程序能够运行,但是会出现内存错误,但我们要的图片还是能够得到. 弄这个的原因是,一个客户要求在一个appcan的应用里面实现一个页面的背景图的缩放.调 ...
- Git 配置
在 windows 上安装完 Git 会右键菜单中看到 Git 的快捷打开选项, 点 Git Bash Here 就可以在当前目录下打开 Git 的命令行 Git shell,初次使用 Git 先配置 ...
- ObReferenceObjectByName函数调用WIN7下的解决
<寒江独钓 Windows内核安全编程>第4章键盘的过滤ctrl2cap代码中,ObReferenceObjectByName函数调用: [1]extern POBJECT_TYPE Io ...
- Sql server 2008 中varbinary查询
sqlserver2008中遇到一个坑爹的问题,使用以下语句添加的数据 insert into testtable ( username, password, productcode ) select ...
- 【转】修改LINUX时间
命令格式为: date -s 时间字符串 例如只修改系统的日期,不修改时间(时分秒) date -s 2012-08-02 或只修改时间不修改日期 date -s 10:08:00 当然也可以同时修改 ...
- AX多线程编译
1.在命令行里先定位到AOS sever的BIN文件夹下(CD "AOS sever的BIN路径") CD C:\Program Files\Microsoft Dynamics ...