【题目描述】

	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.

【题目链接】

Addition Chains

【算法】

枚举构造答案,迭代加深搜索。
剪枝:
1、从大到小枚举i,j
2、排除冗余(判重)

【代码】

#include <stdio.h>
#include <cstring>
using namespace std;
int n,deep;
int ans[1000],v[1000];
bool dfs(int cur) {
if(cur>deep) {
if(ans[deep]==n) return 1;
return 0;
}
for(int i=cur-1;i>=0;i--) {
for(int j=i;j>=0;j--) {
if(ans[i]+ans[j]<=n&&!v[ans[i]+ans[j]]) {
ans[cur]=ans[i]+ans[j]; v[ans[i]+ans[j]]=1;
if(dfs(cur+1)) return 1;
v[ans[i]+ans[j]]=0;
}else if(ans[i]+ans[j]<=ans[cur-1]) break;
}
}
return 0;
}
int main() {
while(~scanf("%d",&n)&&n) {
printf("1"); ans[0]=1;
if(n==1) { puts(""); continue; }
for(deep=1;!dfs(1);deep++) memset(v,0,sizeof(v));
for(int i=1;i<=deep;i++) printf(" %d",ans[i]);
puts("");
}
return 0;
}

poj 2248 Addition Chains (迭代加深搜索)的更多相关文章

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

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

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

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

  3. [POJ 2248]Addition Chains

    Description An addition chain for n is an integer sequence with the following four properties: a0 = ...

  4. POJ2248 Addition Chains 迭代加深

    不知蓝书的标程在说什么,,,,于是自己想了一下...发现自己的代码短的一批... 限制搜索深度+枚举时从大往小枚举,以更接近n+bool判重,避免重复搜索 #include<cstdio> ...

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

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

  6. UVA 529 - Addition Chains,迭代加深搜索+剪枝

    Description An addition chain for n is an integer sequence  with the following four properties: a0 = ...

  7. C++解题报告 : 迭代加深搜索之 ZOJ 1937 Addition Chains

    此题不难,主要思路便是IDDFS(迭代加深搜索),关键在于优化. 一个IDDFS的简单介绍,没有了解的同学可以看看: https://www.cnblogs.com/MisakaMKT/article ...

  8. 迭代加深搜索 POJ 1129 Channel Allocation

    POJ 1129 Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14191   Acc ...

  9. POJ1129Channel Allocation[迭代加深搜索 四色定理]

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14601   Accepted: 74 ...

随机推荐

  1. MySQL--全文索引作用、原理及使用注意

    作用 MySQL索引可以分为:主键索引.普通索引.唯一索引.全文索引.其中,全文索引应该是是比较特殊的,它只有少数的几个存储引擎支持,且只有类型为char.vchar.text的列能建立全文索引.以前 ...

  2. 【02】Python 字符串、列表、元组、字典

    1 列表 list就是一种采用分离式技术实现的动态顺序表(tuple也一样): 在建立空表(或者很小的表)时,系统分配一块能容纳8个元素的存储区: 在执行插入操作(insert或append)时,如果 ...

  3. java调用存储过程的方式

    1.问号是入参和出参,出参要指定类型 CallableStatement pstmt = conn.prepareCall("{call dbo.UP_CodeUp_***(?,?,?,?, ...

  4. Spring Cloud(2)主要组件应用实例

    SpringCloud SpringCloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.负载均衡.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行 ...

  5. Cookie 跨域???

    跨域的 “域” 指的是 URL,包括协议.域名.端口. cookie 和跨域没关系,只和域名有关,和端口无关. http://test1.demo.com:8080 与 http://test2.de ...

  6. php array_flip()函数 语法

    php array_flip()函数 语法 作用:用于反转/交换数组中所有的键名以及它们关联的键值.富瑞联华 语法:array_flip(array); 参数: 参数 描述 array 必需.规定需进 ...

  7. JSP 自动刷新

    JSP 自动刷新 想象一下,如果要直播比赛的比分,或股票市场的实时状态,或当前的外汇配给,该怎么实现呢?显然,要实现这种实时功能,您就不得不规律性地刷新页面.JSP提供了一种机制来使这种工作变得简单, ...

  8. Linux6.6及以上版本配置oracle-ASM共享储存-UDEV

    在linux6.6版本之前,我们又两种方式可以配置asm共享磁盘,一种是安装asm驱动包进行asm磁盘组配置,另一种是UDEV通过识别共享存储UUID号进行asm磁盘组配置. 但在linux6.6之后 ...

  9. 查看jQuery file upload的image preview如何工作的

    查看readme中的说明, JavaScript Load Image library v. 1.13.0+: Required for the image previews and resizing ...

  10. Linux_SquidProxyServer代理服务器

    目录 目录 Squid proxy server Web proxy server operating principle Squid features Setup squid server Setu ...