[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次运算就可以计算出 ...
随机推荐
- mapper 传多个参数
Mybatis的Mapper接口的参数,一般是一个对象,但如果不是对象,并且有多个参数的时候呢?我们第一个的想法是把参数封装成一个java.util.Map类型,然后在方法的注释上面写上map的key ...
- maven引入本地jar包的方法
maven作为包管理工具,好处不必多说 但是有些情况,比如需要引入第三方包,如快递鸟,支付宝,微信等jar包(当然有可能直接提供maven依赖) 如果直接下载到本地之后,怎么整合到自己的maven工程 ...
- CSS3动画animation认识,animate.css的使用
CSS动画 可以取代js动画 在移动端会更加流畅! 下面是一个的绘制太阳系各大行星运行轨迹笔记,可以自学参考! -------------------------------------------- ...
- 使用 Eslint & standard 规范前端代码
前言 JavaScript的动态语言类型,给它带来了独特的魅力,产生了风格多样的开发范式,同时也带来了一些问题,从运行时常见的 undefined .null 报错,到代码随意的加减分号.换行.空格, ...
- Salesforce学习之路-admin篇(三)role hierarchy & sharing
1. Role Hierarchy 在私有或者混合模型中,如果在organization-wide defaults设置某个对象为Private,那么对象的记录只有拥有者可以查看.但是,role hi ...
- scrapy爬取迅雷电影天堂最新电影ed2k
前言 几天没用scrapy爬网站了,正好最近在刷电影,就想着把自己常用的一个电影分享网站给爬取下来保存到本地mongodb中 项目开始 第一步仍然是创建scrapy项目与spider文件 切换到工作目 ...
- mysql replace into使用
使用mysql插入数据时,我们通常使用的是insert into来处理,replace into有时可以替代insert into功能,但replace into也有自己的用法 准备一张测试表: CR ...
- git 查看分支
1.查看本地分支 git branch 2.查看所有分支 git branch -a 2.查看所有分支及对应版本信息 git branch -va
- WordPress新用户注册时提示“您的密码重设链接无效”
在使用Wordpress密码找回功能及新用户注册邮件中的重置密码链接时,Wordpress提示“您的密码重设链接无效,请在下方请求新链接.”.“该key似乎无效”.“invalid key”. 这个其 ...
- Python内置函数之enumerate() 函数
enumerate() 函数属于python的内置函数之一: python内置函数参考文档:python内置函数 转载自enumerate参考文档:python-enumerate() 函数 描述 e ...