[POJ 2248]Addition Chains
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的更多相关文章
- poj 2248 Addition Chains (迭代加深搜索)
[题目描述] An addition chain for n is an integer sequence with the following four properties: a0 = 1 am ...
- POJ 2248 - Addition Chains - [迭代加深DFS]
题目链接:http://bailian.openjudge.cn/practice/2248 题解: 迭代加深DFS. DFS思路:从目前 $x[1 \sim p]$ 中选取两个,作为一个新的值尝试放 ...
- [zoj] 1937 [poj] 2248 Addition Chains || ID-DFS
原题 给出数n,求出1......n 一串数,其中每个数字分解的两个加数都在这个序列中(除了1,两个加数可以相同),要求这个序列最短. ++m,dfs得到即可.并且事实上不需要提前打好表,直接输出就可 ...
- POJ 2245 Addition Chains(算竞进阶习题)
迭代加深dfs 每次控制序列的长度,依次加深搜索 有几个剪枝: 优化搜索顺序,从大往下枚举i, j这样能够让序列中的数尽快逼近n 对于不同i,j和可能是相等的,在枚举的时候用过的数肯定不会再被填上所以 ...
- [POJ2248] Addition Chains 迭代加深搜索
Addition Chains Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5454 Accepted: 2923 ...
- UVA 529 Addition Chains(迭代搜索)
Addition Chains An addition chain for n is an integer sequence with the following four propertie ...
- 1443:【例题4】Addition Chains
1443:[例题4]Addition Chains 题解 注释在代码里 注意优化搜索顺序以及最优化剪枝 代码 #include<iostream> #include<cstdio&g ...
- 「一本通 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\). 数列中的一 ...
- Addition Chains POJ - 2248 (bfs / dfs / 迭代加深)
An addition chain for n is an integer sequence <a0, a1,a2,...,am=""> with the follow ...
随机推荐
- Alpha冲刺总结
团队成员 陈家权 031502107 赖晓连 031502118 雷晶 031502119 林巧娜 031502125 庄加鑫 031502147 一.项目预期计划及现实进展 项目预期计划 现实进展 ...
- 张金禹 C语言--第0次作业
1:在填报专业的时候,我也犹豫了很久,但最后还是选择了计算机专业.因为在上大学之前我就对编程.设计等有浓厚的兴趣,但繁重的高中学习任务使我没有过多的去关注,所以我选择了计算机专业去培养我在这方面的兴趣 ...
- router问题
var http = require("http"); var router = require("./router.js"); //创建服务器 var ser ...
- django获取ip与数据重复性判定
获取ip if request.META.has_key('HTTP_X_FORWARDED_FOR'): ip_c = request.META['HTTP_X_FORWARDED_FOR'] el ...
- [2]十道算法题【Java实现】
前言 清明不小心就拖了两天没更了-- 这是十道算法题的第二篇了-上一篇回顾:十道简单算法题 最近在回顾以前使用C写过的数据结构和算法的东西,发现自己的算法和数据结构是真的薄弱,现在用Java改写一下, ...
- Python内置函数(36)——reversed
英文文档: reversed(seq) Return a reverse iterator. seq must be an object which has a __reversed__() meth ...
- javascript实现浏览器窗口大小被改变时触发事件的方法
转载 当浏览器的窗口大小被改变时触发的事件window.onresize 为事件指定代码: 复制代码代码如下: window.onresize = function(){ } 例如: 浏览器可见区域信 ...
- OpenID Connect 是什么?
一.OpenID Connect的概念 1.OpenID Connect 是什么? OpenID Connect 是一套基于 OAuth 2.0 协议的轻量级规范,提供通过 API 进行身份交互的框架 ...
- J2ee入门:servlet-mapping的映射配置
<servlet-mapping>元素在Servlet和URL样式之间定义一个映射.它包含了两个子元素<servlet- name>和<url-pattern> & ...
- .net 4种单例模式
转载: https://www.cnblogs.com/dreign/archive/2012/05/08/2490212.html using System; using System.Collec ...