'''python
from qiskit import QuantumCircuit
import math

# def intlog2_math(v):
#     return int(math.floor(math.log2(v)))

def get_bit(x, y):
    return 1 if x & int(2 ** y) != 0 else 0

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

    for i in list(range(M - 1))[::-1]:
        b = get_bit(L, i)
        if b == 0:
            qc.x(i)
        else:
            qc.x(i)
            qc.h(i)
            qc.mcx(list(range(i + 1, M)), i)
            qc.h(i)
            qc.x(i)

    for i in range(M):
        b = get_bit(L, i)
        if b == 0:
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    for i in range(L):
        temp = i
        control = []
        while(temp > 0):
            control.append(temp % 2)
            temp = temp // 2

        while(len(control) < n):
            control.append(0)

        for j in range(n):
            if(control[j] == 0):
                qc.x(j)

        qc.h(0)
        qc.mcx([i for i in range(1, n)], 0, 0)
        qc.h(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    for i in range(L):
        temp = i
        control = []
        while(temp > 0):
            control.append(temp % 2)
            temp = temp // 2

        while(len(control) < n):
            control.append(0)

        for j in range(n):
            if(control[j] == 0):
                qc.x(j)

        qc.h(0)
        qc.mcx([i for i in range(1, n)], 0)
        qc.h(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    qc.h([i for i in range(n)])

    for i in range(L):
        temp = i
        control = []
        while(temp > 0):
            control.append(temp % 2)
            temp = temp // 2

        while(len(control) < n):
            control.append(0)

        for j in range(n):
            if(control[j] == 0):
                qc.x(j)

        qc.h(0)
        qc.mcx([i for i in range(1, n)], 0, 0)
        qc.h(0)

        for j in range(n):
            if(control[j] == 0):
                qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math
def solve(n: int, L) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    for i in range(L):
        temp = i
        control = []
        while(temp > 0):
            control.append(temp % 2)
            temp = temp // 2

        while(len(control) < n):
            control.append(0)

        for j in range(n):
            if(control[j] == 0):
                qc.x(j)

        qc.h(0)
        qc.mcx([i for i in range(1, n)], 0, mode="noancilla")
        qc.h(0)

        for j in range(n):
            if(control[j] == 0):
                qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for i in range(1):
        temp = i
        control = []
        while(temp > 0):
            control.append(temp % 2)
            temp = temp // 2

        while(len(control) < n):
            control.append(0)

        for j in range(n):
            if(control[j] == 0):
                qc.x(j)

        qc.h(0)
        qc.mcx([i for i in range(1, n)], 0, mode="noancilla")
        qc.h(0)

        for j in range(n):
            if(control[j] == 0):
                qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for i in range(L):
        temp = i
        control = []
        while(temp > 0):
            control.append(temp % 2)
            temp = temp // 2

        while(len(control) < n):
            control.append(0)

        for j in range(n):
            if(control[j] == 0):
                qc.x(j)

        qc.h(0)
        qc.mcx([i for i in range(1, n)], 0)
        qc.h(0)

        for j in range(n):
            if(control[j] == 0):
                qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for i in range(L):
        temp = i
        control = []
        while(temp > 0):
            control.append(temp % 2)
            temp = temp // 2

        while(len(control) < n):
            control.append(0)

        for j in range(n):
            if(control[j] == 0):
                qc.x(j)

        qc.h(0)
        if n > 1:
            qc.mcx([i for i in range(1, n)], 0)
        qc.h(0)

        for j in range(n):
            if(control[j] == 0):
                qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    if n == 1:
        qc.rz(1, 0)
        return qc
    else:

        for i in range(L):
            temp = i
            control = []
            while(temp > 0):
                control.append(temp % 2)
                temp = temp // 2

            while(len(control) < n):
                control.append(0)

            for j in range(n):
                if(control[j] == 0):
                    qc.x(j)

            qc.h(0)
            qc.mcx([i for i in range(1, n)], 0, mode="noancilla")
            qc.h(0)

            for j in range(n):
                if(control[j] == 0):
                    qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    if n == 1:
        return qc
    else:

        for i in range(L):
            temp = i
            control = []
            while(temp > 0):
                control.append(temp % 2)
                temp = temp // 2

            while(len(control) < n):
                control.append(0)

            for j in range(n):
                if(control[j] == 0):
                    qc.x(j)

            qc.h(0)
            qc.mcx([i for i in range(1, n)], 0, mode="noancilla")
            qc.h(0)

            for j in range(n):
                if(control[j] == 0):
                    qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    if n == 1:
        return qc
    else:

        for i in range(L):
            temp = i
            control = []
            while(temp > 0):
                control.append(temp % 2)
                temp = temp // 2

            while(len(control) < n):
                control.append(0)

            for j in range(n):
                if(control[j] == 0):
                    qc.x(j)

            qc.h(0)
            qc.mcx([i for i in range(1, n)], 0)
            qc.h(0)

            for j in range(n):
                if(control[j] == 0):
                    qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit
def solve(n: int, L) -> QuantumCircuit:
    qr = QuantumRegister(n)

    qc = QuantumCircuit(qr)
    qr = qr[::-1]

    if n == 1:
        return qc
    else:

        for i in range(L):
            temp = i
            control = []
            while(temp > 0):
                control.append(temp % 2)
                temp = temp // 2

            while(len(control) < n):
                control.append(0)

            for j in range(n):
                if(control[j] == 0):
                    qc.x(qr[j])

            qc.h(qr[0])
            qc.mcx([qr[i] for i in range(1, n)], qr[0], mode="noancilla")
            qc.h(qr[0])

            for j in range(n):
                if(control[j] == 0):
                    qc.x(qr[j])

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L) -> QuantumCircuit:
    qr = QuantumRegister(n)
    qc = QuantumCircuit(qr)
    qr = qr[::-1]

    if n == 1:
        return qc
    else:

        for i in range(L):
            temp = i
            control = []
            while(temp > 0):
                control.append(temp % 2)
                temp = temp // 2

            while(len(control) < n):
                control.append(0)

            for j in range(n):
                if(control[j] == 0):
                    qc.x(qr[j])

            qc.h(qr[0])
            qc.mcx([qr[i] for i in range(1, n)], qr[0], mode="noancilla")
            qc.h(qr[0])

            for j in range(n):
                if(control[j] == 0):
                    qc.x(qr[j])

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L) -> QuantumCircuit:
    qr = QuantumRegister(n)
    qc = QuantumCircuit(qr)
    qr = qr[::-1]

    if n == 1:
        return qc
    else:

        for i in range(L):
            temp = i
            control = []
            while(temp > 0):
                control.append(temp % 2)
                temp = temp // 2

            while(len(control) < n):
                control.append(0)

            for j in range(n):
                if(control[j] == 0):
                    qc.x(qr[j])

            qc.h(qr[0])
            qc.mcx([qr[i] for i in range(1, n)], qr[0],)
            qc.h(qr[0])

            for j in range(n):
                if(control[j] == 0):
                    qc.x(qr[j])

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for i in range(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

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

    for i in range(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
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    theta = 2*math.pi
    for i in range(L-1):
        qc.ry(theta, i)
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    theta = 2*math.pi
    for i in range(2^n-1):
        qc.ry(theta, 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):
            # check if i-th bit of l is 0 or 1
            if not ((l >> i) & 1):
                qc.x(i)
        if n == 1:
            qc.z(0)
        else:
            # apply multiple controlled Z gate
            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 ZGate

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

    for i in range(L):
        # |i> -> |2^(n-1)>
        for j in range(n):
            if not ((i >> j) & 1):
                qc.x(j)
        if n == 1:
            qc.z(0)
        else:
            qc.append(ZGate().control(n - 1), range(n))
        for j in range(n):
            if not ((i >> 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(L):
        i_ = 2**n - L - 1
        for j in range(n):
            if (i_ // (2**j)) % 2 == 1:
                qc.cx(j)
        qc.mcz([_ in range(n)])
        for j in range(n):
            if (i_ // (2**j)) % 2 == 1:
                qc.cx(j)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(L):
        i_ = 2**n - L - 1
        for j in range(n):
            if (i_ // (2**j)) % 2 == 1:
                qc.cx(j)
        qc.mcp(math.pi, [_ in range(n)])
        for j in range(n):
            if (i_ // (2**j)) % 2 == 1:
                qc.cx(j)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(L):
        i_ = 2**n - L - 1
        for j in range(n):
            if (i_ // (2**j)) % 2 == 1:
                qc.cx(j)
        if n == 1:
            qc.z(0)
        else:
            qc.mcp(math.pi, [_ in range(n - 1)], n - 1)
        for j in range(n):
            if (i_ // (2**j)) % 2 == 1:
                qc.cx(j)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(L):
        i_ = 2**n - L - 1
        for j in range(n):
            if (i_ // (2**j)) % 2 == 1:
                qc.cx(j)
        if n == 1:
            qc.z(0)
        else:
            qc.h(n - 1)
            qc.mcx([_ in range(n - 1)], n - 1)
            qc.h(n - 1)
        for j in range(n):
            if (i_ // (2**j)) % 2 == 1:
                qc.cx(j)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(L):
        i_ = 2**n - L - 1
        for j in range(n):
            if (i_ // (2**j)) % 2 == 1:
                qc.cx(j)
        if n == 1:
            qc.z(0)
        else:
            qc.h(n - 1)
            qc.mcx([_ for _ in range(n - 1)], n - 1)
            qc.h(n - 1)
        for j in range(n):
            if (i_ // (2**j)) % 2 == 1:
                qc.cx(j)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(L):
        i_ = 2**n - L - 1
        for j in range(n):
            if (i_ // (2**j)) % 2 == 1:
                qc.x(j)
        if n == 1:
            qc.z(0)
        else:
            qc.h(n - 1)
            qc.mcx([_ for _in range(n - 1)], n - 1)
            qc.h(n - 1)
        for j in range(n):
            if (i_ // (2**j)) % 2 == 1:
                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:
    for i in range(L):
        i_ = 2**n - i - 1
        for j in range(n):
            if (i_ // (2**j)) % 2 == 1:
                qc.x(j)
        if n == 1:
            qc.z(0)
        else:
            qc.h(n - 1)
            qc.mcx([_ for _ in range(n - 1)], n - 1)
            qc.h(n - 1)
        for j in range(n):
            if (i_ // (2**j)) % 2 == 1:
                qc.x(j)
    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)
    # Write your code here:
    qc.cx(5,32)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.h(range(5))
    qc.cx(5,32)

    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 Gate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)
    for j in range(L):
        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)
    # Write your code here:
    for i in range(n):
        qc.h(i)
    for j in range(L):
        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)
    # Write your code here:
    for i in range(n):
        qc.h(i)
    for j in range(L):
        qc.append(ZGate().control(j - 1), range(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)

    for i in range(L):
        # Zをかけたいビットの0を1にする
        for j in range(n):
            if((i & j) == j):
                qc.x(j)
        # cotrolZをかける
        qc.append(ZGate().control(n - 1), range(n))
        # Zをかけたいビットの1を0に戻す
        for j in range(n):
            if((i & j) == j):
                qc.x(j)
    # エンディアンを逆にする
    qc.reverse_bits()

    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)

    for i in range(L):
        # Zをかけたいビットの0を1にする
        for j in range(n):
            if((i & j) == i):
                qc.x(j)
        # cotrolZをかける
        qc.append(ZGate().control(n - 1), range(n))
        # Zをかけたいビットの1を0に戻す
        for j in range(n):
            if((i & j) == i):
                qc.x(j)
        qc.barrier()
    # エンディアンを逆にする
    qc.reverse_bits()

    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)

    for i in range(L):
        # Zをかけたいビットの0を1にする
        for j in range(n):
            if((i & j) == i):
                qc.x(j)
        # cotrolZをかける
        qc.append(ZGate().control(n - 1), range(n))
        # Zをかけたいビットの1を0に戻す
        for j in range(n):
            if((i & j) == i):
                qc.x(j)
        #qc.barrier()
    # エンディアンを逆にする
    #qc.reverse_bits()

    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)

    for i in range(L):
        # Zをかけたいビットの0を1にする
        for j in range(n):
            if((i & j) == i):
                qc.x(j)
        # cotrolZをかける
        qc.append(ZGate().control(n - 1), range(n))
        # Zをかけたいビットの1を0に戻す
        for j in range(n):
            if((i & j) == i):
                qc.x(j)
        #qc.barrier()
    # エンディアンを逆にする
    qc.reverse_bits()

    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)

    for i in range(L):
        # Zをかけたいビットの0を1にする
        for j in range(n):
            if((i & j) == 2**j):
                qc.x(j)
        # cotrolZをかける
        qc.append(ZGate().control(n - 1), range(n))
        # Zをかけたいビットの1を0に戻す
        for j in range(n):
            if((i & j) == 2**j):
                qc.x(j)
        qc.barrier()
    # エンディアンを逆にする
    qc.reverse_bits()

    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)

    # L=0のとき
    for j in range(n):
        qc.x(j)
    qc.append(ZGate().control(n - 1), range(n))
    for j in range(n):
        qc.x(j)
    qc.barrier()

    for i in range(1, L):
        # Zをかけたいビットの0を1にする
        for j in range(n):
            if((i & 2**j) == 2**j):
                qc.x(j)
        # cotrolZをかける
        qc.append(ZGate().control(n - 1), range(n))
        # Zをかけたいビットの1を0に戻す
        for j in range(n):
            if((i & 2**j) == 2**j):
                qc.x(j)
        qc.barrier()
    # エンディアンを逆にする
    qc.reverse_bits()

    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)

    # L=0のとき
    for j in range(n):
        qc.x(j)
    qc.append(ZGate().control(n - 1), range(n))
    for j in range(n):
        qc.x(j)
    qc.barrier()

    for i in range(1, L):
        # Zをかけたいビットの0を1にする
        for j in range(n):
            if((i & 2**j) != 2**j):
                qc.x(j)
        # cotrolZをかける
        qc.append(ZGate().control(n - 1), range(n))
        # Zをかけたいビットの1を0に戻す
        for j in range(n):
            if((i & 2**j) != 2**j):
                qc.x(j)
        qc.barrier()
    # エンディアンを逆にする
    #qc.reverse_bits()

    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)

    # L=0のとき
    for j in range(n):
        qc.x(j)
    qc.append(ZGate().control(n - 1), range(n))
    for j in range(n):
        qc.x(j)
    qc.barrier()

    for i in range(1, L):
        # Zをかけたいビットの0を1にする
        for j in range(0, n):
            if((i & 2**j) != 2**j):
                qc.x(n - 1 - j)
        # cotrolZをかける
        qc.append(ZGate().control(n - 1), range(n))
        # Zをかけたいビットの1を0に戻す
        for j in range(0, n):
            if((i & 2**j) != 2**j):
                qc.x(n -1 - j)
        qc.barrier()
    # エンディアンを逆にする
    #qc.reverse_ops()

    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)

    # i=0のとき
    for j in range(n):
        qc.x(j)
    qc.z(0)
    for j in range(n):
        qc.x(j)
    qc.barrier()

    for i in range(1, L):
        # Zをかけたいビットの0を1にする
        for j in range(0, n):
            if((i & 2**j) != 2**j):
                qc.x(n - 1 - j)
        # cotrolZをかける
        qc.append(ZGate().control(n - 1), range(n))
        # Zをかけたいビットの1を0に戻す
        for j in range(0, n):
            if((i & 2**j) != 2**j):
                qc.x(n -1 - j)
        qc.barrier()
    # エンディアンを逆にする
    #qc.reverse_ops()

    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)

    # i=0のとき
    for j in range(n):
        qc.x(j)
    qc.z(0)
    for j in range(n):
        qc.x(j)
    #qc.barrier()

    for i in range(1, L):
        # Zをかけたいビットの0を1にする
        for j in range(0, n):
            if((i & 2**j) != 2**j):
                qc.x(n - 1 - j)
        # cotrolZをかける
        qc.append(ZGate().control(n - 1), range(n))
        # Zをかけたいビットの1を0に戻す
        for j in range(0, n):
            if((i & 2**j) != 2**j):
                qc.x(n -1 - j)
        #qc.barrier()
    # エンディアンを逆にする
    #qc.reverse_ops()

    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)

    # i=0のとき
    for j in range(n):
        qc.x(j)
    if(n==1):
        qc.z(0)
    else:
        qc.append(ZGate().control(n - 1), range(n))
    for j in range(n):
        qc.x(j)
    #qc.barrier()

    for i in range(1, L):
        # Zをかけたいビットの0を1にする
        for j in range(0, n):
            if((i & 2**j) != 2**j):
                qc.x(n - 1 - j)
        # cotrolZをかける
        qc.append(ZGate().control(n - 1), range(n))
        # Zをかけたいビットの1を0に戻す
        for j in range(0, n):
            if((i & 2**j) != 2**j):
                qc.x(n -1 - j)
        #qc.barrier()
    # エンディアンを逆にする
    #qc.reverse_ops()

    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)

    # i=0のとき
    for j in range(n):
        qc.x(j)
    if(n==1):
        qc.z(0)
    else:
        qc.append(ZGate().control(n - 1), range(n))
    for j in range(n):
        qc.x(j)
    #qc.barrier()

    for i in range(1, L):
        # Zをかけたいビットの0を1にする
        for j in range(n):
            if((i & 2**j) != 2**j):
                qc.x(n - 1 - j)
        # cotrolZをかける
        qc.append(ZGate().control(n - 1), range(n))
        # Zをかけたいビットの1を0に戻す
        for j in range(n):
            if((i & 2**j) != 2**j):
                qc.x(n -1 - j)
        #qc.barrier()
    # エンディアンを逆にする
    #qc.reverse_ops()

    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)

    # i=0のとき
    for j in range(n):
        qc.x(j)
    if(n==1):
        qc.z(0)
    else:
        qc.append(ZGate().control(n - 1), range(n))
    for j in range(n):
        qc.x(j)
    #qc.barrier()

    for i in range(1, L):
        # Zをかけたいビットの0を1にする
        for j in range(n):
            if((i & 2**j) != 2**j):
                qc.x(j)
        # cotrolZをかける
        qc.append(ZGate().control(n - 1), range(n))
        # Zをかけたいビットの1を0に戻す
        for j in range(n):
            if((i & 2**j) != 2**j):
                qc.x(j)
        #qc.barrier()
    # エンディアンを逆にする
    #qc.reverse_ops()

    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)

    # i=0のとき
    for j in range(n):
        qc.x(j)
    if(n==1):
        qc.z(0)
    else:
        qc.append(ZGate().control(n - 1), range(n))
    for j in range(n):
        qc.x(j)
    #qc.barrier()

    for i in range(1, L):
        # Zをかけたいビットの0を1にする
        for j in range(n):
            if((i & 2**j) != 2**j):
                qc.x(n - 1 -j)
        # cotrolZをかける
        qc.append(ZGate().control(n - 1), range(n))
        # Zをかけたいビットの1を0に戻す
        for j in range(n):
            if((i & 2**j) != 2**j):
                qc.x(n - 1 - j)
        #qc.barrier()
    # エンディアンを逆にする
    #qc.reverse_ops()

    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)

    # i=0のとき
    for j in range(n):
        qc.x(j)
    if(n==1):
        qc.z(0)
    else:
        qc.append(ZGate().control(n), range(n))
    for j in range(n):
        qc.x(j)
    #qc.barrier()

    for i in range(1, L):
        # Zをかけたいビットの0を1にする
        for j in range(n):
            if((i & 2**j) != 2**j):
                qc.x(n - 1 -j)
        # cotrolZをかける
        qc.append(ZGate().control(n - 1), range(n))
        # Zをかけたいビットの1を0に戻す
        for j in range(n):
            if((i & 2**j) != 2**j):
                qc.x(n - 1 - j)
        #qc.barrier()
    # エンディアンを逆にする
    #qc.reverse_ops()

    return qc
'''


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

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.appen(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 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.appen(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 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

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

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

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
	qc = QuantumCircuit(n)
	diag = [1] * (1 << n)
	for i in range(L):
		diag[i] = -1
	qc.append(DiagonalGate(diag), [i for i in range(n)])
	return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
	qc = QuantumCircuit(n)
	qc.append(MCMT(ZGate(), n - 1, 1), [i for i in range(n)])
	return qc
'''


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

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)
	else:
		qc.append(MCMT(ZGate(), n - 1, 1), [i for i in range(n)])
	return qc
'''


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

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)
	else:
		qc.append(MCMT(ZGate(), n - 1, 1), [i for i in 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)
	qc.append(ZGate(0), 0)
	return qc
'''


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

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


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

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

	return qc
'''


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

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

	return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
	qc = QuantumCircuit(n)
	if n >= 2:
		qc.append(MCMT(ZGate(), 1, 1), 0, 1)
	return qc
'''


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

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)
	else:
		pass
		#qc.append(MCMT(ZGate(), n - 1, 1), [i for i in range(n)])
	return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
	qc = QuantumCircuit(n)
	if n == 1:
		gate = ZGate()
		qc.x(0)
		qc.append(gate, 0)
		qc.x(0)
		if L == 2:
			qc.z(0)
	else:
		pass
		#qc.append(MCMT(ZGate(), n - 1, 1), [i for i in range(n)])
	return qc
'''


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

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)
	else:
		for i in range(L):
			for bit in range(n):
				if ~i >> bit & 1:
					qc.x(bit)
			qc.append(MCMT(ZGate(), n - 1, 1), [i for i in range(n)])
			for bit in range(n):
				if ~i >> bit & 1:
					qc.x(bit)
	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 n == 1:
		qc.x(0)
		qc.z(0)
		qc.x(0)
		if L == 2:
			qc.z(0)
	else:
		for i in range(L):
			for bit in range(n):
				if ~i >> bit & 1:
					qc.x(bit)
			qc.append(ZGate().control(n - 1), [i for i in range(n)])
			for bit in range(n):
				if ~i >> bit & 1:
					qc.x(bit)
	return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if L == 1:
        for i in range(n):
            qc.h(i)
        qc.append(ZGate().control(n - 1), range(n))
        for i in range(n):
            qc.x(i)
    elif L == [2 ** i for i in range(1, 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:

    if n == 1:
        for l in range(L):
            if l & 1 == 0:
                qc.x(0)
            qc.z(0)
            if l & 1 == 0:
                qc.x(0)
        return qc

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    # remove
    qc.h(range(n))

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    # remove
    qc.h(range(n))

    if n > 1:
        x = qulib.ZGate().control(n-1)
        qc.append(x, range(n))

    return qc
'''


'''python
import matplotlib.pyplot as plt
import math
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector

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

    i = 0
    while(L > 0):
        r = L % 2
        L = L // 2
        if(r == 1):
            if i == n-1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                qc.x(i)
                qc.mcp(math.pi, [x for x in range(i+1, n)], i, ctrl_state=L)
                qc.x(i)

        i += 1

    return qc
'''


'''python
import math
from qiskit import QuantumCircuit

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

    i = 0
    while(L > 0):
        r = L % 2
        L = L // 2
        if(r == 1):
            if i == n-1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                qc.x(i)
                qc.mcp(math.pi, [x for x in range(i+1, n)], i, ctrl_state=L)
                qc.x(i)

        i += 1

    return qc
'''


'''python
import math
from qiskit import QuantumCircuit

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

    i = 0
    while(L > 0) and i < n:
        r = L % 2
        L = L // 2
        if(r == 1):
            if i == n-1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                qc.x(i)
                qc.mcp(math.pi, [x for x in range(i+1, n)], i, ctrl_state=L)
                qc.x(i)

        i += 1

    return qc
'''


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.y(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):
            # check if i-th bit of l is 0 or 1
            if not ((l >> i) & 1):
                qc.x(i)
        if n == 1:
            qc.z(0)
        else:
            # apply multiple controlled Z gate
            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 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):
            # check if i-th bit of l is 0 or 1
            if not ((l >> i) & 1):
                qc.x(i)
        if n == 1:
            qc.z(0)
        else:
            # apply multiple controlled Z gate
            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

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

    for l in range(L):
        for i in range(n):
            # check if i-th bit of l is 0 or 1
            if not ((l >> i) & 1):
                qc.x(i)
        if n == 1:
            qc.z(0)
        else:
            # apply multiple controlled Z gate
            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 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

def num_inv(qc: QuantumCircuit, n: int, val: int):
    for i in range(n):
        if (val >> i) & 1:
            qc.x(i)
    qc.h(0)
    qc.mcx(list(range(1, n)), 0)
    qc.h(0)
    for i in range(n):
        if (val >> i) & 1:
            qc.x(i)

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

    # qc.h(range(n))

    for i in range((1 << n) - L, (1 << n)):
        num_inv(qc, n, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def mcz(qc: QuantumCircuit, n: int):
    qc.h(0)
    if n == 1:
        qc.x(0)
    else:
        qc.mcx(list(range(1, n)), 0)
    qc.h(0)

def num_inv(qc: QuantumCircuit, n: int, val: int):
    for i in range(n):
        if (val >> i) & 1:
            qc.x(i)
    mcz(qc, n)
    for i in range(n):
        if (val >> i) & 1:
            qc.x(i)

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

    qc.h(range(n))

    for i in range((1 << n) - L, (1 << n)):
        num_inv(qc, n, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def mcz(qc: QuantumCircuit, n: int):
    qc.h(0)
    if n == 1:
        qc.x(0)
    else:
        qc.mcx(list(range(1, n)), 0)
    qc.h(0)

def num_inv(qc: QuantumCircuit, n: int, val: int):
    for i in range(n):
        if (val >> i) & 1:
            qc.x(i)
    mcz(qc, n)
    for i in range(n):
        if (val >> i) & 1:
            qc.x(i)

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

    # qc.h(range(n))

    for i in range((1 << n) - L, (1 << n)):
        num_inv(qc, n, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    qc.z(range(L))
    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):
        qc.z(i)
    return qc
'''


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

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 not ((i >> j) & 1):
                qc.x(j)

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

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

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

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

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

        for j in range(n):
            if not ((i >> 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):
        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)

    # エンコードされた値L-1に対してバイナリ表現を作成
    Lbinary = format(L-1, '0' + str(n) + 'b')

    # QC上で必要な制御Zゲートを適用する
    for i, bit in enumerate(reversed(Lbinary)):
        if bit == '1':
            qc.x(i) # 制御されるビットが0のときはXゲートで反転させる

    # 制御Zゲートを適用する。qiskitでは制御量子ビットの数は制御されるゲートに引数として与える
    qc.append(ZGate().control(n - 1), range(n))

    # ビットを元に戻す
    for i, bit in enumerate(reversed(Lbinary)):
        if bit == '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)

    # We first need a list of binary strings representing the states
    # that need to have their amplitudes flipped.
    states_to_flip = [format(i, f"0{n}b") for i in range(L)]

    # For each state, we'll apply a controlled-Z operation, flipping the amplitude
    for state_string in states_to_flip:
        # Create a series of gates with X-gates on the qubits that are 0 in the state
        # because control operations are applied on qubits in the |1> state by default.
        for qubit_index, state_bit in enumerate(reversed(state_string)):
            if state_bit == '0':
                qc.x(qubit_index)

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

        # Undo the X-gates
        for qubit_index, state_bit in enumerate(reversed(state_string)):
            if state_bit == '0':
                qc.x(qubit_index)

    return qc
'''


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

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

    # Initialize list for control qubits, which will be all qubits
    controls = list(range(n))

    # Determine the binary representation of L-1
    # and reverse it for little endian order
    # (This could also be obtained from 'format(L-1, '0{}b'.format(n))')
    target_state_bin = bin(L-1)[2:].rjust(n, '0')[::-1]

    # Apply X gates to initialize the controls, flipping 0s to 1s
    # to obtain the correct control pattern
    for i, bit in enumerate(target_state_bin):
        if bit == '0':
            # Negates the amplitude for states < L
            qc.x(i)

    # Apply MCMT (Multi-Control Multi-Target) gate,
    # since it applies the Z gate only when all controls are 1,
    # effectively negating the amplitude for the target state.
    # Here we're creating a Multi-Controlled Z gate targeting a dummy qubit (ignored).
    # The dummy qubit is used because MCMT requires a target, but Z gate does not have one.
    qc.append(MCMTVChain('z', num_ctrl_qubits=n, num_target_qubits=1), controls + [n])

    # Apply X gates again to reset controls
    for i, bit in enumerate(target_state_bin):
        if bit == '0':
            qc.x(i)

    return qc
'''


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

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 (make all 1 state)
        current_qubit = 0
        for j in range(len(little_endian_binary_repr)):
            if little_endian_binary_repr[j] == '0':
                qc.x(current_qubit)
            current_qubit += 1

        # Apply the multi-controlled gate (MCMT) here
        # Control qubits are 0 to n-2, target qubit is n-1
        control_qubits = list(range(n - 1))  # Control qubits from 0 to n-2
        target_qubit = n - 1  # Target qubit is n-1
        qc.append(MCMT('z', num_ctrl_qubits=n - 1, num_target_qubits=1), control_qubits + [target_qubit])

        # Reset X gates for the next iteration
        current_qubit = 0
        for j in range(len(little_endian_binary_repr)):
            if little_endian_binary_repr[j] == '0':
                qc.x(current_qubit)
            current_qubit += 1

    return qc
from qiskit import QuantumCircuit
from qiskit.circuit.library import MCMT

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 (make all 1 state)
        current_qubit = 0
        for j in range(len(little_endian_binary_repr)):
            if little_endian_binary_repr[j] == '0':
                qc.x(current_qubit)
            current_qubit += 1

        # Apply the multi-controlled gate (MCMT) here
        # Control qubits are 0 to n-2, target qubit is n-1
        control_qubits = list(range(n - 1))  # Control qubits from 0 to n-2
        target_qubit = n - 1  # Target qubit is n-1
        qc.append(MCMT('z', num_ctrl_qubits=n - 1, num_target_qubits=1), control_qubits + [target_qubit])

        # Reset X gates for the next iteration
        current_qubit = 0
        for j in range(len(little_endian_binary_repr)):
            if little_endian_binary_repr[j] == '0':
                qc.x(current_qubit)
            current_qubit += 1

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import MCPhaseGate
import numpy as np

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 phase gate (equivalent to MCZ)
        control_qubits = list(range(n - 1))  # Control qubits from 0 to n-2
        target_qubit = n - 1  # Target qubit is n-1
        qc.append(MCPhaseGate(lam=np.pi, num_ctrl_qubits=n - 1), control_qubits + [target_qubit])

        # 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 Gate

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

    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
        cz_gate = Gate(name="Z", num_qubits=1, params=[])  # Define a Z gate
        qc.append(cz_gate.control(n - 1), range(n))  # Apply MCZ

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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.h(0)
    for i in range(L-1):
        qc.z(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 l in range(L):
        for i in range(n):
            # check if i-th bit of l is 0 or 1
            if not ((l >> i) & 1):
                qc.x(i)
        if n == 1:
            qc.z(0)
        else:
            # apply multiple controlled Z gate
            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
import math

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

    for marked in range(L):
        b = '{:05b}'.format(marked)[::-1]
        for zero_bit in range(n):
            if b[zero_bit] == '0':
                qc.x(zero_bit)
        # apply MCZ
        qc.mcp(math.pi, [qbit for qbit in range(n-1)], n-1)
        for zero_bit in range(n):
            if b[zero_bit] == '0':
                qc.x(zero_bit)

    return qc
'''


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

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

    for marked in range(L):
        b = '{:05b}'.format(marked)[::-1]
        for zero_bit in range(n):
            if b[zero_bit] == '0':
                qc.x(zero_bit)
        # apply MCZ
        qc.mcp(math.pi, [qbit for qbit in range(n-1)], n-1)
        for zero_bit in range(n):
            if b[zero_bit] == '0':
                qc.x(zero_bit)

    return qc
'''


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

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

    for marked in range(L):
        b = '{:05b}'.format(marked)[::-1][0:n]
        for zero_bit in range(n):
            if b[zero_bit] == '0':
                qc.x(zero_bit)
        # apply MCZ
        qc.mcp(math.pi, [qbit for qbit in range(n-1)], n-1)
        for zero_bit in range(n):
            if b[zero_bit] == '0':
                qc.x(zero_bit)

    return qc
'''


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

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

    for marked in range(L):
        b = '{:05b}'.format(marked)[::-1][0:n]
        for zero_bit in range(n):
            if b[zero_bit] == '0':
                qc.x(zero_bit)
        # apply MCZ
        qc.mcp(math.pi, [qbit for qbit in range(n-1)], n-1)
        for zero_bit in range(n):
            if b[zero_bit] == '0':
                qc.x(zero_bit)

    return qc
'''


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

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(1)
    qc.z(math.pi / 2.0, 1)
    qc.x(1)

    qc.cx(1, 0)
    qc.z(math.pi / 2.0, 0)
    qc.cx(1, 0)

    qc.x(0)
    qc.z(math.pi / 2.0, 0)
    qc.x(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(1)
    qc.rz(math.pi / 2.0, 1)
    qc.x(1)

    qc.cx(1, 0)
    qc.rz(math.pi / 2.0, 0)
    qc.cx(1, 0)

    qc.x(0)
    qc.rz(math.pi / 2.0, 0)
    qc.x(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(1)
    qc.rz(math.pi / 2.0, 1)
    qc.x(1)

    qc.cx(1, 0)
    qc.rz(math.pi / 2.0, 0)
    qc.cx(1, 0)

    qc.x(0)
    qc.rz(math.pi / 2.0, 0)
    qc.x(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import ma

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n == 1:
        qc.rz(math.pi / 2.0, 0)
    if n == 2:
        qc.x(1)
        qc.rz(math.pi / 2.0, 1)
        qc.x(1)

        qc.cx(1, 0)
        qc.rz(math.pi / 2.0, 0)
        qc.cx(1, 0)

        qc.x(0)
        qc.rz(math.pi / 2.0, 0)
        qc.x(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n == 1:
        qc.rz(math.pi / 2.0, 0)
    if n == 2:

        qc.x(1)
        qc.rz(math.pi / 2.0, 1)
        qc.x(1)

        qc.cx(1, 0)
        qc.rz(math.pi / 2.0, 0)
        qc.cx(1, 0)

        qc.x(0)
        qc.rz(math.pi / 2.0, 0)
        qc.x(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import mat

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n == 1:
        qc.rz(math.pi, 0)
    if n == 2:
        qc.x(1)
        qc.rz(math.pi / 2.0, 1)
        qc.x(1)

        qc.cx(1, 0)
        qc.rz(math.pi / 2.0, 0)
        qc.cx(1, 0)

        qc.x(0)
        qc.rz(math.pi / 2.0, 0)
        qc.x(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n == 1:
        qc.rz(math.pi , 0)
    if n == 2:
        qc.x(1)
        qc.rz(math.pi / 2.0, 1)
        qc.x(1)

        qc.cx(1, 0)
        qc.rz(math.pi / 2.0, 0)
        qc.cx(1, 0)

        qc.x(0)
        qc.rz(math.pi / 2.0, 0)
        qc.x(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def (qc, ts):

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    xs = []
    for i in range(n-1):
        xs.append(i)
    qc.mcp(xs, n-1)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    xs = []
    for i in range(n-1):
        xs.append(i)
    qc.mcp(xs, n-1)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    xs = []
    for i in range(n-1):
        xs.append(i)
    qc.mcp(math.pi, xs, n-1)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n == 1:
        qc.z(0)
    else:
        xs = []
        for i in range(n-1):
            xs.append(i)
        qc.mcp(math.pi, xs, n-1)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    # グローバル位相は無視して良いので、全部ひっくり返す場合は何もしない
    if L == 1 << n:
        return qc
    # 引く
    L2 = (1 << n) - L
    print("L2 = ", L2)
    for i in range(n):
        if L2 & (1 << (n - i - 1)) != 0:
            print("i = ", i)
            controls = []
            for k in range(i):
                controls.append(n-1-k)
            if len(controls) > 0:
                qc.mcp(math.pi, controls, n-1-i)
            else:
                qc.z(n-1-i)
            qc.x(n-1-i)
    for i in reversed(range(n)):
        if L2 & (1 << (n - i - 1)) != 0:
            qc.x(n-1-i)

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

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

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

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

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

            if l == 0:
                qc.x(0)
            qc.z(0)
            if l == 0:
                qc.x(0)

        return qc

    for l in range(L):
        for i in range(n):
            if (l >> i) & 1 == 0:
                qc.x(i)
        qc.append(ZGate().control(n-1), range(n))
        for i in range(n):
            if (l >> i) & 1 == 0:
                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 = 1<<n
    # qc.h(0)
    # qc.h(1)
    # qc.h(2)
    for i in range(L):
        code = bin(i+b)[3:]
        print(code)
        zg = ZGate()
        if code[-1] == "1":
            zg = zg.control(n-1, ctrl_state=code[:-1])
            qc.append(zg, list(range(n)))
        else:
            qc.x(n-1)
            zg = zg.control(n-1, ctrl_state=code[:-1])
            qc.append(zg, list(range(n)))
            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:
    b = 1<<n
    # qc.h(0)
    # qc.h(1)
    # qc.h(2)
    for i in range(L):
        code = bin(i+b)[3:]
        print(code)
        zg = ZGate()
        if code[-1] == "1":
            zg = zg.control(n-1, ctrl_state=code[:-1])
            qc.append(zg, list(range(n)))
        else:
            qc.x(n-1)
            zg = zg.control(n-1, ctrl_state=code[:-1])
            qc.append(zg, list(range(n)))
            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:
    # qc.h(0)
    # qc.h(1)
    # qc.h(2)
    b = 1<<n
    # qc.h(0)
    # qc.h(1)
    # qc.h(2)
    for i in range(L):
        code = bin(i+b)[3:]
        print(code)
        print(code[:-1])
        zg = ZGate()
        if code[0] == "1":
            zg = zg.control(n-1, ctrl_state=code[1:][::-1])
            qc.append(zg, list(range(n)))
        else:
            qc.x(n-1)
            zg = zg.control(n-1, ctrl_state=code[1:][::-1])
            qc.append(zg, list(range(n)))
            qc.x(n-1)

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    numbin = bin(L + (1<<n))[3:]
    qc = QuantumCircuit(n)
    # for i in range(n):
    #     qc.h(i)
    if numbin[0]== "1":
        qc.x(n-1)
        qc.z(n-1)
        qc.x(n-1)
    print(numbin)
    for pos, v in enumerate(numbin[1:]):
        pos += 1
        if v=="0":
            qc.x(n-1-pos)
        else:
            qc.x(n-1-pos)
            nn = pos + 1
            circuit=QuantumCircuit(nn)
            circuit.h(nn-1)
            gate = MCXGate(nn-1)
            circuit.append(gate, range(nn))
            circuit.h(nn-1)
            qc.append(circuit.to_gate(), range(n-1,n-2-pos,-1))
            qc.x(n-pos-1)
    for pos, v in enumerate(numbin[1:]):
        if v == "0":
            qc.x(n-pos-1)
    qc = qc.decompose()
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    numbin = bin(L + (1<<n))[3:][::-1]
    qc = QuantumCircuit(n)
    # for i in range(n):
    #     qc.h(i)
    if numbin[0]== "1":
        qc.x(n-1)
        qc.z(n-1)
        qc.x(n-1)
    print(numbin)
    for pos, v in enumerate(numbin[1:]):
        pos += 1
        if v=="0":
            qc.x(n-1-pos)
        else:
            qc.x(n-1-pos)
            nn = pos + 1
            circuit=QuantumCircuit(nn)
            circuit.h(nn-1)
            gate = MCXGate(nn-1)
            circuit.append(gate, range(nn))
            circuit.h(nn-1)
            qc.append(circuit.to_gate(), range(n-1,n-2-pos,-1))
            qc.x(n-pos-1)
    for pos, v in enumerate(numbin[1:]):
        if v == "0":
            qc.x(n-pos-1)
    qc = qc.decompose()
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    numbin = bin(L + (1<<n))[3:]
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.h(i)
    numbin = numbin.rstrip("0")
    print(numbin)
    if numbin[0]== "1":
        qc.x(n-1)
        qc.z(n-1)
        qc.x(n-1)
    else:
        qc.x(n-1)
    for pos, v in enumerate(numbin[1:]):
        pos += 1
        if v=="0":
            qc.x(n-1-pos)
        else:
            qc.x(n-1-pos)
            nn = pos + 1
            circuit=QuantumCircuit(nn)
            circuit.h(nn-1)
            gate = MCXGate(nn-1)
            circuit.append(gate, range(nn))
            circuit.h(nn-1)
            qc.append(circuit.to_gate(), range(n-1,n-2-pos,-1))
            qc.x(n-pos-1)
    for pos, v in enumerate(numbin):
        if v == "0":
            qc.x(n-pos-1)
    qc = qc.decompose()
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    numbin = bin(L + (1<<n))[3:]
    qc = QuantumCircuit(n)
    numbin = numbin.rstrip("0")
    print(numbin)
    if numbin[0]== "1":
        qc.x(n-1)
        qc.z(n-1)
        qc.x(n-1)
    else:
        qc.x(n-1)
    for pos, v in enumerate(numbin[1:]):
        pos += 1
        if v=="0":
            qc.x(n-1-pos)
        else:
            qc.x(n-1-pos)
            nn = pos + 1
            circuit=QuantumCircuit(nn)
            circuit.h(nn-1)
            gate = MCXGate(nn-1)
            circuit.append(gate, range(nn))
            circuit.h(nn-1)
            qc.append(circuit.to_gate(), range(n-1,n-2-pos,-1))
            qc.x(n-pos-1)
    for pos, v in enumerate(numbin):
        if v == "0":
            qc.x(n-pos-1)
    qc = qc.decompose()
    return qc
    qc1 = QuantumCircuit(n)
    qc1.append(qc, range(n-1,-1,-1))
    qc1 = qc1.decompose()
    return qc1
'''


'''python
from qiskit import QuantumCircuit

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

    qc.x(range(n))
    qc.h(n-1)
    qc.mcx(list(range(n-1)), n-1)
    qc.h(n-1)
    qc.x(range(n))
    return qc
'''


'''python
from qiskit import QuantumCircuit
from numpy import pi

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.ry(2*np.pi, 0)
    qc.append(z().control(n - 1), range(n))

    return qc
'''


'''python
from qiskit import QuantumCircuit
from numpy import pi

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.ry(2*np.pi, 0)
    qc.append(z(n).control(n - 1), range(n))

    return qc
'''


'''python
from qiskit import QuantumCircuit
import numpy
from numpy import pi

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.ry(2*np.pi, 0)
    qc.append(z(n-1).control(n - 1), range(n))

    return qc
'''


'''python
from qiskit import QuantumCircuit
import numpy
from numpy import pi
from qiskit.circuit.library import Gate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.ry(2*np.pi, 0)
    qc.append(ZGate().control(n - 1), range(n))

    return qc
'''


'''python
from qiskit import QuantumCircuit
import numpy
from qiskit.circuit.library import Gate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.ry(2*numpy.pi, 0)
    qc.append(ZGate().control(n - 1), range(n))

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.ry(2*numpy.pi, 0)
    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)
    qc.ry(2*numpy.pi, 0)
    qc.append(ZGate().control(n - 1), range(n))

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.ry(2*numpy.pi, 0)
    qc.append(ZGate().control(n - 1), range(n))

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.ry(2*pi, 0)
    qc.append(ZGate().control(n - 1), range(n))

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n==1:
        return qc
    else
        qc.ry(2*npi, 0)
        qc.append(ZGate().control(n - 1), range(n))
        return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n==1:
        return qc
    else
        qc.global_phase = np.pi
        qc.append(ZGate().control(n - 1), range(n))
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate
import numpy as np
from numpy import pi

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n==1:
        return qc
    else:
        qc.global_phase = np.pi
        qc.append(ZGate().control(n - 1), range(n))
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate
import numpy as np
from numpy import pi

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n==1:
        return qc
    else:
        for l in range (L):
            for i in range (n):
                if not ((l >> i) & 1):
                    qc.x(i)
            qc.append(Gate().control(n - 1), range(n))
            for i in range (n):
                if not ((l >> i) & 1):
                    qc.x(i)
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate
import numpy as np
from numpy import pi

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n==1:
        return qc
    else:
        for l in range (L):
            for i in range (n):
                if not ((l >> i) & 1):
                    qc.x(i)
            qc.append(Gate().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 ZGate
import numpy as np
from numpy import pi

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n==1:
        return qc
    else:
        for l in range (L):
            for i in range (n):
                if not ((l >> i) & 1):
                    qc.x(i)
            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 ZGate
import numpy as np

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n==1:
        return qc
    else:
        for l in range (L):
            for i in range (n):
                if not ((l >> i) & 1):
                    qc.x(i)
            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 ZGate
import numpy as np

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.append(ZGate)
            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 ZGate
import numpy as np

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:
                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 ZGate
import numpy as np

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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.x(i)
        qc.mcz(list(range(i),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:
    for i in range(n):
        qc.x(i)
        qc.mcz(list(range(i)),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:
    for i in range(n):
        qc.x(i)
        if i!=0:
            qc.mcz(list(range(i)),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:
    def f():
        qc.append(Gate().control(n - 1), range(n))
    def g(n):
        qc.x(n)
    def h(n):
        g(n)
        f(n)
        if n>0:
            h(n-1)
    h(n)
    g(n)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    def f():
        qc.append(XGate.control(n - 1), range(n))
    def g(n):
        qc.x(n)
    def h(n):
        g(n)
        f()
        if n>0:
            h(n-1)
    h(n-1)
    g(n-1)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate
v=0
def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    # Write your code here:
    def f():
        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)

    return qc
#solve(3,4).draw()
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate
v=0
def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

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

    return qc
'''


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

    return qc
'''


'''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:
        qc.append(ZGate().control(n - 1), range(n))

    return qc
solve(4,16).draw()
'''


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

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

    # Apply phase flip to states |0⟩ to |L-1⟩
    for i in range(L):
        binary_str = format(i, f'0{n}b')  # Convert to binary with n bits

        # Apply X gates to prepare the |i⟩ state
        for j, bit in enumerate(reversed(binary_str)):
            if bit == '0':
                qc.x(j)

        # Multi-controlled Z gate
        qc.h(n-1)
        qc.mcx(list(range(n-1)), n-1)
        qc.h(n-1)

        # Revert the X gates
        for j, bit in enumerate(reversed(binary_str)):
            if bit == '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 l in range(L):
        for i in range(n):
            if (l & (1<<i)):
                continue
            else:
                qc.x(i)
        qc.append(ZGate().control(n-1), range(n))
        for i in range(n):
            if (l & (1<<i)):
                continue
            else:
                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 (l & (1<<i)):
                continue
            else:
                qc.x(i)
        ## n == 1のときの例外処理
        if n == 1:
            qc.z(0)
        else:
            qc.append(ZGate().control(n-1), range(n))
        for i in range(n):
            if (l & (1<<i)):
                continue
            else:
                qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.h(n-1)
    qc.cx(range(0:n-2),n-1)
    qc.h(n-1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.h(n-1)
    qc.mcx(range(0:n-2),n-1)
    qc.h(n-1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.h(n-1)
    qc.mcx([0:n-2],n-1)
    qc.h(n-1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.h(n-1)
    qc.mcx(list(range(n-1)),n-1)
    qc.h(n-1)

    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):
        binary_l = format(l, f'0{n}b')

        for qubit, bit in enumerate(binary_l):
            if bit == '0':
                qc.x(qubit)

        qc.h(n-1)
        qc.mcx(list(range(n-1)), n-1)
        qc.h(n-1)

        for qubit, bit in enumerate(binary_l):
            if bit == '0':
                qc.x(qubit)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    # すべての量子ビットにアダマール変換を適用
    for q in range(n):
        qc.h(q)

    # |0>, |1>, ..., |L-1> に対して位相反転
    for i in range(L):
        # iをnビットのバイナリ表現に変換
        binary_i = format(i, '0' + str(n) + 'b')

        # ビットが0の場合にXゲートを適用（位相キックバックを使用）
        for qubit, bit in enumerate(reversed(binary_i)):
            if bit == '0':
                qc.x(qubit)

        # すべての量子ビットに制御Zゲートを適用
        qc.h(n-1)
        qc.mcx(list(range(n-1)), n-1)
        qc.h(n-1)

        # ビットが0の場合にXゲートを適用
        for qubit, bit in enumerate(reversed(binary_i)):
            if bit == '0':
                qc.x(qubit)

    # すべての量子ビットにアダマール変換を適用
    for q in range(n):
        qc.h(q)

    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 str_int[i] == '1':
            ctr_state = (str_int[:i])
            print(ctr_state)
            cx = qiskit.circuit.library.standard_gates.ZGate().control(i, ctrl_state=ctr_state)
            qc.append(cx, list(range(i+1)))

    return qc
'''


'''python
from qiskit import QuantumCircuit
import qiskit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if str_int[i] == '1':
            ctr_state = (str_int[:i])
            print(ctr_state)
            cx = qiskit.circuit.library.standard_gates.ZGate().control(i, ctrl_state=ctr_state)
            qc.append(cx, list(range(i+1)))

    return qc
'''


'''python
from qiskit import QuantumCircuit
import qiskit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    str_int = str(bin(L)).replace('0b', '').zfill(n)
    # Write your code here:
    for i in range(n):
        if str_int[i] == '1':
            ctr_state = (str_int[:i])
            print(ctr_state)
            cx = qiskit.circuit.library.standard_gates.ZGate().control(i, ctrl_state=ctr_state)
            qc.append(cx, list(range(i+1)))

    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)  # 各状態に対してZゲートを適用

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
     # オラクルの実装
    for i in range(L):
        qc.cz(i)  # 各状態に対して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)  # 各状態に対してZゲートを適用
    qc.cx(0,1)

    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)  # アダマールゲートを適用
        qc.z(i)  # 各状態に対してZゲートを適用
        qc.h(i)  # アダマールゲートを再度適用

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
     # オラクルの実装
    for i in range(n):
        qc.x([i])

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
     # オラクルの実装
    for i in range(n):
        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])

    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])
        qc.x([i])
        qc.h([i])

    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])
        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(n):
        qc.x(i)  # 各量子ビットにXゲートを適用
        qc.cz(i, n)  # 各量子ビットと補助ビット（nビット目）に対してControlled-Zゲートを適用
        qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
     # オラクルの実装
    for i in range(n):
        qc.x([i])  # 各量子ビットにXゲートを適用
        qc.cz([i], [n])  # 各量子ビットと補助ビット（nビット目）に対してControlled-Zゲートを適用
        qc.x([i])

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    if n == 1:
        qc.z(0)
        return qc

    for l in range(L):
        for i in range(n):
            if not ((l >> i) & 1):
                qc.x(i)
        # multi-controlled-Zゲートを適用
        qc.mcz(range(n-1), n-1)
        # 状態の復元
        # 理由：オラクル適用のために行った一時的な変更を元に戻すため
        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)
    # Write your code here:
    for i in range(n):
        qc.z(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    return qc
'''


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

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


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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

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

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

    return qc
'''