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

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

        for bit in bits:
            qc.x(bit)
    return qc
'''


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

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

        for bit in bits:
            qc.x(n - bit - 1)
    return qc
'''


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

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

        for bit in bits:
            qc.x(bit)
    return qc
'''


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

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

        for bit in bits:
            qc.x(n - bit - 1)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(L):
        for j in range(n):
            if (i&(1<<j)) <= 0:
                qc.x(j)
        qc.mcp(math.pi, list(range(n-1)), n-1)
        for j in range(n):
            if (i&(1<<j)) <= 0:
                qc.x(j)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n == 1 and L == 1:
        qc.p(math.pi, 0)
    if n == 1:
        return qc
    for i in range(L):
        for j in range(n):
            if (i&(1<<j)) <= 0:
                qc.x(j)
        qc.mcp(math.pi, list(range(n-1)), n-1)
        for j in range(n):
            if (i&(1<<j)) <= 0:
                qc.x(j)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

for k in range(L):
    # Get binary representation of the number, padded to the number of qubits
    binary_rep = format(k, '0' + str(num_qubits) + 'b')

    # List to keep track of which qubits need the X gate (for flipping |1> to |0>)
    x_gates_needed = []

    for qubit_index, bit in enumerate(reversed(binary_rep)):
        if bit == '0':
            # Apply X gate to flip |0> to |1>, if needed
            qc.x(qubit_index)
            x_gates_needed.append(qubit_index)

    # Apply controlled Z gate
    if len(x_gates_needed) < num_qubits:
        control_qubits = [i for i in range(num_qubits) if i not in x_gates_needed]
        target_qubit = control_qubits[-1]
        if len(control_qubits) > 1:
            qc.h(target_qubit)
            qc.mct(control_qubits[:-1], target_qubit)  # Multi-controlled Toffoli
            qc.h(target_qubit)
        else:
            # If there is only one control qubit, apply Z gate directly
            qc.z(target_qubit)

    # Revert the X gates
    for qubit_index in x_gates_needed:
        qc.x(qubit_index)

    qc.barrier()
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for k in range(L):
        binary_rep = format(k, '0' + str(num_qubits) + 'b')

        # List to keep track of which qubits need the X gate (for flipping |1> to |0>)
        x_gates_needed = []

        for qubit_index, bit in enumerate(reversed(binary_rep)):
            if bit == '0':
                # Apply X gate to flip |0> to |1>, if needed
                qc.x(qubit_index)
                x_gates_needed.append(qubit_index)

        # Apply controlled Z gate
        if len(x_gates_needed) < num_qubits:
            control_qubits = [i for i in range(num_qubits) if i not in x_gates_needed]
            target_qubit = control_qubits[-1]
            if len(control_qubits) > 1:
                qc.h(target_qubit)
                qc.mct(control_qubits[:-1], target_qubit)  # Multi-controlled Toffoli
                qc.h(target_qubit)
            else:
                # If there is only one control qubit, apply Z gate directly
                qc.z(target_qubit)

        # Revert the X gates
        for qubit_index in x_gates_needed:
            qc.x(qubit_index)

        qc.barrier()
        return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for k in range(L):
        binary_rep = format(k, '0' + str(num_qubits) + 'b')

        x_gates_needed = []

        for qubit_index, bit in enumerate(reversed(binary_rep)):
            if bit == '0':
                qc.x(qubit_index)
                x_gates_needed.append(qubit_index)

        if len(x_gates_needed) < num_qubits:
            control_qubits = [i for i in range(num_qubits) if i not in x_gates_needed]
            target_qubit = control_qubits[-1]
            if len(control_qubits) > 1:
                qc.h(target_qubit)
                qc.mct(control_qubits[:-1], target_qubit)
                qc.h(target_qubit)
            else:
                qc.z(target_qubit)

        for qubit_index in x_gates_needed:
            qc.x(qubit_index)

        # qc.barrier()
        return qc
'''


'''python
from qiskit import QuantumCircuit

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

for k in range(L):
    # Get binary representation of the number, padded to the number of qubits
    binary_rep = format(k, '0' + str(num_qubits) + 'b')

    # List to keep track of which qubits need the X gate (for flipping |1> to |0>)
    x_gates_needed = []

    for qubit_index, bit in enumerate(reversed(binary_rep)):
        if bit == '0':
            # Apply X gate to flip |0> to |1>, if needed
            qc.x(qubit_index)
            x_gates_needed.append(qubit_index)

    # Apply controlled Z gate
    # if len(x_gates_needed) < num_qubits:
    control_qubits = [i for i in range(num_qubits) if i not in x_gates_needed]
    print(len(control_qubits))
    if len(control_qubits) >= 1:
        qc.append(ZGate().control(len(x_gates_needed)), x_gates_needed+[control_qubits[0]])
        # qc.h(target_qubit)
        # qc.mcZ(x_gates_needed, control_qubits[0])  # Multi-controlled Toffoli
        # qc.h(target_qubit)
    # else:
        # If there is only one control qubit, apply Z gate directly
        # qc.z(target_qubit)

    # # Revert the X gates
    # for qubit_index in x_gates_needed:
    #     qc.x(qubit_index)

    qc.barrier()
  return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for k in range(L):
        # Get binary representation of the number, padded to the number of qubits
        binary_rep = format(k, '0' + str(num_qubits) + 'b')

        # List to keep track of which qubits need the X gate (for flipping |1> to |0>)
        x_gates_needed = []

        for qubit_index, bit in enumerate(reversed(binary_rep)):
            if bit == '0':
                # Apply X gate to flip |0> to |1>, if needed
                qc.x(qubit_index)
                x_gates_needed.append(qubit_index)

        # Apply controlled Z gate
        # if len(x_gates_needed) < num_qubits:
        control_qubits = [i for i in range(num_qubits) if i not in x_gates_needed]
        print(len(control_qubits))
        if len(control_qubits) >= 1:
            qc.append(ZGate().control(len(x_gates_needed)), x_gates_needed+[control_qubits[0]])
            # qc.h(target_qubit)
            # qc.mcZ(x_gates_needed, control_qubits[0])  # Multi-controlled Toffoli
            # qc.h(target_qubit)
        # else:
            # If there is only one control qubit, apply Z gate directly
            # qc.z(target_qubit)

        # # Revert the X gates
        # for qubit_index in x_gates_needed:
        #     qc.x(qubit_index)

        qc.barrier()
  return qc
'''


