'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)

    if n == 1:
        if L == 1:
            qc.x(0)
            qc.z(0)
            qc.x(0)
        if L == 2:
            qc.x(0)
            qc.z(0)
            qc.x(0)
            qc.z(0)
        return qc
    qc.barrier()

    circuitlist = ["O" for i in range(n)]

    for l_little in range(L):
        binary_L_minus_1 = format(l_little, '0' + str(n) + 'b')[::-1]
        for qubit in range(n):
            if binary_L_minus_1[qubit] == '0':
                if circuitlist[qubit][-1] == 'X':
                    circuitlist[qubit] = circuitlist[qubit][:len(circuitlist[qubit])-1]

                else:
                    circuitlist[qubit] = circuitlist[qubit] + 'X'
                #qc.x(qubit)

        #qc.h(n-1)
        if circuitlist[n-1][-1] == 'H':
            circuitlist[n-1] = circuitlist[n-1][:len(circuitlist[n-1])-1]
        else:
            circuitlist[n-1] = circuitlist[n-1] + 'H'

        for i in range(n):
            for s in range(len(circuitlist[i])):
                if circuitlist[i][s] == 'H':
                    qc.h(i)
                elif circuitlist[i][s] == 'X':
                    qc.x(i)
        circuitlist = ["O" for i in range(n)]

        qc.mcx(list(range(n-1)),n-1)
        #qc.h(n-1)
        if circuitlist[n-1][-1] == 'H':
            circuitlist[n-1] = circuitlist[n-1][:len(circuitlist[n-1])-1]
        else:
            circuitlist[n-1] = circuitlist[n-1] + 'H'

        for qubit in range(n):
            if binary_L_minus_1[qubit] == '0':
                #qc.x(qubit)
                if circuitlist[qubit][-1] == 'X':
                    circuitlist[qubit] = circuitlist[qubit][:len(circuitlist[qubit])-1]
                else:
                    circuitlist[qubit] = circuitlist[qubit] + 'X'
        #qc.barrier()
    for i in range(n):
        for s in range(len(circuitlist[i])):
            if circuitlist[i][s] == 'H':
                qc.h(i)
            elif circuitlist[i][s] == 'X':
                qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)

    if n == 1:
        if L == 1:
            qc.x(0)
            qc.z(0)
            qc.x(0)
        if L == 2:
            qc.x(0)
            qc.z(0)
            qc.x(0)
            qc.z(0)
        return qc
    #qc.barrier()

    circuitlist = ["O" for i in range(n)]

    for l_little in range(L):
        binary_L_minus_1 = format(l_little, '0' + str(n) + 'b')[::-1]
        for qubit in range(n):
            if binary_L_minus_1[qubit] == '0':
                if circuitlist[qubit][-1] == 'X':
                    circuitlist[qubit] = circuitlist[qubit][:len(circuitlist[qubit])-1]

                else:
                    circuitlist[qubit] = circuitlist[qubit] + 'X'
                #qc.x(qubit)

        #qc.h(n-1)
        if circuitlist[n-1][-1] == 'H':
            circuitlist[n-1] = circuitlist[n-1][:len(circuitlist[n-1])-1]
        else:
            circuitlist[n-1] = circuitlist[n-1] + 'H'

        for i in range(n):
            for s in range(len(circuitlist[i])):
                if circuitlist[i][s] == 'H':
                    qc.h(i)
                elif circuitlist[i][s] == 'X':
                    qc.x(i)
        circuitlist = ["O" for i in range(n)]

        qc.mcx(list(range(n-1)),n-1)
        #qc.h(n-1)
        if circuitlist[n-1][-1] == 'H':
            circuitlist[n-1] = circuitlist[n-1][:len(circuitlist[n-1])-1]
        else:
            circuitlist[n-1] = circuitlist[n-1] + 'H'

        for qubit in range(n):
            if binary_L_minus_1[qubit] == '0':
                #qc.x(qubit)
                if circuitlist[qubit][-1] == 'X':
                    circuitlist[qubit] = circuitlist[qubit][:len(circuitlist[qubit])-1]
                else:
                    circuitlist[qubit] = circuitlist[qubit] + 'X'
        #qc.barrier()
    for i in range(n):
        for s in range(len(circuitlist[i])):
            if circuitlist[i][s] == 'H':
                qc.h(i)
            elif circuitlist[i][s] == 'X':
                qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import qiskit.circuit.library as qlib
