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.15.1) (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();
    let mut xor = 0;
    for i in 1..(n + 1) {
        xor ^= i;
    }

    if xor == 0 {
        println!("Yes");
        for i in 0..(2 * n - 1) {
            println!("{} {}", i + 1, i + 2);
        }
    } else if xor == 1 {
        if n == 1 {
            println!("No");
            return;
        }
        println!("Yes");
        for i in 0..(n - 2) {
            println!("{} {}", i + 2, i + 3);
        }
        for i in 0..(n - 2) {
            println!("{} {}", i + 2 + n, i + 3 + n);
        }
        println!("{} {}", n, n + 2);
        println!("1 2");
        println!("3 {}", n + 1);
    } else if xor == n {
        if n.count_ones() == 1 {
            println!("No");
            return;
        }
        let mut set = BTreeSet::new();
        for i in 0..30 {
            let x = 1 << i;
            if x & n != 0 {
                set.insert(x);
            }
        }

        let mut other = vec![];
        for i in 1..n {
            if set.contains(&i) {
                continue;
            }
            other.push(i);
        }
        let set = set.into_iter().collect::<Vec<_>>();

        println!("Yes");
        for i in 1..set.len() {
            println!("{} {}", set[i - 1], set[i]);
            println!("{} {}", set[i - 1] + n, set[i] + n);
        }
        for i in 1..other.len() {
            println!("{} {}", other[i - 1], other[i]);
            println!("{} {}", other[i - 1] + n, other[i] + n);
        }
        println!("{} {}", set[set.len() - 1], other[0]);
        println!("{} {}", set[0] + n, other[other.len() - 1]);
        println!("{} {}", set[set.len() - 1] + n, other[0] + n);
        println!("{} {}", n, set[0]);
        println!("{} {}", 2 * n, set[set.len() - 1]);
    } else if xor == n + 1 {
        if n == 2 {
            println!("No");
            return;
        }
        let mut set = BTreeSet::new();
        for i in 1..30 {
            let x = 1 << i;
            if x & n != 0 {
                set.insert(x);
            }
        }

        let mut other = vec![];
        for i in 2..n {
            if set.contains(&i) {
                continue;
            }
            other.push(i);
        }
        let mut set = set.into_iter().collect::<Vec<_>>();
        set.sort();
        set.reverse();
        other.sort();

        println!("Yes");
        for i in 1..set.len() {
            println!("{} {}", set[i - 1], set[i]);
            println!("{} {}", set[i - 1] + n, set[i] + n);
        }
        for i in 1..other.len() {
            println!("{} {}", other[i - 1], other[i]);
            println!("{} {}", other[i - 1] + n, other[i] + n);
        }

        println!("{} {}", set[set.len() - 1], other[0]);
        println!("{} {}", set[0] + n, other[other.len() - 1]);
        println!("{} {}", set[set.len() - 1] + n, other[0] + n);
        println!("{} {}", n, set[0]);
        println!("{} {}", 2 * n, set[set.len() - 1]);
        println!("1 2");
        println!("3 {}", n + 1);
    } else {
        unreachable!()
    }
}

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 {
        IO(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()
    }
}