Powers of Three
Time limit1sMemory limit128 MB
Given n, list the elements of the n-th smallest subset of powers of 3 when subsets are ordered by their sums.
- Level
Medium6 of 10
- Topics
- Math, Combinatorics, Bit manipulation, Implementation
- Solved
- No attempts yet
Problem
Consider the set containing every power of with a non-negative integer exponent.
Sort all subsets of in ascending order by the sum of the elements in each subset. Because the sum of distinct powers of is always unique, this ordering is uniquely determined. The empty set, whose sum is , comes first.
Write a program that finds the -th subset in this order.
Input
The input consists of several lines. Each line contains a single integer . is a positive integer with at most digits, that is, . The last line contains , which is not processed.
Output
For each , print the -th set of the sorted order on its own line. List the elements in ascending order in the format { e1, e2, ... }, with a single space just inside each brace and the elements separated by a comma and a space (, ). Print the empty set as { }.