'''python
from qiskit import QuantumCircuit, ZGate

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

    for k in range(L):
        binary_rep = format(k, '0' + str(num_qubits) + 'b')

        x_gates_needed = []

        for qubit_index, bit in enumerate(reversed(binary_rep)):
            if bit == '0':
                qc.x(qubit_index)
                x_gates_needed.append(qubit_index)

        # Apply controlled Z gate
        control_qubits = [i for i in range(num_qubits) if i not in x_gates_needed]
        print(len(control_qubits))
        if len(control_qubits) >= 1:
            qc.append(ZGate().control(len(x_gates_needed)), x_gates_needed+[control_qubits[0]])

        qc.barrier()
  return qc
'''


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

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

    for k in range(L):
        binary_rep = format(k, '0' + str(num_qubits) + 'b')

        x_gates_needed = []

        for qubit_index, bit in enumerate(reversed(binary_rep)):
            if bit == '0':
                qc.x(qubit_index)
                x_gates_needed.append(qubit_index)

        # Apply controlled Z gate
        control_qubits = [i for i in range(num_qubits) if i not in x_gates_needed]
        print(len(control_qubits))
        if len(control_qubits) >= 1:
            qc.append(ZGate().control(len(x_gates_needed)), x_gates_needed+[control_qubits[0]])

        qc.barrier()
  return qc
'''


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

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

    for k in range(L):
        binary_rep = format(k, '0' + str(n) + 'b')

        x_gates_needed = []

        for qubit_index, bit in enumerate(reversed(binary_rep)):
            if bit == '0':
                qc.x(qubit_index)
                x_gates_needed.append(qubit_index)

        # Apply controlled Z gate
        control_qubits = [i for i in range(n) if i not in x_gates_needed]
        print(len(control_qubits))
        if len(control_qubits) >= 1:
            qc.append(ZGate().control(len(x_gates_needed)), x_gates_needed+[control_qubits[0]])

        qc.barrier()
  return qc
'''


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

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

    for k in range(L):
        binary_rep = format(k, '0' + str(n) + 'b')

        x_gates_needed = []

        for qubit_index, bit in enumerate(reversed(binary_rep)):
            if bit == '0':
                qc.x(qubit_index)
                x_gates_needed.append(qubit_index)

        # Apply controlled Z gate
        control_qubits = [i for i in range(n) if i not in x_gates_needed]
        print(len(control_qubits))
        if len(control_qubits) >= 1:
            qc.append(ZGate().control(len(x_gates_needed)), x_gates_needed+[control_qubits[0]])

    return qc
'''


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

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

    for k in range(L):
        binary_rep = format(k, '0' + str(n) + 'b')

        x_gates_needed = []

        for qubit_index, bit in enumerate(reversed(binary_rep)):
            if bit == '0':
                qc.x(qubit_index)
                x_gates_needed.append(qubit_index)

        # Apply controlled Z gate
        control_qubits = [i for i in range(n) if i not in x_gates_needed]
        print(len(control_qubits))
        if len(control_qubits) >= 1:
            qc.append(ZGate().control(len(x_gates_needed)), x_gates_needed+[control_qubits[0]])

return qc
'''


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

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

    for l in range(L):
        for i in range(n):
            # check if i-th bit of l is 0 or 1
            if not ((l >> i) & 1):
                qc.x(i)
        if n == 1:
            qc.z(0)
        else:
            # apply multiple controlled Z gate
            qc.append(ZGate().control(n - 1), range(n))
        for i in range(n):
            if not ((l >> i) & 1):
                qc.x(i)

return qc
'''


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

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

    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)

    # for l in range(L):
    #     for i in range(n):
    #         # check if i-th bit of l is 0 or 1
    #         if not ((l >> i) & 1):
    #             qc.x(i)
        # if n == 1:
        #     qc.z(0)
        # else:
        #     # apply multiple controlled Z gate
        #     qc.append(ZGate().control(n - 1), range(n))
        # for i in range(n):
        #     if not ((l >> i) & 1):
        #         qc.x(i)

return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

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


'''python
from qiskit import QuantumCircuit
def solve(n: int, L: int) -> QuantumCircuit:
  qc = QuantumCircuit(n)
  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
import math
def solve(n: int, L: int) -> QuantumCircuit:
  qc = QuantumCircuit(n)
  for i in range(L):
    for j in range(n):
      if ((i >> j) & 1) == 0:
        qc.x(j)
    qc.mcp(math.pi, range(n - 1), n - 1)
    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)
  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)
  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:
    for i in range(n):
    qc.h(i)

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

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

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

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

    return qc
'''


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

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

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

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

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

    return qc
'''


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

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

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

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

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

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

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

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

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

    return qc
'''


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

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

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

        qc.x(data_0_bits)

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

        qc.x(data_0_bits)

    return qc
'''


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

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

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

        qc.x(data_0_bits)

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

        qc.x(data_0_bit)

    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):
        bin_data = format(i, f'0{n}b')
        data_0_bits = [idx for idx, digit in enumerate(reversed(bin_data)) if digit == '0']

        qc.x(data_0_bits)

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

        qc.x(data_0_bits)

    return qc
'''


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

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

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

        qc.x(data_0_bits)

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

        qc.x(data_0_bits)

    return qc
'''


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

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

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

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

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

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

    return qc
'''


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

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

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

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

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

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

    return qc
'''


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

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

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

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

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    def get_indices(m: int):
        a = []
        idx = 0
        while m:
            if m % 2:
               a.append(index)
            index += 1
            m //= 2
        return a
    for i in range(L, 2**n):
        a = get_indices(i)
        target = a[-1]
        controls = a[:-1]
        qc.h(target); qc.mcx(controls, target); qc.h(target)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    def get_indices(m: int):
        a = []
        index = 0
        while m:
            if m % 2:
               a.append(index)
            index += 1
            m //= 2
        return a
    for i in range(L, 2**n):
        a = get_indices(i)
        target = a[-1]
        controls = a[:-1]#; print(target, controls, "!!!!")
        if controls:
            qc.h(target); qc.mcx(controls, target); qc.h(target)
        else:
            qc.z(target)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.h(i)
    # Write your code here:
    def get_indices(m: int):
        a = []
        index = 0
        while m:
            if m % 2:
               a.append(index)
            index += 1
            m //= 2
        return a
    for i in range(L, 2**n):
        a = get_indices(i)
        target = a[-1]
        controls = a[:-1]#; print(target, controls, "!!!!")
        if controls:
            qc.h(target); qc.mcx(controls, target); qc.h(target)
        else:
            qc.z(target)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.cz(i, 0)  # 制御Zゲートを使用して各状態に -1 をかける
    return qc
