Problem:
Score : 700 points

Problem Statement

You are given an integer N. Determine if there exists a tree with 2N vertices numbered 1 to 2N satisfying the following condition, and show one such tree if the answer is yes.

Assume that, for each integer i between 1 and N (inclusive), Vertex i and N+i have the weight i. Then, for each integer i between 1 and N, the bitwise XOR of the weights of the vertices on the path between Vertex i and N+i (including themselves) is i.

Constraints

N is an integer.

1 \leq N \leq 10^{5}

Input

Input is given from Standard Input in the following format:

N

Output

If there exists a tree satisfying the condition in the statement, print Yes; otherwise, print No.
Then, if such a tree exists, print the 2N-1 edges of such a tree in the subsequent 2N-1 lines, in the following format:

a_{1} b_{1}
\vdots
a_{2N-1} b_{2N-1}

Here each pair (a_i, b_i) means that there is an edge connecting Vertex a_i and b_i. The edges may be printed in any order.

Sample Input 1

3

Sample Output 1

Yes
1 2
2 3
3 4
4 5
5 6

The sample output represents the following graph:

Sample Input 2

1

Sample Output 2

No

There is no tree satisfying the condition.

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

use std::collections::BTreeSet;

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());
    let n: usize = sc.read();

    match solve(n) {
        Some(edges) => {
            sc.write("Yes\n");
            for (a, b) in edges.into_iter() {
                sc.write(format!("{} {}\n", a, b));
            }
        }
        None => println!("No"),
    }
}

fn solve(n: usize) -> Option<Vec<(usize, usize)>> {
    let mut total_xor = 0;
    for i in 1..=n {
        total_xor ^= i;
    }

    if total_xor == 0 {
        let mut edges = vec![];
        for i in 2..=n {
            edges.push((i - 1, i));
            edges.push((i - 1 + n, i + n));
        }
        edges.push((n, n + 1));
        return Some(edges);
    }

    let mut pairs = BTreeSet::new();
    for i in 1..=n {
        let j = total_xor ^ i;
        let (i, j) = if i < j { (i, j) } else { (j, i) };
        if j <= n && i <= n {
            pairs.insert((i, j));
        }
    }

    if pairs.len() <= 1 {
        return None;
    }

    let mut iter = pairs.into_iter();
    let (a1, b1) = iter.next().unwrap();
    let (a2, b2) = iter.next().unwrap();

    let mut x1 = vec![];
    if a1 != 0 {
        x1.push(a1);
    }
    x1.push(b1);

    let mut x2 = vec![];
    if a2 != 0 {
        x2.push(a2);
    }
    x2.push(b2);

    let mut x3 = vec![];
    for i in 1..=n {
        if !x1.contains(&i) && !x2.contains(&i) {
            x3.push(i);
        }
    }

    let mut edges = vec![];
    for i in 1..x1.len() {
        edges.push((x1[i - 1], x1[i]));
        edges.push((x1[i - 1] + n, x1[i] + n));
    }
    for i in 1..x2.len() {
        edges.push((x2[i - 1], x2[i]));
        edges.push((x2[i - 1] + n, x2[i] + n));
    }
    for i in 1..x3.len() {
        edges.push((x3[i - 1], x3[i]));
        edges.push((x3[i - 1] + n, x3[i] + n));
    }
    edges.push((x1[x1.len() - 1], x2[0]));
    edges.push((x2[x2.len() - 1], x3[0]));
    edges.push((x3[x3.len() - 1], x2[0] + n));
    edges.push((x2[x2.len() - 1] + n, x3[0] + n));
    edges.push((x2[x2.len() - 1], x1[0] + n));
    Some(edges)
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> Self {
        Self(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.to_string().as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn 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()
    }
}