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: Wrong Answer)

#[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 binary_to_num(binary: &Vec<i32>) -> u64 {
    let mut ret = 0u64;
    let mut coef = 1;
    for i in 0..17 {
        ret += binary[i] as u64 * coef;
        coef *= 2;
    }
    ret
}

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

fn calc_toggle_index(n: usize, i: usize) -> usize {
    if i % 2 == 1 {
        return 0;
    }

    for j in (0..n - 1).rev() {
        let candidate_const = 1 << j;
        let candidate_coef = 1 << (j + 1);
        if i >= candidate_const && (i - candidate_const) % candidate_coef == 0 {
            return j;
        }
    }
    assert!(false);
    0
}

fn main() {
    let v: Vec<u64> = read_vec();
    let (n, a, b) = (v[0], v[1], v[2]);
    let mut a_binary = vec![0; 17];
    let mut b_binary = vec![0; 17];

    let mut a_copy = a;
    for i in 0..17 {
        if a_copy % 2 == 1 {
            a_binary[i] = 1;
        }
        a_copy /= 2;
    }

    let mut b_copy = b;
    for i in 0..17 {
        if b_copy % 2 == 1 {
            b_binary[i] = 1;
        }
        b_copy /= 2;
    }

    let mut different_digit_idx = Vec::new();
    let mut different_digit_count = 0;

    for i in 0..17 {
        if a_binary[i] != b_binary[i] {
            different_digit_count += 1;
            different_digit_idx.push(i);
        }
    }
    if different_digit_count % 2 != 1 {
        println!("NO");
        return;
    }
    println!("YES");

    let mut etc_indexes: Vec<usize> = Vec::new();
    for i in 1..different_digit_idx.len() {
        etc_indexes.push(different_digit_idx[i]);
    }
    let different_digit_dixt = different_digit_idx.iter().cloned().collect::<HashSet<_>>();
    for i in 0..n as usize {
        if !different_digit_dixt.contains(&i) {
            etc_indexes.push(i);
        }
    }

    let mut answers = Vec::new();
    answers.push(binary_to_num(&a_binary).to_string());
    for i in 1..(1 << n) {
        let mut toggle_index;
        if i == 1 << (n - 1) {
            toggle_index = different_digit_idx[0];
            for j in 0..(different_digit_idx.len() - 1) / 2 {
                let mut temp = etc_indexes[2 * j + 1];
                let temp2 = etc_indexes[2 * j + 2];
                etc_indexes[2 * j] = temp2;
                etc_indexes[2 * j + 1] = temp;
            }
        } else {
            toggle_index = etc_indexes[calc_toggle_index(n as usize, i)];
        }
        toggle_binary(toggle_index, &mut a_binary);
        answers.push(binary_to_num(&a_binary).to_string());
    }
    println!("{}", answers.join(" "));
}