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_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

fn num_to_bits(mut num: u64, n: usize) -> Vec<i32> {
    let mut res = vec![0; n];
    for i in 0..n {
        res[i] = (num % 2) as i32;
        num /= 2;
    }
    res
}

fn bits_to_num(bits: &Vec<i32>) -> u64 {
    let mut res = 0;
    for i in 0..bits.len() {
        res += bits[i] as u64 * (1 << i);
    }
    res
}

fn invert_bit(i: usize, bits: &mut Vec<i32>) {
    if bits[i] == 1 {
        bits[i] = 0;
    } else {
        bits[i] = 1;
    }
}

fn route(start_bits: &Vec<i32>, end_bits: &Vec<i32>, bit_mask: &Vec<usize>) -> Vec<usize> {
    if bit_mask.len() == 2 {
        if start_bits[bit_mask[0]] != end_bits[bit_mask[0]] {
            assert!(start_bits[bit_mask[1]] == end_bits[bit_mask[1]]);
            return vec![bit_mask[1], bit_mask[0], bit_mask[1]];
        } else {
            assert!(start_bits[bit_mask[1]] != end_bits[bit_mask[1]]);
            return vec![bit_mask[0], bit_mask[1], bit_mask[0]];
        }
    }
    let mut new_bit_mask = Vec::new();
    let mut first_different_bit = 0usize;
    for &bit in bit_mask.iter() {
        if start_bits[bit] != end_bits[bit] {
            first_different_bit = bit;
            break;
        }
    }
    for &bit in bit_mask.iter() {
        if bit != first_different_bit {
            new_bit_mask.push(bit);
        }
    }

    let mut intermediate_bits = start_bits.clone();
    invert_bit(new_bit_mask[0], &mut intermediate_bits);
    let mut ret1 = route(&start_bits, &intermediate_bits, &new_bit_mask);

    invert_bit(first_different_bit, &mut intermediate_bits);
    ret1.push(first_different_bit);
    let ret2 = route(&intermediate_bits, &end_bits, &new_bit_mask);

    [ret1, ret2].concat()
}

fn main() {
    let v: Vec<u64> = read_vec();
    let (n, a, b) = (v[0], v[1], v[2]);
    let n = n as usize;

    let a_bits = num_to_bits(a, n);
    let b_bits = num_to_bits(b, n);

    if (a_bits.iter().sum::<i32>() - b_bits.iter().sum::<i32>()) % 2 == 0 {
        println!("NO");
        return;
    }
    println!("YES");
    if n == 1 {
        if a == 0 {
            println!("0 1");
        } else {
            println!("1 0");
        }
        return;
    }
    let bit_mask = (0..n).collect::<Vec<usize>>();
    let route_bits = route(&a_bits, &b_bits, &bit_mask);

    let mut answers = Vec::with_capacity(1 << n);
    let mut start_bits = a_bits;
    answers.push(bits_to_num(&start_bits));
    for &bit in route_bits.iter() {
        invert_bit(bit, &mut start_bits);
        answers.push(bits_to_num(&start_bits));
    }
    println!(
        "{}",
        answers
            .iter()
            .map(|&x| x.to_string())
            .collect::<Vec<_>>()
            .join(" ")
    );
}