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 C# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Math;
using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;
using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;
using System.Runtime.CompilerServices;


static class P
{
    static void Main()
    {
        Random RNG = new Random();
        var nab = Console.ReadLine().Split().Select(int.Parse).ToList();
        while (true)
        {
            try
            {
                Solve(nab.ToList());
            }
            catch
            {
                Solve2(nab.ToList());
            }
        }
    }

    static void Solve(List<int> nab)
    {
        int[] magic = { 0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13, 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14 };
        var diffBit = nab[1] ^ nab[2];

        if (PopCount((uint)diffBit) % 2 == 0)
        {
            Console.WriteLine("NO");
            return;
        }
        Console.WriteLine("YES");

        int leastRev = PopCount((uint)diffBit) / 2;
        //00 → 01 → 11 → 10
        //最上位はこの法則に従うので、最上位はrev無意味(先に最上位つけちゃいましょうね
        int total = nab[0] % 2 == 0 ? 4 : 2;
        BaseIterator iterator = nab[0] % 2 == 0 ? (BaseIterator)new CodeIterator(total, false, new NothingIterator()) : (BaseIterator)new SingleIterator();
        total *= 2;
        for (int i = (nab[0] - 1) / 2 - 1; i >= 0; i--)
        {
            iterator = new CodeIterator(total, leastRev > 0, iterator);
            total *= 2;
            if (leastRev > 0) leastRev--;
        }

        List<int> res = new List<int>();
        res.Add(iterator.GetCode());
        for (int i = 1; i < (1 << nab[0]); i++)
        {
            iterator.Next();
            res.Add(iterator.GetCode());
        }

        var last = res.Last();
        //diffBitを並び替えてこれにした並び替え順において全てを並び替え、それとnab[1]のxorを取ったら終わり? っぽいね
        // table[i] := 現在の数列のi番目が行くべき場所(つまり、diffBit)
        int zeroIndex = -1;
        int oneIndex = -1;
        int[] SubstituteTable = new int[nab[0]];
        for (int i = 0; i < nab[0]; i++)
        {
            if (((diffBit >> i) & 1) == 1)
            {
                do
                {
                    oneIndex++;
                } while (((last >> oneIndex) & 1) != 1);
                SubstituteTable[oneIndex] = i;
            }
            else
            {
                do
                {
                    zeroIndex++;
                } while (((last >> zeroIndex) & 1) != 0);
                SubstituteTable[zeroIndex] = i;
            }
        }

        Console.WriteLine(string.Join(" ", res.Select(x => Substitution(nab[0], x, SubstituteTable) ^ nab[1])));
    }

    static void Solve2(List<int> nab)
    {
        int[] magic = { 0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13, 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14 };
        

        var diffBit = nab[1] ^ nab[2];

        if (PopCount((uint)diffBit) % 2 == 0)
        {
            Console.WriteLine("NO");
            return;
        }
        Console.WriteLine("YES");

        int leastRev = (PopCount((uint)diffBit) / 2) ^ (nab[0] % 2 == 0 ? 0 : 1);
        //00 → 01 → 11 → 10
        //最上位はこの法則に従うので、最上位はrev無意味(先に最上位つけちゃいましょうね
        int total = nab[0] % 2 == 0 ? 8 : 4;
        BaseIterator iterator = nab[0] % 2 == 0 ? (BaseIterator)new CodeIterator(total, false, new NothingIterator()) : (BaseIterator)new SingleIterator();
        total *= 2;
        for (int i = (nab[0] - 1) / 2 - 1; i >= 0; i--)
        {
            iterator = new CodeIterator(total, leastRev > 0, iterator);
            total *= 2;
            if (leastRev > 0) leastRev--;
        }

        List<int> res = new List<int>();
        res.Add(iterator.GetCode());
        for (int i = 1; i < (1 << nab[0]); i++)
        {
            iterator.Next();
            res.Add(iterator.GetCode());
        }

        var last = res.Last();
        //diffBitを並び替えてこれにした並び替え順において全てを並び替え、それとnab[1]のxorを取ったら終わり? っぽいね
        // table[i] := 現在の数列のi番目が行くべき場所(つまり、diffBit)
        int zeroIndex = -1;
        int oneIndex = -1;
        int[] SubstituteTable = new int[nab[0]];
        for (int i = 0; i < nab[0]; i++)
        {
            if (((diffBit >> i) & 1) == 1)
            {
                do
                {
                    oneIndex++;
                } while (((last >> oneIndex) & 1) != 1);
                SubstituteTable[oneIndex] = i;
            }
            else
            {
                do
                {
                    zeroIndex++;
                } while (((last >> zeroIndex) & 1) != 0);
                SubstituteTable[zeroIndex] = i;
            }
        }

        Console.WriteLine(string.Join(" ", res.Select(x => Substitution(nab[0], x, SubstituteTable) ^ nab[1])));
    }

    public static int Substitution(int n,int val,int[] table)
    {
        int res = 0;
        for (int i = 0; i < n; i++)
        {
            if (((val >> i) & 1) == 1) res |= (1 << table[i]);
        }
        return res;
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static byte PopCount(uint n)
    {
        unchecked
        {
            n = (n & 0x55555555u) + ((n >> 1) & 0x55555555u);
            n = (n & 0x33333333u) + ((n >> 2) & 0x33333333u);
            n = (n & 0x0f0f0f0fu) + ((n >> 4) & 0x0f0f0f0fu);
            n = (n & 0x00ff00ffu) + ((n >> 8) & 0x00ff00ffu);
            return (byte)((n & 0x0000ffffu) + ((n >> 16) & 0x0000ffffu));
        }
    }
}

abstract class BaseIterator
{
    public abstract void Next();
    public abstract int GetCode();
}

class CodeIterator : BaseIterator
{
    int Total = 0;
    int Count = 0;
    bool DoLastReverse = false;
    int Order = 1;
    int State = 0;//00 → 01 → 11 → 10
    BaseIterator NextIter;
    

    public CodeIterator(int total, bool doLastReverse, BaseIterator nextIter)
    {
        Total = total;
        DoLastReverse = doLastReverse;
        NextIter = nextIter;
    }
    public override void Next()
    {
        if (Count % 4 == 3) NextIter.Next();
        else if (Total - Count <= 4 && DoLastReverse)
        {
            switch (State)
            {
                case 0:
                    State = 2;
                    break;
                case 1:
                    State = 0;
                    break;
                case 2:
                    State = 3;
                    break;
                case 3:
                    State = 1;
                    break;
            }
        }
        else
        {
            switch (State)
            {
                case 0:
                    State = 1;
                    break;
                case 1:
                    State = 3;
                    break;
                case 2:
                    State = 0;
                    break;
                case 3:
                    State = 2;
                    break;
            }
        }
        Count++;
    }
    public override int GetCode()
    {
        return (NextIter.GetCode() << 2) + State;
    }
}

class SingleIterator : BaseIterator
{
    int state = 0;
    public override void Next()
    {
        state ^= 1;
    }
    public override int GetCode()
    {
        return state;
    }
}

class NothingIterator : BaseIterator
{
    public override void Next(){}
    public override int GetCode()
    {
        return 0;
    }
}