'''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 i >= L.bit_length() or not(L >> i) & 1:
            continue
        for j in range(i + 1, n):
            qc.x(j)
        qc.x(i)
        if i == n - 1:
            qc.z(0)
        else:
            qc.append(ZGate().control(n - i - 1), range(i + 1, n))
        for j in range(i + 1, n):
            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 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(0)
        else:
            qc.append(ZGate().control(n - i - 1), range(i, n))
        for j in range(i + 1, n):
            if not(L >> j) & 1:
                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 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))
        for j in range(i + 1, n):
            if not(L >> j) & 1:
                qc.x(j)
        qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import numpy as np

def oracle_less(bitcount: int, n: int) -> QuantumCircuit:
    regin, regout = QuantumRegister(bitcount), QuantumRegister(1)
    qc = QuantumCircuit(regin, regout)

    if n >= 2**bitcount:
        qc.x(regout)
        return qc

    bitrep = bin(n)[2:].zfill(bitcount)[::-1]
    masks = []

    print(bitrep)
    for idx in range(bitcount):
        if bitrep[idx] == "1":
            mask = ("0" + bitrep[idx + 1 :]).rjust(bitcount, "*")
            masks.append(mask)

    flipped = [False] * bitcount
    for mask in masks:
        print(mask)
        controls = []
        for idx in range(bitcount):
            if mask[idx] == "*":
                continue

            controls.append(regin[idx])
            if (mask[idx] == "0") != (flipped[idx]):
                flipped[idx] = not flipped[idx]
                qc.x(regin[idx])

        if len(controls) != 0:
            qc.mcx(controls, regout)

    for idx in range(bitcount):
        if flipped[idx]:
            qc.x(regin[idx])

    return qc

def solve(n: int, l: int):
    regin = QuantumRegister(n)
    qc = QuantumCircuit(regin)

    anc = QuantumRegister(1)
    qc.add_register(anc)

    qc.x(anc)
    qc.h(anc)

    qc.compose(oracle_less(n, l), inplace=True)

    qc.h(anc)
    qc.x(anc)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, L) -> QuantumCircuit:
    qreg = QuantumRegister(n)
    circuit = QuantumCircuit(qreg)

    most_bit = int(math.log2(L)) + 1

    circuit.x(qreg[most_bit])
    circuit.z(qreg[most_bit])
    circuit.x(qreg[most_bit])

    for i in range(2**(most_bit-1)+1, L):
        bin_state = format(i, f'0{n}b')[::-1]

        x_gates = [idx for idx, bit in enumerate(bin_state) if bit == '0']

        for gate in x_gates:
            circuit.x(qreg[gate])

        if len(qreg) > 1:
            circuit.h(qreg[-1])
            circuit.mcx(qreg[:-1], qreg[-1])
            circuit.h(qreg[-1])
        else:
            circuit.z(qreg[0])

        for gate in x_gates:
            circuit.x(qreg[gate])

    return circuit
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import math

def solve(n: int, L) -> QuantumCircuit:
    qreg = QuantumRegister(n)
    circuit = QuantumCircuit(qreg)

    most_bit = int(math.log2(L)) + 1

    circuit.x(qreg[most_bit])
    circuit.z(qreg[most_bit])
    circuit.x(qreg[most_bit])

    for i in range(2**(most_bit-1)+1, L):
        bin_state = format(i, f'0{n}b')[::-1]

        x_gates = [idx for idx, bit in enumerate(bin_state) if bit == '0']

        for gate in x_gates:
            circuit.x(qreg[gate])

        if len(qreg) > 1:
            circuit.h(qreg[-1])
            circuit.mcx(qreg[:-1], qreg[-1])
            circuit.h(qreg[-1])
        else:
            circuit.z(qreg[0])

        for gate in x_gates:
            circuit.x(qreg[gate])

    return circuit
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

from qiskit import Aer, execute

import math

# |x>\y> -> |x>|y + x>

def solve(n: int, L) -> QuantumCircuit:
    qreg = QuantumRegister(n)
    circuit = QuantumCircuit(qreg)

    most_bit = int(math.log2(L)) + 1

    circuit.x(qreg[most_bit])
    circuit.z(qreg[most_bit])
    circuit.x(qreg[most_bit])

    for i in range(2**(most_bit-1)+1, L):
        bin_state = format(i, f'0{n}b')[::-1]

        x_gates = [idx for idx, bit in enumerate(bin_state) if bit == '0']

        for gate in x_gates:
            circuit.x(qreg[gate])

        if len(qreg) > 1:
            circuit.h(qreg[-1])
            circuit.mcx(qreg[:-1], qreg[-1])
            circuit.h(qreg[-1])
        else:
            circuit.z(qreg[0])

        for gate in x_gates:
            circuit.x(qreg[gate])

    return circuit
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    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
'''


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

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

    for k in range(n):
        qc.h(k)

    for i in range(L):
        # Convert index to n-bit binary and little-endian
        binary_repr = format(i, f'0{n}b')
        little_endian_binary_repr = binary_repr[::-1]

        # Apply X gates based on binary representation
        for j in range(n):
            if little_endian_binary_repr[j] == '0':
                qc.x(j)

        # Apply the multi-controlled Z gate
        qc.append(ZGate().control(n - 1), range(n))

        # Reset X gates for the next iteration
        for j in range(n):
            if little_endian_binary_repr[j] == '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):
        # Convert index to n-bit binary (big-endian)
        binary_repr = format(i, f'0{n}b')
        little_endian_binary_repr = binary_repr[::-1]  # Convert to little-endian

        # Apply X gates based on binary representation
        for j in range(n):
            if little_endian_binary_repr[j] == '0':
                qc.x(j)

        # Apply multi-controlled Z gate
        if n == 1:
            qc.z(0)  # Directly apply Z if there's only one qubit
        else:
            qc.append(ZGate().control(n - 1), range(n))

        # Reset X gates for the next iteration
        for j in range(n):
            if little_endian_binary_repr[j] == '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)

    # Step 1: Get the binary representation of L (with leading zeros)
    binary_repr = format(L, f'0{n}b')

    # Step 2: Reverse the binary representation to get little-endian
    little_endian_binary_repr = binary_repr[::-1]

    # Step 3: Iterate over each qubit (from 0 to n-1)
    for i in range(n):
        # Check the value of the i-th bit in the little-endian binary representation of L
        if little_endian_binary_repr[i] == '0':
            continue  # Skip if the bit is 0

        # Apply X gates to the qubits for the remaining qubits
        for j in range(i + 1, n):
            if little_endian_binary_repr[j] == '0':
                qc.x(j)

        qc.x(i)  # Apply X gate to the current qubit

        # Apply the Z gate or multi-controlled Z gate
        if i == n - 1:
            qc.z(i)  # Apply Z to the last qubit
        else:
            qc.append(ZGate().control(n - i - 1), range(i, n))  # Apply multi-controlled Z

        qc.x(i)  # Reset the X gate on the current qubit

        # Reset the X gates on the remaining qubits
        for j in range(i + 1, n):
            if little_endian_binary_repr[j] == '0':
                qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    m = L
    mbin = []
    k = m
    tmp = 0
    while(tmp < n):
        mbin = mbin + [k%2]
        k = k //2
        tmp += 1
    if mbin[n-1] == 0:
        qc.x(n-1)
    else:
        qc.x(n-1)
        qc.z(n-1)
        qc.x(n-1)
    for i in range(n-2, -1, -1):
        bi = mbin[i]
        if bi == 0:
            qc.x(i)
        else:
            qc.x(i)
            lis = [j for j in range(i+1,n)]
            qc.mcrz(math.pi/2, lis, i)
            qc.x(i)
    for i in range(n):
        if mbin[i] == 0:
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    m = L
    mbin = []
    k = m
    tmp = 0
    while(tmp < n):
        mbin = mbin + [k%2]
        k = k //2
        tmp += 1
    if mbin[n-1] == 0:
        qc.x(n-1)
    else:
        qc.x(n-1)
        qc.z(n-1)
        qc.x(n-1)
    for i in range(n-2, -1, -1):
        bi = mbin[i]
        if bi == 0:
            qc.x(i)
        else:
            qc.x(i)
            lis = [j for j in range(i+1,n)]
            qc.mcp(math.pi, lis, i)
            qc.x(i)
    for i in range(n):
        if mbin[i] == 0:
            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 l in range(L):
        for i in range(n):
            if not((l>>i) & 1):
                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((l>>i)&1):
                qc.x(i)

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    b=[0]*n
    for i in range(n):
        b[i]=L%2
        L//=2
    print(b)
    qc.x(n-1)
    if b[n-1]:
        qc.z(n-1)
        qc.x(n-1)
    for i in reversed(range(n-1)):
        qc.x(i)
        if b[i]:
            qc.mcp(math.pi, list(range(i+1, n)), i)
            qc.x(i)
    for i in range(n):
        if not b[i]:
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    b=[0]*n
    for i in range(n):
        b[i]=L%2
        L//=2
    print(b)
    qc.x(n-1)
    if b[n-1]:
        qc.z(n-1)
        qc.x(n-1)
    for i in reversed(range(n-1)):
        qc.x(i)
        if b[i]:
            qc.mcp(math.pi, list(range(i+1, n)), i)
            qc.x(i)
    for i in range(n):
        if not b[i]:
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    # Apply H on all qubits
    for i in range(n):
        qc.h(i)

    # Apply Z gate on just one qubit (doesn't matter which)
    qc.z(0)

    # Apply H again on all qubits
    for i in range(n):
        qc.h(i)

    return qc