import numpy as np

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    for i in range(n - 1, -1, -1):
        for j in range(n - 1, i, -1):
            qc.cp(np.pi / (2 ** (j - i)), j, i)

        qc.h(i)  # Hadamardゲートも逆順で適

    for i in range(n // 2):
        qc.cx(i, n - i - 1)
        qc.cx(i, n - i - 1)
        qc.cx(i, n - i - 1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i in range(L):
        bin_data = format(i, f'0{n}b')
        data_0_bits = [idx for idx, digit in enumerate(reversed(bin_data)) if digit == '0']

        if len(data_0_bits) > 0:
            qc.x(data_0_bits)

        if n>1:
            qc.append(ZGate().control(n-1), range(n))
        else:
            qc.z(0)

        if len(data_0_bits) > 0:
            qc.x(data_0_bits)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i in range(L):
        bin_data = format(i, f'0{n}b')
        data_0_bits = [idx for idx, digit in enumerate(reversed(bin_data)) if digit == '0']

        if len(data_0_bits) > 0:
            qc.x(data_0_bits)

        if n>1:
            qc.append(ZGate().control(n-1), range(n))
        else:
            qc.z(0)

        if len(data_0_bits) > 0:
            qc.x(data_0_bits)

    qc = transpile(qc, optimization_level=3)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.compiler import transpile

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i in range(L):
        bin_data = format(i, f'0{n}b')
        data_0_bits = [idx for idx, digit in enumerate(reversed(bin_data)) if digit == '0']

        if len(data_0_bits) > 0:
            qc.x(data_0_bits)

        if n>1:
            qc.append(ZGate().control(n-1), range(n))
        else:
            qc.z(0)

        if len(data_0_bits) > 0:
            qc.x(data_0_bits)

    qc = transpile(qc, optimization_level=3)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    bin_data = format(L, f'0{n}b')
    set_bits = [idx for idx, digit in enumerate(reversed(bin_data)) if digit == '1']

    for i in set_bits:

        if i>0:
            qc.h(range(i-1))

        qc.x(i)

        if n>1:
            qc.append(ZGate().control(n-1), range(n))
        else:
            qc.z(0)

        qc.x(i)

        if i>0:
            qc.h(range(i-1))

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n == 1:
        if L == 1:
            qc.x(0)
            qc.z(0)
            qc.x(0)
        else:
            qc.z(0)
            qc.x(0)
            qc.z(0)
            qc.x(0)
        return qc

    MX = L-1
    bit_position = []
    for i in range(n):
        if (MX>>i)&1:
            bit_position.append(i)
    bit_position.reverse()
    current_state = "0" *(n-bit_position[0]-1)
    for i in range(len(bit_position)):
        pos = bit_position[i]
        if n-pos-1 == 0:
            qc.x(pos)
            qc.z(pos)
            qc.x(pos)
        else:
            custom = ZGate().control(n-pos-1,ctrl_state=current_state)
            qc.x(pos)
            qc.append(custom,list(range(n-1,pos-1,-1)))
            qc.x(pos)
        if i != len(bit_position)-1:
            current_state += "1"
            current_state += "0" * (bit_position[i] - bit_position[i+1]-1)

    while len(current_state) < n-1:
        current_state += "0"
    print(current_state)
    print(list(range(n-1,pos,-1)) + list(range(pos)))
    custom = ZGate().control(n-1,ctrl_state=current_state)
    pos = bit_position[-1]
    qc.append(custom,list(range(n-1,pos,-1)) + list(range(pos+1)))
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if L == 1:
        qc.x(0)
        qc.z(0)
        qc.x(0)
        return qc
    if n == 1:
        qc.z(0)
        qc.x(0)
        qc.z(0)
        qc.x(0)
        return qc

    MX = L-1
    bit_position = []
    for i in range(n):
        if (MX>>i)&1:
            bit_position.append(i)
    bit_position.reverse()
    current_state = "0" *(n-bit_position[0]-1)
    for i in range(len(bit_position)):
        pos = bit_position[i]
        if n-pos-1 == 0:
            qc.x(pos)
            qc.z(pos)
            qc.x(pos)
        else:
            custom = ZGate().control(n-pos-1,ctrl_state=current_state)
            qc.x(pos)
            qc.append(custom,list(range(n-1,pos-1,-1)))
            qc.x(pos)
        if i != len(bit_position)-1:
            current_state += "1"
            current_state += "0" * (bit_position[i] - bit_position[i+1]-1)

    while len(current_state) < n-1:
        current_state += "0"
    custom = ZGate().control(n-1,ctrl_state=current_state)
    pos = bit_position[-1]
    qc.append(custom,list(range(n-1,pos,-1)) + list(range(pos+1)))
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if L == 1:
        qc.x(0)
        qc.z(0)
        qc.x(0)
        return qc
    if n == 1:
        qc.z(0)
        qc.x(0)
        qc.z(0)
        qc.x(0)
        return qc

    MX = L-1
    bit_position = []
    for i in range(n):
        if (MX>>i)&1:
            bit_position.append(i)
    bit_position.reverse()
    current_state = "0" *(n-bit_position[0]-1)
    for i in range(len(bit_position)):
        pos = bit_position[i]
        if n-pos-1 == 0:
            qc.x(pos)
            qc.z(pos)
            qc.x(pos)
        else:
            custom = ZGate().control(n-pos-1,ctrl_state=current_state)
            qc.x(pos)
            qc.append(custom,list(range(n-1,pos-1,-1)))
            qc.x(pos)
        if i != len(bit_position)-1:
            current_state += "1"
            current_state += "0" * (bit_position[i] - bit_position[i+1]-1)

    while len(current_state) < n-1:
        current_state += "0"
    custom = ZGate().control(n-1,ctrl_state=current_state)
    pos = bit_position[-1]
    qc.append(custom,list(range(n-1,pos,-1)) + list(range(pos+1)))
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if L == 1:
        qc.x(0)
        qc.z(0)
        qc.x(0)
        return qc
    if n == 1:
        qc.z(0)
        qc.x(0)
        qc.z(0)
        qc.x(0)
        return qc

    MX = L-1
    bit_position = []
    for i in range(n):
        if (MX>>i)&1:
            bit_position.append(i)
    bit_position.reverse()
    current_state = "0" *(n-bit_position[0]-1)
    # print(current_state)
    for i in range(len(bit_position)):
        pos = bit_position[i]
        if n-pos-1 == 0:
            qc.x(pos)
            qc.z(pos)
            qc.x(pos)
        else:
            custom = ZGate().control(n-pos-1,ctrl_state=current_state)
            qc.x(pos)
            # print(list(range(n-1,pos-1,-1)))
            qc.append(custom,list(range(n-1,pos-1,-1)))
            qc.x(pos)
        if i==1:
            break
        if i != len(bit_position)-1:
            current_state = "1" + current_state
            current_state = "0" * (bit_position[i] - bit_position[i+1]-1) + current_state
            print(current_state)
    while len(current_state) < n-1:
        current_state = "0" + current_state
    # print(current_state)
    # print(list(range(n-1,pos,-1)) + list(range(pos)))
    custom = ZGate().control(n-1,ctrl_state=current_state)
    pos = bit_position[-1]
    qc.append(custom,list(range(n-1,pos,-1)) + list(range(pos+1)))
    # print(qc.depth())
    # print(qc)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if L == 1:
        qc.x(0)
        qc.z(0)
        qc.x(0)
        return qc
    if n == 1:
        qc.z(0)
        qc.x(0)
        qc.z(0)
        qc.x(0)
        return qc

    MX = L-1
    bit_position = []
    for i in range(n):
        if (MX>>i)&1:
            bit_position.append(i)
    bit_position.reverse()
    current_state = "0" *(n-bit_position[0]-1)
    # print(current_state)
    for i in range(len(bit_position)):
        pos = bit_position[i]
        if n-pos-1 == 0:
            qc.x(pos)
            qc.z(pos)
            qc.x(pos)
        else:
            custom = ZGate().control(n-pos-1,ctrl_state=current_state)
            qc.x(pos)
            # print(list(range(n-1,pos-1,-1)))
            qc.append(custom,list(range(n-1,pos-1,-1)))
            qc.x(pos)
        if i != len(bit_position)-1:
            current_state = "1" + current_state
            current_state = "0" * (bit_position[i] - bit_position[i+1]-1) + current_state
            print(current_state)
    while len(current_state) < n-1:
        current_state = "0" + current_state
    # print(current_state)
    # print(list(range(n-1,pos,-1)) + list(range(pos)))
    custom = ZGate().control(n-1,ctrl_state=current_state)
    pos = bit_position[-1]
    qc.append(custom,list(range(n-1,pos,-1)) + list(range(pos+1)))
    # print(qc.depth())
    # print(qc)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if L == 1:
        qc.x(0)
        custom = ZGate().control(n-1,"0"*(n-1))
        qc.append(custom,list(range(n-1,-1,-1)))
        qc.x(0)
        return qc
    if n == 1:
        qc.z(0)
        qc.x(0)
        qc.z(0)
        qc.x(0)
        return qc

    MX = L-1
    bit_position = []
    for i in range(n):
        if (MX>>i)&1:
            bit_position.append(i)
    bit_position.reverse()
    current_state = "0" *(n-bit_position[0]-1)
    # print(current_state)
    for i in range(len(bit_position)):
        pos = bit_position[i]
        if n-pos-1 == 0:
            qc.x(pos)
            qc.z(pos)
            qc.x(pos)
        else:
            custom = ZGate().control(n-pos-1,ctrl_state=current_state)
            qc.x(pos)
            # print(list(range(n-1,pos-1,-1)))
            qc.append(custom,list(range(n-1,pos-1,-1)))
            qc.x(pos)
        if i != len(bit_position)-1:
            current_state = "1" + current_state
            current_state = "0" * (bit_position[i] - bit_position[i+1]-1) + current_state
            print(current_state)
    while len(current_state) < n-1:
        current_state = "0" + current_state
    # print(current_state)
    # print(list(range(n-1,pos,-1)) + list(range(pos)))
    custom = ZGate().control(n-1,ctrl_state=current_state)
    pos = bit_position[-1]
    qc.append(custom,list(range(n-1,pos,-1)) + list(range(pos+1)))
    # print(qc.depth())
    # print(qc)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if L == 1:
        if n==1:
            qc.x(0)
            qc.z(0)
            qc.x(0)
        else:
            qc.x(0)
            custom = ZGate().control(n-1,"0"*(n-1))
            qc.append(custom,list(range(n-1,-1,-1)))
            qc.x(0)
        return qc
    if n == 1:
        qc.z(0)
        qc.x(0)
        qc.z(0)
        qc.x(0)
        return qc

    MX = L-1
    bit_position = []
    for i in range(n):
        if (MX>>i)&1:
            bit_position.append(i)
    bit_position.reverse()
    current_state = "0" *(n-bit_position[0]-1)
    # print(current_state)
    for i in range(len(bit_position)):
        pos = bit_position[i]
        if n-pos-1 == 0:
            qc.x(pos)
            qc.z(pos)
            qc.x(pos)
        else:
            custom = ZGate().control(n-pos-1,ctrl_state=current_state)
            qc.x(pos)
            # print(list(range(n-1,pos-1,-1)))
            qc.append(custom,list(range(n-1,pos-1,-1)))
            qc.x(pos)
        if i != len(bit_position)-1:
            current_state = "1" + current_state
            current_state = "0" * (bit_position[i] - bit_position[i+1]-1) + current_state
            print(current_state)
    while len(current_state) < n-1:
        current_state = "0" + current_state
    # print(current_state)
    # print(list(range(n-1,pos,-1)) + list(range(pos)))
    custom = ZGate().control(n-1,ctrl_state=current_state)
    pos = bit_position[-1]
    qc.append(custom,list(range(n-1,pos,-1)) + list(range(pos+1)))
    # print(qc.depth())
    # print(qc)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if L == 1:
        if n==1:
            qc.x(0)
            qc.z(0)
            qc.x(0)
        else:
            qc.x(0)
            custom = ZGate().control(n-1,ctrl_state="0"*(n-1))
            qc.append(custom,list(range(n-1,-1,-1)))
            qc.x(0)
        return qc
    if n == 1:
        qc.z(0)
        qc.x(0)
        qc.z(0)
        qc.x(0)
        return qc

    MX = L-1
    bit_position = []
    for i in range(n):
        if (MX>>i)&1:
            bit_position.append(i)
    bit_position.reverse()
    current_state = "0" *(n-bit_position[0]-1)
    # print(current_state)
    for i in range(len(bit_position)):
        pos = bit_position[i]
        if n-pos-1 == 0:
            qc.x(pos)
            qc.z(pos)
            qc.x(pos)
        else:
            custom = ZGate().control(n-pos-1,ctrl_state=current_state)
            qc.x(pos)
            # print(list(range(n-1,pos-1,-1)))
            qc.append(custom,list(range(n-1,pos-1,-1)))
            qc.x(pos)
        if i != len(bit_position)-1:
            current_state = "1" + current_state
            current_state = "0" * (bit_position[i] - bit_position[i+1]-1) + current_state
            print(current_state)
    while len(current_state) < n-1:
        current_state = "0" + current_state
    # print(current_state)
    # print(list(range(n-1,pos,-1)) + list(range(pos)))
    custom = ZGate().control(n-1,ctrl_state=current_state)
    pos = bit_position[-1]
    qc.append(custom,list(range(n-1,pos,-1)) + list(range(pos+1)))
    # print(qc.depth())
    # print(qc)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import XGate, ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(L):
        append_gate = lambda gate: qc.append(gate(), [0]) if n == 1 else \
                qc.append(gate().control(n - 1, ctrl_state = i & ((1 << n - 1) - 1)), range(n))
        if not (i >> n - 1 & 1):
            append_gate(XGate)
        append_gate(ZGate)
        if not (i >> n - 1 & 1):
            append_gate(XGate)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import XGate, ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    msk = 0
    for k in range(n - 1, -1, -1):
        append_gate = lambda gate: qc.append(gate(), [k]) if n - k - 1 == 0 else \
                    qc.append(gate().control(n - k - 1, ctrl_state=msk), list(range(k + 1, n)) + [k])
        if L >> k & 1:
            append_gate(XGate)
            append_gate(ZGate)
            append_gate(XGate)
            msk = (msk << 1) ^ 1
        else:
            msk <<= 1
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for maska in range(L):
        for i in range(n):
            if (not (maska&(1<<i))):
                qc.x(i)
        if (n == 1):
            qc.z(0)
        else:
            qc.append(ZGate().control(n-1),range(n))
        for i in range(n):
            if (not (maska&(1<<i))):
                qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import *

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    sufiks = 0;
    sufiksik = []
    for i in range(n-1,-1,-1):
        sufiksik.append(i)
        print(i,(1<<(n-i-1)))
        if (L&(1<<i)):
            print("sufiks1: ",sufiks)
            sufiks += (1<<(n-i-1))
        else:
            print("ZGate: ",sufiks, n-i-1)
            for x in sufiksik:
                print(x,end=" ")
            qc.append(ZGate().control(n-i-1),sufiksik)
    for i in range(n):
        qc.z(i)
        for j in range(n):
            if i != j:
                qc.cz(i,j)
        qc.z(i)
    #print(qc)
    return qc
#solve(10,567)
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    sufiks = 0;
    sufiksik = []
    for i in range(n-1,-1,-1):
        sufiksik.append(i)
        print(i,(1<<(n-i-1)))
        if (L&(1<<i)):
            print("sufiks1: ",sufiks)
            sufiks += (1<<(n-i-1))
        else:
            print("ZGate: ",sufiks, n-i-1)
            for x in sufiksik:
                print(x,end=" ")
            qc.append(ZGate().control(n-i-1),sufiksik)
    for i in range(n):
        qc.z(i)
        for j in range(n):
            if i != j:
                qc.cz(i,j)
        qc.z(i)
    #print(qc)
    return qc
#solve(10,567)
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    sufiks = 0;
    sufiksik = []
    for i in range(n-1,-1,-1):
        sufiksik.append(i)
        print(i,(1<<(n-i-1)))
        if (L&(1<<i)):
            sufiks += (1<<(n-i-1))
        else:
            qc.append(ZGate().control(n-i-1),sufiksik)
    for i in range(n):
        qc.z(i)
        for j in range(n):
            if i != j:
                qc.cz(i,j)
        qc.z(i)
    #print(qc)
    return qc
#solve(10,567)
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    sufiks = 0;
    sufiksik = []
    for i in range(n-1,-1,-1):
        sufiksik.append(i)
        if (L&(1<<i)):
            sufiks += (1<<(n-i-1))
        else:
            qc.append(ZGate().control(n-i-1),sufiksik)
    for i in range(n):
        qc.z(i)
        for j in range(n):
            if i != j:
                qc.cz(i,j)
        qc.z(i)
    #print(qc)
    return qc
#solve(10,567)
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    sufiks = 0;
    sufiksik = []
    #for i in range(n-1,-1,-1):
     #   sufiksik.append(i)
      #  if (L&(1<<i)):
       #     sufiks += (1<<(n-i-1))
        #else:
         #   qc.append(ZGate().control(n-i-1),sufiksik)
    for i in range(n):
        qc.z(i)
        for j in range(n):
            if i != j:
                qc.cz(i,j)
        qc.z(i)
    #print(qc)
    return qc
#solve(10,567)
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    sufiks = 0;
    sufiksik = []
   # for i in range(n-1,-1,-1):
    #    sufiksik.append(i)
     #   if (L&(1<<i)):
      #      sufiks += (1<<(n-i-1))
       # else:
        #    qc.append(ZGate().control(n-i-1),sufiksik)
    for i in range(n):
        for j in range(i-1):
            qc.z(j)
            qc.cz(j,i)
            qc.z(j)
    for i in range(n-1,-1,-1):
        for j in range(i+1,n):
            qc.z(j)
            qc.cz(j,i)
            qc.z(j)
    #print(qc)
    return qc
#solve(10,567)
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    sufiks = 0
    sufiksik = []
    for i in range(n-1,-1,-1):
        sufiksik.push_back(i)
        if (L&(1<<i)):
            sufiks += (1<<(n-i-1))
        else:
            qc.append(ZGate().control(n-i-1),sufiksik)
    #print(qc)
    return qc
#solve(10,567)
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    sufiks = 0
    sufiksik = []
    for i in range(n-1,-1,-1):
        sufiksik.push_back(i)
        if (L&(1<<i)):
            sufiks += (1<<(n-i-1))
        else:
            if (i != n-1):
                qc.append(ZGate().control(n-i-1),sufiksik)
    #print(qc)
    return qc
#solve(10,567)
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    sufiks = 0
    sufiksik = []
    for i in range(n-1,-1,-1):
        sufiksik.append(i)
        if (L&(1<<i)):
            sufiks += (1<<(n-i-1))
        else:
            qc.append(ZGate().control(n-i-1),sufiksik)
    #print(qc)
    return qc
#solve(10,567)
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    same = ''
    for i in range(n-1, -1, -1):
        nxt = L >> i & 1
        if nxt == 1:
            if i == n-1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                qc.x(i)
                qc.append(ZGate().control(n-i-1, ctrl_state=same), range(n-1, i-1, -1))
                qc.x(i)
        same = str(nxt) + same

    return qc

print(solve(3, 5))
'''


'''python
from qiskit import QuantumCircuit
from math import pi

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    bitstring = ''
    controls = []
    for i in range(n-1, -1, -1):
        msb = (L >> i) & 1
        L = L & (2 ** i - 1)
        qc.mcp(pi, controls, i, ctrl_state=bitstring)
        controls.append(i)
        bitstring = bitstring + str(msb)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from math import pi

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    bitstring = ''
    controls = []
    L = 2 ** n - L
    for i in range(n-1, -1, -1):
        msb = (L >> i) & 1
        L = L & (2 ** i - 1)
        if msb == 1:
            if not controls:
                qc.p(pi, i)
            else:
                qc.mcp(pi, controls, i, ctrl_state=bitstring)
        controls.append(i)
        bitstring = bitstring + str(msb)

        return qc
'''


'''python
from qiskit import QuantumCircuit
from math import pi

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    bitstring = ''
    controls = []
    L = 2 ** n - L
    for i in range(n-1, -1, -1):
        msb = (L >> i) & 1
        L = L & (2 ** i - 1)
        print(i, msb, bitstring, controls)
        if msb == 1:
            if not controls:
                qc.p(pi, i)
            else:
                qc.mcp(pi, controls, i, ctrl_state=bitstring)
        controls.append(i)
        bitstring = str(1 - msb) + bitstring

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if(L & (1<<i)):
            for j in range(i-1):
                if(not (L & (1<<j))):
                    qc.x(j)
            qc.x(i)
            if i == 0:
                qc.z(0)
            else:
                qc.append(ZGate().control(i), range(i+1))
            for j in range(i-1):
                if(not (L & (1<<j))):
                    qc.x(j)
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if(L & (1<<i)):
            for j in range(i):
                if(not (L & (1<<j))):
                    qc.x(j)
            qc.x(i)
            if i == 0:
                qc.z(0)
            else:
                qc.append(ZGate().control(i), range(i+1))
            for j in range(i):
                if(not (L & (1<<j))):
                    qc.x(j)
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if(L & (1<<i)):
            for j in range(i+1, n):
                if(not (L & (1<<j))):
                    qc.x(j)
            qc.x(i)
            if i == n-1:
                qc.z(0)
            else:
                qc.append(ZGate().control(n-i-1), range(i,n))
            for j in range(i+1, n):
                if(not (L & (1<<j))):
                    qc.x(j)
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if(L & (1<<i)):
            for j in range(i+1, n):
                if(not (L & (1<<j))):
                    qc.x(j)
            qc.x(i)
            if i == n-1:
                qc.z(i)
            else:
                qc.append(ZGate().control(n-i-1), range(i,n))
            for j in range(i+1, ):
                if(not (L & (1<<j))):
                    qc.x(j)
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if(L & (1<<i)):
            for j in range(i+1, n):
                if(not (L & (1<<j))):
                    qc.x(j)
            qc.x(i)
            if i == n-1:
                qc.z(i)
            else:
                qc.append(ZGate().control(n-i-1), range(i,n))
            for j in range(i+1, n):
                if(not (L & (1<<j))):
                    qc.x(j)
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    #for i in range(n):
    #    qc.h(i)
    def xmap(m: int, size: int) -> list[int]:
         a = []
         idx = 0
         while m:
             if m % 2:
                 a.append(idx)
             idx += 1
             m //= 2
         return [i for i in range(size) if i not in a]
    for i in range(L, 2**n):
        a = xmap(i, n)
        if a:
            qc.x(a)
        if n > 1:
            qc.h(0); qc.mcx(list(range(1, n)), 0); qc.h(0)
        else:
            qc.z(0)
        if a:
            qc.x(a)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)
    def to_bin(m: int, size: int) -> list[int]:
        a = [0] * size
        idx = 0
        while m:
            if m % 2:
                a[idx] = 1
            idx += 1
            m //= 2
        return a

    def xmap(m: int, size: int) -> list[int]:
        a = []
        idx = 0
        while m:
            if m % 2:
                a.append(idx)
            idx += 1
            m //= 2
        return [i for i in range(size) if i not in a]

    def mcz(qc: QuantumCircuit, regs: list[int]) -> None:
        if len(regs) == 0:
            return
        if len(regs) == 1:
            qc.z(regs[0])
        else:
            qc.h(regs[-1])
            qc.mcx(regs[:-1], regs[-1])
            qc.h(regs[-1])

    def xmcz(qc: QuantumCircuit, xregs: list[int], zregs: list[int]) -> None:
        if xregs:
            qc.x(xregs)
        mcz(qc, zregs)
        if xregs:
            qc.x(xregs)

    a = to_bin(L, n)
    xs = xmap(L, n)
    #print(xs)
    for i in range(n):
        if a[n - 1 - i] == 0:
            #print("xmcz", [x for x in xs if x > n - 1 - i], list(range(n - 1 - i, n)))
            xmcz(qc, [x for x in xs if x > n - 1 - i], list(range(n - 1 - i, n)))
    xmcz(qc, xs, list(range(n)))
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    def to_bin(m: int, size: int) -> list[int]:
        a = [0] * size
        idx = 0
        while m:
            if m % 2:
                a[idx] = 1
            idx += 1
            m //= 2
        return a

    def xmap(m: int, size: int) -> list[int]:
        a = []
        idx = 0
        while m:
            if m % 2:
                a.append(idx)
            idx += 1
            m //= 2
        return [i for i in range(size) if i not in a]

    def mcz(qc: QuantumCircuit, regs: list[int]) -> None:
        if len(regs) == 0:
            return
        if len(regs) == 1:
            qc.z(regs[0])
        else:
            qc.h(regs[-1])
            qc.mcx(regs[:-1], regs[-1])
            qc.h(regs[-1])

    def xmcz(qc: QuantumCircuit, xregs: list[int], zregs: list[int]) -> None:
        if xregs:
            qc.x(xregs)
        mcz(qc, zregs)
        if xregs:
            qc.x(xregs)

    a = to_bin(L, n)
    xs = xmap(L, n)
    #print(xs)
    for i in range(n):
        if a[n - 1 - i] == 0:
            #print("xmcz", [x for x in xs if x > n - 1 - i], list(range(n - 1 - i, n)))
            xmcz(qc, [x for x in xs if x > n - 1 - i], list(range(n - 1 - i, n)))
    xmcz(qc, xs, list(range(n)))

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L == 2**n:
        return qc
    def to_bin(m: int, size: int) -> list[int]:
        a = [0] * size
        idx = 0
        while m:
            if m % 2:
                a[idx] = 1
            idx += 1
            m //= 2
        return a

    def xmap(m: int, size: int) -> list[int]:
        a = []
        idx = 0
        while m:
            if m % 2:
                a.append(idx)
            idx += 1
            m //= 2
        return [i for i in range(size) if i not in a]

    def mcz(qc: QuantumCircuit, regs: list[int]) -> None:
        if len(regs) == 0:
            return
        if len(regs) == 1:
            qc.z(regs[0])
        else:
            qc.h(regs[-1])
            qc.mcx(regs[:-1], regs[-1])
            qc.h(regs[-1])

    def xmcz(qc: QuantumCircuit, xregs: list[int], zregs: list[int]) -> None:
        if xregs:
            qc.x(xregs)
        mcz(qc, zregs)
        if xregs:
            qc.x(xregs)

    a = to_bin(L, n)
    xs = xmap(L, n)
    #print(xs)
    for i in range(n):
        if a[n - 1 - i] == 0:
            #print("xmcz", [x for x in xs if x > n - 1 - i], list(range(n - 1 - i, n)))
            xmcz(qc, [x for x in xs if x > n - 1 - i], list(range(n - 1 - i, n)))
    xmcz(qc, xs, list(range(n)))

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1,-1,-1):
        if L&(1<<i)==0:
            continue
        for j in range(n-1,i,-1):
            if L&(1<<j)==0:
                qc.x(j)

        qc.x(i)
        if i==n-1:
            qc.z(i)
        else:
            qc.append(ZGate().control(n-i-1),range(n-1,i-1,-1))

        for j in range(n-1,i,-1):
            if L&(1<<j)==0:
                qc.x(j)
        qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    for i in range(n):
        if not (L >> i) & 1:
            continue
        for j in range(i + 1, n):
            if not (L >> j) & 1:
                qc.x(j)
        qc.x(i)
        if i == n - 1:
            qc.z(i)
        else:
            qc.append(ZGate().control(n - i - 1), range(i, n))
        qc.x(i)
        for j in range(i + 1, n):
            if not (L >> j) & 1:
                qc.x(j)
        qc.barrier()

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i in range(n):
        if((L>>j) & 1):
            for j in range(i+1,n):
                if not ((L>>j) & 1):
                    qc.x(j)
        qc.x(i)
        if( i == n-1):
            qc.z(i)
        else:
            qc.append(Zgate().control(n-i-1),range(i,n))
        qc.x(i)
        for j in range(i+1, n):
            if not (L >> j) & 1:
                qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i in range(n):
        if((L>>i) & 1):
            for j in range(i+1,n):
                if not ((L>>j) & 1):
                    qc.x(j)
            qc.x(i)
            if( i == n-1):
                qc.z(i)
            else:
                qc.append(Zgate().control(n-i-1),range(i,n))
            qc.x(i)
            for j in range(i+1, n):
                if not (L >> j) & 1:
                    qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i in range(n):
        if((L>>i) & 1):
            for j in range(i+1,n):
                if not ((L>>j) & 1):
                    qc.x(j)
            qc.x(i)
            if( i == n-1):
                qc.z(i)
            else:
                qc.append(ZGate().control(n-i-1),range(i,n))
            qc.x(i)
            for j in range(i+1, n):
                if not (L >> j) & 1:
                    qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    cnz = ZGate().control(n - 1)
    skip_idx = []
    first = [True for _ in range(n)]
    for d in reversed(range(n)):
        if (L>>d)&1:
            flip = L&(((1<<n)-1)-((1<<d)-1))
            if d > 0:
                flip -= 1
            for i in range(n):
                if (not (i in skip_idx) or first[i]) and ((flip>>i)&1) == 0:
                    qc.x(i)
                    first[i] = False
            qc.append(cnz, range(n))
            for i in range(n):
                if (not (i in skip_idx) or first[i]) and ((flip>>i)&1) == 0:
                    qc.x(i)
                    first[i] = False
        else:
            skip_idx.append(d)

    for i in skip_idx:
        qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def flip_single_state(n: int, k: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    print('n: ', n, ' k: ', k)
    for i in range(n):
        if ((k>>i)&1) == 0:
            qc.x(i)
    qc.append(ZGate().control(n-1), range(n))
    for i in range(n):
        if ((k>>i)&1) == 0:
            qc.x(i)
    return qc

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    for d in reversed(range(n)):
        if (L>>d)&1:
            flip = (L>>d)-1
            fss = flip_single_state(n-d, flip)
            #fss.draw('mpl', style='iqp')
            qc.append(fss.to_gate(), range(d, n))

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    for d in reversed(range(n)):
        if (L>>d)&1:
            flip = (L>>d)-1
            for i in range(n-d):
                if ((flip>>i)&1) == 0:
                    qc.x(i+d)
            qc.append(ZGate().control(n-d-1), range(d, n))
            for i in range(n-d):
                if ((flip>>i)&1) == 0:
                    qc.x(i+d)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    for d in reversed(range(n)):
        if (L>>d)&1:
            flip = (L>>d)-1
            for i in range(n-d):
                if ((flip>>i)&1) == 0:
                    qc.x(i+d)
            if n == d + 1:
                qc.z(d)
            else:
                qc.append(ZGate().control(n-d-1), range(d, n))
            for i in range(n-d):
                if ((flip>>i)&1) == 0:
                    qc.x(i+d)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def mcz(qc: QuantumCircuit, cs: list[int]):
    if len(cs) == 0:
        return
    if len(cs) == 1:
        qc.z(cs[0])
    else:
        mcz_gate = ZGate().control(len(cs) - 1)
        qc.append(mcz_gate, cs)

# val = 2^k のとき
def num_inv_2(qc: QuantumCircuit, n: int, k: int):
    qc.x(range(k, n))
    mcz(qc, list(range(k, n)))
    qc.x(range(k, n))

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    qc.h(range(n))

    for i in range(n):
        if (L >> i) & 1:
            qc.x(i)
            num_inv_2(qc, n, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def mcz(qc: QuantumCircuit, cs: list[int]):
    if len(cs) == 0:
        return
    if len(cs) == 1:
        qc.z(cs[0])
    else:
        mcz_gate = ZGate().control(len(cs) - 1)
        qc.append(mcz_gate, cs)

# val = 2^k のとき
def num_inv_2(qc: QuantumCircuit, n: int, k: int):
    qc.x(range(k, n))
    mcz(qc, list(range(k, n)))
    qc.x(range(k, n))

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    # qc.h(range(n))

    for i in range(n):
        if (L >> i) & 1:
            qc.x(i)
            num_inv_2(qc, n, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def mcz(qc: QuantumCircuit, cs: list[int]):
    if len(cs) == 1:
        qc.z(cs[0])
    else:
        mcz_gate = ZGate().control(len(cs) - 1)
        qc.append(mcz_gate, cs)

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    # qc.h(range(n))

    for i in range(n):
        if (L >> i) & 1 == 0:
            continue

        # flip
        qc.x(i)
        for j in range(i + 1, n):
            if (L >> j) & 1 == 0:
                qc.x(j)

        mcz(qc, list(range(i, n)))

        # flip
        qc.x(i)
        for j in range(i + 1, n):
            if (L >> j) & 1 == 0:
                qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    for i in range(L):
        log=[]
        for j,x in enumerate(f"{i:0{n}b}"[::-1]):
            if x=="0":
                log.append(j)
                qc.x(j)

        if n!=1:
            qc.append(ZGate().control(n-1), range(n))
        else:
            qc.z(0)

        for j in log:
            qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate
import math

def append_cz(qc, ctrl_lst, tgt):
    if ctrl_lst:
        qc.append(ZGate().control(len(ctrl_lst)), ctrl_lst+[tgt])
    else:
        qc.z(tgt)

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    for i in range(n):
        if not (L >> i) & 1:
            continue
        for j in range(i + 1, n):
            if not (L >> j) & 1:
                qc.x(j)
        qc.x(i)
        # if i == n - 1:
        #     qc.z(i)
        # else:
        #     qc.append(ZGate().control(n - i - 1), range(i, n))
        append_cz(qc, [_ for _ in range(i, n-1)], n-1)
        qc.x(i)
        for j in range(i + 1, n):
            if not (L >> j) & 1:
                qc.x(j)

    return qc
'''


'''python
#%%
from qiskit import QuantumCircuit
import math

PI = math.pi
def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n == 1:
        if L == 0:
            return qc
        else:
            qc.x(0)
            qc.z(0)
            qc.x(0)
            return qc

    for marked in range(L):
        if marked == 0:
            for i in range(n):
                qc.x(i)
            # apply MCZ
            qc.mcp(PI, [qbit for qbit in range(n-1)], n-1)
        else:
            for apply_x_bit in range(n):
                if marked & (1<<apply_x_bit) == 0 and not((marked-1) & (1<<apply_x_bit) == 0):
                    qc.x(apply_x_bit)
                elif not(marked & (1<<apply_x_bit) == 0) and (marked-1) & (1<<apply_x_bit) == 0:
                    qc.x(apply_x_bit)
            # apply MCZ
            qc.mcp(PI, [qbit for qbit in range(n-1)], n-1)

    return qc
'''


'''python
#%%
from qiskit import QuantumCircuit
import math

PI = math.pi
def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n == 1:
        if L == 0:
            return qc
        else:
            qc.x(0)
            qc.z(0)
            qc.x(0)
            return qc

    for marked in range(L):
        for zero_bit in range(n):
            if marked & (1<<zero_bit) == 0:
                qc.x(zero_bit)
        # apply MCZ
        qc.mcp(PI, [qbit for qbit in range(n-1)], n-1)
        for zero_bit in range(n):
            if marked & (1<<zero_bit) == 0:
                qc.x(zero_bit)

    return qc
'''


'''python
#%%
from qiskit import QuantumCircuit
import math

PI = math.pi
def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n == 1:
        if L == 0:
            return qc
        else:
            qc.x(0)
            qc.z(0)
            qc.x(0)
            return qc

    for marked in range(L):
        for zero_bit in range(n):
            if marked & (1<<zero_bit) == 0:
                qc.x(zero_bit)
        # apply MCZ
        qc.mcp(PI, [qbit for qbit in range(n-1)], n-1)
        for zero_bit in range(n):
            if marked & (1<<zero_bit) == 0:
                qc.x(zero_bit)

    return qc.decompose(rep=5)
'''


'''python
#%%
from qiskit import QuantumCircuit
import math

PI = math.pi
def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n == 1:
        if L == 0:
            return qc
        else:
            qc.x(0)
            qc.z(0)
            qc.x(0)
            return qc

    for marked in range(L):
        for zero_bit in range(n):
            if marked & (1<<zero_bit) == 0:
                qc.x(zero_bit)
        # apply MCZ
        qc.mcp(PI, [qbit for qbit in range(n-1)], n-1)
        for zero_bit in range(n):
            if marked & (1<<zero_bit) == 0:
                qc.x(zero_bit)

    return qc.decompose(reps=5)
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    k = 0
    while True:
        qc.h(k)
        k += 1
        if (1<<k) >= L:
            break

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import MCMT
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    v = []
    s = 0
    for j in range(n - 1, -1, -1):
        if ((L-1) >> j) & 1:
            s += (1 << j)
            theta = 2 * math.acos(((1 << j) / ((1 << j) + (L - s))) ** 0.5)
            if(len(v) == 0):
                qc.ry(theta, j)
            else:
                for k in range(len(v)):
                    if v[k] == 0:
                        qc.x((n - 1) - k)
                qc.mcrx(theta, qc.qregs[0][(j+1):], qc.qregs[0][j])
                '''
                cnh = MCMT('h',len(v),1)
                qc.compose(cnh,qubits=range(n - 1, (n - 1) - len(v) - 1, -1),inplace=True)
                '''
                for k in range(len(v)):
                    if v[k] == 0:
                        qc.x((n - 1) - k)
            '''
            qc.x(j)
            for k in range(j):
                qc.ch(j, k)
            qc.x(j)
            '''
            v.append(1)
        else:
            v.append(0)
            continue
    for j in range(n - 1):
        if v[j] == 1:
            for k in range(j):
                if(v[k] == 0):
                    qc.x((n - 1) - k)
            qc.x((n - 1) - j)
            cnh = MCMT('h',j + 1,1)
            for k in range((n - 1) - j):
                qc.compose(cnh,qubits = list(range(n - 1, (n - 1) - (j + 1), -1)) + [k],inplace=True)
            for k in range(j):
                if(v[k] == 0):
                    qc.x((n - 1) - k)
            qc.x((n - 1) - j)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    v = []
    s = 0
    for j in range(n - 1, -1, -1):
        if ((L-1) >> j) & 1:
            s += (1 << j)
            theta = 2 * math.acos(((1 << j) / ((1 << j) + (L - s))) ** 0.5)
            if(len(v) == 0):
                qc.ry(theta, j)
            else:
                for k in range(len(v)):
                    if v[k] == 0:
                        qc.x((n - 1) - k)
                qc.mcrx(theta, qc.qregs[0][(j+1):], qc.qregs[0][j])
                '''
                cnh = MCMT('h',len(v),1)
                qc.compose(cnh,qubits=range(n - 1, (n - 1) - len(v) - 1, -1),inplace=True)
                '''
                for k in range(len(v)):
                    if v[k] == 0:
                        qc.x((n - 1) - k)
            '''
            qc.x(j)
            for k in range(j):
                qc.ch(j, k)
            qc.x(j)
            '''
            v.append(1)
        else:
            v.append(0)
            continue
    for j in range(n - 1):
        if v[j] == 1:
            for k in range(j):
                if(v[k] == 0):
                    qc.x((n - 1) - k)
            qc.x((n - 1) - j)
            cnh = MCMT('h',j + 1,1)
            for k in range((n - 1) - j):
                qc.compose(cnh,qubits = list(range(n - 1, (n - 1) - (j + 1), -1)) + [k],inplace=True)
            for k in range(j):
                if(v[k] == 0):
                    qc.x((n - 1) - k)
            qc.x((n - 1) - j)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    v = []
    s = 0
    for j in range(n - 1, -1, -1):
        if ((L-1) >> j) & 1:
            s += (1 << j)
            theta = 2 * math.acos(((1 << j) / ((1 << j) + (L - s))) ** 0.5)
            if(len(v) == 0):
                qc.ry(theta, j)
            else:
                for k in range(len(v)):
                    if v[k] == 0:
                        qc.x((n - 1) - k)
                qc.mcrx(theta, qc.qregs[0][(j+1):], qc.qregs[0][j])
                '''
                cnh = MCMT('h',len(v),1)
                qc.compose(cnh,qubits=range(n - 1, (n - 1) - len(v) - 1, -1),inplace=True)
                '''
                for k in range(len(v)):
                    if v[k] == 0:
                        qc.x((n - 1) - k)
            '''
            qc.x(j)
            for k in range(j):
                qc.ch(j, k)
            qc.x(j)
            '''
            v.append(1)
        else:
            v.append(0)
            continue
    for j in range(n - 1):
        if v[j] == 1:
            for k in range(j):
                if(v[k] == 0):
                    qc.x((n - 1) - k)
            qc.x((n - 1) - j)
            for k in range(j):
                if(v[k] == 0):
                    qc.x((n - 1) - k)
            qc.x((n - 1) - j)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    v = []
    s = 0
    for j in range(n - 1, -1, -1):
        if ((L-1) >> j) & 1:
            s += (1 << j)
            theta = 2 * math.acos(((1 << j) / ((1 << j) + (L - s))) ** 0.5)
            if(len(v) == 0):
                qc.ry(theta, j)
            else:
                for k in range(len(v)):
                    if v[k] == 0:
                        qc.x((n - 1) - k)
                qc.mcrx(theta, qc.qregs[0][(j+1):], qc.qregs[0][j])
                '''
                cnh = MCMT('h',len(v),1)
                qc.compose(cnh,qubits=range(n - 1, (n - 1) - len(v) - 1, -1),inplace=True)
                '''
                for k in range(len(v)):
                    if v[k] == 0:
                        qc.x((n - 1) - k)
            '''
            qc.x(j)
            for k in range(j):
                qc.ch(j, k)
            qc.x(j)
            '''
            v.append(1)
        else:
            v.append(0)
            continue
    for j in range(n - 1):
        if v[j] == 1:
            for k in range(j):
                if(v[k] == 0):
                    qc.x((n - 1) - k)
            qc.x((n - 1) - j)
            for k in range(j):
                if(v[k] == 0):
                    qc.x((n - 1) - k)
            qc.x((n - 1) - j)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import MCMT
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    v = []
    s = 0
    for j in range(n - 1, -1, -1):
        if ((L-1) >> j) & 1:
            s += (1 << j)
            theta = 2 * math.acos(((1 << j) / ((1 << j) + (L - s))) ** 0.5)
            if(len(v) == 0):
                qc.ry(theta, j)
            else:
                for k in range(len(v)):
                    if v[k] == 0:
                        qc.x((n - 1) - k)
                qc.mcrx(theta, qc.qregs[0][(j+1):], qc.qregs[0][j])
                for k in range(len(v)):
                    if v[k] == 0:
                        qc.x((n - 1) - k)
            v.append(1)
        else:
            v.append(0)
            continue
    for j in range(n - 1):
        if v[j] == 1:
            for k in range(j):
                if(v[k] == 0):
                    qc.x((n - 1) - k)
            qc.x((n - 1) - j)
            cnh = MCMT('h',j + 1,1)
            for k in range((n - 1) - j):
                qc.compose(cnh,qubits = list(range(n - 1, (n - 1) - (j + 1), -1)) + [k],inplace=True)
            for k in range(j):
                if(v[k] == 0):
                    qc.x((n - 1) - k)
            qc.x((n - 1) - j)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import MCMT
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    v = []
    s = 0
    for j in range(n - 1, -1, -1):
        if ((L-1) >> j) & 1:
            s += (1 << j)
            theta = 2 * math.acos(((1 << j) / ((1 << j) + (L - s))) ** 0.5)
            if(len(v) == 0):
                qc.ry(theta, j)
            else:
                for k in range(len(v)):
                    if v[k] == 0:
                        qc.x((n - 1) - k)
                qc.mcrx(theta, qc.qregs[0][(j+1):], qc.qregs[0][j])
                for k in range(len(v)):
                    if v[k] == 0:
                        qc.x((n - 1) - k)
            v.append(1)
        else:
            v.append(0)
            continue
    for j in range(n - 1):
        if v[j] == 1:
            for k in range(j):
                if(v[k] == 0):
                    qc.x((n - 1) - k)
            qc.x((n - 1) - j)
            cnh = MCMT('h',j + 1,1)
            for k in range((n - 1) - j):
#                qc.compose(cnh,qubits = list(range(n - 1, (n - 1) - (j + 1), -1)) + [k],inplace=True)
            for k in range(j):
                if(v[k] == 0):
                    qc.x((n - 1) - k)
            qc.x((n - 1) - j)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import MCMT
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    v = []
    s = 0
    for j in range(n - 1, -1, -1):
        if ((L-1) >> j) & 1:
            s += (1 << j)
            theta = 2 * math.acos(((1 << j) / ((1 << j) + (L - s))) ** 0.5)
            if(len(v) == 0):
                qc.ry(theta, j)
            else:
                for k in range(len(v)):
                    if v[k] == 0:
                        qc.x((n - 1) - k)
                qc.mcrx(theta, qc.qregs[0][(j+1):], qc.qregs[0][j])
                for k in range(len(v)):
                    if v[k] == 0:
                        qc.x((n - 1) - k)
            v.append(1)
        else:
            v.append(0)
            continue
    for j in range(n - 1):
        if v[j] == 1:
            for k in range(j):
                if(v[k] == 0):
                    qc.x((n - 1) - k)
            qc.x((n - 1) - j)
            cnh = MCMT('h',j + 1,1)
            for k in range((n - 1) - j):
                pass
#                qc.compose(cnh,qubits = list(range(n - 1, (n - 1) - (j + 1), -1)) + [k],inplace=True)
            for k in range(j):
                if(v[k] == 0):
                    qc.x((n - 1) - k)
            qc.x((n - 1) - j)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import MCMT
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    v = []
    s = 0
    for j in range(n - 1, -1, -1):
        if ((L-1) >> j) & 1:
            s += (1 << j)
            theta = 2 * math.acos(((1 << j) / ((1 << j) + (L - s))) ** 0.5)
            if(len(v) == 0):
                qc.ry(theta, j)
            else:
                for k in range(len(v)):
                    if v[k] == 0:
                        qc.x((n - 1) - k)
#                qc.mcrx(theta, qc.qregs[0][(j+1):], qc.qregs[0][j])
                for k in range(len(v)):
                    if v[k] == 0:
                        qc.x((n - 1) - k)
            v.append(1)
        else:
            v.append(0)
            continue
    for j in range(n - 1):
        if v[j] == 1:
            for k in range(j):
                if(v[k] == 0):
                    qc.x((n - 1) - k)
            qc.x((n - 1) - j)
            cnh = MCMT('h',j + 1,1)
            for k in range((n - 1) - j):
                pass
#                qc.compose(cnh,qubits = list(range(n - 1, (n - 1) - (j + 1), -1)) + [k],inplace=True)
            for k in range(j):
                if(v[k] == 0):
                    qc.x((n - 1) - k)
            qc.x((n - 1) - j)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import MCMT
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    v = []
    s = 0
    for j in range(n - 1, -1, -1):
        if ((L-1) >> j) & 1:
            s += (1 << j)
            theta = 2 * math.acos(((1 << j) / ((1 << j) + (L - s))) ** 0.5)
            if(len(v) == 0):
                qc.ry(theta, j)
            else:
                for k in range(len(v)):
                    if v[k] == 0:
                        qc.x((n - 1) - k)
#                qc.mcrx(theta, qc.qregs[0][(j+1):], qc.qregs[0][j])
                for k in range(len(v)):
                    if v[k] == 0:
                        qc.x((n - 1) - k)
            v.append(1)
        else:
            v.append(0)
            continue
    for j in range(n - 1):
        if v[j] == 1:
            for k in range(j):
                if(v[k] == 0):
                    qc.x((n - 1) - k)
            qc.x((n - 1) - j)
            cnh = MCMT('h',j + 1,1)
            for k in range((n - 1) - j):
                pass
                qc.compose(cnh,qubits = list(range(n - 1, (n - 1) - (j + 1), -1)) + [k],inplace=True)
            for k in range(j):
                if(v[k] == 0):
                    qc.x((n - 1) - k)
            qc.x((n - 1) - j)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import MCMT
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    v = []
    s = 0
    for j in range(n - 1, -1, -1):
        if ((L-1) >> j) & 1:
            s += (1 << j)
            theta = 2 * math.acos(((1 << j) / ((1 << j) + (L - s))) ** 0.5)
            if(len(v) == 0):
                qc.ry(theta, j)
            else:
                for k in range(len(v)):
                    if v[k] == 0:
                        qc.x((n - 1) - k)
                cnh = MCMT('h', (n - 1) - j,1)
                qc.compose(cnh,qubits = list(range(n - 1, j - 1, -1)),inplace=True)
                qc.mcp(theta, qc.qregs[0][(j+1):], qc.qregs[0][j])
                cnh = MCMT('h', (n - 1) - j,1)
                qc.compose(cnh,qubits = list(range(n - 1, j - 1, -1)),inplace=True)

                for k in range(len(v)):
                    if v[k] == 0:
                        qc.x((n - 1) - k)
            v.append(1)
        else:
            v.append(0)
            continue
    for j in range(n - 1):
        if v[j] == 1:
            for k in range(j):
                if(v[k] == 0):
                    qc.x((n - 1) - k)
            qc.x((n - 1) - j)
            cnh = MCMT('h',j + 1,1)
            for k in range((n - 1) - j):
                qc.compose(cnh,qubits = list(range(n - 1, (n - 1) - (j + 1), -1)) + [k],inplace=True)
            for k in range(j):
                if(v[k] == 0):
                    qc.x((n - 1) - k)
            qc.x((n - 1) - j)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.h(range(math.ceil(math.log2(L))))
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L == 1:
        return qc
    qc.h(range(math.ceil(math.log2(L))))
    return qc
'''


'''python
from qiskit import QuantumCircuit
from math import sqrt

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    amplitude = sqrt(1 / L)
    amplitudes = [amplitude] * L + [0] * (2**n - L)
    qc.initialize(amplitudes, range(n))

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    # Apply Hadamard gates to all qubits to create superposition
    for qubit in range(n):
        qc.h(qubit)

    # Apply controlled rotations to zero out states beyond L-1
    for i in range(L, 2**n):
        # Convert i to binary and apply a series of X gates to flip the states
        binary_representation = format(i, f'0{n}b')
        for qubit in range(n):
            if binary_representation[qubit] == '1':
                qc.x(qubit)
        # Apply a controlled-NOT to flip the state back to |0> if the control qubit is |1>
        qc.mct(list(range(n)), n)  # Multi-controlled Toffoli gate
        for qubit in range(n):
            if binary_representation[qubit] == '1':
                qc.x(qubit)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    # Apply Hadamard gates to the first L qubits
    for i in range(L):
        qc.h(i)  # Apply Hadamard to the first L qubits

    # Normalize the amplitudes
    # The amplitude for each of the first L states will be 1/sqrt(L)
    # This is already achieved by applying H gates to L qubits.

    # The remaining qubits (if any) should be set to |0>
    # This is automatically the case since we initialized the circuit with |0>

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:

    qc = QuantumCircuit(n)

    # Number of qubits that must be in uniform superposition
    k = 0 if L == 1 else math.ceil(math.log2(L))

    # Apply Hadamard to the first k qubits (indices 0 … k-1)
    for q in range(k):
        qc.h(q)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    m=0
    result=1
    while result*2 < L:
        result *= 2
        m += 1
    qc.ry(2*math.asin(math.sqrt(L-result/L)),m)
    for num in range(m):
        qc.h(num)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    m=0
    result=1
    while result*2 < L:
        result *= 2
        m += 1
    qc.ry(2*math.asin(math.sqrt((L-result)/L)),m)
    for num in range(m):
        qc.h(num)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    m=0
    result=1
    while result*2 < L:
        result *= 2
        m += 1
    qc.ry(2*math.asin(math.sqrt((L-result)/L)),m)
    for num in range(m):
        qc.h(num)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import mat

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    m=0
    result=1
    while result*2 < L:
        result *= 2
        m += 1
    for num in range(m):
        qc.h(num)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    m=0
    result=1
    while result*2 < L-:
        result *= 2
        m += 1
    for num in range(m):
        qc.h(num)

    return qc
'''


'''python
from qiskit import QuantumCircuit
i

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    m=0
    result=1
    while result*2 < L:
        result *= 2
        m += 1
    for num in range(m):
        qc.h(num)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    m=0
    result=1
    while result*2 < L:
        result *= 2
        m += 1
    for num in range(m):
        qc.h(num)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    m=0
    result=1
    while result*2 < L:
        result *= 2
        m += 1
        qc.h(range(num))

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    m=0
    result=1
    while result*2 < L:
        result *= 2
        m += 1
    qc.h(range(num))

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L==1:
        return qc
    m=0
    result=1
    while result*2 < L:
        result *= 2
        m += 1
    qc.h(range(num))

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L==1:
        return qc
    m=0
    result=1
    while result*2 < L:
        result *= 2
        m += 1
    qc.h(range(m))

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # for i in range(n):
    #     qc.h(i)
    # Write your code here:
    if L == (1<<n):
        for i in range(n):
            qc.h(i)
        return qc
    for i in reversed(range(n)):
        if L&(1<<i) :
            for j in range(i+1):
                qc.h(j)
            break

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.h(range(math.ceil(math.log2(L))))
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.h(range(max(1, math.ceil(math.log2(L)))))
    return qc
'''


'''python
import math
from qiskit import QuantumCircuit
from qiskit import QuantumRegister
from qiskit.circuit.library import HGate, CXGate, MCPhaseGate, MCMT

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if 2 ** i >= L:
            break
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    qc.ry(2 * math.acos(1 / math.sqrt(L)), 0)
    qc.ch(0, 1)
    for i in range(n - 1):
        qc.cx(i, i + 1)

    qc.x(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    qc.ry(2 * math.acos(1 / math.sqrt(L)), 0)
    qc.ch(0, 1)

    for i in range(n - 1, 0, -1):
        qc.cx(i, i + 1)

    qc.x(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    qc.ry(2 * math.acos(1 / math.sqrt(L)), 0)
    qc.ch(0, 1)

    for i in range(n - 2):
        qc.cx(i, i + 1)

    qc.x(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    if n == 1:
        return qc

    qc.ry(2 * math.acos(1 / math.sqrt(L)), 0)
    qc.ch(0, 1)

    for i in range(n - 2):
        qc.cx(i, i + 1)

    qc.x(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    if n == 1:
        return qc

    qc.ry(2 * math.acos(1 / math.sqrt(L)), 0)
    qc.ch(0, 1)

    # qc.cx(1, 2)
    # qc.cx(0, 1)  # n = 3, L = 3

    for i in range(n - 2, 0, -1):
        qc.cx(0, i)

    qc.x(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1):
        qc.h(i)
        qc.cz(i,i+1)
        qc.h(i+1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)
        qc.cz(i,i+1)
        qc.h(i+1)
    return qc
'''


'''python
from qiskit import QuantumCircuit, Aer, transpile, assemble, execute
from qiskit.visualization import plot_histogram
from qiskit.circuit.library import MCPhaseGate
import math

def oracle(n: int, L: int) -> QuantumCircuit:
    """ オラクル関数 """
    qc = QuantumCircuit(n)
    ato = []
    for i in range(n-1,-1,-1):
        lst = []
        for j in range(i+1, n):
            lst.append(j)

        if len(lst) == 0:
            if (L >> i) & 1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)
        else:
            if (L >> i) & 1:
                qc.x(i)
                qc.append(MCPhaseGate(math.pi, len(lst)), lst + [i])
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)

    for i in ato:
        qc.x(i)
    return qc

def diffusion_operator(n):
    """ 拡散操作関数 """
    qc = QuantumCircuit(n)
    qc.h(range(n))
    qc.x(range(n))
    qc.h(n-1)
    qc.mcx(list(range(n-1)), n-1)
    qc.h(n-1)
    qc.x(range(n))
    qc.h(range(n))
    return qc

def solve(n, L):
    """ グローバーのアルゴリズムを使って特定のビット列 L を見つけ出す関数 """
    qc = QuantumCircuit(n, n)

    # 初期化
    for i in range(n):
        qc.h(i)

    # 繰り返し回数
    iterations = int(math.sqrt((2**n)))

    for _ in range(iterations):
        # オラクルの適用
        qc.append(oracle(n, L), range(n))

        # 拡散操作の適用
        qc.append(diffusion_operator(n), range(n))
'''


'''python
from qiskit import QuantumCircuit, Aer, transpile, assemble, execute
#from qiskit.visualization import plot_histogram
from qiskit.circuit.library import MCPhaseGate
import math

def oracle(n: int, L: int) -> QuantumCircuit:
    """ オラクル関数 """
    qc = QuantumCircuit(n)
    ato = []
    for i in range(n-1,-1,-1):
        lst = []
        for j in range(i+1, n):
            lst.append(j)

        if len(lst) == 0:
            if (L >> i) & 1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)
        else:
            if (L >> i) & 1:
                qc.x(i)
                qc.append(MCPhaseGate(math.pi, len(lst)), lst + [i])
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)

    for i in ato:
        qc.x(i)
    return qc

def diffusion_operator(n):
    """ 拡散操作関数 """
    qc = QuantumCircuit(n)
    qc.h(range(n))
    qc.x(range(n))
    qc.h(n-1)
    qc.mcx(list(range(n-1)), n-1)
    qc.h(n-1)
    qc.x(range(n))
    qc.h(range(n))
    return qc

def solve(n, L):
    """ グローバーのアルゴリズムを使って特定のビット列 L を見つけ出す関数 """
    qc = QuantumCircuit(n, n)

    # 初期化
    for i in range(n):
        qc.h(i)

    # 繰り返し回数
    iterations = int(math.sqrt((2**n)))

    for _ in range(iterations):
        # オラクルの適用
        qc.append(oracle(n, L), range(n))

        # 拡散操作の適用
        qc.append(diffusion_operator(n), range(n))
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import MCPhaseGate
import math

def oracle(n: int, L: int) -> QuantumCircuit:
    """ オラクル関数 """
    qc = QuantumCircuit(n)
    ato = []
    for i in range(n-1,-1,-1):
        lst = []
        for j in range(i+1, n):
            lst.append(j)

        if len(lst) == 0:
            if (L >> i) & 1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)
        else:
            if (L >> i) & 1:
                qc.x(i)
                qc.append(MCPhaseGate(math.pi, len(lst)), lst + [i])
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)

    for i in ato:
        qc.x(i)
    return qc

def diffusion_operator(n):
    """ 拡散操作関数 """
    qc = QuantumCircuit(n)
    qc.h(range(n))
    qc.x(range(n))
    qc.h(n-1)
    qc.mcx(list(range(n-1)), n-1)
    qc.h(n-1)
    qc.x(range(n))
    qc.h(range(n))
    return qc

def solve(n, L):
    """ グローバーのアルゴリズムを使って特定のビット列 L を見つけ出す関数 """
    qc = QuantumCircuit(n, n)

    # 初期化
    for i in range(n):
        qc.h(i)

    # 繰り返し回数
    iterations = int(math.sqrt((2**n)))

    for _ in range(iterations):
        # オラクルの適用
        qc.append(oracle(n, L), range(n))

        # 拡散操作の適用
        qc.append(diffusion_operator(n), range(n))
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import MCPhaseGate
import math

def oracle(n: int, L: int) -> QuantumCircuit:
    """ オラクル関数 """
    qc = QuantumCircuit(n)
    ato = []
    for i in range(n-1,-1,-1):
        lst = []
        for j in range(i+1, n):
            lst.append(j)

        if len(lst) == 0:
            if (L >> i) & 1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)
        else:
            if (L >> i) & 1:
                qc.x(i)
                qc.append(MCPhaseGate(math.pi, len(lst)), lst + [i])
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)

    for i in ato:
        qc.x(i)
    return qc

