Ultraviolet A 529-Addition Chains

Source: Internet
Author: User

Addition Chains


An addition chainNIs an integer sequence with the following four properties:

  • A0 = 1
  • AM=N
  • A0 <A1 <A2 <... <am-1 <AM
  • For eachK() There exist two (not neccessarily different) Integers
    IAndJ()
    AK=AI+AJ

You are given an integerN. Your job is to construct an addition chain
NWith minimal length. If there is more than one such sequence, any one is acceptable.

For example, <,> and <,> are both valid solutions when you are asked for an addition chain for 5.

Input Specification

The input file will contain in 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)
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
571215770

Sample output
1 2 4 51 2 4 6 71 2 4 8 121 2 4 5 10 151 2 4 8 9 17 34 68 77
Iterative deepening DFS is suitable for n = large and 17 numbers. Although there are a large number of repeated computations in the iteration itself, the depth of the repeated search tree is small;
The iterative deep search algorithm is the depth-first search of the imitation breadth-first search. It not only meets the linear storage requirements of Deep-first search, but also ensures the discovery of a minimum-depth target node.
#include <string.h>#include <stdio.h>int a[100],best[100],n,min,f;int minlen(int num,int len){int x=num,y=len; while (x<n) {x=x<<1;++y;} return y;}void dfs(int len){ int i,j,time,x; if ((a[len]==n)&&(len<min)) {f=0; min=len; for (i=1;i<=len;i++) best[i]=a[i]; } if ((a[len]>=n)||(len>=min)) return ; if (minlen(a[len],len)>=min) return ; for (i=1;i<=len;i++) for (j=i;j<=len;j++) {  a[len+1]=a[i]+a[j];  if (a[len+1]>a[len]) dfs(len+1); }}int main(){ int i; a[1]=1; a[2]=2; best[1]=1; best[2]=2; while (scanf("%d",&n),n) {  f=1; min=minlen(2,2)-1;  if (n<=2) {min=n;f=0;}  while (f)  {++min;   dfs(2);  }  for (i=1;i<min;i++)     printf("%d ",best[i]);  printf("%d\n",n); } return 0;}

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.