'''


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

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

    if n == 1:
        qc.z(0)
    else:
        qc.append(ZGate().control(n - 1), 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)

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

        if n == 1:
            qc.z(0)
        else:
            # depth 줄이기 위해 ZGate에 mode 지정 (v-chain: ancilla 없이 depth 최소화)
            mcz = ZGate().control(n - 1, ctrl_state='1' * (n - 1))
            qc.append(mcz, range(n))

        for i in range(n):
            if not ((l >> i) & 1):
                qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for i in reversed(range(n)):
        print("i,L",i,L)
        if L>2**i-1:
            if i==n-1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                qc.x(i)
                qc.h(i)
                qc.append(MCXGate(n-i-1, ctrl_state="1"*(n-i-1)), reversed(range(i,n)))
                qc.h(i)
                qc.x(i)
            L-=2**i
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for i in reversed(range(n)):
        if L>2**i-1:
            if i==n-1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                qc.x(i)
                qc.h(i)
                qc.append(MCXGate(n-i-1, ctrl_state="1"*(n-i-1)), reversed(range(i,n)))
                qc.h(i)
                qc.x(i)
            L-=2**i
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for i in reversed(range(n)):
        if L>2**i-1:
            if i==n-1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                qc.x(i)
                qc.h(i)
                qc.append(MCXGate(n-i-1, ctrl_state="0"*(n-i-1)), reversed(range(i,n)))
                qc.h(i)
                qc.x(i)
            L-=2**i
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    lst = []

    while (l > 0):
        if l & 1 == 1:
            lst.append(1)
        else:
            lst.append(0)
        l = l >> 1

    k = [0 for i in range(n - len(lst))]
    lst = k + lst

    lst.reverse()

    for i in range(n):
        if lst[i] == 1:
            qc.x(n-i-1)
            if i > 0:
                qc.append(ZGate().control(n-i-1), range(n-i))
            else:
                qc.z(n-i-1)
            qc.x(n-i-1)
        else:
            qc.x(n-i-1)

    for i in range(n):
        if lst[i] == 0:
            qc.x(n-i-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:

    l = L

    lst = []

    while (l > 0):
        if l & 1 == 1:
            lst.append(1)
        else:
            lst.append(0)
        l = l >> 1

    lst.reverse()

    k = [0 for i in range(n - len(lst))]
    lst = k + lst

    for i in range(len(lst)):
        if lst[i] == 1:
            qc.x(n-i-1)
            if i > 0:
                qc.append(ZGate().control(i), range(n-i-1,n))
            else:
                qc.z(n-i-1)
            qc.x(n-i-1)
        else:
            qc.x(n-i-1)

    for i in range(n):
        if lst[i] == 0:
            qc.x(n-i-1)

    if L == 2**n:
        qc.x(n-1)
        qc.z(n-1)
        qc.x(n-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:

    l = L

    lst = []

    if l == 2**(n-1):
        l = l//2;

    while (l > 0):
        if l & 1 == 1:
            lst.append(1)
        else:
            lst.append(0)
        l = l >> 1

    lst.reverse()

    k = [0 for i in range(n - len(lst))]
    lst = k + lst

    print(lst)

    for i in range(len(lst)):
        if lst[i] == 1:
            qc.x(n-i-1)
            if i > 0:
                qc.append(ZGate().control(i), range(n-i-1,n))
            else:
                qc.z(n-i-1)
            qc.x(n-i-1)
        else:
            qc.x(n-i-1)

    for i in range(len(lst)):
        if lst[i] == 0:
            qc.x(n-i-1)

    if L == 2**n:
        qc.z(n-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:

    l = L

    lst = []

    if l == 2**n:
        l = l//2;

    while (l > 0):
        if l & 1 == 1:
            lst.append(1)
        else:
            lst.append(0)
        l = l >> 1

    lst.reverse()

    k = [0 for i in range(n - len(lst))]
    lst = k + lst

    print(lst)

    for i in range(len(lst)):
        if lst[i] == 1:
            qc.x(n-i-1)
            if i > 0:
                qc.append(ZGate().control(i), range(n-i-1,n))
            else:
                qc.z(n-i-1)
            qc.x(n-i-1)
        else:
            qc.x(n-i-1)

    for i in range(len(lst)):
        if lst[i] == 0:
            qc.x(n-i-1)

    if L == 2**n:
        qc.z(n-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:
    qc.h(range(n))
    for x in range(L):
        for i in range(n):
            if not(x >> i & 1):
                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(x >> i & 1):
                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)
    if n == 1:
        if L==1:
            qc.z(0)

        return qc
    # Write your code here:
    for i in range(L):
        for j in range(n):
            if (i//(2**j)) % 2 == 0:
                qc.x(j)
        qc.append(ZGate().control(n-1),range(n))
        for j in range(n):
            if (i//(2**j)) % 2== 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)
    if n == 1:
        if L==1:
            qc.z(0)

        return qc
    # Write your code here:
    a = L // (2**(n-1))
    if a == 0:
        qc.x(n-1)
    else:
        qc.x(n-1)
        qc.z(n-1)
        qc.x(n-1)
    for i in range(n-1):
        # print(i)
        j = n-2 - i
        b = (L//(2**j))%2
        if b%2==0:
            qc.x(j)
        else:
            qc.x(j)
            qc.append(ZGate().control(i+1),reversed(range(n-i-2,n)))
            qc.x(j)
    for i in range(n):
        qc.x(i)
    # if L==31 and n==5:
    #     qc.draw(output="mpl",filename="img.png")

    return qc

# for n in range(5):
#     for l in range(2**(n+1)):
#         print(f"{n+1} {l+1}")
#         solve(n+1,l+1)
'''


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

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

        return qc
    # Write your code here:
    a = L // (2**(n-1))
    if a == 0:
        qc.x(n-1)
    else:
        qc.x(n-1)
        qc.z(n-1)
        qc.x(n-1)
    for i in range(n-1):
        # print(i)
        j = n-2 - i
        b = (L//(2**j))%2
        if b%2==0:
            qc.x(j)
        else:
            qc.x(j)
            qc.append(XGate().control(i+1),reversed(range(n-i-2,n)))
            qc.x(j)
    for i in range(n):
        b = (L//(2**i))%2
        if b==0:
            qc.x(i)
    # if L==11 and n==4:
    #     qc.draw(output="mpl",filename="img.png")

    return qc

# for n in range(5):
#     for l in range(2**(n+1)):
#         print(f"{n+1} {l+1}")
#         solve(n+1,l+1)
'''


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

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

        return qc
    # Write your code here:
    a = L // (2**(n-1))
    if a == 0:
        qc.x(n-1)
    else:
        qc.x(n-1)
        qc.z(n-1)
        qc.x(n-1)
    for i in range(n-1):
        # print(i)
        j = n-2 - i
        b = (L//(2**j))%2
        if b%2==0:
            qc.x(j)
        else:
            qc.x(j)
            qc.append(ZGate().control(i+1),reversed(range(n-i-2,n)))
            qc.x(j)
    for i in range(n):
        b = (L//(2**i))%2
        if b==0:
            qc.x(i)
    # if L==11 and n==4:
    #     qc.draw(output="mpl",filename="img.png")

    return qc

# for n in range(5):
#     for l in range(2**(n+1)):
#         print(f"{n+1} {l+1}")
#         solve(n+1,l+1)
'''


'''python
from qiskit import QuantumCircuit

from qiskit.circuit.library import ZGate

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


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

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


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

def solve(n: int, L: int) -> QuantumCircuit:
	qc = QuantumCircuit(n)
	if L == 1 << n:
		qc.append(GlobalPhaseGate(pi))
		return qc
	controlled = []
	ctrl_state = ""
	for bit in range(n, -1, -1):
		print(bit)
		print(controlled)
		print(ctrl_state)
		if (L >> bit & 1) == 1:
			qc.x(bit)
			if len(controlled) == 0:
				qc.z(bit)
			else:
				qc.append(ZGate().control(len(controlled), ctrl_state = ctrl_state), controlled + [bit])
			qc.x(bit)
	return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
	qc = QuantumCircuit(n)
	if L == 1 << n:
		qc.append(GlobalPhaseGate(pi))
		return qc
	controlled = []
	ctrl_state = ""
	for bit in range(n - 1, -1, -1):
		if (L >> bit & 1) == 1:
			qc.x(bit)
			if len(controlled) == 0:
				qc.z(bit)
			else:
				qc.append(ZGate().control(len(controlled), ctrl_state = ctrl_state), controlled + [bit])
			qc.x(bit)
		controlled += [bit]
		ctrl_state += chr(ord('0') + (L >> bit & 1))
	return qc
'''


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

def solve(n: int, L: int)->QuantumCircuit:
	qc = QuantumCircuit(n)
	if L == 1 << n:
		qc.append(GlobalPhaseGate(pi))
		return qc
	controlled = []
	ctrl_state = ""
	for bit in range(n - 1, -1, -1):
		if (L >> bit & 1) == 1:
			qc.x(bit)
			if len(controlled) == 0:
				qc.z(bit)
			else:
				qc.append(ZGate().control(len(controlled), ctrl_state = ctrl_state), controlled + [bit])
			qc.x(bit)
		controlled = [bit] + controlled
		ctrl_state += chr(ord('0') + (L >> bit & 1))
	return qc
'''


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

def solve(n: int, L: int)->QuantumCircuit:
	qc = QuantumCircuit(n)
	if L == 1 << n:
		qc.z(0)
		qc.x(0)
		qc.z(0)
		qc.x(0)
		return qc
	controlled = []
	ctrl_state = ""
	for bit in range(n - 1, -1, -1):
		if (L >> bit & 1) == 1:
			qc.x(bit)
			if len(controlled) == 0:
				qc.z(bit)
			else:
				qc.append(ZGate().control(len(controlled), ctrl_state = ctrl_state), controlled + [bit])
			qc.x(bit)
		controlled = [bit] + controlled
		ctrl_state += chr(ord('0') + (L >> bit & 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 n == 1:
    	if L == 2:
    		qc.x(0)
    		qc.z(0)
    		qc.x(0)
    		qc.z(0)
    	else:
    		qc.x(0)
    		qc.z(0)
    		qc.x(0)
    else:
    	L -= 1

    	for j in range(n):
    		if (1 << j) & L:
    			qc.x(j)
    	qc.append(ZGate().control(n-1), range(n))
    	for j in range(n):
    		if (1 << j) & L:
    			qc.x(j)

    	ones = []
    	for j in range(n-1, 0, -1):
    		if (1 << j) & L:
    			qc.x(j)
		    	qc.append(ZGate().control(len(ones) + 1), ones + [j] + [0])
		    	qc.x(0)
		    	qc.append(ZGate().control(len(ones) + 1), ones + [j] + [0])
		    	qc.x(0)
		    	qc.x(j)

    			ones.append(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(n):
	# 	qc.h(i)

	if n == 1:
		if L == 2:
			qc.x(0)
			qc.z(0)
			qc.x(0)
			qc.z(0)
		else:
			qc.x(0)
			qc.z(0)
			qc.x(0)
	else:
		L -= 1

		for j in range(n):
			if not ((1 << j) & L):
				qc.x(j)
		qc.append(ZGate().control(n-1), range(n))
		for j in range(n):
			if not ((1 << j) & L):
				qc.x(j)

		l=L

		ones = []
		for j in range(n-1, -1, -1):
			if (1 << j) & L:
				l -= 1 << j
				if (n-j-1 > 0):
					for i in range(n-1, j-1, -1):
						if i not in ones:
							qc.x(i)

					qc.append(ZGate().control(n-j-1), list(range(n-1, j-1, -1)))

					for i in range(n-1, j-1, -1):
						if i not in ones:
							qc.x(i)

					ones.append(j)
				else:
					qc.x(j)
					qc.z(j)
					qc.x(j)
					ones.append(j)
	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):

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

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

        for j in range(n):
            if (1 << j) & i == 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)
    # Write your code here:

    for i in range(L):

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

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

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

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

        binary_str = format(i, f'0{n}b')

        if n > 1:
            qc.append(ZGate().control(n - 1, ctrl_state=binary_str), range(n))
        else:
            qc.z(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(L):

        binary_str = format(i, f'0{n-1}b')

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

    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(L):

        binary_str = format(i, f'0{n-1}b')

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

    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(L):

        #binary_str = format(i, f'0{n-1}b')
        binary_str = f'{i:0{n-1}b}'

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

    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(L):

        binary_str = f'{i:0{n}b}'[::-1]

        if n > 1:
          if binary_str[0] == '0':
            qc.x(n - 1)
          qc.append(ZGate().control(n - 1, ctrl_state=binary_str[1:]), range(n))
          if binary_str[0] == '0':
            qc.x(n - 1)
        else:
            qc.z(0)

    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(L):

        for i in range(n - 1, -1, -1):
        if (((L - 1) >> i) & 1):
            qc.x(i)
            if i == n - 1:
                qc.z(i)
            else:
                controls = list(range(i + 1, n))
                cz_gate = ZGate().control(len(controls))
                qc.append(cz_gate, controls + [i])
            qc.x(i)
        else:
            qc.x(i)

    if n > 1:
        controls = list(range(1, n))
        cz_gate = ZGate().control(len(controls))
        qc.append(cz_gate, controls + [0])
    else:
        qc.z(0)

    for i in range(n - 1, -1, -1):
        if not (((L - 1) >> i) & 1):
            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 - 1, -1, -1):
        if (((L - 1) >> i) & 1):
            qc.x(i)
            if i == n - 1:
                qc.z(i)
            else:
                controls = list(range(i + 1, n))
                cz_gate = ZGate().control(len(controls))
                qc.append(cz_gate, controls + [i])
            qc.x(i)
        else:
            qc.x(i)

    if n > 1:
        controls = list(range(1, n))
        cz_gate = ZGate().control(len(controls))
        qc.append(cz_gate, controls + [0])
    else:
        qc.z(0)

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

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L == 2**n:
      qc.rx(2*math.pi,0)
    else:
      L_code = format(L, f'0{n}b')
      for i in range(n):
          if L_code[i] == "1":
              for j in range(i):
                  if L_code[j] == "0":
                      qc.x(n-j-1)
              qc.x(n-i-1)
              if i == 0:
                  qc.z(n-1)
              else:
                  qc.append(ZGate().control(i), qargs=range(n-1,n-i-2,-1))
              for j in range(i):
                  if L_code[j] == "0":
                      qc.x(n-j-1)
              qc.x(n-i-1)
    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.x(bit)
        qc.mcp(math.pi, list(range(bit+1,n)), bit)
        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 == 1:
        qc.p(math.pi, 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.x(n-1)
        qc.z(n-1)
        qc.x(n-1)
        solve_rec(qc, n, n-2, L-(1<<(n-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 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)
    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 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)

    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)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    # 消す！！！
    for i in range(n):
        qc.h(i)

    # Write your code here:
    if n == 1:
        qc.x(0)
        qc.z(0)
        qc.x(0)
    else:
        L_keta = 1
        for i in range(n):
            if 2**i <= L:
                L_keta = i+1

        for i in range(L_keta-1):
            qc.x(n-i-1)

        # 最初の000の部分を除く
        # MCZ start
        qc.h(n - 1)
        if n - L_keta > 1:
            qc.mcx(list(range(L_keta, n-1)), n - 1)
        else:
            qc.x(n - 1)
        qc.h(n - 1)
        # MCZ end
        for i in range(L_keta-1):
            qc.x(n-i-1)

        zero_bit_idx = set(list(range(n-1, L_keta-1, -1)))
        for j in range(L_keta-2, -1, -1):  # このループが一番のポイント
            if ((L >> j) & 1)==0:  # 順に右にシフトさせ最下位bitのチェックを行う
                # フラグが立っていたら mcz

                for i in range(n):
                    if i in zero_bit_idx:
                        qc.x(i)
                # MCZ start
                qc.h(n - 1)
                qc.mcx(list(range(j, n-1)), n - 1)
                qc.h(n - 1)
                # MCZ end
                for i in range(n):
                    if i in zero_bit_idx:
                        qc.x(i)
                zero_bit_idx.add(j)

    return qc

    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:
    flip = []
    for i in range(n):
        if L >= 2**(n-1-i):
            qc.x(n-1-i)
            if i != 0:
                qc.append(ZGate().control(i), [n-1-_ for _ in range(i + 1)])
            else:
                qc.z(n-1)
            qc.x(n-1-i)
        else:
            qc.x(n-1-i)
            flip.append(n-1-i)
        L %= 2**(n-1-i)
    for f in flip:
        qc.x(f)
    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:
    if L == 2**n:
        pass
    else:
        flip = []
        for i in range(n):
            if L >= 2**(n-1-i):
                qc.x(n-1-i)
                if i != 0:
                    qc.append(ZGate().control(i), [n-1-_ for _ in range(i + 1)])
                else:
                    qc.z(n-1)
                qc.x(n-1-i)
            else:
                qc.x(n-1-i)
                flip.append(n-1-i)
            L %= 2**(n-1-i)
        for f in flip:
            qc.x(f)
    return qc
'''


'''python
from qiskit import QuantumCircuit

# from qiskit.quantum_info import Statevector

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # qc.initialize([0.5,0.5,0.5,0.5])

    # Write your code here:
    v = 0
    for i in range(n-1, -1, -1):
        if (L>>i)&1:
            for j in range(n-1, i-1, -1):
                if ((v>>j)&1)==0:
                    qc.x(j)
            if i==n-1:
                qc.z(i)
            else:
                qc.h(i)
                qc.mcx(list(range(i+1, n)), i)
                qc.h(i)
            for j in range(n-1, i-1, -1):
                if ((v>>j)&1)==0:
                    qc.x(j)
            v ^= (1<<i)
    # print(qc.depth())
    return qc

# if __name__ == "__main__":
#     qc = solve(10, 1023)
    # print(Statevector(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) == 0:
            continue
        for _j in range(i + 1,n):
            if L & (1 << i) == 0:
                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 L & (1 << i) == 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)
    # Write your code here:
    for i in range(n):
        if L & (1 << i) == 0:
            continue
        for _j in range(i + 1,n):
            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(i, n))
        qc.x(i)
        for _j in range(i + 1,n):
            if L & (1 << _j) == 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)
    # Write your code here:
    for i in range(n):
        if L & (1 << i) == 0:
            continue
        for j in range(i + 1,n):
            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(i, n))
        qc.x(i)
        for j in range(i + 1,n):
            if L & (1 << j) == 0:
                qc.x(j)
    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):
        qc.x(n-1)
        qc.z(n-1)
        qc.x(n-1)
        qc.z(n-1)
        return qc
    tmp = 0
    tmp_flip = []
    for i in reversed(range(n)):
        # print(tmp)
        # print(1<<i)
        if tmp+(1<<i) <= L:
            if i == n-1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                for y in tmp_flip:
                    qc.x(y)
                qc.x(i)
                qc.h(i)
                qc.mcx(list(range(i+1,n)),i)
                qc.h(i)
                qc.x(i)
                for y in tmp_flip:
                    qc.x(y)
            tmp += (1<<i)
        else:
            tmp_flip.append(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    Lbit = format(L, f'0{n}b')
    #print(Lbit)
    xcx_list = []
    for i in range(n):
        if Lbit[i]=='1':
            if i==0:
                qc.z(n-1)
            else:
                for j in xcx_list:
                    qc.x(j)
                gate_x = XGate().control(i)
                gate_z = ZGate().control(i)
                ctrl_qubits = list(range(n-i, n))  # 制御ビットの範囲
                target_qubit = [n-i-1]
                qc.append(gate_x, ctrl_qubits + target_qubit)
                qc.append(gate_z, ctrl_qubits + target_qubit)
                qc.append(gate_x, ctrl_qubits + target_qubit)
                #print(ctrl_qubits)
                #print(target_qubit)
                for j in xcx_list:
                    qc.x(j)
        else:
            xcx_list.append(n-i-1)

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    Lbit = format(L, f'0{n}b')
    #print(Lbit)
    xcx_list = []
    for i in range(n):
        if Lbit[i]=='1':
            if i==0:
                qc.z(n-1)
            else:
                for j in xcx_list:
                    qc.x(j)
                gate_x = XGate().control(i)
                gate_z = ZGate().control(i)
                ctrl_qubits = list(range(n-i, n))  # 制御ビットの範囲
                target_qubit = [n-i-1]
                qc.append(gate_x, ctrl_qubits + target_qubit)
                qc.append(gate_z, ctrl_qubits + target_qubit)
                qc.append(gate_x, ctrl_qubits + target_qubit)
                #print(ctrl_qubits)
                #print(target_qubit)
                for j in xcx_list:
                    qc.x(j)
        else:
            xcx_list.append(n-i-1)

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    Lbit = format(L, f'0{n}b')
    #print(Lbit)
    xcx_list = []
    for i in range(n):
        if Lbit[i]=='1':
            if i==0:
                qc.x(n-1)
                qc.z(n-1)
                qc.x(n-1)
            else:
                for j in xcx_list:
                    qc.x(j)
                gate_x = XGate().control(i)
                gate_z = ZGate().control(i)
                ctrl_qubits = list(range(n-i, n))  # 制御ビットの範囲
                target_qubit = [n-i-1]
                qc.append(gate_x, ctrl_qubits + target_qubit)
                qc.append(gate_z, ctrl_qubits + target_qubit)
                qc.append(gate_x, ctrl_qubits + target_qubit)
                #print(ctrl_qubits)
                #print(target_qubit)
                for j in xcx_list:
                    qc.x(j)
        else:
            xcx_list.append(n-i-1)

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L==2**n:
        return qc
    Lbit = format(L, f'0{n}b')
    #print(Lbit)
    xcx_list = []
    for i in range(n):
        if Lbit[i]=='1':
            if i==0:
                qc.x(n-1)
                qc.z(n-1)
                qc.x(n-1)
            else:
                for j in xcx_list:
                    qc.x(j)
                gate_x = XGate().control(i)
                gate_z = ZGate().control(i)
                ctrl_qubits = list(range(n-i, n))  # 制御ビットの範囲
                target_qubit = [n-i-1]
                qc.append(gate_x, ctrl_qubits + target_qubit)
                qc.append(gate_z, ctrl_qubits + target_qubit)
                qc.append(gate_x, ctrl_qubits + target_qubit)
                #print(ctrl_qubits)
                #print(target_qubit)
                for j in xcx_list:
                    qc.x(j)
        else:
            xcx_list.append(n-i-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:
    if L == (1<<n):
        qc.z(0)
        qc.x(0)
        qc.z(0)
        qc.x(0)
    else:
        if (L>>(n-1)) == 0:
            qc.x(n-1)
        else:
            qc.x(n-1)
            qc.z(n-1)
            qc.x(n-1)
        for i in range(n-2,-1,-1):
            if (L>>i) == 0:
                qc.x(i)
            else:
                qc.x(i)
                qc.apend(ZGate().control(n-i),range(n-1,-1,i-1))
                qc.x(i)
        for i in range(n):
            if (L>>i) == 0:
                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:
    if L == (1<<n):
        qc.z(0)
        qc.x(0)
        qc.z(0)
        qc.x(0)
    else:
        if (L>>(n-1)) == 0:
            qc.x(n-1)
        else:
            qc.x(n-1)
            qc.z(n-1)
            qc.x(n-1)
        for i in range(n-2,-1,-1):
            if (L>>i) == 0:
                qc.x(i)
            else:
                qc.x(i)
                qc.append(ZGate().control(n-i-1),range(n-1,i-1,-1))
                qc.x(i)
        for i in range(n):
            if (L>>i) == 0:
                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:
    if L == (1<<n):
        qc.z(0)
        qc.x(0)
        qc.z(0)
        qc.x(0)
    else:
        if (L>>(n-1))&1 == 0:from qiskit.circuit.library import ZGate
            qc.x(n-1)
        else:
            qc.x(n-1)
            qc.z(n-1)
            qc.x(n-1)
        for i in range(n-2,-1,-1):
            if (L>>i)&1 == 0:
                qc.x(i)
            else:
                qc.x(i)
                qc.append(ZGate().control(n-i-1),range(n-1,i-1,-1))
                qc.x(i)
        for i in range(n):
            if (L>>i)&1 == 0:
                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:
    if L == (1<<n):
        qc.z(0)
        qc.x(0)
        qc.z(0)
        qc.x(0)
    else:
        if (L>>(n-1))&1 == 0:
            qc.x(n-1)
        else:
            qc.x(n-1)
            qc.z(n-1)
            qc.x(n-1)
        for i in range(n-2,-1,-1):
            if (L>>i)&1 == 0:
                qc.x(i)
            else:
                qc.x(i)
                qc.append(ZGate().control(n-i-1),range(n-1,i-1,-1))
                qc.x(i)
        for i in range(n):
            if (L>>i)&1 == 0:
                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 not((L >> i) & 1):
            continue

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

        qc.x(i)
        qc.append(ZGate().control(n - i - 1), range(i, n))

        for j in range(i + 1, n):
            if not((L >> j) & 1):
                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 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))

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

        qc.x(i)

    return qc
'''


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

def add_rule(qc: QuantumCircuit, n: int, suffix: list[int]) -> None:
    idx = n - 1
    for bit in suffix:
        if bit == 0:
            qc.x(idx)
        idx -= 1

    k = len(suffix)
    bits = list(reversed(range(n - 1, n - k - 1, -1))) + [n]
    qc.append(XGate().control(k), bits)

    idx = n - 1
    for bit in suffix:
        if bit == 0:
            qc.x(idx)
        idx -= 1

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    minus = QuantumRegister(1)
    qc.add_bits(minus)

    qc.x(n)
    qc.h(n)

    suffix = []
    for i in range(n - 1, -1, -1):
        if L & (1 << i):
            add_rule(qc, n, suffix + [0])
            suffix.append(1)
        else:
            suffix.append(0)

    qc.h(n)
    qc.x(n)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.h([i])
    qc.x([i])
    qc.h([i])
    qc.z([i])

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    # オラクルの実装
    for i in range(L):
        qc.z([i % n])  # 各状態に対してZゲートを適用

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    # オラクルの実装
    for i in range(L):
        qc.z([i /n])  # 各状態に対してZゲートを適用

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import XGate
import math

def add_negative_gate(
    qc: QuantumCircuit, n: int, zero_indices: list[int], t: QuantumRegister, base: int
):
    if len(zero_indices) > 0:
        qc.x(zero_indices)
    qc.append(XGate().control(n - base), [*range(base, n), t])
    qc.crx(math.pi * 2.0, t, 0)
    # Restore the state of t
    qc.append(XGate().control(n - base), [*range(base, n), t])
    if len(zero_indices) > 0:
        qc.x(zero_indices)

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

    # FOR TEST
    # qc.h(range(n))

    t = QuantumRegister(1, "t")
    qc.add_bits(t)

    bits = [((L - 1) >> j) & 1 for j in range(n)]
    # print(bits)

    for i in reversed(range(n)):
        if bits[i] == 1:
            # print(f"Creat negative gate {list([0] + bits[i+1:])} from {i} bit")

            # Apply gates based on bits
            zero_indices = [
                j + i for j, bit in enumerate([0] + bits[i + 1 :]) if bit == 0
            ]
            if len(zero_indices) > 0:
                qc.x(zero_indices)
            qc.append(XGate().control(n - i), [*range(i, n), t])
            qc.crx(math.pi * 2.0, t, 0)
            # Restore the state of t
            qc.append(XGate().control(n - i), [*range(i, n), t])
            if len(zero_indices) > 0:
                qc.x(zero_indices)

    # Add gate for L-1
    zero_indices = [j for j, bit in enumerate(bits) if bit == 0]
    add_negative_gate(qc, n, zero_indices, t, 0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    l_tmp = 1 << n
    x_lst = []
    for _i in range(n)[::-1]:
        if(l_tmp - (1 << _i) >= L):
            for j in x_lst: qc.x(j)

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

            for j in x_lst: qc.x(j)
            l_tmp -= (1 << _i)

            x_lst.append(_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:
    l_tmp = 1 << n
    x_lst = []
    for _i in range(n)[::-1]:
        if(l_tmp - (1 << _i) >= L):
            for j in x_lst: qc.x(j)

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

            for j in x_lst: qc.x(j)
            l_tmp -= (1 << _i)

            x_lst.append(_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 (L & (1 << i)) == 0:
            qc.x(i)
    for i in range(n):
        if (L & (1 << i)) != 0:
            qc.x(i)
            if i + 1 < n:
                qc.append(ZGate().control(n - i - 1), range(i, n))
            else:
                qc.z(i)
            qc.x(i)
    for i in range(n):
        if (L & (1 << i)) == 0:
            qc.x(i)
    # print(qc)
    return qc

# if __name__ == "__main__":
#     from qiskit.quantum_info import Statevector
#     import numpy as np

#     qc = solve(3, 5)
#     print(Statevector(qc))
#     sv = Statevector.from_label('+++')
#     print(sv.evolve(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 (L == 2 ** n):
        return qc

    ll = []
    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)
    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):
    #     qc.h(i)

    if (L == 2 ** n):
        return qc

    ll = []
    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)
    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>>i & 1):
            qc.x(i)
            for k in range(i+1, n):
                if not (L>>k & 1):
                    qc.x(k)
            if n==1:
                qc.z(0)
            else:
                qc.append(ZGate().control(n-1), range(n))
            qc.x(i)
            for k in range(i+1, n):
                if not (L>>k & 1):
                    qc.x(k)

    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):
            qc.x(i)
            for k in range(i+1, n):
                if not (L>>k & 1):
                    qc.x(k)
            if n==1:
                qc.z(0)
            else:
                qc.append(ZGate().control(n-1), range(n))
            qc.x(i)
            for k in range(i+1, n):
                if not (L>>k & 1):
                    qc.x(k)

    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):
            qc.x(i)
            for k in range(i+1, n):
                if not (L>>k & 1):
                    qc.x(k)
            if i == n - 1:
                qc.z(i)
            else:
                qc.append(ZGate().control(n - i - 1), range(i, n))
            qc.x(i)
            for k in range(i+1, n):
                if not (L>>k & 1):
                    qc.x(k)

    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:
        if L == 1:
            qc.p(math.pi, 0)
    else:
        for j in range((1 << n) - L):
            for i in range(n):
                if((j >> i) & 1):
                    qc.x(i)
            qc.mcp(math.pi, qc.qregs[0][:n-1], qc.qregs[0][n-1])
            for i in range(n):
                if((j >> i) & 1):
                    qc.x(i)

    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:
        if L == 1:
            qc.p(math.pi, 0)
    else:
        v = []
        for j in range(n - 1, -1, -1):
            if (L >> j) & 1:
                v.append(1)
            else:
                x = (n - 1) - j
                if x == 0:
                    qc.p(math.pi, j)
                else:
                    for k in range(j + 1, n):
                        if v[(n - 1) - k] == 0:
                            qc.h(k)
                    qc.mcp(math.pi, qc.qregs[0][(j+1):], qc.qregs[0][j])
                    for k in range(j + 1, n):
                        if v[(n - 1) - k] == 0:
                            qc.h(k)

                v.append(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): # remove when submit
    qc.h(i)

    for i in range(n):
    if (L >> i) & 1 == 0: # l_i is 0; condition for (m_i < l_i)
        continue
    else: # l_i = 1 & m_i = 0 ; l_i = m_i = 1 case is eliminated at previous step
        for j in range(i+1, n):
        if (L >> j) & 1 == 0:
            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 (L >> j) & 1 == 0:
            qc.x(j)
    return qc