def diffusion_operator(n):
    """ 拡散操作関数 """
    qc = QuantumCircuit(n)
    qc.h(range(n))
    qc.x(range(n))
    qc.h(n-1)
    qc.mcx(list(range(n-1)), n-1)
    qc.h(n-1)
    qc.x(range(n))
    qc.h(range(n))
    return qc

def solve(n, L):
    """ グローバーのアルゴリズムを使って特定のビット列 L を見つけ出す関数 """
    qc = QuantumCircuit(n, n)

    # 初期化
    for i in range(n):
        qc.h(i)

    # 繰り返し回数
    iterations = int(math.sqrt((2**n)))

    for _ in range(iterations):
        # オラクルの適用
        qc.append(oracle(n, L), range(n))

        # 拡散操作の適用
        qc.append(diffusion_operator(n), range(n))
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import MCPhaseGate
import math

def oracle(qc,n, L):
    """ オラクル関数 """
    #qc = QuantumCircuit(n)
    ato = []
    for i in range(n-1,-1,-1):
        lst = []
        for j in range(i+1, n):
            lst.append(j)

        if len(lst) == 0:
            if (L >> i) & 1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)
        else:
            if (L >> i) & 1:
                qc.x(i)
                qc.append(MCPhaseGate(math.pi, len(lst)), lst + [i])
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)

    for i in ato:
        qc.x(i)
    return qc

