Addition Chains 

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 ( ) there exist two (not neccessarily different) integers i and j ( ) 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 Specification

The input file will contain one or more test cases. Each test case consists of one line containing one integer  n  (  ). Input is terminated by a value of zero (0) for  n .

Output Specification

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

题意:给定一个n,要求求出这样一条最短的满足以下条件的加法链:

1、起点为1,终点为n, 2、递增 3、每个数字可以找到该链中其他两个数字相加组成

思路:本来我的思路是直接暴力的。。果断超时。大一点的数据都跑不出来。。原因是如果直接搜。加法链的个数没有限制,直接要多很多不必要的搜索。

然后在网上看到了一种迭代搜索法。是先确定一个最小的条件。在本题中,仔细观察不难发现,如果每次都是以两倍的趋势上升,那么加法链将会最短。那么由最短的这个条件开始,进行搜索,如果这个条件搜不到,就把条件+1,继续搜索。直到出现一个满足条件的方案结束。。

不过这题要多一个优化。举个例子。比如n为16时。最短条件为5.前3个数字为1 2 3时候,最大将只能为1 2 3 6 12,是到不了16的,因此可以发现。如果一个数字,不断乘2直到到达那个条件。如果小于n,那么这条搜索多余的,直接返回。

#include <stdio.h>
#include <string.h> int n;
int ci;
int judge;
int out[10005];
void dfs(int num)
{
if (judge)
return;
if (num == ci)
{
if (out[num] == n)
{
judge = 1;
}
return;
}
for (int i = num; i >= 0; i --)
{
for (int j = num; j >= i; j --)
{
if (out[i] + out[j] > out[num] && out[i] + out[j] <= n)
{
int sum = out[i] + out[j];
for (int k = num + 1; k <= ci; k ++)
{
sum *= 2;
}
if (sum < n)
continue;
out[num + 1] = out[i] + out[j];
dfs(num + 1);
if (judge)
return;
}
}
}
}
int main()
{
while (scanf("%d", &n) != EOF && n)
{
judge = 0;
ci = 0;
int sb = 1;
while (sb < n)
{
sb *= 2;
ci ++;
}
while (1)
{
memset(out, 0, sizeof(out));
out[0] = 1;
dfs(0);
if (judge)
break;
ci ++;
}
for (int i = 0; i < ci; i ++)
{
printf("%d ", out[i]);
}
printf("%d\n", out[ci]);
}
return 0;
}

但是这个写法还是有一定问题的。。我测试了一些数据。如1111.还是要跑好一会的。。不过没超时。估计是数据的问题。。然后看了别人的代码。写了另一个。这个方法是先确定一个上限。每次找到后,把上限进行缩小。直到找到最小为止。。

#include <stdio.h>
#include <string.h> int n;
int end;
int num[35];
int output[35];
void dfs(int star)
{
if (star < end)
{
for (int i = star - 1; i >= 0; i --)
{
int sum = num[star - 1] + num[i];
if (sum <= n)
{
num[star] = sum;
if (num[star] == n && end > star)
{
for (int j = 0; j <= end; j ++)
{
output[j] = num[j];
}
end = star;
}
int sb = sum;
for (int j = star + 1; j <= end; j ++)
sb *= 2;
if (sb < n)
continue;
dfs(star + 1);
} }
}
}
int main()
{
while (scanf("%d", &n) != EOF && n)
{
memset(num, 0, sizeof(num));
memset(output, 0, sizeof(output));
if (n == 1)
{
end = 0;
output[0] = 1;
}
else
end = 30;
num[0] = 1;
dfs(1);
for (int i = 0; i < end; i ++)
printf("%d ", output[i]);
printf("%d\n", output[end]);
}
return 0;
}

UVA 529 Addition Chains(迭代搜索)的更多相关文章

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

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

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

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

  3. 1443:【例题4】Addition Chains

    1443:[例题4]Addition Chains 题解 注释在代码里 注意优化搜索顺序以及最优化剪枝 代码 #include<iostream> #include<cstdio&g ...

  4. poj 2248 Addition Chains (迭代加深搜索)

    [题目描述] An addition chain for n is an integer sequence with the following four properties: a0 = 1 am ...

  5. 「一本通 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\). 数列中的一 ...

  6. [POJ 2248]Addition Chains

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

  7. Addition Chains POJ - 2248 (bfs / dfs / 迭代加深)

    An addition chain for n is an integer sequence <a0, a1,a2,...,am=""> with the follow ...

  8. 【POJ2248、LOJ#10021】 Addition Chains

    事先预警:由于我太蒻了,本做法只能在POJ.LOJ等小数据(N<=100)平台上通过,在UVa(洛谷)上大数据并不能通过 戳我获得更好的观看效果 本题不用看,爆搜就是了,但是纯爆搜显然会爆时间, ...

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

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

随机推荐

  1. DG创建和提取虚拟机文件

    http://www.cr173.com/soft/33359.html http://www.diskgenius.cn/help/newvmdk.php

  2. 制作Linux(Fedora、Ubuntu、CentOS)优盘启动

    随着嵌入式技术的快速发展,Linux快速发展过一段时间.虽然现在不是很热,但是linux在现实社会的使用还是很有用处.而光盘有有些落伍,不仅浪费而且不环保,所以质优价廉的优盘就脱颖而出.所以,用优盘制 ...

  3. hpu第五届acm比赛

    vijos P1211生日日数   描述 CCC老师的生日是YY年MM月DD日,他想知道自己出生后第一万天纪念日的日期(出生日算第0天). 格式 输入格式 从文件的第一行分别读入YY,MM,DD其中1 ...

  4. Dockerfile指令总结

    指令的一般格式为INSTRUCTION arguments,指令包含FROM.MAINTAINER.RUN等. FROM 格式为FROM <image>或FROM <image> ...

  5. 关于Cocos2d-x 3.0正式版 粒子问题在IOS上正常显示,在Android下有问题的解决方式

    前几个在Cocos2d-x论坛上,有人提到粒子系统的问题..这里列举一下解决的方法: 或许到时候大家用粒子效果的时候也会发现这个问题,如今把这个问题的解决办法说出来.至于原因我也不知道是引擎的问题还是 ...

  6. [转]PostgreSQL 中文资料汇总

    原文链接:http://francs3.blog.163.com/blog/static/405767272014017341219/ --1 中文社区网站  PostgreSQL 中文社区官网: h ...

  7. Python常用模块中常用内置函数的具体介绍

    Python作为计算机语言中常用的语言,它具有十分强大的功能,但是你知道Python常用模块I的内置模块中常用内置函数都包括哪些具体的函数吗?以下的文章就是对Python常用模块I的内置模块的常用内置 ...

  8. fg、bg、jobs、&、nohup、ctrl + z命令

    fg.bg.jobs.&.nohup.ctrl + z命令 一.& 加在一个命令的最后,可以把这个命令放到后台执行,如gftp &, 二.ctrl + z 可以将一个正在前台执 ...

  9. 怎样实现cocos2d-x之文字渲染

    // 1.创建一段文本 // create函数的三个参数分别为:文本内容.字体和字体大小 CCLabelTTF *font=CCLabelTTF::create("Hello World&q ...

  10. 软件project师的属性与发展

    工作近十年了.[软件project师] 一直是我职业 title 的中心词,仅仅是前面的修饰语在不断变化,从0基础.中级.高级到资深. 事实上 [软件project师] 是一个非常泛化的定义.工作现实 ...