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)

// https://atcoder.jp/contests/agc031/tasks/agc031_c
//
#![allow(unused_imports)]
use std::io::*;
use std::io::Write;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

// Input macros.
// Original by tanakh: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };

    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, switch) => {
        {
            let ty = read_value!($iter, i32);
            if ty == 1 {
                vec![ty, read_value!($iter, i32), read_value!($iter, i32)]
            } else if ty == 2 {
                vec![ty, read_value!($iter, i32)]
            } else {
                vec![ty, read_value!($iter, i32)]
            }
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

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

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

#[allow(unused_macros)]
macro_rules! read_line {
    ($t:tt) => {
        {
            let mut s = String::new();
            std::io::stdin().read_line(&mut s).unwrap();
            s.trim_right().parse::<$t>().unwrap()
        }
    }
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

// ===

fn chain_same(list: Vec<usize>, a: usize, b: usize, nth: usize) -> Vec<usize> {
    let mut new_list = vec![];
    let n = list.len();
    assert!(n % 2 == 0);

    let mut upper = ifv!(bit(a, nth), 1, 0)<<nth;
    let mut lower = upper ^ (1<<nth);

    for i in 0..n/2 {
        let a = list[i*2];
        let b = list[i*2+1];
        new_list.push(upper|a);
        new_list.push(lower|a);
        new_list.push(lower|b);
        new_list.push(upper|b);
    }
    new_list
}

fn chain_diff(list: Vec<usize>, a: usize, b: usize, uth: usize, vth: usize) -> Vec<usize> {
    let mut new_list = vec![];
    let mut n = list.len();
    let mut upper0 = ifv!(bit(a, uth), 1, 0)<<uth;
    let mut upper1 = ifv!(bit(a, vth), 1, 0)<<vth;
    let mut lower0 = upper0 ^ (1<<uth);
    let mut lower1 = upper1 ^ (1<<vth);

    for i in 0..n {
        new_list.push(list[i] | upper0 | upper1);
    }
    for i in (0..n).rev() {
        new_list.push(list[i] | upper0 | lower1);
    }
    let mut later_half = chain_same(list, a ^ (1<<uth) ^ (1<<vth), b, vth);
    for i in 0..2*n {
        later_half[i] |= lower0;
    }
    new_list.append(&mut later_half);
    new_list
}

fn bit(a: usize, nth: usize) -> bool {
    (a >> nth) & 1 == 1
}

fn solve(n: usize, a: usize, b: usize) -> Option<Vec<usize>> {
    let mut same_bit = vec![];
    let mut diff_bit = vec![];
    for i in 0..n {
        if (a >> i) & 1 == (b >> i) & 1 {
            same_bit.push(i);
        } else {
            diff_bit.push(i);
        }
    }
    assert!(diff_bit.len() >= 1);

    if diff_bit.len() % 2 == 0 {
        return None;
    }
    let d0 = diff_bit[0];
    let mut list = ifv!(bit(a, diff_bit[0]), vec![1<<d0, 0], vec![0, 1<<d0]);
    diff_bit.remove(0);
    let dn = diff_bit.len();
    for i in 0..dn/2 {
        list = chain_diff(list, a, b, diff_bit[i*2], diff_bit[i*2+1]);
    }
    for sth in same_bit {
        list = chain_same(list, a, b, sth);
    }
    Some(list)
}

fn assert_list(m: usize, a: usize, b: usize, list: &Vec<usize>) {
    // debug!(list);
    let n = list.len();
    let mut cnt = vec![0; 1<<m];
    for i in 0..n-1 {
        let diff = list[i] ^ list[i+1];
        assert_eq!((diff as u64).count_ones(), 1);
    }
    for i in 0..n {
        cnt[list[i]] += 1;
    }
    for i in 0..(1<<m) {
        assert_eq!(cnt[i], 1);
    }
    cnt[0] == a;
    cnt[(1<<m)-1] == b;
}

fn main() {
    input! {
        n: usize, a: usize, b: usize
    };

    if let Some(list) = solve(n, a, b) {
        assert_list(n, a, b, &list);
        println!("YES");
        println!("{}", join!(list, " "));
    } else {
        println!("NO");
    }
}