Hamiltonian path P28992


Statement
 

pdf   zip

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 nn be the number of vertices of the graph. For every pair of vertices xx and yy, with x<yx < y, there is exactly one arc connecting them, either from xx to yy or from yy to xx. We will use a constant kk to decide the direction of each arc. Compute r=(k+x)(kx)(k+y)(ky)r = (k + x)(k - x)(k + y)(k - y), and let dd be the central digit of rr (the one to the right, if rr has an even number of digits). Then, if dd is odd, the arc goes from xx to yy. Otherwise, the arc goes from yy to xx.

For instance, if k=4k = 4, x=1x = 1 and y=2y = 2, then r=5362=180r = 5 \cdot 3 \cdot 6 \cdot 2 = 180. Since 8 is even, the arc goes from 2 to 1. As another example, if k=21k = 21, x=2x = 2 and y=3y = 3, then r=23192418=188784r = 23 \cdot 19 \cdot 24 \cdot 18 = 188784. Since 7 is odd, the arc goes from 2 to 3.

Given nn and the constant kk that implicitly defines the arcs of the graph, can you find a Hamiltonian path?

Input

Input consists of several cases, each one with nn and kk. Assume 2n1042 \le n \le 10^4, n<k3104n < k \le 3 \cdot 10^4, and that vertices are numbered starting at one.

Output

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”.

Observations

  • The definition of rr is only meant to be pseudo-random.

  • Be aware that you need long longs to compute rr.

Public test cases
  • 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
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Official solutions
    C++
    User solutions
    C++