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. Java线程常见面试题

    v 多线程实现手段: (1).继承Thread类 (2)实现Runable接口 (3)使用线程池 v 线程控制在那个包:java.util.concurrent. (1)提供了线程的运行.(2)线程池 ...

  2. [VB.NET Tips]字符串分隔

    在实际应用中,很多场景下都需要分隔字符串,如解析CSV文件等. 一般我们使用split方法来按照指定的分隔符来进行分隔字符串获得一个数组. Split方法的签名是: Split(ParamArray ...

  3. (三)Spring 高级装配 bean的作用域@Scope

    1.默认情况下,spring通过@Autowared注入的bean是单例的bean,但有些情况是不满足的,例如:购物车,每个会话,或每个用户登录使用的购物车都是独立的 spring的定义的作用域: a ...

  4. elasticsearch的分布式基础概念(1)

    Elasticsearch对复杂分布式机制的透明隐藏特性 Elasticsearch是一套分布式的系统,分布式是为了应对大数据量 隐藏了复杂的分布式机制 分片机制(随随便便就将一些document插入 ...

  5. Flutter免费(视频)教程汇总

    Flutter学习导航 Flutter简介: Flutter可以轻松快速地构建漂亮的移动应用程序. Flutter是谷歌的移动应用SDK,用于短时间内在iOS和Android上制作高质量的原生界面应用 ...

  6. SD-WAN 配置及应用模板**(二)

    目录 0. 前言 1. 配置模板 1.1 创建各类 'Feature' 模板: 1.1.1 添加波特率模板 1.1.2 添加 'VPN0' 模板 1.1.3 添加 'VPN10' 模板 1.1.4 添 ...

  7. 百万it资源百度网盘链接分享

    自己大量时间整理的优质资源,容量达3000多G,有需要的朋友可以微我,资源截图:  面试资料: 书籍类: 视频类: 以上只是部分资源,想要资源的亲请加微信咨询. 欢迎加微信咨询,请备注资源: 独乐乐不 ...

  8. SUSE CaaS Platform 4 - 安装技巧

    1.虚拟化环境搭建 -  网络 首先,虚拟机其中一块网卡桥接到 VMnet8 上,通过 VMnet8 地址转换出去访问互联网,如果我们直接桥接到 WIFI 网卡上,由于在不同的的网络环境,地址会时长会 ...

  9. xpath语法分享

    # xpath语法: ## 使用方式: 使用//获取整个页面当中的元素,然后写标签名,然后再写谓词进行提取.比如: ``` //div[@class='abc'] ``` ## 需要注意的知识点: 1 ...

  10. 品Spring:对@Resource注解的处理方法

    @Resource是Java的注解,表示一个资源,它具有双向的含义,一个是从外部获取一个资源,一个是向外部提供一个资源. 这其实就对应于Spring的注入和注册.当它用在字段和方法上时,表示前者.当它 ...