def diffusion_operator(qc,n):
    """ 拡散操作関数 """
    #qc = QuantumCircuit(n)
    qc.h(range(n))
    qc.x(range(n))
    qc.h(n-1)
    qc.mcx(list(range(n-1)), n-1)
    qc.h(n-1)
    qc.x(range(n))
    qc.h(range(n))
    return qc

def solve(n:int, L:int):
    """ グローバーのアルゴリズムを使って特定のビット列 L を見つけ出す関数 """
    qc = QuantumCircuit(n)

    # 初期化
    for i in range(n):
        qc.h(i)

    # 繰り返し回数
    iterations = int(math.sqrt((2**n)))

    for _ in range(iterations):
        # オラクルの適用
        #qc.append(oracle(n, L), range(n))
        oracle(qc,n, L)

        # 拡散操作の適用
        #qc.append(diffusion_operator(n), range(n))
        diffusion_operator(qc,n)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import MCPhaseGate
import math

def oracle(qc,n, L):
    """ オラクル関数 """
    #qc = QuantumCircuit(n)
    ato = []
    for i in range(n-1,-1,-1):
        lst = []
        for j in range(i+1, n):
            lst.append(j)

        if len(lst) == 0:
            if (L >> i) & 1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)
        else:
            if (L >> i) & 1:
                qc.x(i)
                qc.append(MCPhaseGate(math.pi, len(lst)), lst + [i])
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)

    for i in ato:
        qc.x(i)
    return qc

