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 (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

题解

考虑迭代加深的$dfs$
我们一开始可以算出最少需要多少个,就是答案的下界
这个怎么算呢?从$1$开始不断乘$2$,看什么时候比$n$大,就是下界
然后将答案往上加,用$dfs$判断是否可行
这样我们可以进行减枝了
如果当前的答案是$ans$,当前搜索的位置是$x$
如果$a[x]*2^{ans-x}$还比$n$小,就可以$return$了
这样就可以搜过去了

 #include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
#define RE register
#define IL inline
using namespace std; int n,depth;
int ans[]; bool Dfs(int cen,int dep)
{
if (cen==dep)
{
if (ans[cen]==n) return true;
return false;
}
for (int i=;i<=cen;i++) for (RE int j=;j<=i;j++)
{
if (((ans[i]+ans[j])<<(dep-cen-))<n) continue;
ans[cen+]=ans[i]+ans[j];
if (Dfs(cen+,dep)) return true;
}
return false;
} int main()
{
ans[]=;
while (scanf("%d",&n)&&n)
{
depth=log2(n)+;
while (true)
{
if (Dfs(,depth)) break;
depth++;
}
for (RE int i=;i<=depth;i++) printf("%d ",ans[i]);
printf("\n");
}
return ;
}

[POJ 2248]Addition Chains的更多相关文章

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

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

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

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

  3. [zoj] 1937 [poj] 2248 Addition Chains || ID-DFS

    原题 给出数n,求出1......n 一串数,其中每个数字分解的两个加数都在这个序列中(除了1,两个加数可以相同),要求这个序列最短. ++m,dfs得到即可.并且事实上不需要提前打好表,直接输出就可 ...

  4. POJ 2245 Addition Chains(算竞进阶习题)

    迭代加深dfs 每次控制序列的长度,依次加深搜索 有几个剪枝: 优化搜索顺序,从大往下枚举i, j这样能够让序列中的数尽快逼近n 对于不同i,j和可能是相等的,在枚举的时候用过的数肯定不会再被填上所以 ...

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

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

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

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

  7. 1443:【例题4】Addition Chains

    1443:[例题4]Addition Chains 题解 注释在代码里 注意优化搜索顺序以及最优化剪枝 代码 #include<iostream> #include<cstdio&g ...

  8. 「一本通 1.3 例 4」Addition Chains

    Addition Chains 题面 对于一个数列 \(a_1,a_2 \dots a_{m-1},a_m\) 且 \(a_1<a_2 \dots a_{m-1}<a_m\). 数列中的一 ...

  9. Addition Chains POJ - 2248 (bfs / dfs / 迭代加深)

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

随机推荐

  1. swift textview禁止用户使用复制粘贴

    //自定义一个TextView class Own_TextView: UITextView { override func caretRect(for position: UITextPositio ...

  2. java调用kettle的job和transfer工具类

    package com.woaiyitiaocai.util; import java.util.Map; import java.util.UUID; import org.apache.log4j ...

  3. Alpha第八天

    Alpha第八天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...

  4. JS中的 map, filter, some, every, forEach, for...in, for...of 用法总结

    1.map 有返回值,返回一个新的数组,每个元素为调用func的结果. let list = [1, 2, 3, 4, 5]; let other = list.map((d, i) => { ...

  5. mui对话框事件

    mui.confirm('生成成功,是否跳转到订单页面?','',['跳转','取消'],function(e){ if(e.index==0){ //点击跳转 }else if(e.index==1 ...

  6. Web Api 接收图片

    public async Task<HttpResponseMessage> Upload() { if (!Request.Content.IsMimeMultipartContent( ...

  7. 聊一聊C#的Equals()和GetHashCode()方法

    博客创建一年多,还是第一次写博文,有什么不对的地方还请多多指教. 关于这次写的内容可以说是老生长谈,百度一搜一大堆.大神可自行绕路. 最近在看Jeffrey Richter的CLR Via C#,在看 ...

  8. 在WebStorm中启动Angular项目

    点击配置 创建 选择命令 package.json 运行 查看运行结果

  9. Angular 学习笔记 ( 链接服务器 )

    ng 是做前端开发的, 所以通常我们会配上一个 API server. 本地调试时通常使用 proxy https://github.com/angular/angular-cli/blob/mast ...

  10. GridControl的常用操作

    1.GridView的回车跳转单元格和换行 private void gridView1_KeyPress(object sender, KeyPressEventArgs e)        {   ...