'''


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

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


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(L):
        qc.append(CZGate(), [i, 0])  # CZゲートを使用して各状態に -1 をかける
    return qc
'''


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

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

    # Oracleの実装
    for i in range(L):
        qc.append(CZGate(), [i, 0])  # CZゲートを使用して各状態に -1 をかける

    return qc
'''


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

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

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

    return qc
'''


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

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

    for i in range(L):
        qc.append(CZGate(), [i] + list(range(n, n+L)))

    return qc
'''


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

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

    for i in range(L):
        qc.append(CZGate(), list(range(n - L, n)) + [i])

    return qc
'''


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

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

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

    return qc
'''


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

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

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

    return qc
'''


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

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

    for i in range(L):
        # i番目のビットを制御ビット、最後のビットをターゲットビットとするCZゲートを追加
        qc.append(CZGate().control(n - 1), [i] + [n - 1])

    return qc
'''


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

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

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

    return qc
'''


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

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

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

    return qc
'''


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

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def oracle(qc: QuantumCircuit, n: int, L: int):
    bits_L = [int(i) for i in bin(L)[2:].zfill(n)][::-1]

    for i, bit in enumerate(bits_L):
        if bit == 0:
            qc.x(i)

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

    for i, bit in enumerate(bits_L):
        if bit == 0:
            qc.x(i)

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


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(L):
        binary_i = bin(i)[2:].zfill(n)[::-1]
        for bit_position, bit in enumerate(binary_i):
            if bit == '0':
                qc.x(bit_position)
    multi_control_z = ZGate().control(n)
    qc.append(multi_control_z, range(n))
    for bit_position, bit in enumerate(binary_i):
        if bit == '0':
            qc.x(bit_position)
    return qc
'''


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

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

    binary_L = format(L, f'0{n}b')[::-1]

    z_gate = ZGate().control(n - 1)
    control_qubits = [i for i in range(n) if binary_L[i] == '1']
    if control_qubits:
        qc.append(z_gate, control_qubits + [n - 1])

    return qc
'''


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

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

    return qc
'''


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

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

    # Apply the Z gate (phase flip) to all states |0⟩, |1⟩, ..., |L-1⟩
    for i in range(L):
        binary_repr = f"{i:0{n}b}"  # Convert i to binary with n bits
        controls = [j for j in range(n) if binary_repr[j] == '1']  # Determine which qubits are 1

        if controls:
            qc.append(ZGate().control(len(controls)), controls)
        else:
            qc.z(0)  # Global phase, ignored in judge

    return qc
'''


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

    # Apply the Z gate (phase flip) to all states |0⟩, |1⟩, ..., |L-1⟩
    for i in range(L):
        binary_repr = f"{i:0{n}b}"  # Convert i to binary with n bits
        controls = [j for j in range(n) if binary_repr[j] == '1']  # Determine which qubits are 1

        if len(controls) == 0:
            # If no controls (i.e., for |0...0⟩), apply Z to any qubit (global phase, ignored in judge)
            qc.z(0)
        elif len(controls) == 1:
            # If only one control, just apply a Z gate directly
            qc.z(controls[0])
        else:
            # Apply a multi-controlled Z gate
            qc.append(ZGate().control(len(controls)), controls + [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)

    # Apply the Z gate (phase flip) to all states |0⟩, |1⟩, ..., |L-1⟩
    for i in range(L):
        binary_repr = f"{i:0{n}b}"  # Convert i to binary with n bits
        controls = [j for j in range(n) if binary_repr[j] == '1']  # Determine which qubits are 1

        if len(controls) == 0:
            # If no controls (i.e., for |0...0⟩), apply Z to any qubit (global phase, ignored in judge)
            qc.z(0)
        elif len(controls) == 1:
            # If only one control, just apply a Z gate directly
            qc.z(controls[0])
        else:
            # Apply a multi-controlled Z gate
            qc.append(ZGate().control(len(controls)), controls + [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)

    # If L is 0, no operation is needed
    if L == 0:
        return qc

    # Create a multi-controlled Z gate that flips the phase of the first L states
    # We need to find a way to control the Z gate such that it applies to the first L states
    # One way is to use a combination of X gates and multi-controlled Z gates

    # First, find the binary representation of L-1
    max_state = L - 1
    binary_max = format(max_state, f'0{n}b')

    # We will apply X gates to qubits where the corresponding bit in binary_max is 0
    # This will transform the state |max_state> to |11...1>
    # Then we can apply a multi-controlled Z gate with all controls being 1

    # Apply X gates to qubits where the corresponding bit in binary_max is 0
    for i in range(n):
        if binary_max[i] == '0':
            qc.x(i)

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

    # Apply X gates again to revert the state
    for i in range(n):
        if binary_max[i] == '0':
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    # If L == 2^n, then every state gets a -1 overall,
    # i.e. the oracle is -I which is just a global phase.
    if L == 2**n:
        return qc  # global phase ignored

    # Special case for n == 1.
    if n == 1:
        # For one qubit, possible L are 1 or 2.
        # If L == 1 then only |0> should get a -1.
        if L == 1:
            qc.x(0)
            qc.z(0)
            qc.x(0)
        return qc

    # For n >= 2:
    # Loop over all integers x such that 0 <= x < L.
    # For each such x, we want to flip the phase of |x>.
    for x in range(L):
        # For each qubit i, if the i-th bit (little–endian) of x is 0,
        # apply an X gate so that |x> is mapped to |11...1>.
        for i in range(n):
            if ((x >> i) & 1) == 0:
                qc.x(i)
        # Now, the state corresponding to x has been mapped to |11...1>.
        # We now flip the phase of |11...1> by applying a multi–controlled Z.
        # A convenient way is to conjugate a multi–controlled X with H on the target.
        qc.h(n-1)
        qc.mcx(list(range(n-1)), n-1)  # multi–controlled X with controls 0,...,n-2 and target n-1.
        qc.h(n-1)
        # Undo the X gates.
        for i in range(n):
            if ((x >> i) & 1) == 0:
                qc.x(i)

    return qc
'''


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

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

    return qc