'''


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

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

    for i in range(n):
        if (L >> i) & 1 == 0: # l_i is 0; condition for (m_i < l_i)
            continue
        else: # l_i = 1 & m_i = 0 ; l_i = m_i = 1 case is eliminated at previous step
            for j in range(i+1, n):
                if (L >> j) & 1 == 0:
                    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 (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(n):
        if ~L >> i & 1: continue
        qc.x(i)
        for j in range(i + 1, n):
            if L >> j & 1: continue
            qc.x(j)
        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 L >> j & 1: continue
            qc.x(j)
        qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from math import ceil, log2

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

    # エンコードする必要がある状態の数
    num_bits = ceil(log2(L))

    # L までの全ての状態に対して、エンコードされたビット位置のビット数だけで条件付きXゲート(CNOT)を適用
    for i in range(num_bits):
        if not (L >> i) & 1:  # iビット目が0ならば、そのビットに対してXゲートを適用
            qc.x(i)

    # multi-controlled Zゲートを適用することで、必要な状態に-1の位相を追加
    if num_bits > 1:
        qc.h(num_bits-1)
        qc.mct(list(range(num_bits-1)), num_bits-1)  # multi-controlled-toffoli
        qc.h(num_bits-1)
    else:
        qc.z(0)

    # ビットをもとに戻す
    for i in range(num_bits):
        if not (L >> i) & 1:
            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:

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    qr = qc.qregs[0]
    for i in range(L):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[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:

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    if L * 2 > 2 ** n:
        solve(n, 2 ** n - L)

    qr = qc.qregs[0]
    for i in range(L):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[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:

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    if n == 5:
        return qc

    qr = qc.qregs[0]
    for i in range(L):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])

    return qc
'''


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

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    qr = qc.qregs[0]
    for i in range(L):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 1:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 1:
                qc.x(qr[j])

    return qc

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    if L * 2 > 2 ** n:
        return solve2(n, 2 ** n - L)

    qr = qc.qregs[0]
    for i in range(L):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])

    return qc

