[POJ2248] Addition Chains 迭代加深搜索
Addition Chains
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 5454 | Accepted: 2923 | Special Judge | ||
Description
- 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
Output
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
//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 迭代加深搜索的更多相关文章
- POJ2248 Addition Chains 迭代加深
不知蓝书的标程在说什么,,,,于是自己想了一下...发现自己的代码短的一批... 限制搜索深度+枚举时从大往小枚举,以更接近n+bool判重,避免重复搜索 #include<cstdio> ...
- POJ 2248 - Addition Chains - [迭代加深DFS]
题目链接:http://bailian.openjudge.cn/practice/2248 题解: 迭代加深DFS. DFS思路:从目前 $x[1 \sim p]$ 中选取两个,作为一个新的值尝试放 ...
- poj 2248 Addition Chains (迭代加深搜索)
[题目描述] An addition chain for n is an integer sequence with the following four properties: a0 = 1 am ...
- UVA 529 - Addition Chains,迭代加深搜索+剪枝
Description An addition chain for n is an integer sequence with the following four properties: a0 = ...
- C++解题报告 : 迭代加深搜索之 ZOJ 1937 Addition Chains
此题不难,主要思路便是IDDFS(迭代加深搜索),关键在于优化. 一个IDDFS的简单介绍,没有了解的同学可以看看: https://www.cnblogs.com/MisakaMKT/article ...
- POJ1129Channel Allocation[迭代加深搜索 四色定理]
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14601 Accepted: 74 ...
- BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1800 Solved: 984[Submit][Statu ...
- 迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14191 Acc ...
- 迭代加深搜索 codevs 2541 幂运算
codevs 2541 幂运算 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...
随机推荐
- JAVA内存模型与JVM内存结构
问题:什么事java内存模型? 首先呢不要答堆.栈.方法区.这是JVM的内存结构.下面阐述了JMM和JVM的区别和自己对JMM的见解 1.Java内存模型(JMM):即多线程相关的.定义了一个线程对另 ...
- 无法解析的外部符号,该符号在xxx函数中被引用
无法解析的外部符号.........,该符号在函数.........被引用 在我们敲代码的过程中,我们偶尔会遇到这个问题,这个问题大多数都是因为你自己的程序有问题,而不是缺少相应的库文件.话不多说,直 ...
- PiVot 用法
基本语法: SELECT <非透视的列>, [第一个透视的列] AS <列名称>, [第二个透视的列] AS <列名称>, ... [最后一个透视的列] AS &l ...
- JavaScript之基本概念(二)
今天主要介绍两个东西:JS标识符和数据类型 一 变量和常量 在介绍标识符之前有必要先了解一下JS中的变量和常量. 变量:程序执行期间可操作的临时存储数据的内存空间. 声明方式: var:函数作用域,变 ...
- 10 大 python 库
TensorFlow Scikit-Learn Numpy Keras PyTorch LightGBM Eli5 SciPy Theano Pandas 简介 python 是最流行和使用最广泛的编 ...
- selenium-03-常用操作
基本介绍: Selenium工具专门为WEB应用程序编写的一个验收测试工具. Selenium的核心:browser bot,是用JavaScript编写的. Selenium工具有4种:Seleni ...
- redirectTo、navigateTo与switchTap区别
老是记忆不大清楚,简单写一下 简单作区分就是: redirectTo:关闭当前页(卸载),跳转到指定页 navigateTo:保留当前页(隐藏),跳转到指定页 switchTap:只能用于跳转到tab ...
- 在Docker中启动Cloudera
写在前面 记录一下,一个简单的cloudera处理平台的构建过程和一些基本组件的使用 前置说明 需要一台安装有Docker的机器 docker常用命令: docker ps docker ps -a ...
- 使用path监听指定文件系统的变化
在以前的JAVA版本中,如果程序需要检测文件的变化,那么需要开辟一个线程每隔一段时间去遍历一次指定的目录,如果发现此次遍历结果和上次不同,那么就认为文件变动了 ,这样的方式非常繁琐,JAVA 7之后的 ...
- mui中判断是点击还是滑动
判断和滑动是两种触发方式 滑动分为四种,上下左右(swipeup,swipedown,swipeleft,swiperight) 点击分为两种,点击和双击,一般用单机(tap) 根据自己不同的需求进行 ...