'''


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

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

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    # L-1 のビット表現を取得し、0のビット位置を特定
    bits_to_flip = [i for i in range(n) if not ((L - 1) >> i) & 1]

    # 必要なビットを反転
    for bit in bits_to_flip:
        qc.x(bit)

    # マルチ制御Zゲートを適用
    qc.append(ZGate().control(n - 1), range(n))

    # ビットを元に戻す
    for bit in bits_to_flip:
        qc.x(bit)

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    # L-1 のビット表現を取得し、0のビット位置を特定
    bits_to_flip = [i for i in range(n) if not ((L - 1) >> i) & 1]

    # 必要なビットを反転
    for bit in bits_to_flip:
        qc.x(bit)

    # マルチ制御Zゲートを適用
    qc.append(ZGate().control(n - 1), range(n))

    # ビットを元に戻す
    for bit in bits_to_flip:
        qc.x(bit)

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for state in range(L):
        # Convert the state to binary representation and apply X gates accordingly
        state_binary = format(state, f'0{n}b')
        for qubit, bit in enumerate(state_binary):
            if bit == '0':
                qc.x(qubit)

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

        # Reapply X gates to revert to the original state
        for qubit, bit in enumerate(state_binary):
            if bit == '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:

    for state in range(L):

        state_binary = format(state, f'0{n}b')[::-1]
        for qubit, bit in enumerate(state_binary):
            if bit == '0':
                qc.x(qubit)

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

        for qubit, bit in enumerate(state_binary):
            if bit == '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:

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

        if n > 1:
            qc.mct(list(range(n - 1)), n - 1)
        else:
            qc.z(0)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    from qiskit.circuit.library import cz
    qc.append(cz().control(L), range(n))
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


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

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

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

    return qc
'''


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

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

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


'''python
from qiskit import QuantumCircuit

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

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


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

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

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


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

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

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

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

    return qc
'''


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

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

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

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

    return qc
'''


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

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

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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

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

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

            for j in range(i, n):
                qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    if (L == pow(2, n)):
        pass
    else:
        for i in range(n):
            if (L >> i) & 1 == 1:
                for j in range(i, n):
                    qc.x(j)

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

                for j in range(i, n):
                    qc.x(j)

    return qc
'''


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

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

    if (L == pow(2, n)):
        pass
    else:
        for i in range(n):
            if (L >> i) & 1 == 1:
                for j in range(i, n):
                    qc.x(j)

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

                for j in range(i, n):
                    qc.x(j)

    return qc
'''


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

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

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

            qc.x(i)

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

            qc.x(i)

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


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

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

    for i in range(n):
        qc.h(i)
    for i in range(L):
        # 2進数の配列に変換
        b = list(map(int, list(format(i, "b"))))
        for k in range(b.len()):
            if b[k] == 0:
                qc.x(k)
        qc.append(ZGate().control(n-1), range(n))
        for k in range(b.len()):
            if b[k] == 0:
                qc.x(k)
    return qc
'''


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

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

    for i in range(n):
        qc.h(i)
    for i in range(L):
        # 2進数の配列に変換
        b = list(map(int, list(format(i, "b"))))
        for k in range(b.len()):
            if b[k] == 0:
                qc.x(k)
        qc.append(ZGate().control(n-1), range(n))
        for k in range(b.len()):
            if b[k] == 0:
                qc.x(k)

    return qc
'''


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

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

    for i in range(n):
        qc.h(i)
    for i in range(L):
        # 2進数の配列に変換
        b = list(map(int, list(format(i, "b"))))
        for k in range(len(b)):
            if b[k] == 0:
                qc.x(k)
        qc.append(ZGate().control(n-1), range(n))
        for k in range(len(b)):
            if b[k] == 0:
                qc.x(k)

    return qc
'''


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

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

    for i in range(L):
        # 2進数の配列に変換
        b = list(map(int, list(format(i, "b"))))
        for k in range(len(b)):
            if b[k] == 0:
                qc.x(k)
        qc.append(ZGate().control(n-1), range(n))
        for k in range(len(b)):
            if b[k] == 0:
                qc.x(k)

    return qc
'''


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

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

    for i in range(min(L, 2**n)):
        # 2進数の配列に変換
        b = list(map(int, list(format(i, "b"))))
        for k in range(len(b)):
            if b[k] == 0:
                qc.x(k)
        qc.append(ZGate().control(n-1), range(n))
        for k in range(len(b)):
            if b[k] == 0:
                qc.x(k)

    return qc
'''


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

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

    for i in range(L):
        # 2進数の配列に変換
        b = list(map(int, list(format(i, "0"+str(n)+"b"))))
        print(b)
        for k in range(len(b)):
            if b[k] == 0:
                qc.x(k)
        qc.append(ZGate().control(n-1), range(n))
        for k in range(len(b)):
            if b[k] == 0:
                qc.x(k)

    return qc
'''


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

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

    for i in range(L):
        # 2進数の配列に変換
        b = list(map(int, list(format(i, "0"+str(n)+"b"))))
        print(b)
        for k in range(len(b)):
            if b[k] == 0:
                qc.x(k)
        if n == 1:
            qc.z(0)
        else:
            qc.append(ZGate().control(n-1), range(n))
        for k in range(len(b)):
            if b[k] == 0:
                qc.x(k)

    return qc
'''


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

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

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

    for i in range(L):
        # 2進数の配列に変換
        b = list(map(int, list(format(i, "0"+str(n)+"b"))))
        print(b)
        for k in range(len(b)):
            if b[k] == 0:
                qc.x(k)
        if n == 1:
            qc.z(0)
        else:
            qc.append(ZGate().control(n-1), range(n))
        for k in range(len(b)):
            if b[k] == 0:
                qc.x(k)

    return qc
'''


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

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

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

    for i in range(L):
        # 2進数の配列に変換
        b = list(map(int, list(format(i, "0"+str(n)+"b"))))
        print(b)
        for k in range(n):
            if b[k] == 0:
                qc.x(n-k-1)
        if n == 1:
            qc.z(0)
        else:
            qc.append(ZGate().control(n-1), range(n))
        for k in range(n):
            if b[k] == 0:
                qc.x(n-k-1)

    return qc
'''


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

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

    return qc