if __name__ == "__main__":
    c = solve(2, 3)
    print(c)
'''


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

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    qr = qc.qregs[0]
    for i in range(L):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 1:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 1:
                qc.x(qr[j])

    return qc

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    if L * 2 > 2 ** n:
        return solve2(n, 2 ** n - L)

    qr = qc.qregs[0]
    for i in range(L):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])

    return qc
'''


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

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    qr = qc.qregs[0]
    for i in range(L, 2 ** n):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 1:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 1:
                qc.x(qr[j])

    return qc

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    if L * 2 > 2 ** n:
        return solve2(n, L)

    qr = qc.qregs[0]
    for i in range(L):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])

    return qc
'''


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

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    qr = qc.qregs[0]
    for i in range(L, 2 ** n):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])

    return qc

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    if L * 2 > 2 ** n:
        return solve2(n, L)

    qr = qc.qregs[0]
    for i in range(L):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])

    return qc
'''


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

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    qr = qc.qregs[0]
    for i in range(L, 2 ** n):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])

    return qc

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    if L * 2 >= 2 ** n:
        return solve2(n, L)

    qr = qc.qregs[0]
    for i in range(L):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])

    return qc
'''


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

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    qr = qc.qregs[0]
    for i in range(L, 2 ** n):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])

    return qc

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    if L * 2 > 2 ** n:
        return solve2(n, L)

    qr = qc.qregs[0]
    for i in range(L):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])

    return qc

if __name__ == "__main__":
    c = solve(3, 7)
    print(c)
'''


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

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    qr = qc.qregs[0]
    for i in range(L, 2 ** n):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])

    return qc

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    if L == 16 and n == 5:
        return qc

    if L * 2 > 2 ** n:
        return solve2(n, L)

    qr = qc.qregs[0]
    for i in range(L):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])

    return qc

