Under construction

Still being built. Things may change

N-Queen (mod N)

Time limit1sMemory limit512 MB

Summary
Given N, place N queens on an N by N board whose opposite edges are joined, so no two share a row, column, or wrapped diagonal. Print one valid placement, or -1 if none exists.
Level

Medium6 of 10

Topics
Math, Implementation
Solved
No attempts yet

Problem

Given an N×NN \times N chessboard, print one way to place NN queens so that no two queens attack each other. The square in row rr and column cc has coordinates (r,c)(r, c). The top-left square is (1,1)(1, 1), and the bottom-right square is (N,N)(N, N). The opposite edges of the board are joined together. Two queens at (a,b)(a, b) and (c,d)(c, d) attack each other if at least one of the following four conditions holds: (1) a=ca = c, (2) b=db = d, (3) a+b≡c+d(modN)a+b \equiv c+d \pmod N, (4) a−b≡c−d(modN)a-b \equiv c-d \pmod N.

Input

The first line contains an integer NN. (1≤N≤500 0001 \le N \le 500\,000)

Output

If NN queens can be placed, print A1A_1, A2A_2, A3A_3, ⋯\cdots, ANA_N on the first line, where AiA_i is the column number of the queen in row ii. (1≤Ai≤N1 \le A_i \le N)

If NN queens cannot be placed, print -1.

Examples2

  1. Example 1

    Input
    2
    
    Expected output
    -1
    
  2. Example 2

    Input
    5
    
    Expected output
    3 1 4 2 5