def diffusion_operator(qc,n):
    """ 拡散操作関数 """
    #qc = QuantumCircuit(n)
    qc.h(range(n))
    qc.x(range(n))
    qc.h(n-1)
    qc.mcx(list(range(n-1)), n-1)
    qc.h(n-1)
    qc.x(range(n))
    qc.h(range(n))
    return qc

def solve(n:int, L:int):
    """ グローバーのアルゴリズムを使って特定のビット列 L を見つけ出す関数 """
    qc = QuantumCircuit(n)

    # 初期化
    for i in range(n):
        qc.h(i)

    # 繰り返し回数
    iterations = int(math.sqrt((2**n)/L))

    for _ in range(iterations):
        # オラクルの適用
        #qc.append(oracle(n, L), range(n))
        oracle(qc,n, L)

        # 拡散操作の適用
        #qc.append(diffusion_operator(n), range(n))
        diffusion_operator(qc,n)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import MCPhaseGate
import math

def oracle(qc,n, L):
    """ オラクル関数 """
    #qc = QuantumCircuit(n)
    ato = []
    for i in range(n-1,-1,-1):
        lst = []
        for j in range(i+1, n):
            lst.append(j)

        if len(lst) == 0:
            if (L >> i) & 1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)
        else:
            if (L >> i) & 1:
                qc.x(i)
                qc.append(MCPhaseGate(math.pi, len(lst)), lst + [i])
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)

    for i in ato:
        qc.x(i)
    return qc