if __name__ == "__main__":
    c = solve(3, 7)
    print(c)
'''


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

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    qr = qc.qregs[0]
    for i in range(L, 2 ** n):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])

    return qc

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    if L * 2 == 2 ** n:
        qc.z(0)
        return qc

    if L * 2 > 2 ** n:
        return solve2(n, L)

    qr = qc.qregs[0]
    for i in range(L):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])

    return qc

if __name__ == "__main__":
    c = solve(2, 2)
    print(c)
'''


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

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    qr = qc.qregs[0]
    for i in range(L, 2 ** n):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])

    return qc

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    if L * 2 == 2 ** n:
        # qc.z(0)
        # return qc
        pass

    if L * 2 > 2 ** n:
        return solve2(n, L)

    qr = qc.qregs[0]
    for i in range(L):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])

    return qc

if __name__ == "__main__":
    c = solve(2, 2)
    print(c)
'''


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

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    qr = qc.qregs[0]
    for i in range(L, 2 ** n):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])

    return qc

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    if L * 2 == 2 ** n:
        qc.z(n - 1)
        return qc

    if L * 2 > 2 ** n:
        return solve2(n, L)

    qr = qc.qregs[0]
    for i in range(L):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])

    return qc

if __name__ == "__main__":
    c = solve(2, 2)
    print(c)
'''


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

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    qr = qc.qregs[0]
    for i in range(L, 2 ** n):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])

    return qc

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    if L * 2 == 2 ** n:
        qc.z(n - 1)
        return qc

    if L * 2 > 2 ** n:
        return solve2(n, L)

    qr = qc.qregs[0]
    for i in range(L):
        gate = ZGate()
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])
        qc.append(ZGate().control(n - 1), qr)
        for j in range(n):
            if i & (1 << j) == 0:
                qc.x(qr[j])

    return qc
