Problem:
Score : 800 points

Problem Statement

Takahashi is doing a research on sets of points in a plane.
Takahashi thinks a set S of points in a coordinate plane is a good set when S satisfies both of the following conditions:

The distance between any two points in S is not \sqrt{D_1}.

The distance between any two points in S is not \sqrt{D_2}.

Here, D_1 and D_2 are positive integer constants that Takahashi specified.

Let X be a set of points (i,j) on a coordinate plane where i and j are integers and satisfy 0 ≤ i,j < 2N.

Takahashi has proved that, for any choice of D_1 and D_2, there exists a way to choose N^2 points from X so that the chosen points form a good set.
However, he does not know the specific way to choose such points to form a good set.
Find a subset of X whose size is N^2 that forms a good set.

Constraints

1 ≤ N ≤ 300

1 ≤ D_1 ≤ 2×10^5

1 ≤ D_2 ≤ 2×10^5

All values in the input are integers.

Input

Input is given from Standard Input in the following format:

N D_1 D_2

Output

Print N^2 distinct points that satisfy the condition in the following format:

x_1 y_1
x_2 y_2
:
x_{N^2} y_{N^2}

Here, (x_i,y_i) represents the i-th chosen point.
0 ≤ x_i,y_i < 2N must hold, and they must be integers.
The chosen points may be printed in any order.
In case there are multiple possible solutions, you can output any.

Sample Input 1

2 1 2

Sample Output 1

0 0
0 2
2 0
2 2

Among these points, the distance between 2 points is either 2 or 2\sqrt{2}, thus it satisfies the condition.

Sample Input 2

3 1 5

Sample Output 2

0 0
0 2
0 4
1 1
1 3
1 5
2 0
2 2
2 4

Solution in Rust (1.15.1) (judge verdict: Accepted)

use std::cmp;

fn main() {
    let s = std::io::stdin();
    let mut sc = Scanner { reader: s.lock() };
    let n: usize = sc.read();
    let d1: i64 = sc.read();
    let d2: i64 = sc.read();

    let mut vertices = vec![];
    for i in 0..(2 * n) {
        for j in 0..(2 * n) {
            vertices.push((i, j));
        }
    }

    let num_v = vertices.len();
    assert_eq!(num_v, 4 * n * n);
    let graph1 = construct_graph(&vertices, n, d1);
    let mut color1 = vec![0; num_v];
    for i in 0..num_v {
        if color1[i] == 0 {
            color1[i] = 1;
            dfs(i, &graph1, &mut color1);
        }
    }

    let graph2 = construct_graph(&vertices, n, d2);
    let mut color2 = vec![0; num_v];
    for i in 0..num_v {
        if color2[i] == 0 {
            color2[i] = 1;
            dfs(i, &graph2, &mut color2);
        }
    }

    let mut component = vec![vec![vec![]; 2]; 2];
    for i in 0..num_v {
        component[color1[i] - 1][color2[i] - 1].push(vertices[i]);
    }

    for i in 0..2 {
        for j in 0..2 {
            if component[i][j].len() >= n * n {
                for &(x, y) in component[i][j].iter().take(n * n) {
                    println!("{} {}", x, y);
                }
                return;
            }
        }
    }
    unreachable!();
}

fn dfs(v: usize, graph: &Vec<Vec<usize>>, color: &mut Vec<usize>) {
    for &next in graph[v].iter() {
        if color[next] == 0 {
            color[next] = if color[v] == 1 { 2 } else { 1 };
            dfs(next, graph, color);
        }
        assert!(color[next] != color[v]);
    }
}

fn construct_graph(vertices: &Vec<(usize, usize)>, n: usize, distance: i64) -> Vec<Vec<usize>> {
    let num_v = vertices.len();
    assert_eq!(num_v, 4 * n * n);
    let mut graph = vec![vec![]; num_v];
    for from in 0..num_v {
        let (i, j) = vertices[from];
        let i = i as i64;
        let j = j as i64;
        let n = n as i64;

        for dx in 0..cmp::min(2 * n, distance + 1) {
            let dy2 = distance - dx * dx;
            if dy2 < 0 {
                continue;
            }
            let dy = (dy2 as f64).sqrt() as i64;
            if dy * dy + dx * dx != distance {
                continue;
            }

            for &(x, y) in vec![
                (i + dx, j + dy),
                (i + dx, j - dy),
                (i - dx, j + dy),
                (i - dx, j - dy),
            ]
            .iter()
            {
                if x >= 0 && y >= 0 && x < 2 * n && y < 2 * n {
                    let to = x * 2 * n + y;
                    graph[from].push(to as usize);
                }
            }
        }
    }
    graph
}
pub struct Scanner<R> {
    reader: R,
}

impl<R: std::io::Read> Scanner<R> {
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .reader
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n')
            .take_while(|&b| b != b' ' && b != b'\n')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn read_vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}