def diffusion_operator(qc,n):
    """ 拡散操作関数 """
    #qc = QuantumCircuit(n)
    qc.h(range(n))
    qc.x(range(n))
    qc.h(n-1)
    qc.mcx(list(range(n-1)), n-1)
    qc.h(n-1)
    qc.x(range(n))
    qc.h(range(n))
    return qc

def solve(n:int, L:int):
    """ グローバーのアルゴリズムを使って特定のビット列 L を見つけ出す関数 """
    qc = QuantumCircuit(n)

    # 初期化
    for i in range(n):
        qc.h(i)

    # 繰り返し回数
    iterations = int(math.sqrt((2**n)/(L+1)))

    for _ in range(iterations):
        # オラクルの適用
        #qc.append(oracle(n, L), range(n))
        oracle(qc,n, L)

        # 拡散操作の適用
        #qc.append(diffusion_operator(n), range(n))
        diffusion_operator(qc,n)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import MCPhaseGate
import math

def oracle(qc,n, L):
    """ オラクル関数 """
    #qc = QuantumCircuit(n)
    ato = []
    for i in range(n-1,-1,-1):
        lst = []
        for j in range(i+1, n):
            lst.append(j)

        if len(lst) == 0:
            if (L >> i) & 1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)
        else:
            if (L >> i) & 1:
                qc.x(i)
                qc.append(MCPhaseGate(math.pi, len(lst)), lst + [i])
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)

    for i in ato:
        qc.x(i)
    return qc

