Addition Chains

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5454   Accepted: 2923   Special Judge

Description

An addition chain for n is an integer sequence <a0, a1,a2,...,am="">with the following four properties:

  • a0 = 1
  • am = n
  • a0 < a1 < a2 < ... < am-1 < am
  • For each k (1<=k<=m) there exist two (not necessarily different) integers i and j (0<=i, j<=k-1) with ak=ai+aj

You are given an integer n. Your job is to construct an addition chain for n with minimal length. If there is more than one such sequence, any one is acceptable. 
For example, <1,2,3,5> and <1,2,4,5> are both valid solutions when you are asked for an addition chain for 5.

Input

The input will contain one or more test cases. Each test case consists of one line containing one integer n (1<=n<=100). Input is terminated by a value of zero (0) for n.

Output

For each test case, print one line containing the required integer sequence. Separate the numbers by one blank. 
Hint: The problem is a little time-critical, so use proper break conditions where necessary to reduce the search space. 

Sample Input

5
7
12
15
77
0

Sample Output

1 2 4 5
1 2 4 6 7
1 2 4 8 12
1 2 4 5 10 15
1 2 4 8 9 17 34 68 77

Source

 

 
提交地址 : poj
 
搜索框架:依次搜索一位$k$, 枚举之前的$i$,$j$, 把$a[i] + a[j]$ 加到$a[k]$的位置上, 然后接着搜索;
剪枝:尽量从大到小枚举$i$,$j$让序列的数尽快逼近$n$;
为了不重复搜索,用一个$bool$数组存$a[i] + a[j]$ 是否已经被搜过;
还有一个十分厉害的剪枝,如果现在枚举到的$a[i]+a[j]$比$a[now-1]$小了,但是还没有搜到解,就直接判无解, $now$是现在搜到的位置,十分有用。
然后因为答案的深度很小, 所以一发迭代加深;
这样才能A掉...
 

 
代码奉上:
//By zZhBr
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; int n;
int ans; int a[]; bool use[];
bool DFS(int stp)
{
memset(use, , sizeof use); if(stp > ans)
{
if(a[ans] == n) return ;
else return ;
} for(register int i = stp - ; i >= ; i --)
{
for(register int j = i ; j >= ; j --)
{
if(a[i] + a[j] > n) continue;
if(!use[a[i] + a[j]])
{
if(a[i] + a[j] <= a[stp - ]) return ;
use[a[i] + a[j]] = ;
a[stp] = a[i] + a[j];
if(DFS(stp + )) return ;
a[stp] = ;
use[a[i] + a[j]] = ;
}
}
}
} int main()
{
while(scanf("%d", &n) != EOF)
{
if(n == ) return ;
if(n == )
{
printf("1\n");
continue;
}
if(n == )
{
printf("1 2\n");
continue;
}
a[] = ;a[] = ;
for(ans = ; !DFS() ; ans ++);
for(register int i = ; i <= ans ; i ++)
{
printf("%d ", a[i]);
}
printf("\n");
memset(a, , sizeof a);
}
return ;
} zZhBr
 

[POJ2248] Addition Chains 迭代加深搜索的更多相关文章

  1. POJ2248 Addition Chains 迭代加深

    不知蓝书的标程在说什么,,,,于是自己想了一下...发现自己的代码短的一批... 限制搜索深度+枚举时从大往小枚举,以更接近n+bool判重,避免重复搜索 #include<cstdio> ...

  2. POJ 2248 - Addition Chains - [迭代加深DFS]

    题目链接:http://bailian.openjudge.cn/practice/2248 题解: 迭代加深DFS. DFS思路:从目前 $x[1 \sim p]$ 中选取两个,作为一个新的值尝试放 ...

  3. poj 2248 Addition Chains (迭代加深搜索)

    [题目描述] An addition chain for n is an integer sequence with the following four properties: a0 = 1 am ...

  4. UVA 529 - Addition Chains,迭代加深搜索+剪枝

    Description An addition chain for n is an integer sequence  with the following four properties: a0 = ...

  5. C++解题报告 : 迭代加深搜索之 ZOJ 1937 Addition Chains

    此题不难,主要思路便是IDDFS(迭代加深搜索),关键在于优化. 一个IDDFS的简单介绍,没有了解的同学可以看看: https://www.cnblogs.com/MisakaMKT/article ...

  6. POJ1129Channel Allocation[迭代加深搜索 四色定理]

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14601   Accepted: 74 ...

  7. BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1800  Solved: 984[Submit][Statu ...

  8. 迭代加深搜索 POJ 1129 Channel Allocation

    POJ 1129 Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14191   Acc ...

  9. 迭代加深搜索 codevs 2541 幂运算

    codevs 2541 幂运算  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...

随机推荐

  1. spring data jpa介绍

    首先了解JPA是什么? JPA(JavaPersistence API)是Sun官方提出的Java持久化规范.它为Java开发人员提供了一种对象/关联映射工具来管理Java应用中的关系数据.他的出现主 ...

  2. Knative 实战:三步走!基于 Knative Serverless 技术实现一个短网址服务

    短网址顾名思义就是使用比较短的网址代替很长的网址.维基百科上面的解释是这样的: 短网址又称网址缩短.缩短网址.URL 缩短等,指的是一种互联网上的技术与服务,此服务可以提供一个非常短小的 URL 以代 ...

  3. Vue-学习笔记0-独立项目搭建

    前言 搭建Vue+Webpack项目,使用vue-cli搭建项目. 准备 vue独立项目依赖node的npm包管理器,所以需要先安装node. 相关的npm常用命令文章: Npm-常用命令,点击访问 ...

  4. mysql8.0版本忘记root密码

    1.先关掉系统服务 net stop mysql 2.进入mysql安装目录的bin文件中,以管理员的方式运行cmd,然后输入如下命令,实现无密码登陆 mysqld --console --skip- ...

  5. 关于svn更新失败,clearup异常解决

    直接上主题: 1. 下载sqlite3工具(https://files.cnblogs.com/files/eric-fang/sqlite-tools-win32-x86-3210000.zip), ...

  6. AppScan工具介绍与安装

    本文仅供个人参考学习,如做商业用途,请购买正版,谢谢! 介绍 AppScan是IBM公司出的一款Web应用安全测试工具,采用黑盒测试的方式,可以扫描常见的web应用安全漏洞.其工作原理,首先是根据起始 ...

  7. MIT FiveK图像转化--DNG到TIFF,TIFF到JPEG

    MIT FiveK图像转化--DNG到TIFF,TIFF到JPEG MIT FiveK数据库是研究图像自动修饰算法会用到的基准数据库,然而那个网页上提供给我们的5000张原始图像的格式为DNG格式(一 ...

  8. docker运行jexus+mono爬坑记

    新的.net core都已经支持docker.手头有一些原来开发的asp.net旧项目,用的asp.net mvc开发的,跑在.net formwork 4.6上. 在docker的公共仓库searc ...

  9. Python学习笔记整理总结【Django】:Model操作(二)

    1.操作汇总 # 增 # # models.Tb1.objects.create(c1='xx', c2='oo') 增加一条数据,可以接受字典类型数据 **kwargs # obj = models ...

  10. 隐藏select下拉框的三角按钮

    修改select标签的appearance属性,改成inherit,而不是none. -moz-appearance:inherit;/*?Firefox?*/ -webkit-appearance: ...