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. New UWP Community Toolkit - RadialGauge

    概述 New UWP Community Toolkit  V2.2.0 的版本发布日志中提到了 RadialGauge 的调整,本篇我们结合代码详细讲解  RadialGauge 的实现. Radi ...

  2. alpha冲刺第三天

    一.合照 二.项目燃尽图 三.项目进展 今天是一个瓶颈期,在昨天被困住的地方今天还是没能解决,所以今天的项目进展并没有发生什么变化. 今天晚上xl和lj去实验室找学姐了,在学姐的帮助下大概有了一点思路 ...

  3. Linux下进程间通信的六种机制详解

    linux下进程间通信的几种主要手段:        1.管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具 ...

  4. PTA題目的處理(三)

    题目7-1 高速公路超速處罰 1.實驗代碼 #include <stdio.h> //#include <stdlib.h> int main() { int csp,lsp; ...

  5. 20145237 实验五《Java网络编程》

    20145237 实验五<Java网络编程> 一.实验内容 •1.运行下载的TCP代码,结对进行,一人服务器,一人客户端: •2.利用加解密代码包,编译运行代码,一人加密,一人解密: •3 ...

  6. webview缓存及跳转时截取url地址、监听页面变化

    缓存及一些设定 我在做一些项目时,h5做的项目手机浏览器能使用,但是在搬到webview时候不能用,这个时候通过查阅资料,原来是webview没有设定好,包括缓存.缓存大小及路径等等 mWebview ...

  7. nyoj 非洲小孩

    非洲小孩 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 家住非洲的小孩,都很黑.为什么呢?第一,他们地处热带,太阳辐射严重.第二,他们不经常洗澡.(常年缺水,怎么洗 ...

  8. Centos7 Yum方式安装Mysql7

    不废话,直奔主题,可以覆盖安装. 下载并安装MySQL官方的 Yum Repository [root@localhost ~]# wget -i -c http://dev.mysql.com/ge ...

  9. JAVA_SE基础——71.Random类制作随机验证码

    public class Demo5 { public static void main(String[] args) { char[] arr={'s','b','g','h','a','c'}; ...

  10. lamp环境搭建经验总结

    环境:centos6.4,13个源码包:参考教程高罗峰细说php思路:1.首先确定gcc,g++的安装,因为这是c语言的编译工具,没有它,源码不可能安装,redhat的yum需要配置,分为本地源和网络 ...