Addition Chains
Time limit1sMemory limit256 MB
For each n up to 100, find a shortest addition chain ending at n and print the lexicographically smallest among all shortest ones.
- Level
Hard8 of 10
- Topics
- DFS, Backtracking, Brute force, Greedy
- Solved
- No attempts yet
Problem
For a natural number , a sequence of natural numbers is called an addition chain for if it satisfies all four of the following conditions.
- For every there exist two indices with (they may be equal) such that .
In other words, the first term is , the last term is , the sequence is strictly increasing, and every term is the sum of two earlier terms (the same term may be used twice). An addition chain with the fewest terms (that is, the smallest ) is called a shortest addition chain.
There can be several shortest addition chains. Among them, output the one that is lexicographically smallest. To compare two sequences lexicographically, compare their terms one by one from the front; at the first position where they differ, the sequence with the smaller value comes first.
Given several natural numbers , write a program that, for each of them, finds the lexicographically smallest shortest addition chain.
Input
The input consists of one or more test cases. Each test case is a single line containing a natural number (). The last line of the input contains a single , which is not processed.
Output
For each test case, print on one line the lexicographically smallest shortest addition chain, with the terms separated by single spaces.