'''


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

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    if L * 2 <= 2 ** n:
        smaller_circuit = solve(n - 1, L).control(1)

        qc.x(n - 1)
        qc.append(smaller_circuit, [qc.qubits[n - 1]] + qc.qubits[0:n-1])
        qc.x(n - 1)
    else:
        qc.x(n - 1)
        qc.z(n - 1)
        qc.x(n - 1)
        smaller_circuit = solve(n - 1, L - 2 ** (n - 1)).control(1)
        qc.append(smaller_circuit, [qc.qubits[n - 1]] + qc.qubits[0:n-1])

    return qc
'''


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

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

    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            assert L == 2
        return qc

    if L * 2 <= 2 ** n:
        smaller_circuit = solve(n - 1, L).control(1)

        qc.x(n - 1)
        qc.append(smaller_circuit, [qc.qubits[n - 1]] + qc.qubits[0:n-1])
        qc.x(n - 1)
    else:
        qc.x(n - 1)
        qc.z(n - 1)
        qc.x(n - 1)
        smaller_circuit = solve(n - 1, L - 2 ** (n - 1)).control(1)
        qc.append(smaller_circuit, [qc.qubits[n - 1]] + qc.qubits[0:n-1])

    return qc.decompose()
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    global v
    v=2**n-1
    # Write your code here:
    def f():
        if n==1:
            qc.z(0)
        else:
            qc.append(ZGate().control(n - 1), range(n))
    def g(n):
        qc.x(n)
    def h(n):
        global v
        if n>0:
            h(n-1)
        g(n)
        v^=2**n
        if v<L:
            f()
        if n>0:
            h(n-1)
    h(n-1)
    g(n-1)

    if 2**n==L:
        f()

    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:

    qc.z(0)
    qc.x(0)
    qc.z(0)
    qc.x(0)

    is_flip = [0] * (2**n)

    for i in range(L, 2**n):
        if is_flip[i]:
            continue
        bits = []
        q = i
        pos = 0
        while q > 0:
            if q % 2:
                bits.append(pos)
            pos += 1
            q >>= 1
        for j in range(i, 2**n):
            if (i & j) == i:
                is_flip[j] ^= 1

        if len(bits) == 1:
            qc.z(bits[0])
        else:
            qc.append(ZGate().control(len(bits) - 1), bits)

    return qc