solve(5,4)
'''


'''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(XGate().control(n), 0)
    if (n>1):
        for i in range(L-1):
            # qc.z(0)
            qc.append(ZGate().control(n-1), range(n))
        qc.draw()
        print(qc)
    else:
        for i in range(L-1):
            qc.z(i)

    return qc
solve(5,4)
'''


'''python
from qiskit import QuantumCircuit

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

    for i in range(L):
        for j in range(n):
            if i >> j & 1: continue
            qc.x(j)
        qc.h(n - 1)
        qc.mct(arr, n - 1)
        qc.h(n - 1)
        for j in range(n):
            if i >> j & 1: continue
            qc.x(j)
    # qc.append()
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for i in range(L):
        for j in range(n):
            if i >> j & 1: continue
            qc.x(j)
        qc.h(n - 1)
        qc.mcx(arr, n - 1)
        qc.h(n - 1)
        for j in range(n):
            if i >> j & 1: continue
            qc.x(j)
    # qc.append()
    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:
#     arr = [i for i in range(n - 1)]
    for i in range(n):
        qc.h(i)
    for i in range(L):
        for j in range(n):
            if i >> j & 1: continue
            qc.x(j)
        qc.append(ZGate().control(n - 1), range(n))
#         qc.h(n - 1)
#         qc.mcx(list(range(n - 1)), n - 1)
#         qc.h(n - 1)
        for j in range(n):
            if i >> j & 1: continue
            qc.x(j)

    #qc.append()
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
#     arr = [i for i in range(n - 1)]
    from qiskit.circuit.library import ZGate

    for i in range(n):
        qc.h(i)
    for i in range(L):
        for j in range(n):
            if i >> j & 1: continue
            qc.x(n - 1 - j)
        qc.append(ZGate().control(n - 1), range(n))
#         qc.h(n - 1)
#         qc.mcx(list(range(n - 1)), n - 1)
#         qc.h(n - 1)
        for j in range(n):
            if i >> j & 1: continue
            qc.x(n - 1 - j)

    #qc.append()
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
#     arr = [i for i in range(n - 1)]
    for i in range(n):
        qc.h(i)
    for i in range(L):
        for j in range(n):
            if i >> j & 1: continue
            qc.x(j)
        qc.append(RZGate(math.pi * 2).control(n - 1), range(n))
#         qc.h(n - 1)
#         qc.mcx(list(range(n - 1)), n - 1)
#         qc.h(n - 1)
        for j in range(n):
            if i >> j & 1: continue
            qc.x(j)

    #qc.append()
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
#     arr = [i for i in range(n - 1)]
    for i in range(n):
        qc.h(i)
    for i in range(L):
        for j in range(n):
            if i >> j & 1: continue
            qc.x(j)
        # qc.append(RZGate(math.pi * 2).control(n - 1), range(n))
#         qc.h(n - 1)
#         qc.mcx(list(range(n - 1)), n - 1)
#         qc.h(n - 1)
        for j in range(n):
            if i >> j & 1: continue
            qc.x(j)

    #qc.append()
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
#     arr = [i for i in range(n - 1)]
    for i in range(n):
        qc.h(i)
    for i in range(L):
        for j in range(n):
            if i >> j & 1: continue
            qc.x(j)
        if n == 1:
            qc.z(0)
        else:
            qc.append(ZGate().control(n - 1), range(n))
#         qc.h(n - 1)
#         qc.mcx(list(range(n - 1)), n - 1)
#         qc.h(n - 1)
        for j in range(n):
            if i >> j & 1: continue
            qc.x(j)

    #qc.append()
    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:
#     arr = [i for i in range(n - 1)]
    # for i in range(n):
    #     qc.h(i)
    for i in range(L):
        for j in range(n):
            if i >> j & 1: continue
            qc.x(j)
        qc.append(ZGate().control(n - 1), range(n))
