Problem:
Score : 800 points

Problem Statement

You are given integers N,\ A and B.
Determine if there exists a permutation (P_0,\ P_1,\ ...\ P_{2^N-1}) of (0,\ 1,\ ...\ 2^N-1) that satisfies all of the following conditions, and create one such permutation if it exists.

P_0=A

P_{2^N-1}=B

For all 0 \leq i < 2^N-1, the binary representations of P_i and P_{i+1} differ by exactly one bit.

Constraints

1 \leq N \leq 17

0 \leq A \leq 2^N-1

0 \leq B \leq 2^N-1

A \neq B

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

If there is no permutation that satisfies the conditions, print NO.

If there is such a permutation, print YES in the first line.
Then, print (P_0,\ P_1,\ ...\ P_{2^N-1}) in the second line, with spaces in between.
If there are multiple solutions, any of them is accepted.

Sample Input 1

2 1 3

Sample Output 1

YES
1 0 2 3

The binary representation of P=(1,0,2,3) is (01,00,10,11), where any two adjacent elements differ by exactly one bit.

Sample Input 2

3 2 1

Sample Output 2

NO

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

#[allow(unused_macros)]
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)*}
    };
}

#[allow(unused_macros)]
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)*}
    };
}

#[allow(unused_macros)]
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, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

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

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

use std::collections::BTreeMap;

fn rec(n: usize, b: usize, v: &mut Vec<usize>) {
    if b == 0 {
        let mut ok = true;
        for i in 0..n - 1 {
            let t = v[i] ^ v[i + 1];
            if t.count_ones() != 1 {
                ok = false;
                break;
            }
        }

        if ok {
            if (v[0] ^ v[v.len() - 1]).count_ones() != 1 {
                // dbg!(&v);
                println!(
                    "{}: {} {}",
                    (v[0] ^ v[v.len() - 1]).count_ones(),
                    v[0],
                    v.last().unwrap()
                );
            }
        }

        return;
    }

    for i in 0..n {
        if b & (1 << i) == 0 {
            continue;
        }

        if v.len() > 0 {
            if (v.last().unwrap() ^ i).count_ones() != 1 {
                continue;
            }
        }

        v.push(i);
        rec(n, b & !(1 << i), v);
        v.pop();
    }
}

fn main() {
    if false {
        let n = 32;
        let mut v = vec![];
        rec(n, (1 << n) - 1, &mut v);
        return;
    }

    input! {
        n: usize,
        a: usize,
        b: usize,
    }

    let diffbits = (a ^ b).count_ones();

    if diffbits % 2 == 0 {
        println!("NO");
        return;
    }

    // make gray code 0...0 to 1..10..0
    let mut v = vec![];
    v.push(0);
    v.push(1);
    let mut prev = 1_usize;
    for i in 1..n {
        let from = prev;
        let mut to = from;
        let inc = to.count_ones() < i as u32 && to.count_ones() < diffbits as u32;

        for j in 0..i {
            if inc {
                if to & (1 << j) == 0 {
                    to |= 1 << j;
                    break;
                }
            } else {
                if to & (1 << j) != 0 {
                    to &= !(1 << j);
                    break;
                }
            }
        }

        // dbg!((i, from, to, inc));

        let g = gen_gray(i, from, to);

        for x in g {
            v.push(x | (1 << i));
        }

        prev = to | (1 << i);
    }

    check(&v);

    // dbg!(&v);

    let bb = b ^ a;
    let mut perm = vec![];
    for i in 0..n {
        perm.push((bb & (1 << i), i));
    }

    perm.sort();
    // perm.reverse();
    // dbg!(&perm);

    let mut w = vec![];

    println!("YES");
    for k in 0..v.len() {
        if k > 0 {
            print!(" ");
        }
        let mut t = 0;
        let c = v[k];

        for i in 0..n {
            t |= ((c >> i) & 1) << perm[i].1;
        }
        t ^= a;

        w.push(t);

        print!("{}", t);
    }
    println!();

    check(&w);
}

fn check(v: &Vec<usize>) {
    for i in 1..v.len() {
        assert!((v[i - 1] ^ v[i]).count_ones() == 1);
    }
    // println!("OK");
}

fn gen_gray(n: usize, a: usize, b: usize) -> Vec<usize> {
    // dbg!((n, a, b));

    let mut gray = vec![0; 1 << n];
    let mut v = vec![];

    for i in 0..gray.len() {
        gray[i] = i ^ (i >> 1);
    }

    let mut mm = BTreeMap::<(usize, usize), usize>::new();

    for i in 0..gray.len() {
        mm.insert((gray[i], gray[(i + 1) % gray.len()]), i);
    }

    let mut pos = None;

    for i in 0..n {
        for j in i..n {
            let aa = bswap(a, i, j);
            let bb = bswap(b, i, j);

            if let Some(p) = mm.get(&(aa, bb)) {
                pos = Some((false, p, i, j));
            } else if let Some(p) = mm.get(&(bb, aa)) {
                pos = Some((true, p, i, j));
            }
        }
    }

    let (dir, pos, i, j) = pos.unwrap();

    for k in 0..gray.len() {
        v.push(if !dir {
            bswap(gray[(gray.len() + pos - k) % gray.len()], i, j)
        } else {
            bswap(gray[(pos + 1 + k) % gray.len()], i, j)
        });
    }

    v
}

fn bswap(a: usize, i: usize, j: usize) -> usize {
    (a & !(1 << i) & !(1 << j)) | ((a >> i) & 1) << j | ((a >> j) & 1) << i
}