Problem:
Score : 900 points

Problem Statement

Let us consider a grid of squares with N rows and N columns. You want to put some domino pieces on this grid.
Each domino piece covers two squares that have a common side. Each square can be covered by at most one piece.

For each row of the grid, let's define its quality as the number of domino pieces that cover at least one square in this row.
We define the quality of each column similarly.

Find a way to put at least one domino piece on the grid so that the quality of every row is equal to the quality of every column,
or determine that such a placement doesn't exist.

Constraints

2 \le N \le 1000

Input

Input is given from Standard Input in the following format:

N

Output

If the required domino placement doesn't exist, print a single integer -1.

Otherwise, output your placement as N strings of N characters each.
If a square is not covered, the corresponding character must be . (a dot).
Otherwise, it must contain a lowercase English letter.
Squares covered by the same domino piece must contain the same letter.
If two squares have a common side but belong to different pieces, they must contain different letters.

Sample Input 1

6

Sample Output 1

aabb..
b..zz.
ba....
.a..aa
..a..b
..a..b

The quality of every row and every column is 2.

Sample Input 2

2

Sample Output 2

-1

Solution in LuaJIT (2.0.4) (judge verdict: Accepted)

local mfl = math.floor
local n = io.read("*n")

local function make_singlemap(k)
  local map = {}
  if k == 4 then
    map = {1, 1, 2, 3, 4, 4, 2, 3, 5, 6, 7, 7, 5, 6, 8, 8}
  elseif k == 3 then
    map = {1, 0, 0, 1, 0, 0, 0, 1, 1}
  elseif k == 5 then
    map = {1, 1, 2, 2, 3,
           8, 9, 0, 0, 3,
           8, 9, 0, 0, 4,
           7, 0, 9, 9, 4,
           7, 6, 6, 5, 5}
  else
    for i = 1, k * k do map[i] = 0 end
    local corner = mfl((k - 3) / 2)
    -- row = 1
    do
      local edge = mfl((k - 5) / 2)
      for col = 1, k do
        if col <= edge then map[col] = 0
        elseif k - edge < col then map[col] = 0
        elseif col == edge + 1 then map[col] = 1
        else
          map[col] = (col - edge - 2) % 4 < 2 and 2 or 1
        end
      end
    end
    -- 2 <= row <= corner
    for row = 2, corner do
      for col = 1, k do
        local idx = (row - 1) * k + col
        if col <= corner then
          if row + col == 1 + corner or row + col == 2 + corner then
            if (corner - col) % 2 == 0 then
              map[idx] = 1
            else
              map[idx] = 2
            end
          end
        elseif k - corner < col then
          if col - row == k - corner or col - row == k - corner - 1 then
            map[idx] = row % 2 == 0 and 2 or 1
            if row == corner then
              map[idx] = map[idx] + 2
            end
          end
        end
      end
    end
    -- corner + 1 <= row <= k - corner
    do
      local leftcol = map[1 + k * (corner - 1)]
      map[k * corner + 1] = leftcol
      for row = corner + 2, k - corner, 2 do
        leftcol = 3 - leftcol
        map[k * (row - 1) + 1] = leftcol
        map[k * (row) + 1] = leftcol
      end
      local rightcol = map[k * corner]
      for row = corner + 1, k - corner, 2 do
        rightcol = rightcol == 3 and 4 or 3
        map[k * (row)] = rightcol
        map[k * (row + 1)] = rightcol
      end
      local ofst = k * corner + corner
      map[ofst + 1] = 5
      map[ofst + 1 + k] = 5
      map[ofst + 2 + k * 2] = 5
      map[ofst + 3 + k * 2] = 5
    end
    --k - corner < row < k
    do
      local leftcol = map[(k - corner - 1) * k + 1]
      for row = k - corner + 1, k - 1 do
        leftcol = 3 - leftcol
        for col = 1, k do
          local idx = (row - 1) * k + col
          if col <= corner then
            if row - col == k - corner or row - col == k - corner - 1 then
              map[idx] = leftcol
            end
          elseif k - corner < col then
            if row + col == 2 * k - corner + 1 or row + col == 2 * k - corner then
              if (col - (k - corner)) % 2 == 0 then
                map[idx] = 2
              else
                map[idx] = 1
              end
              if col == k then map[idx] = map[idx] + 2 end
              if col == k - corner + 1 then map[idx] = map[idx] + 2 end
            end
          end
        end
      end
    end
    --k == row
    do
      map[(k - 1) * k + corner] = 3
      map[(k - 1) * k + corner + 1] = 3
      map[(k - 1) * k + corner + 2] = 4
      map[(k - 1) * k + corner + 3] = 4
      map[(k - 1) * k + corner + 4] = 3
    end
  end
  return map
end

local function solve(n)
  if n == 2 then
    print(-1)
    return
  end
  local mul = 1
  local k = n
  while k ~= 4 and k % 2 == 0 do
    mul = mul * 2
    k = mfl(k / 2)
  end
  local map = make_singlemap(k)
  for i = 1, n do
    for j = 1, n do
      local map_i = i % k
      if map_i == 0 then map_i = k end
      local map_j = j % k
      if map_j == 0 then map_j = k end
      local mappos = (map_i - 1) * k + map_j
      if map[mappos] == 0 then
        io.write(".")
      else
        io.write(string.char(96 + map[mappos]))
      end
    end
    io.write("\n")
  end
end

solve(n)