#         qc.h(n - 1)
#         qc.mcx(list(range(n - 1)), n - 1)
#         qc.h(n - 1)
        for j in range(n):
            if i >> j & 1: continue
            qc.x(j)

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

    #qc.append()
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.rx(2*np.pi,0)
    rx_gate = RXGate(2*np.pi)
    mc_rx = rx_gate.control(n - 1)
    qc.append(mc_rx, qargs=range(n))

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.rx(2*math.pi,0)
    rx_gate = RXGate(2*math.pi)
    mc_rx = rx_gate.control(n - 1)
    qc.append(mc_rx, qargs=range(n))

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L_code = format(L, f'0{n}b')
    for i in range(n):
        if L_code[i] == "1":
            for j in range(i):
                if L_code[j] == "0":
                    qc.x(n-j-1)
            qc.x(n-i-1)
            if i == 0:
                qc.z(n-1)
            else:
                qc.append(ZGate().control(i), qargs=range(n-1,n-i-2,-1))
            for j in range(i):
                if L_code[j] == "0":
                    qc.x(n-j-1)
            qc.x(n-i-1)
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L == 2**n:
      qc.rx(2*math.pi,0)
    else:
      L_code = format(L, f'0{n}b')
      for i in range(n):
          if L_code[i] == "1":
              for j in range(i):
                  if L_code[j] == "0":
                      qc.x(n-j-1)
              qc.x(n-i-1)
              if i == 0:
                  qc.z(n-1)
              else:
                  qc.append(ZGate().control(i), qargs=range(n-1,n-i-2,-1))
              for j in range(i):
                  if L_code[j] == "0":
                      qc.x(n-j-1)
              qc.x(n-i-1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for l in range(L):
        # print(l)
        # i=0
        for i in range(n):
            if not (1<<i)&l:
                qc.x(n-i-1)
                # print("i=",i)
        if n>1:
            qc.h(0)
            qc.mcx(list(range(1,n)),0)
            qc.h(0)
        else:
            qc.z(0)

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


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for l in range(L):
        # print(l)
        # i=0
        for i in range(n):
            if not (1<<i)&l:
                qc.x(i)
                # print("i=",i)
        if n>1:
            qc.h(0)
            qc.mcx(list(range(1,n)),0)
            qc.h(0)
        else:
            qc.z(0)

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

    for i in range(L):
        binary_str = format(i, f'0{n}b')

        for qubit_index, bit in enumerate(binary_str):
            if bit == '0':
                qc.x(qubit_index)

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

        for qubit_index, bit in enumerate(binary_str):
            if bit == '0':
                qc.x(qubit_index)

    return qc
'''


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

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

    for i in range(L):
        binary_str = format(i, f'0{n}b')

        for qubit_index, bit in enumerate(binary_str):
            if bit == '0':
                qc.x(qubit_index)

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

        for qubit_index, bit in enumerate(binary_str):
            if bit == '0':
                qc.x(qubit_index)

    return qc
'''


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

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

    for i in range(L):

        for qubit_index in range(n):
            if not (i & (1 << qubit_index)):
                qc.x(qubit_index)

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

        for qubit_index in range(n):
            if not (i & (1 << qubit_index)):
                qc.x(qubit_index)

    return qc
'''


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

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

    for i in range(L):

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

        for qubit_index, bit in enumerate(binary_str[::-1]):
            if bit == '0':
                qc.x(qubit_index)

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

        for qubit_index, bit in enumerate(binary_str[::-1]):
            if bit == '0':
                qc.x(qubit_index)

    return qc
'''


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

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

    for i in range(L):

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

        for qubit_index, bit in enumerate(binary_str[::-1]):
            if bit == '0':
                qc.x(qubit_index)

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

        for qubit_index, bit in enumerate(binary_str[::-1]):
            if bit == '0':
                qc.x(qubit_index)

    return qc
'''


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

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

    for i in range(L):

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

        for qubit_index, bit in enumerate(binary_str):
            if bit == '0':
                qc.x(qubit_index)

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

        for qubit_index, bit in enumerate(binary_str):
            if bit == '0':
                qc.x(qubit_index)

    return qc
'''


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

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

    for i in range(L):

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

        for qubit_index, bit in enumerate(binary_str):
            if bit == '0':
                qc.x(qubit_index)

        #qc.h(n-1)
        #qc.mcx(list(range(n-1)), n-1)
        #qc.h(n-1)
        qc.append(ZGate().control(n - 1), range(n))

        for qubit_index, bit in enumerate(binary_str):
            if bit == '0':
                qc.x(qubit_index)

    return qc
'''


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

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

    for i in range(L):

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

        for qubit_index, bit in enumerate(binary_str):
            if bit == '0':
                qc.x(qubit_index)

        #qc.h(n-1)
        #qc.mcx(list(range(n-1)), n-1)
        #qc.h(n-1)
        qc.append(ZGate().control(n - 1), range(n))

        for qubit_index, bit in enumerate(binary_str):
            if bit == '0':
                qc.x(qubit_index)

    return qc
'''


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

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

    for i in range(L):

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

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

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

    return qc
'''


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

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

    for i in range(L):

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

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

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

    return qc
'''


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

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

    # Write your code here:

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

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

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

    return qc
'''


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

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

    # Write your code here:

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

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

    for i in range(L):
        to_calcable(n,qc,i)
        reverse(n,qc)
        to_calcable(n,qc,i)

    return qc
'''


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

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

    # Write your code here:

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

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

    for i in range(L):
        to_calcable(n,qc,i)
        reverse(n,qc)
        to_calcable(n,qc,i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

        else:
            bit = char(bin(i))
            char = str(bin(4))[2:]
            positions = [pos for pos, char in enumerate(reversed(binary_string)) if char == '1']
            qc.z(positions[0])

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if(i > L):
            bit = char(bin(i))
            char = str(bin(4))[2:]
            positions = [pos for pos, char in enumerate(reversed(binary_string)) if char == '1']
            qc.z(positions[0])
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if(i > L):
            # 例えば、i = 3 の場合

            # i を2進数文字列に変換し、'0b' プレフィックスを削除
            binary_string = bin(i)[2:]

            # 1のビットが存在する位置を見つける
            for j in range(len(binary_string)):
                if(binary_string[j] == '1'):
                    # 1のビットが存在する位置に対応する量子ビットにCNOTゲートを適用
                    print(j)
                    qc.z(j)
                    break
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if(i > L):
            # 例えば、i = 3 の場合

            # i を2進数文字列に変換し、'0b' プレフィックスを削除
            binary_string = bin(i)[2:]

            # 1のビットが存在する位置を見つける
            for j in range(len(binary_string)):
                if(binary_string[j] == '1'):
                    # 1のビットが存在する位置に対応する量子ビットにCNOTゲートを適用
                    qc.z(j)
                    break
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if(i > L):
            # 例えば、i = 3 の場合

            # i を2進数文字列に変換し、'0b' プレフィックスを削除
            binary_string = bin(i)[2:]

            # 1のビットが存在する位置を見つける
            for j in range(len(binary_string)):
                if(binary_string[j] == '1'):
                    # 1のビットが存在する位置に対応する量子ビットにCNOTゲートを適用
                    j = len(binary_string) - j
                    qc.z(j)
                    break
    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):
        if(i > L):
            # 例えば、i = 3 の場合

            # i を2進数文字列に変換し、'0b' プレフィックスを削除
            binary_string = bin(i)[2:]

            # 1のビットが存在する位置を見つける
            for j in range(len(binary_string)):
                if(binary_string[j] == '1'):
                    # 1のビットが存在する位置に対応する量子ビットにCNOTゲートを適用
                    j = len(binary_string) -
                    qc.z(j)
                    break
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    num_qubits = n
    for i in range(L):
    state = format(i, '0' + str(num_qubits) + 'b')
    qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])
    qc.append(ZGate().control(num_qubits-1), qc.qubits)
    qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    num_qubits = n
    for i in range(L):
        state = format(i, '0' + str(num_qubits) + 'b')
        qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])
        qc.append(ZGate().control(num_qubits-1), qc.qubits)
        qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    num_qubits = n
    for i in range(L):
        state = format(i, '0' + str(num_qubits) + 'b')
        qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])
        qc.append(ZGate().control(num_qubits-1), qc.qubits)
        qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    num_qubits = n
    for i in range(n**2):
        state = format(i, '0' + str(num_qubits) + 'b')
        qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])
        qc.append(ZGate().control(num_qubits-1), qc.qubits)
        qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    # 必要な量子ビットの数を計算
    num_qubits = max(n, math.ceil(math.log2(L+1)))
    qc = QuantumCircuit(num_qubits)

    for i in range(L):
        state = format(i, '0' + str(num_qubits) + 'b')
        qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])

        if num_qubits > 1:
            qc.append(ZGate().control(num_qubits-1), qc.qubits)
        else:
            # 1量子ビットの場合、通常のZゲートを使用
            qc.z(0)

        qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])

    return qc