def diffusion_operator(qc,n):
    """ 拡散操作関数 """
    #qc = QuantumCircuit(n)
    qc.h(range(n))
    qc.x(range(n))
    qc.h(n-1)
    qc.mcx(list(range(n-1)), n-1)
    qc.h(n-1)
    qc.x(range(n))
    qc.h(range(n))
    return qc

def solve(n:int, L:int):
    """ グローバーのアルゴリズムを使って特定のビット列 L を見つけ出す関数 """
    qc = QuantumCircuit(n)

    # 初期化
    for i in range(n):
        qc.h(i)

    # 繰り返し回数
    iterations = int(math.sqrt((2**n)/(0+1)))

    for _ in range(iterations):
        # オラクルの適用
        #qc.append(oracle(n, L), range(n))
        oracle(qc,n, L)

        # 拡散操作の適用
        #qc.append(diffusion_operator(n), range(n))
        diffusion_operator(qc,n)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import RYGate, HGate
import math
from qiskit import QuantumCircuit

def append_cry(qc, angle, ctrl_lst, tgt):
    if ctrl_lst:
        # 制御量子ビットがある場合、制御RYゲートを追加
        qc.append(RYGate(angle).control(len(ctrl_lst)), ctrl_lst + [tgt])
    else:
        # 制御量子ビットがない場合、通常のRYゲートを追加
        qc.ry(angle, tgt)

def append_ch(qc, ctrl_lst, tgt):
    if ctrl_lst:
        # 制御量子ビットがある場合、制御Hゲートを追加
        qc.append(HGate().control(len(ctrl_lst)), ctrl_lst + [tgt])
    else:
        # 制御量子ビットがない場合、通常のHゲートを追加
        qc.h(tgt)

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    dq = [(0,n,L)]
    while dq:
        x,m,l = dq.pop(0)
        #print(x,m,l)
        if l == 0 or m == 0:
            continue

        if l == 2**m:
            for j in range(m,n):
                if not (x>>j)&1:
                    qc.x(j)
            for j in range(m):
                append_ch(qc,[_ for _ in range(m,n)],j)
            for j in range(m,n):
                if not (x>>j)&1:
                    qc.x(j)
            #qc.barrier()
            continue

        left = min(2**(m-1),l)
        right = l-left

        if left != l:
            # 処理2
            for j in range(m,n):
                if not (x>>j)&1:
                    qc.x(j)
            append_cry(qc, 2*math.acos((left/l)**0.5),[_ for _ in range(m,n)],m-1)
            for j in range(m,n):
                if not (x>>j)&1:
                    qc.x(j)
            #qc.barrier()
        # 追加2
        dq.append((x,m-1,left))
        dq.append((x+2**(m-1),m-1,right))
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import RYGate, HGate
import math
from qiskit import QuantumCircuit

def append_cry(qc, angle, ctrl_lst, tgt):
    if ctrl_lst:
        # 制御量子ビットがある場合、制御RYゲートを追加
        qc.append(RYGate(angle).control(len(ctrl_lst)), ctrl_lst + [tgt])
    else:
        # 制御量子ビットがない場合、通常のRYゲートを追加
        qc.ry(angle, tgt)

