Description

An addition chain for n is an integer sequence  with the following four properties:

  • a0 = 1
  • am = n
  • a0<a1<a2<...<am-1<am
  • For each k ( ) there exist two (not neccessarily different) integers i and j ( ) 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 Specification

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

Output Specification

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 大致题意:
  给你个n,找从 0 到 n 的最短序列,序列满足 每一个 a[k] 都存在a[i]+a[j]=a[k],如果有很多,找到一个就够了。
解题思路:
  用迭代加深搜索实现,需要注意很多地方剪枝。
 #include <iostream>
#include <cstring>
using namespace std;
int n,ans[];
bool finish;
void dfs(int x,int deep){
if(finish) return ;
if(x==deep) { if(ans[x]==n)finish=; return; }
for(int i=;i<=x;i++){
for(int j=i;j<=x;j++) if(ans[i]+ans[j]>ans[x]&&ans[i]+ans[j]<=n){//剪枝
int sum=ans[i]+ans[j];
for(int k=x+;k<=deep;k++) sum<<=;//sum *= 2;当前为x; sum存于x+1;
if(sum<n) continue;//如果接下来一直是最大策略还是不能达到n,剪枝
ans[x+]=ans[i]+ans[j];
dfs(x+,deep);
if(finish) return ;
}
}
}
int main(){
while(scanf("%d",&n),n){
memset(ans,,sizeof(ans));
ans[finish=]=;
int tmp=n,deep=; while(tmp>>=) deep++;//求出最大深度;
while(!finish) dfs(,deep++);
cout<<ans[];
for(int i=;i<deep;i++) cout<<" "<<ans[i];
cout<<endl;
}return ;
}

UVA 529 - Addition Chains,迭代加深搜索+剪枝的更多相关文章

  1. [POJ2248] Addition Chains 迭代加深搜索

    Addition Chains Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5454   Accepted: 2923   ...

  2. [Vijos1308]埃及分数(迭代加深搜索 + 剪枝)

    传送门 迭代加深搜索是必须的,先枚举加数个数 然后搜索分母 这里有一个强大的剪枝,就是确定分母的范围 #include <cstdio> #include <cstring> ...

  3. UVA 529 Addition Chains(迭代搜索)

      Addition Chains  An addition chain for n is an integer sequence  with the following four propertie ...

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

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

  5. POJ2248 Addition Chains 迭代加深

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

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

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

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

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

  8. 【bzoj1085】【 [SCOI2005]骑士精神】启发式剪枝+迭代加深搜索

    (上不了p站我要死了,侵权度娘背锅) 如果这就是启发式搜索的话,那启发式搜索也不是什么高级玩意嘛..(啪啪打脸) Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且 ...

  9. Power Calculus UVA - 1374 迭代加深搜索

    迭代加深搜索经典题目,好久不做迭代加深搜索题目,拿来复习了,我们直接对当前深度进行搜索,注意剪枝,还有数组要适当开大,因为2^maxd可能很大 题目:题目链接 AC代码: #include <i ...

随机推荐

  1. 伪元素::selection(怎么修改网页中被选中文本的样式)

    当我们用鼠标选中一段文字的时候我们会发现文字的颜色和背景色都改变了, 有时候设计给这种选中状态设计了其他的样式,那么我们怎么来自定义选中的文本的样式呢? 用::selection <p>我 ...

  2. canvas、image src、data url、blob file conversion

    //canvas.toDataURL('image/jpeg'), and convert to blob,blob is a File Object. but UC don't support fu ...

  3. 使用CSC.EXE编译第一个HELLO WORLD

    坐的没事,下了个C#2008,看帮助文件写了个HELLO.CS的源文件: //hello.cs //Show "Hello Word!" using system; class h ...

  4. quick-x 2.2.5 DragonBones 某些fla导出使用后player卡死

    1 2 3 4 5 6 7 8 9 10 11     --test 龙骨   self._db = dragonbones.new({             skeleton="game ...

  5. IOS创建单例的两种方法

    1.0  苹果官方写法:  static AccountManager *DefaultManager = nil; + (AccountManager *)defaultManager { if ( ...

  6. http://www.sufeinet.com/thread-655-1-1.html

    http://www.sufeinet.com/thread-655-1-1.html

  7. SSM三大框架整合详细教程

    使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...

  8. Control character in cookie value, consider BASE64 encoding your value-Cookie保存中文出错[转]

    项目当中用到cookie保存中文,但是会报如下错误: Control character in cookie value, consider BASE64 encoding your value 大概 ...

  9. 转载:EntityFramework 6.0< Code First > 连接 Mysql数据库

    转载自:http://blog.csdn.net/kmguo/article/details/19650299 网上有很多关于用EntityFrame来连接Mysql数据库的教程,可是很多并不靠谱,转 ...

  10. PHP返回JSON和XML类

    <?php class Reponse{ //private $result = array('code'=null,'message'=null,'data'=>null); /** * ...