'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    for i in range(n):
        qc.cx(x[i],y)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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


'''python
from qiskit import QuantumCircuit, QuantumRegister

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


'''python
from qiskit import QuantumCircuit, QuantumRegister

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


'''python
from qiskit import QuantumCircuit, QuantumRegister

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


'''python
from qiskit import QuantumCircuit, QuantumRegister

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


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    if n % 2 == 0:
        qc.x(n)
    for i in range(2**n):
        bit = 0
        for j in range(n):
            if (i / 2**j) % 2 == 1:
                bit += 1
        if bit % 2 == 1:
            continue
        for j in range(n):
            if (i / 2**j) % 2 == 1:
                qc.x(j)
        qc.mcx(x, y)
        for j in range(n):
            if (i / 2**j) % 2 == 1:
                qc.x(j)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    if n % 2 == 0:
        qc.x(y)
    for i in range(2**n):
        bit = 0
        for j in range(n):
            if (i / (2**j)) % 2 == 1:
                bit += 1
        if bit % 2 == 1:
            continue
        for j in range(n):
            if (i / (2**j)) % 2 == 1:
                qc.x(x[j])
        qc.mcx(x, y)
        for j in range(n):
            if (i / (2**j)) % 2 == 1:
                qc.x(x[j])
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    if n % 2 == 0:
        qc.x(y)
    for i in range(2**n):
        bit = 0
        for j in range(n):
            if (i // (2**j)) % 2 == 1:
                bit += 1
        if bit % 2 == 1:
            continue
        for j in range(n):
            if (i // (2**j)) % 2 == 1:
                qc.x(x[j])
        qc.mcx(x, y)
        for j in range(n):
            if (i // (2**j)) % 2 == 1:
                qc.x(x[j])
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(n):
        qc.cx(i, n)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(0, n):
        qc.cx(x[i], y)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)

    # Apply CNOT gates to implement the oracle
    for i in range(n):
        qc.cx(x[i], y[0])

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(n):
        qc.cx(i,n)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(n):
        qc.cx(x[i], y[0])

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    for i in range(n):
        qc.cx(x[i], y)
    return qc

# if __name__ == "__main__":
#     from qiskit.quantum_info import Statevector
#     qc = solve()
#     print(Statevector(qc))
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)  # n量子ビットと1量子ビットを作成
    qc = QuantumCircuit(x, y)  # 量子回路を作成

    # オラクルの実装:
    for i in range(n):
        qc.cx(x[i], y[0])  # xの各ビットを制御ビットとして、yに対してCNOTゲートを適用

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(len(x)):
        qc.cx(x[i],y)
    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.ry(2*pi,i)
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for i in range(n):
        qc.ry(pi,i)
        qc.h(i)

    return qc
'''


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

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

        #if everything is 1, multiply by -1
        qc.append(ZGate().control(n-1), range(n))

        #Put it back
        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):
        #check if ith is 0
        for i in range(n):
            if not ((l >> i) & 1):
                qc.x(i)

        if n == 1:
            qc.z(0)
        else:
            #if everything is 1, multiply by -1
            qc.append(ZGate().control(n-1), range(n))

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


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

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

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

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

    return qc
'''


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

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

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

        for j in range(n):
            if i&(1<<j)!=0:
                continue
            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:

    mask = 2 ** (n - 1) - 1

    for l in range(L):
        qc.append(ZGate().control(n-1, None, l & mask), 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:

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

    mask = 2 ** (n - 1) - 1

    for l in range(L):
        qc.append(ZGate().control(n-1, None, l & mask), 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:

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

    mask = 2 ** (n - 1) - 1

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

    return qc
'''


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

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

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

    mask = 2 ** (n - 1) - 1

    qc.x(n-1)

    for l in range(L):
        if l >> (n - 1) != 0:
            qc.x(n-1)
        qc.append(ZGate().control(n-1, None, l & mask), range(n))
        if l >> (n - 1) != 0:
            qc.x(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:
    l = [(L>>i)for i in range(n)]
    if l[n-1]==0:
        qc.x(n-1)
    else:
        qc.x(n-1)
        qc.z(n-1)
        qc.x(n-1)
    for i in range(n-2,-1,-1):
        if l[i]==0:
            qc.x(i)
        else:
            qc.x(i)
            for j in range(i+1,n):
                qc.cz(i,j)
            qc.z(i)
            qc.x(i)
    for i in range(n):
        if l[i]==0:
            qc.x(i)
    return qc
'''


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

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


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


'''python
from qiskit import QuantumCircuit

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

    target_qubit = n-1
    for num in range(L,pow(2,n)+1):
        binary_string = bin(num)[2:]  # Convert to binary and remove the '0b' prefix
        binary_list = [int(bit) for bit in binary_string[::-1]]
        control_qubits = []
        counter = 0
        for b in binary_list:
            if b==1:
                control_qubits.append(counter)
                counter+=1
            else:
                counter+=1
        if control_qubits == [target_qubit]:
            break
        cz_gate = ZGate().control(len(control_qubits))
        qc.append(cz_gate, control_qubits+[target_qubit])

    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.x(i)
    target_qubit = n-1
    for num in range(L,pow(2,n)+1):
        binary_string = bin(num)[2:]  # Convert to binary and remove the '0b' prefix
        binary_list = [int(bit) for bit in binary_string[::-1]]
        control_qubits = []
        counter = 0
        for b in binary_list:
            if b==1:
                control_qubits.append(counter)
                counter+=1
            else:
                counter+=1
        if control_qubits == [target_qubit]:
            break
        cz_gate = ZGate().control(len(control_qubits))
        qc.append(cz_gate, control_qubits+[target_qubit])
    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.x(i)

    target_qubit = n-1
    for num in range(L,pow(2,n)+1):
        binary_string = bin(num)[2:]  # Convert to binary and remove the '0b' prefix
        binary_list = [int(bit) for bit in binary_string[::-1]]
        control_qubits = []
        counter = 0
        for b in binary_list:
            if b==1:
                control_qubits.append(counter)
                counter+=1
            else:
                counter+=1
        if control_qubits == [target_qubit]:
            break
        cz_gate = ZGate().control(len(control_qubits))
        qc.append(cz_gate, control_qubits+[target_qubit])

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

    target_qubit = n-1
    for num in range(L,pow(2,n)+1):
        binary_string = bin(num)[2:]  # Convert to binary and remove the '0b' prefix
        binary_list = [int(bit) for bit in binary_string]
        control_qubits = []
        counter = 0
        for b in binary_list:
            if b==1:
                control_qubits.append(counter)
                counter+=1
            else:
                counter+=1
        if control_qubits == [target_qubit]:
            break
        cz_gate = ZGate().control(len(control_qubits))
        qc.append(cz_gate, control_qubits+[target_qubit])

    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:
    target_qubit = n-1
    for num in range(L,pow(2,n)+1):
        binary_string = bin(num)[2:]  # Convert to binary and remove the '0b' prefix
        binary_list = [int(bit) for bit in binary_string[::-1]]
        control_qubits = []
        counter = 0
        for b in binary_list:
            if b==1:
                control_qubits.append(counter)
                counter+=1
            else:
                counter+=1
        if control_qubits == [target_qubit]:
            break
        cz_gate = ZGate().control(len(control_qubits))
        qc.append(cz_gate, control_qubits+[target_qubit])
    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:
    target_qubit = n-1
    for num in range(L,pow(2,n)+1):
        binary_string = bin(num)[2:]  # Convert to binary and remove the '0b' prefix
        binary_list = [int(bit) for bit in binary_string[::-]]
        control_qubits = []
        counter = 0
        for b in binary_list:
            if b==1:
                control_qubits.append(counter)
                counter+=1
            else:
                counter+=1
            if control_qubits == [target_qubit]:
                break
        cz_gate = ZGate().control(len(control_qubits))
        qc.append(cz_gate, control_qubits+[target_qubit])
    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:
    target_qubit = n-1
    for num in range(L,pow(2,n)+1):
        binary_string = bin(num)[2:]  # Convert to binary and remove the '0b' prefix
        binary_list = [int(bit) for bit in binary_string[::-1]]
        control_qubits = []
        counter = 0
        for b in binary_list:
            if b==1:
                control_qubits.append(counter)
                counter+=1
            else:
                counter+=1
            if control_qubits == [target_qubit]:
                break
        cz_gate = ZGate().control(len(control_qubits))
        qc.append(cz_gate, control_qubits+[target_qubit])
    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:
    target_qubit = n-1
    for i in range(n):
        qc.h(i)
    for num in range(L,pow(2,n)+1):
        binary_string = bin(num)[2:]  # Convert to binary and remove the '0b' prefix
        binary_list = [int(bit) for bit in binary_string[::-1]]
        control_qubits = []
        counter = 0
        flag = 0
        for b in binary_list:
            if b==1:
                control_qubits.append(counter)
                counter+=1
            else:
                counter+=1
            if counter == target_qubit:
                flag = 1
                break
        if flag == 0:
            cz_gate = ZGate().control(len(control_qubits))
            qc.append(cz_gate, control_qubits+[target_qubit])

    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:
    target_qubit = n-1
    for num in range(L,pow(2,n)+1):
        binary_string = bin(num)[2:]  # Convert to binary and remove the '0b' prefix
        binary_list = [int(bit) for bit in binary_string[::-1]]
        control_qubits = []
        counter = 0
        flag = 0
        for b in binary_list:
            if b==1:
                control_qubits.append(counter)
                counter+=1
            else:
                counter+=1
            if counter == target_qubit:
                flag = 1
                break
        if flag == 0:
            cz_gate = ZGate().control(len(control_qubits))
            qc.append(cz_gate, control_qubits+[target_qubit])

    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:

    target_qubit = n-1
    control_history = []
    for num in range(L,pow(2,n)+1):
        binary_string = bin(num)[2:]  # Convert to binary and remove the '0b' prefix
        binary_list = [int(bit) for bit in binary_string[::-1]]
        control_qubits = []
        counter = 0
        flag = 0
        for b in binary_list:
            if b==1:
                control_qubits.append(counter)
                counter+=1
            else:
                counter+=1
            if counter == target_qubit:
                break
        if [target_qubit]!=control_qubits and len(control_qubits)!=0 and not(control_qubits in control_history):
            print(control_qubits+[target_qubit])
            cz_gate = ZGate().control(len(control_qubits))
            qc.append(cz_gate, control_qubits+[target_qubit])
            control_history.append(control_qubits)
    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:

    target_qubit = n-1
    control_history = []
    for num in range(L,pow(2,n)+1):
        binary_string = bin(num)[2:]  # Convert to binary and remove the '0b' prefix
        binary_list = [int(bit) for bit in binary_string[::-1]]
        control_qubits = []
        counter = 0
        flag = 0
        for b in binary_list:
            if b==1:
                control_qubits.append(counter)
                counter+=1
            else:
                counter+=1
            if counter == target_qubit:
                break
        if [target_qubit]!=control_qubits and len(control_qubits)!=0 and not(control_qubits in control_history):
            print(control_qubits+[target_qubit])
            cz_gate = ZGate().control(len(control_qubits))
            qc.append(cz_gate, control_qubits+[target_qubit])
            control_history.append(control_qubits)
    qc.x(target_qubit)
    for num in range(L,pow(2,n)+1):
        binary_string = bin(num)[2:]  # Convert to binary and remove the '0b' prefix
        binary_list = [int(bit) for bit in binary_string[::-1]]
        control_qubits = []
        counter = 0
        flag = 0
        for b in binary_list:
            if b==1:
                control_qubits.append(counter)
                counter+=1
            else:
                counter+=1
            if counter == target_qubit:
                break
        if [target_qubit]!=control_qubits and len(control_qubits)!=0 and not(control_qubits in control_history):
            print(control_qubits+[target_qubit])
            cz_gate = ZGate().control(len(control_qubits))
            qc.append(cz_gate, control_qubits+[target_qubit])
            control_history.append(control_qubits)
    qc.x(target_qubit)
    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:

    target_qubit = n-1
    control_history = []
    for num in range(L,pow(2,n)+1):
        binary_string = bin(num)[2:]  # Convert to binary and remove the '0b' prefix
        binary_list = [int(bit) for bit in binary_string[::-1]]
        control_qubits = []
        counter = 0
        flag = 0
        for b in binary_list:
            if b==1:
                control_qubits.append(counter)
                counter+=1
            else:
                counter+=1
        if [target_qubit]!=control_qubits and len(control_qubits)!=0 and not(control_qubits in control_history):
            print(control_qubits)
            cz_gate = ZGate().control(len(control_qubits))
            qc.append(cz_gate, control_qubits)
            control_history.append(control_qubits)
    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:
    control_history = []
    for num in range(L,pow(2,n)):
        binary_string = bin(num)[2:]  # Convert to binary and remove the '0b' prefix
        binary_list = [int(bit) for bit in binary_string[::-1]]
        control_qubits = []
        counter = 0
        flag = 0
        for b in binary_list:
            if b==1:
                control_qubits.append(counter)
                counter+=1
            else:
                counter+=1
        if not(control_qubits in control_history):
            print(control_qubits)
            if len(control_qubits) -1 == 0:
                qc.z(control_qubits[0])
            else:
                cz_gate = ZGate().control(len(control_qubits)-1)
                qc.append(cz_gate, control_qubits)
            control_history.append(control_qubits)

    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:
    control_history = []
    for num in range(L,pow(2,n)):
        binary_string = bin(num)[2:]  # Convert to binary and remove the '0b' prefix
        binary_list = [int(bit) for bit in binary_string[::-1]]
        control_qubits = []
        counter = 0
        flag = 0
        all_qubits = []
        for i in range(n):
            all_qubits.append(i)

        for b in binary_list:
            if b==1:
                control_qubits.append(counter)
                counter+=1
            else:
                counter+=1
        if not(control_qubits in control_history):
            print(control_qubits)
            if len(control_qubits) -1 != 0:
                for i in range(n):
                    if not i in control_qubits:
                        qc.x(i)
                cz_gate = ZGate().control(len(all_qubits)-1)
                qc.append(cz_gate, all_qubits)
                for i in range(n):
                    if not i in control_qubits:
                        qc.x(i)
            else:
                for i in range(n):
                    if not i in control_qubits:
                        qc.x(i)
                cz_gate = ZGate().control(len(all_qubits)-1)
                qc.append(cz_gate, all_qubits)
                for i in range(n):
                    if not i in control_qubits:
                        qc.x(i)
            control_history.append(control_qubits)
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n==1:
        if L==1:
            qc.z(i)
    else:
        control_history = []
        for num in range(L,pow(2,n)):
            binary_string = bin(num)[2:]  # Convert to binary and remove the '0b' prefix
            binary_list = [int(bit) for bit in binary_string[::-1]]
            control_qubits = []
            counter = 0
            flag = 0
            all_qubits = []
            for i in range(n):
                all_qubits.append(i)

            for b in binary_list:
                if b==1:
                    control_qubits.append(counter)
                    counter+=1
                else:
                    counter+=1
            if not(control_qubits in control_history):
                print(control_qubits)
                if len(control_qubits) -1 != 0:
                    for i in range(n):
                        if not i in control_qubits:
                            qc.x(i)
                    cz_gate = ZGate().control(len(all_qubits)-1)
                    qc.append(cz_gate, all_qubits)
                    for i in range(n):
                        if not i in control_qubits:
                            qc.x(i)
                else:
                    for i in range(n):
                        if not i in control_qubits:
                            qc.x(i)
                    cz_gate = ZGate().control(len(all_qubits)-1)
                    qc.append(cz_gate, all_qubits)
                    for i in range(n):
                        if not i in control_qubits:
                            qc.x(i)
                control_history.append(control_qubits)
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n==1 and L==1
            qc.z(i)
    else:
        control_history = []
        for num in range(L,pow(2,n)):
            binary_string = bin(num)[2:]  # Convert to binary and remove the '0b' prefix
            binary_list = [int(bit) for bit in binary_string[::-1]]
            control_qubits = []
            counter = 0
            flag = 0
            all_qubits = []
            for i in range(n):
                all_qubits.append(i)

            for b in binary_list:
                if b==1:
                    control_qubits.append(counter)
                    counter+=1
                else:
                    counter+=1
            if not(control_qubits in control_history):
                print(control_qubits)
                if len(control_qubits) -1 != 0:
                    for i in range(n):
                        if not i in control_qubits:
                            qc.x(i)
                    cz_gate = ZGate().control(len(all_qubits)-1)
                    qc.append(cz_gate, all_qubits)
                    for i in range(n):
                        if not i in control_qubits:
                            qc.x(i)
                else:
                    for i in range(n):
                        if not i in control_qubits:
                            qc.x(i)
                    cz_gate = ZGate().control(len(all_qubits)-1)
                    qc.append(cz_gate, all_qubits)
                    for i in range(n):
                        if not i in control_qubits:
                            qc.x(i)
                control_history.append(control_qubits)
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n==1 and L==1
            qc.z(0)
    else:
        control_history = []
        for num in range(L,pow(2,n)):
            binary_string = bin(num)[2:]  # Convert to binary and remove the '0b' prefix
            binary_list = [int(bit) for bit in binary_string[::-1]]
            control_qubits = []
            counter = 0
            flag = 0
            all_qubits = []
            for i in range(n):
                all_qubits.append(i)

            for b in binary_list:
                if b==1:
                    control_qubits.append(counter)
                    counter+=1
                else:
                    counter+=1
            if not(control_qubits in control_history):
                print(control_qubits)
                if len(control_qubits) -1 != 0:
                    for i in range(n):
                        if not i in control_qubits:
                            qc.x(i)
                    cz_gate = ZGate().control(len(all_qubits)-1)
                    qc.append(cz_gate, all_qubits)
                    for i in range(n):
                        if not i in control_qubits:
                            qc.x(i)
                else:
                    for i in range(n):
                        if not i in control_qubits:
                            qc.x(i)
                    cz_gate = ZGate().control(len(all_qubits)-1)
                    qc.append(cz_gate, all_qubits)
                    for i in range(n):
                        if not i in control_qubits:
                            qc.x(i)
                control_history.append(control_qubits)
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n==1 and L==1:
            qc.z(0)
    else:
        control_history = []
        for num in range(L,pow(2,n)):
            binary_string = bin(num)[2:]  # Convert to binary and remove the '0b' prefix
            binary_list = [int(bit) for bit in binary_string[::-1]]
            control_qubits = []
            counter = 0
            flag = 0
            all_qubits = []
            for i in range(n):
                all_qubits.append(i)

            for b in binary_list:
                if b==1:
                    control_qubits.append(counter)
                    counter+=1
                else:
                    counter+=1
            if not(control_qubits in control_history):
                print(control_qubits)
                if len(control_qubits) -1 != 0:
                    for i in range(n):
                        if not i in control_qubits:
                            qc.x(i)
                    cz_gate = ZGate().control(len(all_qubits)-1)
                    qc.append(cz_gate, all_qubits)
                    for i in range(n):
                        if not i in control_qubits:
                            qc.x(i)
                else:
                    for i in range(n):
                        if not i in control_qubits:
                            qc.x(i)
                    cz_gate = ZGate().control(len(all_qubits)-1)
                    qc.append(cz_gate, all_qubits)
                    for i in range(n):
                        if not i in control_qubits:
                            qc.x(i)
                control_history.append(control_qubits)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    l=lst(range(n))
    l.remote(L)
    qc.h(L)
    qc.mcx(l,L)
    qc.h(L)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    l=list(range(n))
    t=L//2
    j=L%2
    l.remove(t)
    qc.h(t)
    qc.mcx(l,t)
    qc.h(t)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    t=L//2
    j=L%2
    l=list(range(n))
    ll=list(range(n))
    ll.remove(t)
    qc.x(l)
    qc.h(t)
    qc.mcx(ll,t)
    qc.h(t)
    qc.x(l)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    t=L//2
    j=L%2
    l=list(range(n))
    ll=list(range(n))
    ll.remove(t)
    if j == 1 : qc.x(l)
    qc.h(t)
    qc.mcx(ll,t)
    qc.h(t)
    if j == 1 : qc.x(l)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    t=L//2
    j=L%2
    l=list(range(n))
    ll=list(range(n))
    ll.remove(t)
    if j == 0 : qc.x(l)
    qc.h(t)
    qc.mcx(ll,t)
    qc.h(t)
    if j == 0 : qc.x(l)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n == 0: return qc.z(0)
    t=L//2
    j=L%2
    l=list(range(n))
    ll=list(range(n))
    ll.remove(t)
    qc.x(l)
    qc.h(t)
    qc.mcx(ll,t)
    qc.h(t)
    qc.x(l)
    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)
    t=L//2
    j=L%2
    l=list(range(n))
    ll=list(range(n))
    ll.remove(t)
    qc.x(l)
    qc.h(t)
    qc.mcx(ll,t)
    qc.h(t)
    qc.x(l)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n == 1:
        if L == 1: return qc.z(0)
        else: return qc.x(0).z(0).x(0)
    t=L//2
    j=L%2
    l=list(range(n))
    ll=list(range(n))
    ll.remove(t)
    qc.x(l)
    qc.h(t)
    qc.mcx(ll,t)
    qc.h(t)
    qc.x(l)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n == 1:
        if L == 1: return qc.z(0)
        else:
            qc.x(0)
            qc.z(0)
            qc.x(0)
            return qc
    t=L//2
    j=L%2
    l=list(range(n))
    ll=list(range(n))
    ll.remove(t)
    qc.x(l)
    qc.h(t)
    qc.mcx(ll,t)
    qc.h(t)
    qc.x(l)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n == 1:
        if L == 1:
            qc.z(0)
        else:
            qc.x(0)
            qc.z(0)
            qc.x(0)
    else:
        t=L//2
        j=L%2
        l=list(range(n))
        ll=list(range(n))
        ll.remove(t)
        qc.x(l)
        qc.h(t)
        qc.mcx(ll,t)
        qc.h(t)
        qc.x(l)
    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:
    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:
    from qiskit.circuit.library import ZGate
    for i in range(L):
        for j in range(n):
            if ((L>>j)&1)==0:
                qc.x(j)
        if n==1:
            qc.z(0)
        else:
            qc.append(ZGate().control(n-1),range(n))
        for j in range(n):
            if ((L>>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:
    from qiskit.circuit.library import ZGate
    for i in range(L):
        for j in range(n):
            if ((i>>j)&1)==0:
                qc.x(j)
        if n==1:
            qc.z(0)
        else:
            qc.append(ZGate().control(n-1),range(n))
        for j in range(n):
            if ((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:
    qc.diagonal([-1 if i < L else 1 for i in range(2**n)], list(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(2**n):
        qc.z(n)  # nビット目に対してフェイズ反転ゲートを適用

    return qc
'''


'''python
from qiskit import QuantumCircuit

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


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

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


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

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


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

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


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

def solve(n, L) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.h(i)
    for i in range(min(1<<(n-1),L)):
        qc.append(ZGate().control(n-1,ctrl_state=i), range(n))
    qc.x(n-1)
    if L > (1<<(n-1)):
        for i in range(L-(1<<(n-1))):
            qc.append(ZGate().control(n-1,ctrl_state=i), range(n))
    return qc
'''


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

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

    for i in range(min(1<<(n-1),L)):
        qc.append(ZGate().control(n-1,ctrl_state=i), range(n))
    qc.x(n-1)
    if L > (1<<(n-1)):
        for i in range(L-(1<<(n-1))):
            qc.append(ZGate().control(n-1,ctrl_state=i), range(n))
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate
def solve(n, L) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n==1:
        if L == 1:
            qc.z(0)
            qc.x(0)
        else:
            qc.z(0)
            qc.x(0)
            qc.z(0)
    else:
        for i in range(min(1<<(n-1),L)):
            qc.append(ZGate().control(n-1,ctrl_state=i), range(n))
        qc.x(n-1)
        if L > (1<<(n-1)):
            for i in range(L-(1<<(n-1))):
                qc.append(ZGate().control(n-1,ctrl_state=i), range(n))
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate
def solve(n, L) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n==1:
        if L == 1:
            qc.z(0)
            qc.x(0)
        else:
            qc.z(0)
            qc.x(0)
            qc.z(0)
    else:
        for i in range(min(1<<(n-1),L)):
            qc.append(ZGate().control(n-1,ctrl_state=i), range(n))
        if L > (1<<(n-1)):
            qc.x(n-1)
            for i in range(L-(1<<(n-1))):
                qc.append(ZGate().control(n-1,ctrl_state=i), range(n))
            qc.x(n-1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


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

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

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

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

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

    for i in xgates:
        qc.x(i)

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.append(XGate().control(n - 1), range(n))

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.append(CXGate().control(n - 1), range(n))

    return qc
'''


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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

from qiskit.circuit.library import ZGate

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


'''python
import qiskit
from qiskit import QuantumCircuit

from qiskit.circuit.library import ZGate

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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

def ccz(qc, n):
    gate = CZGate()
    qc.append(gate.control(n-1), range(n))
'''


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

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

def ccz(qc, n):
    gate = ZGate()
    qc.append(gate.control(n-1), range(n))
'''


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

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


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

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


'''python
from qiskit import QuantumCircuit

from qiskit.circuit.library import ZGate

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


'''python
from qiskit import QuantumCircuit

from qiskit.circuit.library import ZGate

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


'''python
from qiskit import QuantumCircuit

from qiskit.circuit.library import ZGate

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

    for i in range(L):
        qc.z(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    from qiskit.circuit.library import ZGate
    qc.append(ZGate().control(n - 1), range(n))

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    from qiskit.circuit.library import ZGate
    qc.append(ZGate().control(n - 1), range(L))

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    from qiskit.circuit.library import ZGate
    qc.append(ZGate().control(L - 1), range(L))

    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):
        # Create a list of target qubits that need to be flipped
        control_bits = []
        for i in range(n):
            if not ((l >> i) & 1):
                qc.x(i)  # Apply X gate to flip the qubit
            control_bits.append(i)

        # Apply controlled-Z gate
        qc.mcx(control_bits[:-1], control_bits[-1])  # multi-controlled X (equivalent to Z in phase kickback)

        # Revert the X gate
        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):
        for j in range(n):
            if (i >> j) & 1 == 0:
                qc.x(j)

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

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

    return qc
'''


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

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

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

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

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

    return qc
'''


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

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

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

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

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

    return qc
'''


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

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

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

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

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

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

    for qubit in range(n):
        if binary_L_minus_1[qubit] == '0':
            qc.x(qubit)
    """
    for l_little in range(L):
        binary_L_minus_1 = format(l_little, '0' + str(n) + 'b')[::-1]
        for qubit in range(n):
            if binary_L_minus_1[qubit] == '0':
                qc.x(qubit)

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

        for qubit in range(n):
            if binary_L_minus_1[qubit] == '0':
                qc.x(qubit)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    """
    binary_L_minus_1 = format(L - 1, '0' + str(n) + 'b')[::-1]
    for qubit in range(n):
        if binary_L_minus_1[qubit] == '0':
            qc.x(qubit)

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

    for qubit in range(n):
        if binary_L_minus_1[qubit] == '0':
            qc.x(qubit)
    """
    if n == 1:
        if L == 1:
            qc.x(0)
            qc.z(0)
            qc.x(0)
        if L == 2:
            qc.x(0)
            qc.z(0)
            qc.x(0)
            qc.z(0)
        return qc
    for l_little in range(L):
        binary_L_minus_1 = format(l_little, '0' + str(n) + 'b')[::-1]
        for qubit in range(n):
            if binary_L_minus_1[qubit] == '0':
                qc.x(qubit)

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

        for qubit in range(n):
            if binary_L_minus_1[qubit] == '0':
                qc.x(qubit)

    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(n - 1))

    return qc
'''


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

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

    return qc
'''


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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import HGate, 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 HGate, ZGate

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
from qiskit.circuit.library import HGate, 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
from qiskit.circuit.library import MCXGate

import math

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):
        for j in range(n):  # このループが一番のポイント
            if ((i >> j) & 1)==0:  # 順に右にシフトさせ最下位bitのチェックを行う
                qc.x(j)  # フラグが立っていたら j にxgate

        # MCZ start
        qc.h(n - 1)
        qc.append(MCXGate(n - 1), list(range(n)))
        qc.h(n - 1)
        # MCZ end

        for j in range(n):  # このループが一番のポイント
            if ((i >> j) & 1)==0:  # 順に右にシフトさせ最下位bitのチェックを行う
                qc.x(j)  # フラグが立っていたら j にxgate

    return qc
'''


'''python
import math

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

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:  # 順に右にシフトさせ最下位bitのチェックを行う
                qc.x(j)  # フラグが立っていたら j にxgate

        # MCZ start
        qc.h(n - 1)
        qc.append(MCXGate(n - 1), list(range(n)))
        qc.h(n - 1)
        # MCZ end

        for j in range(n):  # このループが一番のポイント
            if ((i >> j) & 1)==0:  # 順に右にシフトさせ最下位bitのチェックを行う
                qc.x(j)  # フラグが立っていたら j にxgate

    return qc
'''


'''python
import math

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

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

    # Write your code here:
    if n == 1:
        qc.x(0)
        qc.z(0)
        qc.x(0)
    else:
        for i in range(L):
            for j in range(n):  # このループが一番のポイント
                if ((i >> j) & 1)==0:  # 順に右にシフトさせ最下位bitのチェックを行う
                    qc.x(j)  # フラグが立っていたら j にxgate

            # MCZ start
            qc.h(n - 1)
            qc.append(MCXGate(n - 1), list(range(n)))
            qc.h(n - 1)
            # MCZ end

            for j in range(n):  # このループが一番のポイント
                if ((i >> j) & 1)==0:  # 順に右にシフトさせ最下位bitのチェックを行う
                    qc.x(j)  # フラグが立っていたら j にxgate

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if (L & (1 << i)) == 0:
            qc.x(i)
    for i in range(n):
        if (L & (1 << i)) != 0:
            qc.x(i)
            if i + 1 < n:
                qc.append(ZGate().control(n - i - 1), range(i, n))
            else:
                qc.z(i)
            qc.x(i)
    for i in range(n):
        if (L & (1 << i)) == 0:
            qc.x(i)
    # print(qc)
    return qc

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

#     qc = solve(3, 5)
#     print(Statevector(qc))
#     sv = Statevector.from_label('+++')
#     print(sv.evolve(qc))
'''


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

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


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    t = QuantumRegister(1, "t")
    qc.add_bits(t)

    qc.x(range(n))
    qc.append(AND(n), [0, 1, t])

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import AND, RXGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    t = QuantumRegister(1, "t")
    qc.add_bits(t)

    qc.x(range(n))
    qc.append(AND(n), [0, 1, t])

    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:
    t = QuantumRegister(1, "t")
    qc.add_bits(t)

    for i in range(L):
        # i to binary bits
        bits = [(i >> j) & 1 for j in range(n)]

        # Apply gates based on bits
        zero_indices = [j for j, bit in enumerate(bits) if bit == 0]
        qc.x(zero_indices)
        qc.append(XGate().control(n), [*range(n), t[0]])
        qc.crx(math.pi * 2.0, t, 0)
        qc.append(XGate().control(n), [*range(n), t[0]])
        qc.x(zero_indices)

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    t = QuantumRegister(1, "t")
    qc.add_bits(t)

    for i in range(L):
        # i to binary bits
        bits = [(i >> j) & 1 for j in range(n)]

        # Apply gates based on bits
        zero_indices = [j for j, bit in enumerate(bits) if bit == 0]
        qc.x(zero_indices)
        qc.append(XGate().control(n), [*range(n), t[0]])
        qc.crx(math.pi * 2.0, t, 0)
        qc.append(XGate().control(n), [*range(n), t[0]])
        qc.x(zero_indices)

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    t = QuantumRegister(1, "t")
    qc.add_bits(t)

    for i in range(L):
        # i to binary bits
        bits = [(i >> j) & 1 for j in range(n)]

        # Apply gates based on bits
        zero_indices = [j for j, bit in enumerate(bits) if bit == 0]
        qc.x(zero_indices)
        qc.append(XGate().control(n), [*range(n), t[0]])
        qc.crx(math.pi * 2.0, t, 0)
        qc.append(XGate().control(n), [*range(n), t[0]])
        qc.x(zero_indices)

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    t = QuantumRegister(1, "t")
    qc.add_bits(t)

    for i in range(L):
        # i to binary bits
        bits = [(i >> j) & 1 for j in range(n)]

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

    return qc
'''


'''python
from qiskit import QuantumCircuit,QuantumRegister
from qiskit.quantum_info import Statevector
from qiskit.circuit.library import ZGate
import numpy as np

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # vec = np.zeros(1<<n)
    # vec[5] = 1
    # qc.initialize(vec)
    # Write your code here:
    for i in range(L):
        for j in range(n):
            if (i & (1<<j)) == 0:
                qc.x(j)
        qc.append(ZGate().control(n-1), range(0, n))
        for j in range(n):
            if (i & (1<<j)) == 0:
                qc.x(j)
    return qc

qc = solve(3,5)
print(qc)
print(Statevector(qc)) #こことimportを消す
'''


'''python
from qiskit import QuantumCircuit,QuantumRegister
#from qiskit.quantum_info import Statevector
from qiskit.circuit.library import ZGate
import numpy as np

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # vec = np.zeros(1<<n)
    # vec[5] = 1
    # qc.initialize(vec)
    # Write your code here:
    for i in range(L):
        for j in range(n):
            if (i & (1<<j)) == 0:
                qc.x(j)
        qc.append(ZGate().control(n-1), range(0, n))
        for j in range(n):
            if (i & (1<<j)) == 0:
                qc.x(j)
    return qc

qc = solve(3,5)
print(qc)
#print(Statevector(qc)) #こことimportを消す
'''


'''python
from qiskit import QuantumCircuit,QuantumRegister
#from qiskit.quantum_info import Statevector
from qiskit.circuit.library import ZGate
import numpy as np

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # vec = np.zeros(1<<n)
    # vec[5] = 1
    # qc.initialize(vec)
    # Write your code here:
    for i in range(L):
        for j in range(n):
            if (i & (1<<j)) == 0:
                qc.x(j)
        if n>=2 :
            qc.append(ZGate().control(n-1), range(0, n))
        else:
            qc.z(0)
        for j in range(n):
            if (i & (1<<j)) == 0:
                qc.x(j)
    return qc

qc = solve(3,5)
print(qc)
#print(Statevector(qc)) #こことimportを消す
'''


'''python
from qiskit import QuantumCircuit

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
rom 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):
            # lを01列に直してiだけ右シフト
            if not ((l >> i) & 0b1): # ibit目が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):
            # lを01列に直してiだけ右シフト
            if not ((l >> i) & 0b1): # ibit目が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):
            # lを01列に直してiだけ右シフト
            if not ((l >> i) & 1): # ibit目が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):
            # lを01列に直してiだけ右シフト
            if not ((l >> i) & 0b1): # ibit目が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)
    # 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:
    for bit in range(L):
        for i in range(n):
            if (bit >> i) & 1 == 0:
                qc.x(i)
        arr = [0] * n
        qc.cz(arr, 0)
        for i in range(n):
            if (bit >> i) & 1 == 0:
                qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for bit in range(L):
        for i in range(n):
            if (bit >> i) % 2 == 0:
                qc.x(i)
        arr = [0] * n
        qc.cz(arr, 0)
        for i in range(n):
            if (bit >> i) % 2 == 0:
                qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for bit in range(L):
        for i in range(n):
            if (bit >> i) % 2 == 0:
                qc.x(i)
        arr = [0 for i in range(n)]
        qc.cz(arr, 0)
        for i in range(n):
            if (bit >> i) %  == 0:
                qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for bit in range(L):
        for i in range(n):
            if (bit >> i) % 2 == 0:
                qc.x(i)
        arr = [0 for i in range(n - 1)]
        qc.cz(arr, 0)
        for i in range(n):
            if (bit >> i) %  == 0:
                qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for bit in range(L):
        for i in range(n):
            if (bit >> i) % 2 == 0:
                qc.x(i)
        arr = [0 for i in range(n - 1)]
        # qc.cz(arr, 0)
        for i in range(n):
            if (bit >> i) %  == 0:
                qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for bit in range(L):
        # for i in range(n):
        #     if (bit >> i) % 2 == 0:
        #         qc.x(i)
        arr = [0 for i in range(n - 1)]
        qc.cz(arr, 0)
        # for i in range(n):
        #     if (bit >> i) %  == 0:
        #         qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for bit in range(L):
        # for i in range(n):
        #     if (bit >> i) % 2 == 0:
        #         qc.x(i)
        arr = [0 for i in range(n - 1)]
        qc.cz(arr, n - 1)
        # for i in range(n):
        #     if (bit >> i) %  == 0:
        #         qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    for bit in range(L):
        # for i in range(n):
        #     if (bit >> i) % 2 == 0:
        #         qc.x(i)
        arr = [0 for i in range(n)]
        qc.cz(arr, n - 1)
        # for i in range(n):
        #     if (bit >> i) %  == 0:
        #         qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    for bit in range(L):
        for i in range(n):
            if (bit >> i) % 2 == 0:
                qc.x(i)
        arr = [0 for i in range(n)]
        qc.cz(arr, n - 1)
        # for i in range(n):
        #     if (bit >> i) %  == 0:
        #         qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    for bit in range(L):
        for i in range(n):
            if (bit >> i) % 2 == 0:
                qc.x(i)
        arr = [0 for i in range(n)]
        qc.cz(arr, n - 1)
        for i in range(n):
            if (bit >> i) %  == 0:
                qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    for bit in range(L):
        for i in range(n):
            if (bit >> i) % 2 == 0:
                qc.x(i)
        arr = [0 for i in range(n)]
        qc.cz(arr, n - 1)
        for i in range(n):
            if (bit >> i) % 2 == 0:
                qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    for bit in range(L):
        for i in range(n):
            if (bit >> i) % 2 == 0:
                qc.x(n - 1 - i)
        arr = [0 for i in range(n)]
        qc.cz(arr, 0)
        for i in range(n):
            if (bit >> i) % 2 == 0:
                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:

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

    for bit in range(L):
        for i in range(n):
            if (bit >> i) % 2 == 0:
                qc.x(i)
        arr = [0 for i in range(n)]
        qc.cz(arr, 0)
        for i in range(n):
            if (bit >> i) % 2 == 0:
                qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    for bit in range(L):
        for i in range(n):
            if (bit >> i) % 2 == 0:
                qc.x(n - 1 - i)
        arr = [0 for i in range(n)]
        qc.cz(arr, n - 1)
        for i in range(n):
            if (bit >> i) % 2 == 0:
                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:

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

    for bit in range(L):
        for i in range(n):
            if (bit >> i) % 2 == 0:
                qc.x(n - 1 - i)
        arr = [i for i in range(n)]
        qc.cz(arr, n - 1)
        for i in range(n):
            if (bit >> i) % 2 == 0:
                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:

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

    for bit in range(L // 2):
        bit <<= 1
        for i in range(1, n):
            if (bit >> i) % 2 == 0:
                qc.x(i)
        arr = [i for i in range(1, n)]
        qc.cz(arr, 0)
        qc.cx(arr, 0)
        qc.cz(arr, 0)
        qc.cx(arr, 0)
        for i in range(1, n):
            if (bit >> i) % 2 == 0:
                qc.x(i)

    if L % 2 == 1:
        for i in range(1, n):
            if (L >> i) %2 == 0:
                qc.x(i)
        arr = [i for i in range(1, n)]
        qc.cx(arr, 0)
        qc.cz(arr, 0)
        ac.cx(arr, 0)
        for i in range(1, n):
            if (L >> i) %2 == 0:
                qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    for bit in range(L // 2):
        bit <<= 1
        for i in range(1, n):
            if (bit >> i) % 2 == 0:
                qc.x(i)
        arr = [i for i in range(1, n)]
        qc.cz(arr, 0)
        qc.cx(arr, 0)
        qc.cz(arr, 0)
        qc.cx(arr, 0)
        for i in range(1, n):
            if (bit >> i) % 2 == 0:
                qc.x(i)

    # if L % 2 == 1:
    #     for i in range(1, n):
    #         if (L >> i) %2 == 0:
    #             qc.x(i)
    #     arr = [i for i in range(1, n)]
    #     qc.cx(arr, 0)
    #     qc.cz(arr, 0)
    #     ac.cx(arr, 0)
    #     for i in range(1, n):
    #         if (L >> i) %2 == 0:
    #             qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    for bit in range(L // 2):
        bit <<= 1
        for i in range(1, n):
            if (bit >> i) % 2 == 0:
                qc.x(i)
        arr = [i for i in range(1, n)]
        qc.cz(arr, 0)
        qc.cx(arr, 0)
        qc.cz(arr, 0)
        qc.cx(arr, 0)
        for i in range(1, n):
            if (bit >> i) % 2 == 0:
                qc.x(i)

    if L % 2 == 1:
        for i in range(1, n):
            if (L >> i) % 2 == 0:
                qc.x(i)
        arr = [i for i in range(1, n)]
        qc.cx(arr, 0)
        qc.cz(arr, 0)
        qc.cx(arr, 0)
        for i in range(1, n):
            if (L >> i) %2 == 0:
                qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    for bit in range(L // 2):
        bit <<= 1
        for i in range(1, n):
            if (bit >> i) % 2 == 0:
                qc.x(i)
        arr = [i for i in range(1, n)]
        qc.cz(arr, 0)
        qc.cx(arr, 0)
        qc.cz(arr, 0)
        qc.cx(arr, 0)
        for i in range(1, n):
            if (bit >> i) % 2 == 0:
                qc.x(i)

    if L % 2 == 1:
        for i in range(1, n):
            if (L >> i) % 2 == 0:
                qc.x(i)
        arr = [i for i in range(1, n)]
        qc.cx(arr, 0)
        qc.cz(arr, 0)
        qc.cx(arr, 0)
        for i in range(1, n):
            if (L >> i) % 2 == 0:
                qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    for bit in range(L // 2):
        bit <<= 1
        for i in range(1, n):
            if (bit >> i) % 2 == 0:
                qc.x(n - 1 - i)
        arr = [i for i in range(0, n - 1)]
        qc.cz(arr, n - 1)
        qc.cx(arr, n - 1)
        qc.cz(arr, n - 1)
        qc.cx(arr, n - 1)
        for i in range(1, n):
            if (bit >> i) % 2 == 0:
                qc.x(n - 1 - i)

    if L % 2 == 1:
        for i in range(1, n):
            if (L >> i) % 2 == 0:
                qc.x(n - 1 - i)
        arr = [i for i in range(0, n - 1)]
        qc.cx(arr, n - 1)
        qc.cz(arr, n - 1)
        qc.cx(arr, n - 1)
        for i in range(1, n):
            if (L >> i) % 2 == 0:
                qc.x(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)
    # Write your code here:

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

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

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

    return qc
'''


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

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

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

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    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:
    for i in range(L):
        qc.rx(pi*2,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):
        log=[]
        for j,x in enumerate(f"{i:b}"):
            if x=="1":
                log.append(j)
                qc.x(j)

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

        for x in log:
            qc.x(j)

    return qc
'''


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

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

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

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

        for x in log:
            qc.x(j)

    return qc
'''


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

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

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

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

        for x in log:
            qc.x(j)

    return qc
'''


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

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

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

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

        for x in log:
            qc.x(j)

    return qc
'''


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

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

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

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

        for x in log:
            qc.x(j)

    return qc
'''


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

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

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

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

        for x in log:
            qc.x(j)

    return qc
'''


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

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

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

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

        for x in log:
            qc.x(j)

    return qc
'''


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

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

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

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

        for x in log:
            qc.x(j)

    return qc
'''


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

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

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

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

        for x in log:
            qc.x(j)

    return qc
'''


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

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

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

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

        for j in log:
            qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

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

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

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

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

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

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

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

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

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

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

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

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

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

    return qc
'''