'''


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

def mex(lis):
    for num in range(100):
        if num not in lis:
            # print(num,flush=True)
            return num

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    D=[0 for i in range(2**n)]
    for i in range(L , 2**n):
        if D[i]==0:
            sub = []
            for q in range(n):
                if i>>q&1:
                    sub.append(q)

            num_ctrl_qubits = sub
            # MCPhaseGate(ZGate(), num_ctrl_qubits/
            # MCPhaseGate()
            # qc.append(ZGate().control(len(sub)-1), sub)

            tgt = mex(sub)
            qc.mcp(-1,sub,tgt)

            # print(sub)
            for num in range(i,2**n):
                j = num&i==i
                if j:
                    D[num]^=1
            # print(D)

    # print(D)

    qc.z(n-1)
    return qc
'''


'''python
from qiskit import Aer, execute, QuantumCircuit, QuantumRegister

def mex(lis):
    for num in range(100):
        if num not in lis:
            # print(num,flush=True)
            return num

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    D=[0 for i in range(2**n)]
    for i in range(L , 2**n):
        if D[i]==0:
            sub = []
            for q in range(n):
                if i>>q&1:
                    sub.append(q)

            num_ctrl_qubits = sub
            # MCPhaseGate(ZGate(), num_ctrl_qubits/
            # MCPhaseGate()
            # qc.append(ZGate().control(len(sub)-1), sub)

            tgt = mex(sub)
            qc.mcp(-1,sub,tgt)

            # print(sub)
            for num in range(i,2**n):
                j = num&i==i
                if j:
                    D[num]^=1
            # print(D)

    # print(D)

    qc.z(n-1)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def mex(lis):
    for num in range(100):
        if num not in lis:
            # print(num,flush=True)
            return num

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    D=[0 for i in range(2**n)]
    for i in range(L , 2**n):
        if D[i]==0:
            sub = []
            for q in range(n):
                if i>>q&1:
                    sub.append(q)

            num_ctrl_qubits = sub
            # MCPhaseGate(ZGate(), num_ctrl_qubits/
            # MCPhaseGate()
            # qc.append(ZGate().control(len(sub)-1), sub)

            tgt = mex(sub)
            qc.mcp(-1,sub,tgt)

            # print(sub)
            for num in range(i,2**n):
                j = num&i==i
                if j:
                    D[num]^=1
            # print(D)

    # print(D)

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


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

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


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

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


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

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


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(0,L):
        qc.append(ZGate().control(n), 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(0,L):
        qc.append(ZGate().control(i), 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(0,L):
        qc.append(ZGate().control(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.x(0)
    qc.z(0)
    qc.x(0)
    qc.z(0)
    A = []
    B = [1] * (2 ** n)
    for i in range(2**n):
      A += [(i.bit_count(),i)]
    A.sort()
    for (_,i) in A:
      tgt = -1
      if i < L:
        tgt = 1
      if tgt != B[i]:
        l = []
        for b in range(n):
          if (i & (1 << b)) > 0:
            l += [b]
        if len(l) == 1:
          qc.z(l[0])
        else:
          qc.append(ZGate().control(len(l) - 1), l)
        for b in range(2**n):
          if (i & b) == i:
            B[b] *= -1
    return qc
'''


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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

    b_n_1 = l_bit_str[0]
    if b_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):
        b_i = l_bit_str[i]
        if b_i == "0":
            qc.x(i)
        else:
            qc.x(i)
            qc.mcx(range(n-1, i, 1), i)
            qc.x(i)

    for i in range(n):
        b_i = l_bit_str[i]
        if b_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:
    l_bit_str = bin(L)[1:]

    b_n_1 = l_bit_str[0]
    if b_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):
        b_i = l_bit_str[i]
        if b_i == "0":
            qc.x(i)
        else:
            qc.x(i)
            qc.append(ZGate().control(i - 1), range(i))
            qc.x(i)

    for i in range(n):
        b_i = l_bit_str[i]
        if b_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:
    l_bit_str = bin(L)[2:]
    print(l_bit_str)

    b_n_1 = l_bit_str[0]
    if b_n_1 == "0":
        qc.x(n-1)
    else:
        qc.x(n-1)
        qc.z(n-1)
        qc.x(n-1)

    # print(n-1, b_n_1)
    # print(qc.draw())
    for i in range(n-2, -1, -1):
        b_i = l_bit_str[n - i -1]
        if b_i == "0":
            qc.x(i)
        else:
            qc.x(i)
            c_bits = list(range(i + 1, n))
            qc.mcx(c_bits, i)
            qc.x(i)

        # print(i, b_i)
        # print(qc.draw())

    for i in range(1, n):
        b_i = l_bit_str[i]
        if b_i == "0":
            qc.x(n-i-1)

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    l_bit_str = bin(L)[2:]
    print(l_bit_str)

    b_n_1 = l_bit_str[0]
    if b_n_1 == "0":
        qc.x(n-1)
    else:
        qc.x(n-1)
        qc.z(n-1)
        qc.x(n-1)

    # print(n-1, b_n_1)
    # print(qc.draw())
    for i in range(n-2, -1, -1):
        b_i = l_bit_str[n - i -1]
        if b_i == "0":
            qc.x(i)
        else:
            qc.x(i)
            c_bits = list(range(i + 1, n))
            qc.mcx(c_bits, i)
            qc.x(i)

        # print(i, b_i)
        # print(qc.draw())

    for i in range(1, n):
        b_i = l_bit_str[i]
        if b_i == "0":
            qc.x(n-i-1)

    return qc.reverse_bits()
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    l_bit_str = bin(L)[2:]
    print(l_bit_str)

    b_n_1 = l_bit_str[0]
    if b_n_1 == "0":
        qc.x(n-1)
    else:
        qc.x(n-1)
        qc.z(n-1)
        qc.x(n-1)

    # print(n-1, b_n_1)
    # print(qc.draw())
    qc.barrier()
    for i in range(n-2, -1, -1):
        b_i = l_bit_str[n - i -1]
        if b_i == "0":
            qc.x(i)
        else:
            qc.x(i)
            qubits = list(range(i, n))
            qc.append(ZGate().control(n - i - 1), qubits)
            qc.x(i)

        # print(i, b_i)
        # print(qc.draw())
        qc.barrier()
    for i in range(1, n):
        b_i = l_bit_str[i]
        if b_i == "0":
            qc.x(n-i-1)
        qc.barrier()

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    l_bit_str = bin(L)[2:]
    print(l_bit_str)

    b_n_1 = l_bit_str[0]
    if b_n_1 == "0":
        qc.x(n-1)
    else:
        qc.x(n-1)
        qc.z(n-1)
        qc.x(n-1)

    # print(n-1, b_n_1)
    # print(qc.draw())
    qc.barrier()
    for i in range(n-2, -1, -1):
        b_i = l_bit_str[n - i -1]
        if b_i == "0":
            qc.x(i)
        else:
            qc.x(i)
            qubits = list(range(i, n))
            qc.append(ZGate().control(n - i - 1), qubits)
            qc.x(i)

        # print(i, b_i)
        # print(qc.draw())
        qc.barrier()
    for i in range(1, n):
        b_i = l_bit_str[i]
        if b_i == "0":
            qc.x(n-i-1)
        qc.barrier()

    return qc.reverse_bits()
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    l_bit_str = np.binary_repr(L, n)
    # print(l_bit_str)

    b_n_1 = l_bit_str[0]
    if b_n_1 == "0":
        qc.x(n-1)
    else:
        qc.x(n-1)
        qc.z(n-1)
        qc.x(n-1)

    # print(n-1, b_n_1)
    # print(qc.draw())
    qc.barrier()
    for i in range(n-2, -1, -1):
        b_i = l_bit_str[n - i -1]
        if b_i == "0":
            qc.x(i)
        else:
            qc.x(i)
            qubits = list(range(i, n))
            qc.append(ZGate().control(n - i - 1), qubits)
            qc.x(i)

        qc.barrier()
    for i in range(1, n):
        b_i = l_bit_str[i]
        if b_i == "0":
            qc.x(n-i-1)
        qc.barrier()

    return qc