def append_ch(qc, ctrl_lst, tgt):
    if ctrl_lst:
        # 制御量子ビットがある場合、制御Hゲートを追加
        qc.append(HGate().control(len(ctrl_lst)), ctrl_lst + [tgt])
    else:
        # 制御量子ビットがない場合、通常のHゲートを追加
        qc.h(tgt)

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    dq = [(0,n,L)]
    while dq:
        x,m,l = dq.pop(0)
        #print(x,m,l)
        if l == 0 or m == 0:
            continue

        if l == 2**m:
            for j in range(m,n):
                if not (x>>j)&1:
                    qc.x(j)
            for j in range(m):
                append_ch(qc,[_ for _ in range(m,n)],j)
            for j in range(m,n):
                if not (x>>j)&1:
                    qc.x(j)
            #qc.barrier()
            continue

        left = min(2**(m-1),l)
        right = l-left

        if left != l:
            # 処理2
            for j in range(m,n):
                if not (x>>j)&1:
                    qc.x(j)
            append_cry(qc, 2*math.acos((left/l)**0.5),[_ for _ in range(m,n)],m-1)
            for j in range(m,n):
                if not (x>>j)&1:
                    qc.x(j)
            #qc.barrier()
        # 追加2
        dq.append((x,m-1,left))
        dq.append((x+2**(m-1),m-1,right))

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve_rec(qc: QuantumCircuit, n: int, bit: int, L: int) -> None:
    if bit < 0:
        return
    if L < (1<<bit):
        qc.x(bit)
        solve_rec(qc, n, bit-1, L)
        qc.x(bit)
    else:
        qc.r(math.pi/2, 0, bit)
        qc.mcp(-2*math.acos(math.sqrt((1<<bit)/L)), list(range(bit+1,n)), bit)
        qc.r(-math.pi/2, 0, bit)
        qc.x(bit)
        for i in range(bit):
            qc.r(math.pi/2, 0, i)
            qc.mcp(math.pi/2, list(range(bit,n)), i)
            qc.r(-math.pi/2, 0, i)
        qc.x(bit)
        solve_rec(qc, n, bit-1, L-(1<<bit))

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n == 1 and L == 2:
        qc.h(0)
    if n == 1:
        return qc
    if L < (1<<(n-1)):
        qc.x(n-1)
        solve_rec(qc, n, n-2, L)
        qc.x(n-1)
    else:
        qc.r(2*math.acos(math.sqrt((1<<(n-1))/L)), math.pi/2, n-1)
        qc.x(n-1)
        for i in range(n-1):
            qc.ch(n-1, i)
        qc.x(n-1)
        solve_rec(qc, n, n-2, L-(1<<(n-1)))
    return qc
'''


'''python
from qiskit import QuantumCircuit
from math import pi, asin, sqrt, floor

def oracle(qc: QuantumCircuit, n: int, L: int) -> QuantumCircuit:
    K = L - 1

    xgates = []
    for i in range(n - 1, -1, -1):
        if (K >> i) & 1:
            continue

        if i < n - 1:
            qc.mcp(pi, list(range(i + 1, n)), i)
        else:
            qc.z(i)

        xgates.append(i)
        qc.x(i)

    for i in xgates:
        qc.x(i)

    return qc

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)

    theta = asin(sqrt(L / (1 << n)))
    r = floor(pi / (4 * theta))
    # print(theta, r)

    for _ in range(r):
        oracle(qc, n, L)

        for i in range(n):
            qc.h(i)
            qc.x(i)

        if n > 1:
            qc.mcp(pi, list(range(n - 1)), n - 1)
        else:
            qc.z(n - 1)

        for i in range(n):
            qc.x(i)
            qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    i = 0
    while (1 << i) < L:
        qc.h(i)
        i += 1

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    i=0
    while 2**i <= L:
        qc.h(i)
        i+=1

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if 2**i <= L:
            qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    k=0
    while L>1:
        k++
        L=L/2
    for i in range(k):
        qc.h(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    i = 0
    while 2 ** i < L:
      qc.h(i)
      i += 1
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import XGate, ZGate, HGate, RYGate
from math import sqrt, acos, pi

def append_control_gate(qc: QuantumCircuit, gate, target_qubit: int, ctrl_state: int, controlled_qubits)->QuantumCircuit:
	if len(controlled_qubits) == 0:
		qc.append(gate, [target_qubit])
	else:
		qc.append(gate.control(len(controlled_qubits), ctrl_state = ctrl_state), controlled_qubits + [target_qubit])
	return qc

def solve(n: int, L: int)->QuantumCircuit:
	qc = QuantumCircuit(n)
	if L == (1 << n):
		for i in range(n):
			qc.h(i)
		return qc
	mask = (1 << n) - 1
	ctrl_state = 0
	controlled_qubits = []
	for bit in range(n - 1, -1, -1):
		if L >> bit & 1:
			left, right = 1 << bit, L - (1 << bit)
			theta = acos(sqrt(left) / sqrt(left + right)) * 2
			append_control_gate(qc, RYGate(theta), bit, ctrl_state, controlled_qubits)
			for bit_next in range(bit - 1, -1, -1):
				append_control_gate(qc, HGate(), bit_next, ctrl_state, controlled_qubits + [bit])
			L ^= 1 << bit
			ctrl_state |= 1 << len(controlled_qubits)
		controlled_qubits += [bit]
	return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    theta = 2 * math.acos(math.sqrt(0.5))
    for i in range(n):
        qc.h(i)
    for i in range(n):
        for j in range(i+1, n):
            qc.cz(i, j)
    qc.u1(theta, n-1)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    theta = 2 * math.acos(math.sqrt(0.5))
    for i in range(n):
        qc.h(i)
    for i in range(n):
        for j in range(i+1, n):
            qc.cz(i, j)
    qc.p(theta, n-1)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    m = math.ceil(math.log2(L))
    for i in range(m):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
	def lessthan(qc: QuantumCircuit, n: int, L: int):
        def xmap(m: int, size: int) -> list[int]:
              a = []
              idx = 0
              while m:
                  if m % 2:
                      a.append(idx)
                  idx += 1
                  m //= 2
              return [i for i in range(size) if i not in a]
        for i in range(L, 2**n):
            a = xmap(i, n)
            if a:
                qc.x(a)
            if n > 1:
                qc.h(0); qc.mcx(list(range(1, n)), 0); qc.h(0)
            else:
                qc.z(0)
            if a:
                qc.x(a)

    def zmark(qc,n):
        for i in range(n):
            qc.h(i); qc.x(i)
        if n==1:
            qc.z(0)
        else:
            qc.h(0); qc.mcx(list(range(1, n)), 0); qc.h(0)
        for i in range(n):
            qc.x(i); qc.h(i)

    for i in range(n):
        qc.h(i)
    lessthan(qc, n, L); zmark(qc, n)
    lessthan(qc, n, L); zmark(qc, n)
    lessthan(qc, n, L); zmark(qc, n)
    lessthan(qc, n, L); zmark(qc, n)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
	def lessthan(qc: QuantumCircuit, n: int, L: int):
        def xmap(m: int, size: int) -> list[int]:
              a = []
              idx = 0
              while m:
                  if m % 2:
                      a.append(idx)
                  idx += 1
                  m //= 2
              return [i for i in range(size) if i not in a]
        for i in range(L, 2**n):
            a = xmap(i, n)
            if a:
                qc.x(a)
            if n > 1:
                qc.h(0); qc.mcx(list(range(1, n)), 0); qc.h(0)
            else:
                qc.z(0)
            if a:
                qc.x(a)

    def zmark(qc,n):
        for i in range(n):
            qc.h(i); qc.x(i)
        if n==1:
            qc.z(0)
        else:
            qc.h(0); qc.mcx(list(range(1, n)), 0); qc.h(0)
        for i in range(n):
            qc.x(i); qc.h(i)

    for i in range(n):
        qc.h(i)
    lessthan(qc, n, L); zmark(qc, n)
    lessthan(qc, n, L); zmark(qc, n)
    lessthan(qc, n, L); zmark(qc, n)
    lessthan(qc, n, L); zmark(qc, n)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    def lessthan(qc: QuantumCircuit, n: int, L: int):
        def xmap(m: int, size: int) -> list[int]:
              a = []
              idx = 0
              while m:
                  if m % 2:
                      a.append(idx)
                  idx += 1
                  m //= 2
              return [i for i in range(size) if i not in a]
        for i in range(L, 2**n):
            a = xmap(i, n)
            if a:
                qc.x(a)
            if n > 1:
                qc.h(0); qc.mcx(list(range(1, n)), 0); qc.h(0)
            else:
                qc.z(0)
            if a:
                qc.x(a)

    def zmark(qc,n):
        for i in range(n):
            qc.h(i); qc.x(i)
        if n==1:
            qc.z(0)
        else:
            qc.h(0); qc.mcx(list(range(1, n)), 0); qc.h(0)
        for i in range(n):
            qc.x(i); qc.h(i)

    for i in range(n):
        qc.h(i)
    lessthan(qc, n, L); zmark(qc, n)
    lessthan(qc, n, L); zmark(qc, n)
    lessthan(qc, n, L); zmark(qc, n)
    lessthan(qc, n, L); zmark(qc, n)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    def lessthan(qc: QuantumCircuit, n: int, L: int):
        def xmap(m: int, size: int) -> list[int]:
              a = []
              idx = 0
              while m:
                  if m % 2:
                      a.append(idx)
                  idx += 1
                  m //= 2
              return [i for i in range(size) if i not in a]
        for i in range(L, 2**n):
            a = xmap(i, n)
            if a:
                qc.x(a)
            if n > 1:
                qc.h(0); qc.mcx(list(range(1, n)), 0); qc.h(0)
            else:
                qc.z(0)
            if a:
                qc.x(a)

    def zmark(qc,n):
        for i in range(n):
            qc.h(i); qc.x(i)
        if n==1:
            qc.z(0)
        else:
            qc.h(0); qc.mcx(list(range(1, n)), 0); qc.h(0)
        for i in range(n):
            qc.x(i); qc.h(i)

    for i in range(n):
        qc.h(i)
    lessthan(qc, n, L); zmark(qc, n)
    lessthan(qc, n, L); zmark(qc, n)
    lessthan(qc, n, L); zmark(qc, n)
    lessthan(qc, n, L); zmark(qc, n)
    lessthan(qc, n, L); zmark(qc, n)
    lessthan(qc, n, L); zmark(qc, n)
    lessthan(qc, n, L); zmark(qc, n)
    lessthan(qc, n, L); zmark(qc, n)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    def xmap(m: int) -> list[int]:
        a = []
        idx = 0
        while m:
            if m % 2:
                a.append(idx)
            idx += 1
            m //= 2
        return a
    if L == 0:
        return qc
    regs = list(range(0, min(n, xmap(L)[-1]+1)))
    if regs:
        qc.h(regs)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    i = 0

    while (l+1 > 2**i):
        i += 1

    for j in range(i):
        qc.h(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    i = 0

    while (L+1 > 2**i):
        i += 1

    for j in range(i):
        qc.h(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    qc = QuantumCircuit(n)

    i = 0

    while (L > 2**i):
        i += 1

    for j in range(i):
        qc.h(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L_keta = 1
    for i in range(n):
        if 2**i <= L:
            L_keta = i+1

    for i in range(L_keta):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.h(range(math.ceil(math.log2(L))))

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    import math
    qc.h(range(math.ceil(math.log2(L))))

    return qc
'''