Recall that a Hamiltonian path of a directed graph is one that visits every vertex exactly once. Most computer scientists believe that no such path can be computed efficiently. Your task here is to prove that they are wrong. Well, more or less...
Let be the number of vertices of the graph. For every pair of vertices and , with , there is exactly one arc connecting them, either from to or from to . We will use a constant to decide the direction of each arc. Compute , and let be the central digit of (the one to the right, if has an even number of digits). Then, if is odd, the arc goes from to . Otherwise, the arc goes from to .
For instance, if , and , then . Since 8 is even, the arc goes from 2 to 1. As another example, if , and , then . Since 7 is odd, the arc goes from 2 to 3.
Given and the constant that implicitly defines the arcs of the graph, can you find a Hamiltonian path?
Input consists of several cases, each one with and . Assume , , and that vertices are numbered starting at one.
For every case, print a line with a Hamiltonian path of the implicit
graph. The path can start and end at any vertices. If there is more than
one solution, print any of them. If there is no solution, print
“weird things happen”.
The definition of is only meant to be pseudo-random.
Be aware that you need long longs to compute
.
Input
2 4 3 21 2 15 10 31
Output
2 1 2 3 1 1 2 8 10 7 9 4 6 1 5 3 2