'''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)
    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 i in range(n):
        qc.h(i)
    for i in range(L-1):
        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(n):
        qc.h(i)
    for i in range(n-1):
        qc.cz(i,i+1)
        qc.cz(i+1,i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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

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

    for j in range(2**n-diff):
        qc.x([k for k in range(n) if k != j])
        for i in range(n-1):
            qc.cz(i,i+1)
        qc.x([k for k in range(n) if k != j])

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    for j in range(2**n-diff):
        qc.x([k for k in range(n) if k != j])
        for i in range(n-1):
            qc.cz(i,i+1)
        qc.x([k for k in range(n) if k != j])

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    for n in range(1,5+1):
        qc = QuantumCircuit(n)
        for L in range(1,2**n):
            qc.h([i for i in range(n)])
            if n == L == 1:
                qc.z(0)
            else:
                for i in range(n-1):
                    qc.cz(i,i+1)

                for j in range(L):
                    qc.x([k for k in range(n) if k != j])
                    for i in range(n-1):
                        qc.cz(i,i+1)
                    qc.x([k for k in range(n) if k != j])

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.h([i for i in range(n)])
    if n == L == 1:
        qc.z(0)
    else:
        for i in range(n-1):
            qc.cz(i,i+1)

        for j in range(L):
            qc.x([k for k in range(n) if k != j])
            for i in range(n-1):
                qc.cz(i,i+1)
            qc.x([k for k in range(n) if k != j])

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.h([i for i in range(n)])
    if n == L == 1:
        qc.z(0)
    else:
        for i in range(n-1):
            qc.cz(i,i+1)
        for j in range(L-1):
            qc.x([k-1 for k in range(n) if k != j])
            for i in range(n-1):
                qc.cz(i,i+1)
            qc.x([k-1 for k in range(n) if k != j])
            qc.barrier()

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.h([i for i in range(n)])
    if n == L == 1:
        qc.z(0)
    else:
        for i in range(n-1):
            qc.cz(i,i+1)
        for j in range(L-1):
            qc.x([k-1 for k in range(n) if k != j])
            for i in range(n-1):
                qc.cz(i,i+1)
            qc.x([k-1 for k in range(n) if k != j])

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.h([i for i in range(n)])
    if n == L == 1:
        qc.z(0)
    else:
        qc.x([i for i in range(n)])
        for i in range(n-1):
            qc.cz(i,i+1)
        qc.x([i for i in range(n)])
        for j in range(L-1):
            if j == n:
                for i in range(n-1):
                    qc.cz(i,i+1)
            else:
                qc.x([k-1 for k in range(n) if k != j])
                for i in range(n-1):
                    qc.cz(i,i+1)
                qc.x([k-1 for k in range(n) if k != j])

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.h([i for i in range(n)])
    if n == L == 1:
        qc.z(0)
    else:
        qc.x([i for i in range(n)])
        for i in range(n-1):
            qc.cz(i,i+1)
        qc.x([i for i in range(n)])

        for j in range(L-1):
            if j == n:
                for i in range(n-1):
                    qc.cz(i,i+1)
            else:
                qc.x([k for k in range(n) if k != j])
                for i in range(n-1):
                    qc.cz(i,i+1)
                qc.x([k for k in range(n) if k != j])

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.h([i for i in range(n)])
    U = MCPhaseGate(lam=math.pi, num_ctrl_qubits=n-1)
    if n == L == 1:
        qc.z(0)
    else:
        qc.x([i for i in range(n)])
        qc.append(U,[i for i in range(n)])
        qc.x([i for i in range(n)])
        for j in range(L):
            if j == n:
                qc.append(U,[i for i in range(n)])
            else:
                qc.x([k for k in range(n) if k != j])
                qc.append(U,[i for i in range(n)])
                qc.x([k for k in range(n) if k != j])

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.h([i for i in range(n)])
    U = MCPhaseGate(lam=math.pi, num_ctrl_qubits=n)
    if n == L == 1:
        qc.x(0)
        qc.z(0)
        qc.x(0)
    else:
        qc.x([i for i in range(n)])
        qc.append(U,[i for i in range(n)])
        qc.x([i for i in range(n)])
        for j in range(L):
            if j == n:
                qc.append(U,[i for i in range(n)])
            else:
                qc.x([k for k in range(n) if k != j])
                qc.append(U,[i for i in range(n)])
                qc.x([k for k in range(n) if k != j])

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.h([i for i in range(n)])
    if n == L == 1:
        qc.x(0)
        qc.z(0)
        qc.x(0)

    else:
        U = MCPhaseGate(lam=math.pi, num_ctrl_qubits=n-1)
        qc.x([i for i in range(n)])
        qc.append(U,[i for i in range(n)])
        qc.x([i for i in range(n)])
        for j in range(L):
            if j == n:
                qc.append(U,[i for i in range(n)])
            else:
                qc.x([k for k in range(n) if k != j])
                qc.append(U,[i for i in range(n)])
                qc.x([k for k in range(n) if k != j])

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.h([i for i in range(n)])
    if n == L == 1:
        qc.x(0)
        qc.z(0)
        qc.x(0)

    else:
        U = MCPhaseGate(lam=math.pi, num_ctrl_qubits=n-)
        qc.x([i for i in range(n)])
        qc.append(U,[i for i in range(n)])
        qc.x([i for i in range(n)])
        for j in range(L-1):
            if j == n:
                qc.append(U,[i for i in range(n)])
            else:
                qc.x([k for k in range(n) if k != j])
                qc.append(U,[i for i in range(n)])
                qc.x([k for k in range(n) if k != j])

    return qc
'''


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

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.x(i)
        else:
            qc.append(qlib.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, QuantumRegister
import qiskit.circuit.library as qlib
import numpy as np

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.x(0)
        else:
            qc.append(qlib.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)

    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 PhaseGate
import math

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

    for i in range(n):
        if (1 << i) & L != 0:
            qc.append(PhaseGate(math.pi).control(i), range(n))

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L -= 1
    info = []
    for i in range(n):
        v = (L >> (n-1 - i)) & 1
        if v == 0:
            for i0, v0 in enumerate(info):
                if v0 == 1:
                    qc.x(i0)

            # mcz
            qc.h(i)
            qc.mcx([j for j in ragne(i)], [i])
            qc.h(i)

            for i0, v0 in enumerate(info):
                if v0 == 1:
                    qc.x(i0)

        info.append(v)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L -= 1
    info = []
    for i in range(n):
        v = (L >> (n-1 - i)) & 1
        if v == 0:
            for i0, v0 in enumerate(info):
                if v0 == 1:
                    qc.x(i0)

            # mcz
            qc.h(i)
            qc.mcx([j for j in ragne(i)], [i]) if i > 0 else qc.x(0)
            qc.h(i)

            for i0, v0 in enumerate(info):
                if v0 == 1:
                    qc.x(i0)

        info.append(v)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L -= 1
    info = []
    for i in range(n):
        v = (L >> (n-1 - i)) & 1
        if v == 0:
            for i0, v0 in enumerate(info):
                if v0 == 1:
                    qc.x(i0)

            for i0, v0 in enumerate(info):
                if v0 == 1:
                    qc.x(i0)

        info.append(v)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L -= 1
    info = []
    for i in range(n):
        v = (L >> (n-1 - i)) & 1
        if v == 0:
            for i0, v0 in enumerate(info):
                if v0 == 1:
                    qc.x(i0)

            # mcz
            qc.h(i)
            qc.mcx([j for j in ragne(i)], i)
            qc.h(i)

            for i0, v0 in enumerate(info):
                if v0 == 1:
                    qc.x(i0)

        info.append(v)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L -= 1
    info = []
    for i in range(n):
        v = (L >> (n-1 - i)) & 1
        if v == 0:
            for i0, v0 in enumerate(info):
                if v0 == 1:
                    qc.x(i0)

            # mcz
            qc.h(i)
            qc.mcx([j for j in ragne(i)], i) if i > 0 else qc.x(i)
            qc.h(i)

            for i0, v0 in enumerate(info):
                if v0 == 1:
                    qc.x(i0)

        info.append(v)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L -= 1
    info = []
    for i in range(n):
        v = (L >> (n-1 - i)) & 1
        if v == 0:
            for i0, v0 in enumerate(info):
                if v0 == 0:
                    qc.x(i0)

            # mcz
            qc.h(i)
            qc.mcx([j for j in ragne(i)], i) if i > 0 else qc.x(i)
            qc.h(i)

            for i0, v0 in enumerate(info):
                if v0 == 0:
                    qc.x(i0)

        info.append(v)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L -= 1
    info = []
    for i in range(n-1, -1, -1):
        v = (L >> (n-1 - i)) & 1
        if v == 0:
            for i0, v0 in enumerate(info):
                if v0 == 0:
                    qc.x(i0)

            # mcz
            qc.h(i)
            qc.mcx([j for j in ragne(i)], i) if i > 0 else qc.x(i)
            qc.h(i)

            for i0, v0 in enumerate(info):
                if v0 == 0:
                    qc.x(i0)

        info.append(v)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L -= 1
    info = []
    for i in range(n):
        v = (L >> (n-1 - i)) & 1
        if v == 0:
            for i0, v0 in enumerate(info):
                if v0 == 0:
                    qc.x(i0)

            qc.mcp([j for j in ragne(i)], i) if i > 0 else qc.x(i)

            for i0, v0 in enumerate(info):
                if v0 == 0:
                    qc.x(i0)

        info.append(v)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L -= 1
    info = []
    for i in range(n):
        v = (L >> (n-1 - i)) & 1
        if v == 0:
            for i0, v0 in enumerate(info):
                if v0 == 0:
                    qc.x(i0)

            qc.mcp(pi, [j for j in ragne(i)], i) if i > 0 else qc.z(i)

            for i0, v0 in enumerate(info):
                if v0 == 0:
                    qc.x(i0)

        info.append(v)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L -= 1
    info = []
    for i in range(n):
        v = (L >> (n-1 - i)) & 1
        if v == 0:
            for i0, v0 in enumerate(info):
                if v0 == 0:
                    qc.x(n-1 - i0)

            qc.mcp(pi, [n-1 - j for j in ragne(i)], n-1 - i) if i > 0 else qc.z(n-1 - i)

            for i0, v0 in enumerate(info):
                if v0 == 0:
                    qc.x(n-1 - i0)

        info.append(v)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L -= 1
    info = []
    for i in range(n):
        v = (L >> (n-1 - i)) & 1
        if v == 0:

            qc.mcp(pi, [n-1 - j for j in ragne(i)], n-1 - i) if i > 0 else qc.z(n-1 - i)

            qc.x(n-1 - i0)

        info.append(v)

    for i0, v0 in enumerate(info):
        if v0 == 0:
            qc.x(n-1 - i0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L -= 1
    info = []
    for i in range(n):
        v = (L >> (n-1 - i)) & 1
        if v == 0:

            qc.mcp(pi, [n-1 - j for j in ragne(i)], n-1 - i) if i > 0 else qc.z(n-1 - i)

            qc.x(n-1 - i)

        info.append(v)

    for i0, v0 in enumerate(info):
        if v0 == 0:
            qc.x(n-1 - i0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L -= 1
    info = []
    for i in range(n):
        v = (L >> (n-1 - i)) & 1
        if v == 0:

            qc.z(n-1 - i)

            qc.x(n-1 - i)

        info.append(v)

    for i0, v0 in enumerate(info):
        if v0 == 0:
            qc.x(n-1 - i0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L -= 1
    info = []
    for i in range(n):
        v = (L >> (n-1 - i)) & 1
        if v == 0:

            qc.mcp(pi, n-1:n-1-i:-1, n-1 - i) if i > 0 else qc.z(n-1 - i)

            qc.x(n-1 - i)

        info.append(v)

    for i0, v0 in enumerate(info):
        if v0 == 0:
            qc.x(n-1 - i0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L -= 1
    info = []
    for i in range(n):
        v = (L >> (n-1 - i)) & 1
        if v == 0:

            qc.mcp(pi, qc.qregs[0][n-1-i+1:n], n-1 - i) if i > 0 else qc.z(n-1 - i)

            qc.x(n-1 - i)

        info.append(v)

    for i0, v0 in enumerate(info):
        if v0 == 0:
            qc.x(n-1 - i0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L -= 1
    from math import pi
    info = []
    for i in range(n):
        v = (L >> (n-1 - i)) & 1
        if v == 0:

            qc.mcp(pi, range(n-1-i+1, n-1), n-1 - i) if i > 0 else qc.z(n-1 - i)

            qc.x(n-1 - i)

        info.append(v)

    for i0, v0 in enumerate(info):
        if v0 == 0:
            qc.x(n-1 - i0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L -= 1
    import math
    info = []
    for i in range(n):
        v = (L >> (n-1 - i)) & 1
        if v == 0:

            qc.mcp(math.pi, range(n-1-i+1,n), n-1 - i) if i > 0 else qc.z(n-1 - i)

            qc.x(n-1 - i)

        info.append(v)

    for i0, v0 in enumerate(info):
        if v0 == 0:
            qc.x(n-1 - i0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L -= 1
    import math
    info = []
    for i in range(n):
        v = (L >> (n-1 - i)) & 1
        if v == 0:

            qc.mcp(math.pi, range(n-1-i+1,n), n-1 - i) if i > 0 else qc.p(math.pi, n-1 - i)

            qc.x(n-1 - i)

        info.append(v)

    for i0, v0 in enumerate(info):
        if v0 == 0:
            qc.x(n-1 - i0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L -= 1
    import math
    info = []
    for i in range(n):
        v = (L >> (n-1 - i)) & 1
        if v == 0:

            qc.mcp(math.pi, range(n-1-i+1,n), n-1 - i)
            qc.x(n-1 - i)

        info.append(v)

    for i0, v0 in enumerate(info):
        if v0 == 0:
            qc.x(n-1 - i0)

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


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

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

    for i in range(n):
        qc.x(i)
    control_qubits = qr[0:n-1]  # First n-1 qubits are controls
    target_qubit = qr[n-1]    # Last qubit is the target
    mcz_gate = ZGate().control(num_ctrl_qubits=n - 1)
    qc.append(mcz_gate, control_qubits[:] + [target_qubit])
    for i in range(n):
        qc.x(i)

    for i in range(1,L):
        x = bin(i)[2:].zfill(n)
        c=0
        for j in range(n):
            if x[j] == 1:
                qc.z(c)
                break
            c+=1

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(L):
        for j in range(n):
            if ~i >> j & 1: qc.x(j)
        qc.append(ZGate().control(n - 1), range(n))
        for j in range(n):
            if ~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)
    for i in range(L):
        for j in range(n):
            if ~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 ~i >> j & 1: qc.x(j)

    return qc
'''


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

#from qiskit import QuantumCircuit, QuantumRegister
#from qiskit import transpile
#from qiskit.circuit.library import RealAmplitudes
#from qiskit.quantum_info import SparsePauliOp
#from qiskit_aer import AerSimulator
#from qiskit import QuantumCircuit
#from qiskit.visualization import plot_histogram, plot_state_city

def flip(qc: QuantumCircuit, n: int, bit: int):
    qc.append(ZGate().control(n - 1), range(n)) ## 0 に - 1 を掛ける
    ## x^{i-1} 1 0^{n-i-1}
    for i in range(bit):
        qc.x(i)
        if i == n - 1:
            qc.z(n - 1)
        else:
            qc.append(ZGate().control(n - i - 1), range(i, n))
        qc.x(i)

## [0, L) を flip
def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.h(i)
    decompose = []
    for i in range(n):
        if (L >> i) & 1:
            decompose = [(L - (1 << i), i)] + decompose
    for (L, i) in decompose:
        ## [L - (1 << i), L) を flip
        flip(qc, n, i)
        qc.x(i)
    for (L, i) in decompose:
        qc.x(i)
    for i in range(n):
        qc.x(i)

    #print(qc.depth())

    #qc.save_statevector()
    #simulator = AerSimulator(method='statevector')
    #circ = transpile(qc, simulator)
    #result = simulator.run(circ).result()
    #statevector = result.get_statevector(circ)
    #plot_state_city(statevector, title='Bell state', filename='statevector.png')
    #print(statevector)
    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 maska in range(L):
        for i in range(n):
            if (not (maska&(1<<i))):
                qc.x(i)
        qc.append(ZGate().control(n-1),range(n))
        for i in range(n):
            if (not (maska&(1<<i))):
                qc.x(i)
    return qc
'''


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

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


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

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


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

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


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.append(ZGate().control(n-1), range(n))

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    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.append(ZGate().control(0), range())

    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:
    a = ZGate()
    # qc.append(ZGate().control(0), range())

    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.append(ZGate().control(0), range(1))

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.append(CZGate().control(0), range(1))

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    a = CZGate()
    # qc.append(CZGate().control(0), range(1))

    return qc
'''


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

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

    for 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

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


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

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


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

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


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

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


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

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

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


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library.standard_gates 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)

        qc.append(ZGate.control(n-1), range(n))
        ## control(n-1): controlled by n-1 qbits.
        ## range(n), this time, use 0~n-2 as controller, n-1 as target

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


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library.standard_gates 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.append(ZGate.control(n-1), range(n))
        ## control(n-1): controlled by n-1 qbits.
        ## range(n), this time, use 0~n-2 as controller, n-1 as target
        else:
            qc.z(0)

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


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library.standard_gates 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.append(ZGate().control(n-1), range(n))
        ## control(n-1): controlled by n-1 qbits.
        ## range(n), this time, use 0~n-2 as controller, n-1 as target
        else:
            qc.z(0)

        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(L):
        qc.z(i)  # Zゲートを使用して複素振幅に -1 をかける
    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().control(n - 1), range(n))

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for i in range(L):
        binary_i = format(i, f'0{n}b')
        for j, bit in enumerate(binary_i):
            if bit == '0':
                qc.x(x[j])

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

        for j, bit in enumerate(binary_i):
            if bit == '0':
                qc.x(x[j])

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for i in range(L):
        binary_i = format(i, f'0{n}b')[::-1]
        for j, bit in enumerate(binary_i):
            if bit == '0':
                qc.x(x[j])

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

        for j, bit in enumerate(binary_i):
            if bit == '0':
                qc.x(x[j])

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for i in range(L):
        binary_i = format(i, f'0{n}b')[::-1]
        for j, bit in enumerate(binary_i):
            if bit == '0':
                qc.x(x[j])

        if n == 1:
            qc.z(x[0])
        else:
            qc.h(x[n-1])
            qc.mcx(list(range(n-1)), x[n-1])
            qc.h(x[n-1])

        for j, bit in enumerate(binary_i):
            if bit == '0':
                qc.x(x[j])

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

        if n == 1:
            qc.z(x[0])
        else:
            qc.h(x[n-1])
            qc.mcx(list(range(n-1)), x[n-1])
            qc.h(x[n-1])

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    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.h(n-1)
            qc.mcx(list(range(n-1)), n-1)
            qc.h(n-1)

        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 XGate, ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.h(range(n))
    for i in range(L):
        s = ""
        for k in range(1, n):
            c = '1' if (i >> k & 1) else '0'
            s += c
        print(s)
        if i % 2 == 0:
            qc.append(XGate().control(n - 1, ctrl_state=s), range(n))
        qc.append(ZGate().control(n - 1, ctrl_state=s), range(n))
        if i % 2 == 0:
            qc.append(XGate().control(n - 1, ctrl_state=s), range(n))
    return qc
'''


'''python
def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.h(range(n))
    for i in range(L):
        s = ""
        for k in range(1, n):
            c = '1' if (i >> k & 1) else '0'
            s += c
        print(s)
        append_gate = lambda gate: qc.append(gate(), [0]) if n == 1 else qc.append(gate().control(n - 1, ctrl_state = s), range(n))
        if i % 2 == 0:
            append_gate(XGate)
        append_gate(ZGate)
        if i % 2 == 0:
            append_gate(XGate)
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.h(range(n))
    for i in range(L):
        s = ""
        for k in range(1, n):
            c = '1' if (i >> k & 1) else '0'
            s += c
        print(s)
        append_gate = lambda gate: qc.append(gate(), [0]) if n == 1 else qc.append(gate().control(n - 1, ctrl_state = s), range(n))
        if i % 2 == 0:
            append_gate(XGate)
        append_gate(ZGate)
        if i % 2 == 0:
            append_gate(XGate)
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(L):
        s = ""
        for k in range(1, n):
            c = '1' if (i >> k & 1) else '0'
            s += c
        print(s)
        append_gate = lambda gate: qc.append(gate(), [0]) if n == 1 else qc.append(gate().control(n - 1, ctrl_state = s), range(n))
        if i % 2 == 0:
            append_gate(XGate)
        append_gate(ZGate)
        if i % 2 == 0:
            append_gate(XGate)
    return qc
'''


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

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


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

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

    # 位相を反転する必要がある状態をマーキング
    # ここでは |00>, |01>, |10> に該当
    for i in range(L - 1):
        # リトルエンディアン表現に基づくビットパターンを生成
        bit_pattern = format(i, f'0{n}b')

        # 必要な CNOT ゲートを適用
        for qubit_idx, bit in enumerate(bit_pattern):
            if bit == '0':
                qc.x(qubit_idx)

        # マルチ制御 Z ゲートを適用（ここでは単一の Z ゲートで代替）
        qc.append(ZGate().control(n - 1), range(n))

        # CNOT ゲートを元に戻す
        for qubit_idx, bit in enumerate(bit_pattern):
            if bit == '0':
                qc.x(qubit_idx)

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

    i = 0
    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 i in range(n):
        qc.h(i)
        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(n):
        qc.h(i)
    qml.z(0)
    qml.x(0)
    qml.z(0)
    qml.x(0)
    qml.cz(0,1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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

    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)
    # 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:
        for i in range(n):
        qc.h(i)

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

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

        print(bits)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

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

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

    return qc

    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.z(0)
    qc.x(0)
    qc.z(0)
    for i in range(1, n):
        qc.h(i)

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

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

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

    return qc

    return qc
'''


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

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

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

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

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

    return qc
'''


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

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.x(j)

    qc.append(GlobalPhase(theta=math.pi).control(n), range(n))

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

    return qc
'''


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

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.x(j)

    qc.append(GlobalPhase(phase=math.pi).control(n), range(n))

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

    return qc
'''


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

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.x(j)

    qc.append(GlobalPhaseGate(phas=math.pi).control(n), range(n))

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

    return qc
'''


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

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.x(j)

    qc.append(GlobalPhaseGat(phase=math.pi).control(n), range(n))

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

    return qc
'''


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

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.x(j)

    qc.append(GlobalPhaseGate(phase=math.pi).control(n), range(n))

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

    return qc
'''


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

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<<(n-1-j)))==0:
                qc.x(j)

    qc.append(GlobalPhaseGate(phase=math.pi).control(n), range(n))

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

    return qc
'''


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

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<<(n-1-j)))==0:
                qc.x(j)

        qc.append(GlobalPhaseGate(phase=math.pi).control(n), range(n))

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

    return qc
'''


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

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.x(j)

        qc.append(GlobalPhaseGate(phase=math.pi).control(n), range(n))

        for j in range(n):
            if (i&(1<<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:
    qc.append(z.control(n-1),range(n))

    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.cz(i,n)

    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.cz(i,i+1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if i<n-1:
            qc.cz(i+1,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.cz(i,n-1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)
    if n==1:
        qc.z(0)
    return qc
    for i in range(n):
        qc.cz(i,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(range(n))
    qc.x(range(n))
    qc.h(n-1)
    qc.append(x.control(n-1),range(n))
    qc.h(n-1)
    qc.x(range(n))
    qc.h(range(n))
    return qc
'''


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.h(range(n))
    qc.append(z.control(n-1),n)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.h(range(n))
    qc.append(z.control(n-1),range(n))
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.h(range(n))
    qc.append(x.control(n-1),range(n))
    return qc
'''


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.h(range(n))
    qc.append(z().control(n-1),range(n))
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n==1:
        return qc.z(0)
    qc.h(range(n))
    qc.append(z.control(n-1),range(n))
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(n-1)
    qc.h(n-1)
    qc.append(z.control(n-1),range(n))
    qc.h(n-1)
    qc.x(n-1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(n-1)
    qc.h(n-1)
    qc.append(x.control(n-1),range(n))
    qc.h(n-1)
    qc.x(n-1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(n-1)
    qc.h(n-1)
    qc.append(.control(n-2),range(n-1))
    qc.h(n-1)
    qc.x(n-1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(n-1)
    qc.h(n-1)
    qc.append(.control(L-1),range(n))
    qc.h(n-1)
    qc.x(n-1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(n-1)
    qc.h(n-1)
    qc.append(z.control(L-1),range(n))
    qc.h(n-1)
    qc.x(n-1)
    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 Gate

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

    qc.append(Gate().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.h(0)

    qc.append(Gate().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.h(0)

    for i in range(L - 1):
        qc.append(Gate().control(n - 1), range(n))

    return qc
'''


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

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

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

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

    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 k in range(n):
            if not ((i >> k) & 1):
                qc.x(k)
    return qc
'''


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

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

    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 k in range(n):
            if not ((i >> k) & 1):
                qc.x(k)
    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):
        bitstr = format(i, f'0{n}b')  # i を nビットの2進数に変換
        ctrl_bits = []

        # Xゲートで0制御を実現（後で戻す）
        for j, bit in enumerate(bitstr):
            if bit == '0':
                qc.x(j)
            ctrl_bits.append(j)

        # 多制御Zゲート：Zゲートに制御n-1個をつけて適用
        mcZ = ZGate().control(n - 1)
        qc.append(mcZ, ctrl_bits)

        # Xゲートを元に戻す
        for j, bit in enumerate(bitstr):
            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 i in range(L):
        bitstr = format(i, f'0{n}b')  # i を nビットの2進数に変換
        ctrl_bits = []

        # Xゲートで0制御を実現（後で戻す）
        for j, bit in enumerate(bitstr):
            if bit == '0':
                qc.x(j)
            ctrl_bits.append(j)

        # 多制御Zゲート：Zゲートに制御n-1個をつけて適用
        mcZ = ZGate().control(n - 1)
        qc.append(mcZ, ctrl_bits)

        # Xゲートを元に戻す
        for j, bit in enumerate(bitstr):
            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)

    flip = list()
    for i in reversed(range(n)):
        flip.append( L // 2**(i) )
        L = L % 2**(i)
    if flip[0]:
        qc.z(n-1)
    for i in range(1,n):
        if flip[i]:
            qc.append(ZGate().control(i), list(range(n-i,n))+[n-1-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 LL in range(L):
        flip = list()
        for i in reversed(range(n)):
            flip.append( LL // 2**(i) )
            LL = LL % 2**(i)

        for i in range(n):
            if not flip[i]:
                qc.z(i)

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

         for i in range(n):
            if not flip[i]:
                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 LL in range(L):
        flip = list()
        for i in reversed(range(n)):
            flip.append( LL // 2**(i) )
            LL = LL % 2**(i)

        for i in range(n):
            if not flip[i]:
                qc.z(i)

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

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

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

    for LL in range(L):
        flip = list()
        for i in reversed(range(n)):
            flip.append( LL // 2**(i) )
            LL = LL % 2**(i)

        for i in range(n):
            if not flip[i]:
                qc.z(i)

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

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

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

    for LL in range(L):
        flip = list()
        for i in reversed(range(n)):
            flip.append( LL // 2**(i) )
            LL = LL % 2**(i)
        print(flip)
        for i in range(n):
            if not flip[i]:
                qc.z(n-i-1)

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

        for i in range(n):
            if not flip[i]:
                qc.z(n-i-1)

    return qc
'''


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

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

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

    for LL in range(L):
        flip = list()
        for i in reversed(range(n)):
            flip.append( LL // 2**(i) )
            LL = LL % 2**(i)
        print(flip)
        for i in range(n):
            if not flip[i]:
                qc.x(i)

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

        for i in range(n):
            if not flip[i]:
                qc.x(i)

    return qc
'''


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

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

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

    for LL in range(L):
        flip = list()
        for i in reversed(range(n)):
            flip.append( LL // 2**(i) )
            LL = LL % 2**(i)
        for i in range(n):
            if not flip[i]:
                qc.x(i)

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

        for i in range(n):
            if not flip[i]:
                qc.x(i)

    return qc
'''


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

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

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

    for LL in range(L):
        flip = list()
        for i in reversed(range(n)):
            flip.append( LL // 2**(i) )
            LL = LL % 2**(i)
        for i in range(n):
            if not flip[i]:
                qc.x(n-1-i)

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

        for i in range(n):
            if not flip[i]:
                qc.x(n-1-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
from qiskit.circuit.library import XGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for l in range(L):
        for i in range(n):
            if (l>>i)&1:
                qc.x(i)
        qc.append(XGate().control(0), range(n))
        for i in range(n):
            if (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)
    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(0), 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)
    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(0), 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)
    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:
    if L == (1<<n):
        return qc
    tmp = 0
    for i in reversed(range(n)):
        if tmp+(1<<i) < L:
            if i == n-1:
                qc.z(i)
            qc.mcz(range(i+1,n),i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L == (1<<n):
        return qc
    tmp = 0
    for i in reversed(range(n)):
        if tmp+(1<<i) < L:
            if i == n-1:
                qc.z(i)
            else:
                qc.mcz(range(i+1,n),i)
            tmp += (1<<i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L == (1<<n):
        return qc
    tmp = 0
    for i in reversed(range(n)):
        if tmp+(1<<i) < L:
            if i == n-1:
                qc.z(i)
            else:
                qc.mcx(list(range(i+1,n)),i)
            tmp += (1<<i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.h(i)
    # Write your code here:
    if L == (1<<n):
        return qc
    tmp = 0
    tmp_flip = []
    for i in reversed(range(n)):
        print(tmp)
        print(1<<i)
        if tmp+(1<<i) <= L:
            if i == n-1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                for y in tmp_flip:
                    qc.x(y)
                qc.x(i)
                qc.h(i)
                qc.mcx(list(range(i+1,n)),i)
                qc.h(i)
                qc.x(i)
                for y in tmp_flip:
                    qc.x(y)
            tmp += (1<<i)
        else:
            tmp_flip.append(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.h(i)
    # Write your code here:
    if L == (1<<n):
        qc.x(n-1)
        qc.z(n-1)
        qc.x(n-1)
        qc.z(n-1)
        return qc
    tmp = 0
    tmp_flip = []
    for i in reversed(range(n)):
        print(tmp)
        print(1<<i)
        if tmp+(1<<i) <= L:
            if i == n-1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                for y in tmp_flip:
                    qc.x(y)
                qc.x(i)
                qc.h(i)
                qc.mcx(list(range(i+1,n)),i)
                qc.h(i)
                qc.x(i)
                for y in tmp_flip:
                    qc.x(y)
            tmp += (1<<i)
        else:
            tmp_flip.append(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.h(i)
    # Write your code here:
    if L == (1<<n):
        qc.x(n-1)
        qc.z(n-1)
        qc.x(n-1)
        qc.z(n-1)
        return qc
    tmp = 0
    tmp_flip = []
    for i in reversed(range(n)):
        # print(tmp)
        # print(1<<i)
        if tmp+(1<<i) <= L:
            if i == n-1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                for y in tmp_flip:
                    qc.x(y)
                qc.x(i)
                qc.h(i)
                qc.mcx(list(range(i+1,n)),i)
                qc.h(i)
                qc.x(i)
                for y in tmp_flip:
                    qc.x(y)
            tmp += (1<<i)
        else:
            tmp_flip.append(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L == (1<<n):
        qc.x(n-1)
        qc.z(n-1)
        qc.x(n-1)
        qc.z(n-1)
        return qc
    tmp = 0
    tmp_flip = []
    for i in reversed(range(n)):
        # print(tmp)
        # print(1<<i)
        if tmp+(1<<i) <= L:
            if i == n-1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                for y in tmp_flip:
                    qc.x(y)
                qc.x(i)
                qc.h(i)
                qc.mcx(list(range(i+1,n)),i)
                qc.h(i)
                qc.x(i)
                for y in tmp_flip:
                    qc.x(y)
            tmp += (1<<i)
        else:
            tmp_flip.append(i)

    return qc
'''


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

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

        qc.append(XGate().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 XGate

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

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

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

    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
import math

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

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

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

    return qc

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


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

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

    return qc

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


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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import 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.append(ZGate().control(n-1),range(n))
        for j in range(n):
            if not (l>>j & 1):
                qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import 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.append(ZGate().control(n-1),range(n))
        for j in range(n):
            if not ((l>>j) & 1):
                qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import 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.append(ZGate().control(n-1),range(n))
        for j in range(n):
            if not ((l>>j) & 1):
                qc.x(j)

    return qc
'''


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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def create_oracle(n, L):
    # Create a quantum register with n qubits
    qr = QuantumRegister(n)
    qc = QuantumCircuit(qr)

    # Apply the oracle O
    for i in range(L):
        # Convert i to binary and apply the necessary gates
        binary_representation = format(i, f'0{n}b')  # Get binary representation of i with n bits
        control_bits = [int(bit) for bit in binary_representation]  # Convert to list of bits

        # Apply a controlled-Z gate (or equivalent) to flip the sign
        # We can use a series of CNOTs to achieve this
        for j in range(n):
            if control_bits[j] == 1:
                # Apply a CNOT from control qubit to the last qubit (target)
                if j == n - 1:
                    # If the control is the last qubit, we need to apply a Z gate instead
                    qc.z(qr[j])
                else:
                    qc.cx(qr[j], qr[n - 1])  # CNOT from control to target

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

from qiskit import QuantumCircuit

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

    # Apply a multi-controlled Z gate for states 0 to L-1
    # We will use a series of CNOT gates to create the effect of a multi-controlled Z gate
    for i in range(L):
        # Convert i to binary and apply CNOTs
        binary_representation = format(i, f'0{n}b')  # Get binary representation of i with n bits
        for j in range(n):
            if binary_representation[j] == '1':
                qc.x(j)  # Apply X gate to flip the qubit to |1> if needed
        qc.h(n-1)  # Apply Hadamard to the last qubit to create superposition
        qc.mct(list(range(n-1)), n-1)  # Apply multi-controlled Toffoli (mct) to flip the last qubit
        qc.h(n-1)  # Apply Hadamard to the last qubit to revert it back
        for j in range(n):
            if binary_representation[j] == '1':
                qc.x(j)  # Revert the X gates to restore original state

    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:
    def flip_phase(i):
        bits = [b == "1" for b in f"{i:05b}"][::-1]
        for j in range(n):
            if bits[j]:
                qc.x(j)
            qc.append(ZGate().control(n - 1), range(n))
            if bits[j]:
                qc.x(j)
    for i in range(L):
        flip_phase(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:
    def flip_phase(i):
        bits = [b == "1" for b in f"{i-1:05b}"][::-1]
        for j in range(n):
            if bits[j]:
                qc.x(j)
            qc.append(ZGate().control(n - 1), range(n))
            if bits[j]:
                qc.x(j)
    for i in range(L):
        flip_phase(i)
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.h(i)
    # Write your code here:
    for i in range(L):
        bits = [b == "1" for b in f"{i:05b}"[::-1]]
        # print(i, bits)
        for j in range(n):
            if bits[j]:
                qc.x(j)
        qc.append(ZGate().control(n - 1), range(n))
        for j in range(n):
            if bits[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)
    for i in range(n):
        qc.h(i)
    # Write your code here:
    for i in range(L):
        bits = [b == "1" for b in f"{i:05b}"]
        # print(i, bits)
        for j in range(n):
            if bits[j]:
                qc.x(j)
        qc.append(ZGate().control(n - 1), range(n))
        for j in range(n):
            if bits[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)
    for i in range(n):
        qc.h(i)
    # Write your code here:
    for i in range(L):
        bits = [b == "1" for b in f"{i:05b}"[::-1]]
        # print(i, bits)
        for j in range(n):
            if not bits[j]:
                qc.x(j)
        qc.append(ZGate().control(n - 1), range(n))
        for j in range(n):
            if not bits[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)
    for i in range(n):
        qc.h(i)
    # Write your code here:
    for i in range(L):
        bits = [b == "1" for b in f"{i:05b}"]
        # print(i, bits)
        for j in range(n):
            if not bits[j]:
                qc.x(j)
        qc.append(ZGate().control(n - 1), range(n))
        for j in range(n):
            if not bits[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):
        bits = [b == "1" for b in f"{i:05b}"]
        # print(i, bits)
        for j in range(n):
            if not bits[j]:
                qc.x(j)
        qc.append(ZGate().control(n - 1), range(n))
        for j in range(n):
            if not bits[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):
        bits = [b == "1" for b in f"{i:05b}"[::-1]]
        # print(i, bits)
        for j in range(n):
            if not bits[j]:
                qc.x(j)
        qc.append(ZGate().control(n - 1), range(n))
        for j in range(n):
            if not bits[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):
        bits = [b == "1" for b in f"{i:05b}"]
        # print(i, bits)
        for j in range(n):
            if not bits[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 bits[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):
        bits = [b == "1" for b in f"{i:05b}"[::-1]]
        # print(i, bits)
        for j in range(n):
            if not bits[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 bits[j]:
                qc.x(j)
    return qc
'''


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

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

    def rec(n: int, L: int, control: list[int]):
        if n == 0:
            return
        nonlocal qc
        i = n - 1
        w = 1 << i
        def X():
            nonlocal qc
            if control:
                qc.append(XGate().control(len(control)), list(control) + [i])
            else:
                qc.x(i)
        def Z():
            nonlocal qc
            if control:
                qc.append(ZGate().control(len(control)), list(control) + [i])
            else:
                qc.z(i)
        X()
        rev = L >= w
        if rev:
            Z()
            L -= w
            X()
        rec(i, L, control + [i])
        if not rev:
            X()

    rec(n, L, [])

    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 set in range(L):
        #print(format(set, f'0{n-1}b'))

        if not (set >> (n - 1)) & 1:
            qc.x(n-1)
            s = format(set, f'0{n-1}b')
            qc.append(ZGate().control(n - 1, ctrl_state=s), range(n))
            qc.x(n-1)
        else:
            s = format(set - (1 << (n - 1)), f'0{n-1}b')
            qc.append(ZGate().control(n - 1, ctrl_state=s), 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)

    if n > 1:
        for set in range(L):
            #print(format(set, f'0{n-1}b'))

            if not (set >> (n - 1)) & 1:
                qc.x(n-1)
                s = format(set, f'0{n-1}b')
                qc.append(ZGate().control(n - 1, ctrl_state=s), range(n))
                qc.x(n-1)
            else:
                s = format(set - (1 << (n - 1)), f'0{n-1}b')
                qc.append(ZGate().control(n - 1, ctrl_state=s), range(n))
    else:
        if L == 1:
            qc.x(0)
            qc.z(0)
            qc.x(0)
        else:
            qc.x(0)
            qc.z(0)
            qc.x(0)
            qc.z(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.append(ZGate().control(L - 1), range(L))

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    bit = "0" + str(n) + "b"

    for l in range(L):
        binN = format(l, bit)
        for k in range(n):
            if binN[n-k-1] == '0':
                qc.x(k)
        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:
    bit = "0" + str(n) + "b"

    for l in range(L):
        binN = format(l, bit)
        for k in range(n):
            if binN[n-k-1] == '0':
                qc.x(k)
        qc.h(n-1)
        qc.mcx(list(range(n-1)), n-1)
        qc.h(n-1)
    for k in range(n):
        if binN[n-k-1] == '0':
            qc.x(k)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    bit = "0" + str(n) + "b"

    for l in range(L-1):
        binN = format(l, bit)
        for k in range(n):
            if binN[n-k-1] == '0':
                qc.x(k)
        qc.h(n-1)
        qc.mcx(list(range(n-1)), n-1)
        qc.h(n-1)
    for k in range(n):
        if binN[n-k-1] == '0':
            qc.x(k)
    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
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), list(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:
    qc = QuantumCircuit(n)
    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:
    qc.append(ZGate().control(n - 1), range(n))
    return qc
'''


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

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

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

    return qc
'''