'''


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

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

    # Write your code here:
    v = np.array([-1] * n + [1]*(L-n))
    matrix = np.diag(v)

    qc.append(UnitaryGate(matrix), range(n))

    return qc
'''


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

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

    # Write your code here:
    v = np.array([-1] * n + [1]* (n - L))
    matrix = np.diag(v)

    qc.append(UnitaryGate(matrix), range(n))

    return qc
'''


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

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

    # Write your code here:
    v = np.array([-1] * L + [1]* (n - L))
    matrix = np.diag(v)

    qc.append(UnitaryGate(matrix), range(n))

    return qc
'''


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

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

    # Write your code here:
    v = np.array([-1] * L + [1]* (2**n - L))
    matrix = np.diag(v)

    qc.append(UnitaryGate(matrix), range(n))

    return qc
'''


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

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

    # Write your code here:
    num_binary = np.binary_repr(L, n)

    # Discard the 0s at the end, as they will not be used and save
    # unnecessary X gates
    num_binary = num_binary.rstrip('0')

    if num_binary[0] == '1':
        qc.x(n - 1)
        qc.z(n - 1)
        qc.x(n - 1)
    else:
        qc.x(n - 1)

    # For loop on the remaining digits
    for position1, value in enumerate(num_binary[1:]):
        # Rename the position as it starts with 0 in the second bit and
        # we want it to be 1.
        position = position1 + 1

        if value == '0':
            qc.x(n - position - 1)
        else:
            qc.x(n - position-1)
            qubits = list(range(n-1, n-position-2, - 1))
            qc.append(ZGate().control(position), qubits)
            qc.x(n-position-1)

    for position, value in enumerate(num_binary):
        # Apply X gates to qubits in position of bits with a 0 value
        if value == '0':
            qc.x(n-position-1)
        else:
            pass

    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

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

    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)

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

    for i in skip_idx:
        qc.x(i)

    return qc
'''


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

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

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

    return qc
'''


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

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


'''python
from qiskit import QuantumCircuit

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

    for ii in range(k-1, 0, -1):
        i = ii - 1
        print(i)
        if L & (1 << i) == 0:
            qc.x(i)
        else:
            qc.x(i)
            for j in range(i+1, k):
                qc.cz(i, j)
            qc.x(i)

    for i in range(k):
        qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for ii in range(k-1, 0, -1):
        i = ii - 1
        if L & (1 << i) == 0:
            qc.x(i)
        else:
            qc.x(i)
            for j in range(i+1, k):
                qc.cz(i, j)
            qc.x(i)

    for i in range(k):
        qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for ii in range(k-1, 0, -1):
        i = ii - 1
        print(i)
        if L & (1 << i) == 0:
            qc.x(i)
        else:
            qc.x(i)
            qc.h(i)
            qc.mcx(list(range(i+1, k)), i)
            qc.h(i)
            qc.x(i)

    for i in range(k):
        qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for ii in range(k-1, 0, -1):
        i = ii - 1
        if L & (1 << i) == 0:
            qc.x(i)
        else:
            qc.x(i)
            qc.h(i)
            qc.mcx(list(range(i+1, k)), i)
            qc.h(i)
            qc.x(i)

    for i in range(k):
        qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    if L & (1 << (k-1)) == 0:
        qc.x(k-1)
    else:
        qc.x(k-1)
        qc.z(k-1)
        qc.x(k-1)

    for ii in range(k-1, 0, -1):
        i = ii - 1
        if L & (1 << i) == 0:
            qc.x(i)
        else:
            qc.x(i)
            qc.h(i)
            qc.mcx(list(range(i+1, k)), i)
            qc.h(i)
            qc.x(i)

    for i in range(k):
        if L & (1 << k) == 0:
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    if L & (1 << (n - 1)):
        qc.x(n-1)
        qc.z(n-1)
        qc.x(n-1)
    else:
        qc.x(n-1)

    for i in range(n - 2, -1, -1):
        if L & (1 << (i)) == 0:
            qc.x(i)
        else:
            qc.x(i)
            qc.h(i)
            qc.mcx(list(range(i+1,n)), i)
            qc.h(i)
            qc.x(i)

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


'''python
from qiskit import QuantumCircuit

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