Sharkovski's Ordering
Time limit2sMemory limit128 MB
Sort each test case of up to 255 integers into Sharkovski ordering and print them space-separated on one line.
Problem
In a 1964 paper on continuous mappings of the reals into the reals, Alexandr Sharkovski used the following ordering of the positive integers.
Here means that comes before .
Ciesielski and Pogoda (2008) describe it this way.
First come the odd numbers, beginning with 3, arranged in increasing order. This sequence is repeated with each odd integer multiplied by . The initial sequence is repeated again with each odd integer multiplied by , and so on. The terminal sequence consists of the nonnegative powers of arranged in decreasing order (note that ).
Write a program that reads a list of at most 255 unsigned integers, not necessarily distinct, with values at most 65,535, separated by white space and terminated by 0. Print the numbers arranged in Sharkovski's ordering on one line. The numbers on the line are separated by exactly one space.
Input
The first line contains an integer (). Then input cases follow. Each input case is a non-empty list of at most 255 unsigned integers, not necessarily distinct, with values at most 65,535. Numbers are separated by white space, and each input case is terminated by 0.
Output
For every input case, print the numbers of that case arranged in Sharkovski's ordering on one line. The numbers on a line are separated by exactly one space.