Under construction

Still being built. Things may change

Hyperloop

Time limit1sMemory limit128 MB

Summary
For odd N, print (N-1)/2 Hamiltonian cycles on the complete graph of N cities that partition all edges, using the given seat-walk construction.
Level

Medium4 of 10

Topics
Greedy, Math, Implementation, Combinatorics
Solved
No attempts yet

Problem

In August 2013 a design appeared for a new means of transport called Hyperloop, in which capsules travel through a tube at a thousand kilometres per hour. One thing nobody knows yet: the first line will not connect two big cities, it will connect two small provincial towns. Following that example, every two of the country's NN cities will be joined directly by one two-way Hyperloop tube.

Once the tubes open, you have to organize several sightseeing routes. One route visits all NN cities, each exactly once, and returns to the city it started from, so it uses exactly NN tubes. To keep the traffic down, two different routes may not share a tube, no matter which way they run through it.

Because NN is odd, you can organize (N−1)/2(N-1)/2 routes and leave no tube unused. Construct those routes.

Input

The first line contains the number of cities NN. NN is odd and 3≤N<20003 \le N < 2000. The cities are numbered from 1 to NN.

Output

Print (N−1)/2(N-1)/2 routes, one per line. On each line print the NN city numbers the route visits, in order from the starting city, separated by single spaces. Do not repeat the starting city at the end.

Many sets of routes satisfy the conditions, so only the one built by the following rule is accepted.

Let M=(N−1)/2M = (N-1)/2. Put city 1 in the centre and place the other N−1N-1 cities around a circle. The seats are numbered 00 to N−2N-2, and seat pp holds city p+2p+2.

For k=0,1,…,M−1k = 0, 1, \dots, M-1, route kk starts at city 1, visits the cities in seats c0,c1,…,cN−2c_0, c_1, \dots, c_{N-2} in that order, and returns to city 1, where

ci={k−i/2(i even)k+(i+1)/2(i odd)c_i = \begin{cases} k - i/2 & (i \text{ even}) \\ k + (i+1)/2 & (i \text{ odd}) \end{cases}

and every seat number is taken modulo N−1N-1. In other words the seats run k,  k+1,  k−1,  k+2,  k−2,  …,  k+(M−1),  k−(M−1),  k+Mk,\; k+1,\; k-1,\; k+2,\; k-2,\; \dots,\; k+(M-1),\; k-(M-1),\; k+M stepping alternately forward and backward with a growing gap, which covers all N−1N-1 seats. Print the routes in increasing order of kk.

Examples2

  1. Example 1

    Input
    5
    
    Expected output
    1 2 3 5 4
    1 3 4 2 5
    
  2. Example 2

    Input
    7
    
    Expected output
    1 2 3 7 4 6 5
    1 3 4 2 5 7 6
    1 4 5 3 6 2 7