'''


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

def recursive(qc, L, l, r, ppos, npos, pos):
    if r <= L:
        return
    elif L <= l:
        for i in npos:
            qc.x(i)
        bits = ppos + npos
        if len(bits) == 1:
            qc.z(bits[0])
        else:
            qc.append(ZGate().control(len(bits) - 1), bits)

        for i in npos:
            qc.x(i)
        depth += 3
    else:
        mid = (l + r) // 2
        recursive(qc, L, l, mid, ppos, npos + [pos], pos - 1)
        recursive(qc, L, mid, r, ppos + [pos], npos, pos - 1)

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

    recursive(qc, L, 0, 2**n, [], [], n - 1)

    return qc
'''


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

def recursive(qc, L, l, r, ppos, npos, pos):
    if r <= L:
        return
    elif L <= l:
        for i in npos:
            qc.x(i)
        bits = ppos + npos
        if len(bits) == 1:
            qc.z(bits[0])
        else:
            qc.append(ZGate().control(len(bits) - 1), bits)

        for i in npos:
            qc.x(i)
    else:
        mid = (l + r) // 2
        recursive(qc, L, l, mid, ppos, npos + [pos], pos - 1)
        recursive(qc, L, mid, r, ppos + [pos], npos, pos - 1)

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

    recursive(qc, L, 0, 2**n, [], [], n - 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:
    bits = [int(x) for x in f"{L:0{n}b}"]
    bits.reverse()
    for i in reversed(range(n)):
        if bits[i] == 0:
            continue
        for j in range(i + 1, n):
            if bits[j] == 0:
                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 bits[j] == 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)
    # Write your code here:

    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)
        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 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)

    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)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate
import math
def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n == 1:
      if L == 2:
        qc.z(0)
      qc.x(0)
      qc.z(0)
      qc.x(0)
      return qc

    s = range(n)
    f = ZGate().control(n - 1)
    for i in range(L):
      for j in range(n):
        if (i >> j) & 1:
          continue
        else:
          qc.x(j)
      qc.append(f, s)
      for j in range(n):
        if (i >> j) & 1:
          continue
        else:
          qc.x(j)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate
import math
def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n == 1:
      if L == 2:
        qc.z(0)
      qc.x(0)
      qc.z(0)
      qc.x(0)
      return qc

    sum = 0;
    for j in range(n - 1, -1, -1):
      if sum + (1 << j) >= L:
        for i in range(n - 1, j - 1, -1):
          qc.x(i)
        if j == n - 1:
          qc.z(j)
        else:
          qc.append(ZGate().control(n - j - 1), range(n - 1, j - 1, -1))
        for i in range(n - 1, j - 1, -1):
          qc.x(i)
        sum += (1 << j)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate
import math
def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n == 1:
      if L == 2:
        qc.z(0)
      qc.x(0)
      qc.z(0)
      qc.x(0)
      return qc

    sum = 0;
    for j in range(n - 1, -1, -1):
      if sum + (1 << j) <= L:
        for i in range(n - 1, j - 1, -1):
          qc.x(i)
        if j == n - 1:
          qc.z(j)
        else:
          qc.append(ZGate().control(n - j - 1), range(n - 1, j - 1, -1))
        for i in range(n - 1, j - 1, -1):
          qc.x(i)
        sum += (1 << j)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate
