UVa 529:addition chains, iterative deepened search + reduced branch

Source: Internet
Author: User
Tags integer

Topic Link:

Uva:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&itemid=8&category=24&page=show_ problem&problem=470

poj:http://poj.org/problem?id=2248

Type: Backtracking, iterative deepening search, reducing branch

Original title:

An addition chain for n was 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 are given an integer n. Your job is to construct a 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 The are for a asked for 5.

Sample input:

5
7
0

Sample output:

1 2 4 5 1 2 4 6 7 1 2 4 8 1 2 4 5 + 1 2 4 8 9 17 34 68 77

The main effect of the topic:

Give a number n, and then output a sequence with the least number of elements from 1 to n (there may be many schemes to output one of them).

For the K-number AK, its value equals Ai+aj ().

Analysis and Summary:

This is a typical iterative deepen search + minus the topic of the branch.

Iterative deepened search (ids,iterative deepening):

Iterative deepened search is essentially a depth-first search that limits the lower bound. That is, first allow depth-first search of the K-level search tree, if no feasible solution, and then repeat the k+1 after the search, until the search to the feasible solution.

In the iterative deepening search algorithm, continuous depth-first search is introduced, with each depth constraint added 1, until the target is searched.

The iterative deepening search algorithm is the depth-first search of the affine breadth-first search. It can not only satisfy the linear storage requirement of depth-first search, but also can find a target node with minimum depth.

From the practical application, the iterative deepening search effect is better, not more than the breadth of the first search is much slower, but the space complexity and depth first search is the same, than the breadth of first search is much smaller.

For this problem, you can first find a minimum of several elements to achieve n. According to greedy strategy, for each element of the value, choose to make it equal to a number of twice times, that is, for each Ai = Ai-1 + Ai-1, when the ai>=n jump out of the loop, to get the least number of elements.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.