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. 一、springboot起航

    # 前言 之前零零散散的学习了一些springboot的知识,以及搭建一些springboot的项目,甚至还有一些项目应用到实际项目中了,但是突然有一天想要建一个自己的项目网站.发现自己不知道从何开始 ...

  2. Java8之熟透Optional

    一.使用Optional引言 1.1.代码问题引出 在写程序的时候一般都遇到过 NullPointerException,所以经常会对程序进行非空的判断: User user = getUserByI ...

  3. MYSQL之查询篇

    2. 数据库操作 数据库在创建以后最常见的操作便是查询 2.1 查询 为了便于学习和理解,我们预先准备了两个表分别是stduents表和classes表两个表的内容和结构如下所示 students表的 ...

  4. ElasticSearch实战系列二: ElasticSearch的DSL语句使用教程---图文详解

    前言 在上一篇中介绍了ElasticSearch集群和kinaba的安装教程,本篇文章就来讲解下 ElasticSearch的DSL语句使用. ElasticSearch DSL 介绍 Elastic ...

  5. python 切片步长

    python切片 切片:list变量[值下标:结束值下标]  什么意思呢? 就是获取 list中 下标从定义的位置开始获取数据到  自定义的下标位置结束, 但是切片有个规矩就是顾头不顾尾, 举个例子 ...

  6. wait()与notify()

    一,前言 ​ ​ 简单画了一下线程的流程图,只是一个大概.如图所示,线程有多种状态,那么不同状态之间是如何切换的,下面主要总结关于wait()和notify()的使用. 二,wait() ​ wait ...

  7. <机器学习>无监督学习算法总结

    本文仅对常见的无监督学习算法进行了简单讲述,其他的如自动编码器,受限玻尔兹曼机用于无监督学习,神经网络用于无监督学习等未包括.同时虽然整体上分为了聚类和降维两大类,但实际上这两类并非完全正交,很多地方 ...

  8. 前端深入之css篇|你真的了解“权重”吗?

    写在前面 权重这个概念,相信对许多进行过前端开发的小伙伴来说肯定并不陌生,有时候一个样式添加不上,我们就会一个 !important 怼上去,一切就好像迎刃而解了.但还有的时候,!important也 ...

  9. JQuery 源码解析 · extend()详解

    前言:最近想重写一个dropdown插件,于是想到了使用jquey实现插件,于是重温了一波$.extend()的知识,然后总结了这篇笔记 正文: $.extend(src)  jQuery.exten ...

  10. golang 你所不知道的 log 和 fmt

    直接点说,就是由于fmt 是线程不安全的, 如果你在多协程场景下使用fmt打印信息可能会得到乱序的结果 就是说 不按代码里的顺序打印. 下面看示例 代码示例 golang fmt 多线程 乱序: fu ...