import math
def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n == 1:
      if L == 2:
        qc.z(0)
      qc.x(0)
      qc.z(0)
      qc.x(0)
      return qc

    sum = 0;
    for j in range(n - 1, -1, -1):
      if sum + (1 << j) <= L:
        for i in range(n - 1, j - 1, -1):
          if (sum >> i) % 2 == 0:
            qc.x(i)
        if j == n - 1:
          qc.z(j)
        else:
          qc.append(ZGate().control(n - j - 1), range(n - 1, j - 1, -1))
        for i in range(n - 1, j - 1, -1):
          if (sum >> i) % 2 == 0:
            qc.x(i)
        sum += (1 << j)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    i=n-1
    p=[]
    while(i>=0):
        p.append(i)
        if(L&(1<<i)):
            qc.append(XGate(), [i])
            if(i<n-1):
                qc.append(ZGate().control(n-i-1), p)
            else:
                qc.append(ZGate(), p)
            qc.append(XGate(), [i])
        else:
            qc.append(XGate(), [i])
        i-=1
    i=n-1
    while(i>=0):
        if((L&(1<<i))==0):
            qc.append(XGate(), [i])
        i-=1
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    i=n-1
    p=[]
    while(i>=0):
        p.append(i)
        if(L&(1<<i)):
            qc.append(XGate(), [i])
            if(i<n-1):
                qc.append(ZGate().control(n-i-1), p)
            else:
                qc.append(ZGate(), p)
            qc.append(XGate(), [i])
        else:
            qc.append(XGate(), [i])
        i-=1
    i=n-1
    while(i>=0):
        if((L&(1<<i))==0):
            qc.append(XGate(), [i])
        i-=1
    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().continue(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)

    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().continue(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)

    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)

    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:

    # 全部1になっている物を反転
    def reverse(qubits,qc):
        if qubits>1:
            qc.append(ZGate().control(qubits - 1), range(qubits))
        else:
            qc.z(0)

    # ある値をall_1に変更する操作
    def to_calcable(qubits,qc,n):
        for i in range(qubits):
            if not (n&(1<<i)):
                qc.x(i)

    for i in range(L):
        to_calcable(n,qc,i)
        reverse(n,qc)
        to_calcable(n,qc,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:

    # 左n bitが1になっている物の位相を反転
    def reverse(qubits,qc,start):
        if qubits>1:
            qc.append(ZGate().control(qubits - 1 -start), range(start,qubits))
        else:
            qc.z(0)

    # ある値をall_1に変更する操作
    # 左何bit目から操作するか指定
    def to_calcable(qubits,qc,n,start):
        for i in range(start,n):
            if not (n&(1<<i)):
                qc.x(i)

    for i in range(n):
        if n&(1<<i):
            to_calcable(n,qc,L,i)
            reverse(n,qc)
            to_calcable(n,qc,L,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:

    # 大きいほうの(qubits-start) bitが1になっている物の位相を反転
    def reverse(qubits,qc,start):
        if qubits-start>1:
            qc.append(ZGate().control(qubits - 1 -start), range(start,qubits))
        else:
            qc.z(0)

    # ある値をall_1に変更する操作
    # 左何bit目から操作するか指定
    def to_calcable(qubits,qc,n,start):
        for i in range(start,n):
            if not (n&(1<<i)):
                qc.x(i)

    for i in range(n):
        if n&(1<<i):
            to_calcable(n,qc,L,i)
            reverse(n,qc,i)
            to_calcable(n,qc,L,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:

    # 左n bitが1になっている物の位相を反転
    def reverse(qubits,qc,start):
        if qubits-start>1:
            qc.append(ZGate().control(qubits - 1 -start), range(start,qubits))
        else:
            qc.z(0)

    # ある値をall_1に変更する操作
    # 左何bit目から操作するか指定
    def to_calcable(qubits,qc,n,start):
        for i in range(start,qubits):
            if not (n&(1<<i)):
                qc.x(i)

    for i in range(n):
        if n&(1<<i):
            to_calcable(n,qc,L,i)
            reverse(n,qc,i)
            to_calcable(n,qc,L,i)

    return qc
'''


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

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

    # Write your code here:
    for i in range(n):
        qc.h(i)

    # 左n bitが1になっている物の位相を反転
    def reverse(qubits,qc,start):
        if qubits-start>1:
            qc.append(ZGate().control(qubits - 1 - start), range(start,qubits))
        else:
            qc.z(start)

    # ある値をall_1に変更する操作
    # 左何bit目から操作するか指定
    def to_calcable(qubits,qc,n,start):
        for i in range(start,qubits):
            if not (n&(1<<i)):
                qc.x(i)

    v=L-1
    for i in range(n):
        if L&(1<<i):
            to_calcable(n,qc,v,i)
            reverse(n,qc,i)
            to_calcable(n,qc,v,i)
            v-=(1<<i)

    # to_calcable(n,qc,0,2)
    # reverse(n,qc,2)
    # to_calcable(n,qc,0,2)

    # to_calcable(n,qc,4,1)
    # reverse(n,qc,1)
    # to_calcable(n,qc,4,1)

    # to_calcable(n,qc,4,0)
    # reverse(n,qc,0)
    # to_calcable(n,qc,4,0)
    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):
        qc.h(i)

    # 左n bitが1になっている物の位相を反転
    def reverse(qubits,qc,start):
        if qubits-start>1:
            qc.append(ZGate().control(qubits - 1 - start), range(start,qubits))
        else:
            qc.z(start)

    # ある値をall_1に変更する操作
    # 左何bit目から操作するか指定
    def to_calcable(qubits,qc,n,start):
        for i in range(start,qubits):
            if not (n&(1<<i)):
                qc.x(i)

    v=L-1
    for i in range(n):
        if L&(1<<i):
            to_calcable(n,qc,v,i)
            reverse(n,qc,i)
            to_calcable(n,qc,v,i)
            v-=(1<<i)

    # to_calcable(n,qc,0,2)
    # reverse(n,qc,2)
    # to_calcable(n,qc,0,2)

    # to_calcable(n,qc,4,1)
    # reverse(n,qc,1)
    # to_calcable(n,qc,4,1)

    # to_calcable(n,qc,4,0)
    # reverse(n,qc,0)
    # to_calcable(n,qc,4,0)
    return qc
'''


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

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

    # Write your code here:
    # for i in range(n):
    #     qc.h(i)

    # 左n bitが1になっている物の位相を反転
    def reverse(qubits,qc,start):
        if qubits-start>1:
            qc.append(ZGate().control(qubits - 1 - start), range(start,qubits))
        else:
            qc.z(start)

    # ある値をall_1に変更する操作
    # 左何bit目から操作するか指定
    def to_calcable(qubits,qc,n,start):
        for i in range(start,qubits):
            if not (n&(1<<i)):
                qc.x(i)

    v=L-1
    for i in range(n):
        if L&(1<<i):
            to_calcable(n,qc,v,i)
            reverse(n,qc,i)
            to_calcable(n,qc,v,i)
            v-=(1<<i)

    # to_calcable(n,qc,0,2)
    # reverse(n,qc,2)
    # to_calcable(n,qc,0,2)

    # to_calcable(n,qc,4,1)
    # reverse(n,qc,1)
    # to_calcable(n,qc,4,1)

    # to_calcable(n,qc,4,0)
    # reverse(n,qc,0)
    # to_calcable(n,qc,4,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)

    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)
        print(circuitlist)
        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
'''