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 Rust (1.15.1) (judge verdict: Accepted)

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String {
            bytes
                .by_ref()
                .map(|r| r.unwrap() as char)
                .skip_while(|c| c.is_whitespace())
                .take_while(|c| !c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_imports)]
use std::cmp::{min, max};

fn main() {
    input!{
        n: usize
    }
    if n == 2 {
        println!("-1");
        return;
    }
    let mut ans = vec![vec!['.'; n]; n];
    let b3 = vec![vec!['a', 'a', '.'],
                  vec!['.', '.', 'a'],
                  vec!['.', '.', 'a']];
    let b4 = vec![vec!['a', 'a', 'b', 'c'],
                  vec!['d', 'd', 'b', 'c'],
                  vec!['e', 'f', 'g', 'g'],
                  vec!['e', 'f', 'h', 'h']];
    let b5 = vec![vec!['a', 'b', 'b', 'c', 'c'],
                  vec!['a', 'd', 'd', '.', 'e'],
                  vec!['f', '.', '.', 'g', 'e'],
                  vec!['f', '.', '.', 'g', 'h'],
                  vec!['i', 'i', 'j', 'j', 'h']];
    let b7 = vec![vec!['.', 'a', 'a', 'b', 'c', 'c', '.'],
                  vec!['d', '.', '.', 'b', '.', '.', 'g'],
                  vec!['d', '.', 'm', 'm', '.', '.', 'g'],
                  vec!['e', 'e', '.', '.', 'n', 'h', 'h'],
                  vec!['f', '.', '.', '.', 'n', '.', 'i'],
                  vec!['f', '.', '.', 'k', '.', '.', 'i'],
                  vec!['.', 'j', 'j', 'k', 'l', 'l', '.']];
    if n == 7 {
        copy(0, &b7, &mut ans);
    } else if n == 11 {
        copy(0, &b4, &mut ans);
        copy(4, &b7, &mut ans);
    } else if n % 3 == 0 {
        for i in 0..n / 3 {
            copy(3 * i, &b3, &mut ans);
        }
    } else {
        let (mut a, mut b) = ex_euc(4, 5, n as i64).unwrap();
        if a < 0 {
            let d = (a.abs() + 4) / 5;
            a += d * 5;
            b -= d * 4;
        }
        if b < 0 {
            let d = (b.abs() + 3) / 4;
            a -= d * 5;
            b += d * 4;
        }
        let a = a as usize;
        let b = b as usize;
        for i in 0..a {
            copy(4 * i, &b4, &mut ans);
        }
        for i in 0..b {
            copy(4 * a + 5 * i, &b5, &mut ans);
        }
    }
    for s in &ans {
        println!("{}", s.iter().cloned().collect::<String>());
    }
}

fn copy(x: usize, a: &Vec<Vec<char>>, b: &mut Vec<Vec<char>>) {
    let l = a.len();
    for i in 0..l {
        for j in 0..l {
            b[x + i][x + j] = a[i][j];
        }
    }
}

fn gcd(n: usize, m: usize) -> usize {
    let mut l = min(n, m);
    let mut r = max(n, m);
    while l > 0 {
        let t = l;
        l = r % l;
        r = t;
    }
    r
}

fn ex_euc(a: i64, b: i64, c: i64) -> Option<(i64, i64)> {
    if a == 0 && b == 0 {
        return if c == 0 { Some((0,0)) } else { None };
    }
    let d = gcd(a.abs() as usize, b.abs() as usize) as i64;
    if c % d != 0 {
        return None;
    }
    Some(ex_euc_rec(a, b, c))
}

fn ex_euc_rec(a: i64, b: i64, c: i64) -> (i64, i64) {
    if b == 0 {
        return (c / a, 0);
    }
    let (x, y) = ex_euc_rec(b, a % b, c);
    (y, x - a / b * y)
}