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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def find_one_positions(n):
    positions = []
    position = 0

    while n:
        if n & 1:
            positions.append(position)
        n >>= 1
        position += 1

    return positions

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

    # qc.h(0)
    # qc.cnot(0, 1)
    # qc.h(1)

    neg_list = [0]*(2**n)
    for i in range(L, 2**n):
        print(neg_list)
        pos1 = find_one_positions(i)
        if neg_list[i] == 0:
            if len(pos1) == 1:
                qc.p(math.pi, pos1[0])
            else:
                qc.mcp(math.pi, pos1[1:], pos1[0])
            for m in range(2**n):
                pos1m = find_one_positions(m)
                if all(elem in pos1m for elem in pos1):
                    print(m)
                    neg_list[m] = 1 - neg_list[m]

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    from qiskit.circuit.library import ZGate
    for i in range(1,n+1):# 第i+1 qubitから第n qubit までbin(L)と一致するような量子ビットb<Lに対して操作を行う
        for j in range(i,n):# bin(L)の第i+1桁からn桁目までのうち0であるものに対しxを作用させる（i+1桁以降が全部1になる）
            if(not (L>>(j) & 1)):
                qc.x(j)
        qc.x(i-1) # i番目の0と1を入れ替えておく．これで1になるものに対し係数の符号を反転させる
        if(i==n):
            qc.x(n-1)
        else:
            v=[]
            for k in range(i,n):
                v.append(k)
            v.append(i-1)
            qc.append(ZGate.control(n-i),v)

        qc.x(i-1)
        for j in range(i,n):# bin(L)の第i+1桁からn桁目までのうち0であるものに対しxを作用させる（i+1桁以降が全部1になる）
            if(not (L>>(j) & 1)):
                qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    from qiskit.circuit.library import ZGate
    for i in range(1,n+1):# 第i+1 qubitから第n qubit までbin(L)と一致するような量子ビットb<Lに対して操作を行う
        for j in range(i,n):# bin(L)の第i+1桁からn桁目までのうち0であるものに対しxを作用させる（i+1桁以降が全部1になる）
            if(not (L>>j & 1)):
                qc.x(j)
        qc.x(i-1) # i番目の0と1を入れ替えておく．これで1になるものに対し係数の符号を反転させる
        if(i==n):
            qc.x(n-1)
        else:
            v=[]
            for k in range(i,n):
                v.append(k)
            v.append(i-1)
            qc.append(ZGate().control(n-i),v)

        qc.x(i-1)
        for j in range(i,n):# bin(L)の第i+1桁からn桁目までのうち0であるものに対しxを作用させる（i+1桁以降が全部1になる）
            if(not (L>>(j) & 1)):
                qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    from qiskit.circuit.library import ZGate
    for i in range(1,n+1):# 第i+1 qubitから第n qubit までbin(L)と一致するような量子ビットb<Lに対して操作を行う
        for j in range(i,n):# bin(L)の第i+1桁からn桁目までのうち0であるものに対しxを作用させる（i+1桁以降が全部1になる）
            if(not (L>>j & 1)):
                qc.x(j)
        qc.x(i-1) # i番目の0と1を入れ替えておく．これで1になるものに対し係数の符号を反転させる
        if(i==n):
            qc.z(n-1)
        else:
            v=[]
            for k in range(i,n):
                v.append(k)
            v.append(i-1)
            qc.append(ZGate().control(n-i),v)

        qc.x(i-1)
        for j in range(i,n):# bin(L)の第i+1桁からn桁目までのうち0であるものに対しxを作用させる（i+1桁以降が全部1になる）
            if(not (L>>(j) & 1)):
                qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    from qiskit.circuit.library import ZGate
    for i in range(1,n+1):# 第i+1 qubitから第n qubit までbin(L)と一致するような量子ビットb<Lに対して操作を行う
        if not ((L>>i-1)&1):
            continue

        for j in range(i,n):# bin(L)の第i+1桁からn桁目までのうち0であるものに対しxを作用させる（i+1桁以降が全部1になる）
            if(not (L>>j & 1)):
                qc.x(j)
        qc.x(i-1) # i番目の0と1を入れ替えておく．これで1になるものに対し係数の符号を反転させる
        if(i==n):
            qc.z(n-1)
        else:
            qc.append(ZGate().control(n-i),range(i-1,n))
        qc.x(i-1)
        for j in range(i,n):# bin(L)の第i+1桁からn桁目までのうち0であるものに対しxを作用させる（i+1桁以降が全部1になる）
            if(not (L>>j & 1)):
                qc.x(j)
    return qc
'''


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

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

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

  return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    if (L & (1 << (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 = (L & (1  << i))
        if b == 0:
            qc.x(i)
        else:
            data = []
            for t in range(n-1,i,-1):
                data.append(t)
            qc.x(i)
            qc.mcp(lam=math.pi,control_qubits=data, target_qubit=i)
            qc.x(i)

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


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    bits = [int(x) for x in f"{L:0{n}b}"]
    bits.reverse()
    for i in reversed(range(n)):
        if bits[i] == 0:
            continue
        for j in range(i + 1, n):
            if bits[j] == 0:
                qc.x(j)
        qc.x(i)
        if i == n - 1:
            qc.z(i)
        else:
            qc.append(ZGate().control(n - i - 1), range(i, n))
        qc.x(i)
        for j in range(i + 1, n):
            if bits[j] == 0:
                qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

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

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

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

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

        i += 1

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    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
from qiskit.circuit.library import ZGate

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

    lbit = l.bit_length()
    for i in range(lbit - 1):
        for j in range(i + 1, n):
            qc.x(j)

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

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

    for i in range(2**lbit, l):
        for j in range(n):
            if (i >> j) & 1 == 0:
                qc.x(j)

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

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

    return qc
'''


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

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

    lbit = L.bit_length()
    for i in range(lbit - 1):
        for j in range(i + 1, n):
            qc.x(j)

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

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

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

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

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

    return qc
'''


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

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

    lbit = L.bit_length()
    for i in range(lbit - 1):
        for j in range(i + 1, n):
            qc.x(j)

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

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

    for i in range(2**(lbit - 1), L):
        for j in range(n):
            if (i >> j) & 1 == 0:
                qc.x(j)

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

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

    return qc
'''


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

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

    lbit = L.bit_length()
    for i in range(lbit - 1):
        for j in range(i + 1, n):
            qc.x(j)

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

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

    for i in range(2**(lbit - 1), L):
        for j in range(n):
            if (i >> j) & 1 == 0:
                qc.x(j)

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

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

    return qc
'''


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

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

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

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

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

    lbit = L.bit_length()
    for i in range(lbit - 1):
        for j in range(i + 1, n):
            qc.x(j)

        if i == n - 1:
            qc.z(n - 1)
        else:
            print(f'num: {n - i}, c: ({i}, {n})', flush=True)
            qc.append(ZGate().control(n - i - 1), range(i, n))

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

    for i in range(2**(lbit - 1), L):
        for j in range(n):
            if (i >> j) & 1 == 0:
                qc.x(j)

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

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

    return qc
'''


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

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

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

    for i in range(n):
        if (L >> i) & 1 == 0:
            continue

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

        if i == n - 1:
            qc.z(n - 1)
        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)

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

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

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

    return qc
'''


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

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

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

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

    for i in range(n):
        if (L >> i) & 1 == 0:
            continue

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

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

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

    return qc
'''


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

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

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

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

    for i in range(n):
        if (L >> i) & 1 == 0:
            continue

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

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

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

    return qc
'''


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

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

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


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

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


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

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


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

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


'''python
from qiskit import QuantumCircuit
import math

def find_one_positions(n):
    positions = []
    position = 0

    while n:
        if n & 1:
            positions.append(position)
        n >>= 1
        position += 1

    return positions

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

    neg_list = [0]*(2**n)
    for i in range(L, 2**n):
        print(neg_list)
        pos1 = find_one_positions(i)
        if neg_list[i] == 0:
            if len(pos1) == 1:
                qc.p(math.pi, pos1[0])
            else:
                qc.mcp(math.pi, pos1[1:], pos1[0])
            for m in range(2**n):
                pos1m = find_one_positions(m)
                if all(elem in pos1m for elem in pos1):
                    print(m)
                    neg_list[m] = 1 - neg_list[m]

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def find_one_positions(n):
    """
    Returns the positions of '1' bits in the binary representation of a natural number n.
    The position is counted from the right, starting with 0.
    """
    positions = []
    position = 0

    while n:
        if n & 1:
            positions.append(position)
        n >>= 1
        position += 1

    return positions

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

    qc.h(0)
    qc.cnot(0, 1)
    qc.h(1)

    if L == 1:
        for i in range(0, n, -1):
            for j in range(i, n):
                qc.x(j)
            if i == n - 1:
                qc.p(math.pi, i)
            else:
                qc.mcp(math.pi, list(range(i+1,n)), i)
            for j in range(i, n):
                qc.x(j)
    else:
        neg_list = [0]*(2**n)
        for i in range(L, 2**n):
            print(neg_list)
            pos1 = find_one_positions(i)
            if neg_list[i] == 0:
                if len(pos1) == 1:
                    qc.p(math.pi, pos1[0])
                else:
                    qc.mcp(math.pi, pos1[1:], pos1[0])
                for m in range(2**n):
                    pos1m = find_one_positions(m)
                    if all(elem in pos1m for elem in pos1):
                        print(m)
                        neg_list[m] = 1 - neg_list[m]

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def find_one_positions(n):
    """
    Returns the positions of '1' bits in the binary representation of a natural number n.
    The position is counted from the right, starting with 0.
    """
    positions = []
    position = 0

    while n:
        if n & 1:
            positions.append(position)
        n >>= 1
        position += 1

    return positions

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

    if L == 1:
        for i in range(0, n, -1):
            for j in range(i, n):
                qc.x(j)
            if i == n - 1:
                qc.p(math.pi, i)
            else:
                qc.mcp(math.pi, list(range(i+1,n)), i)
            for j in range(i, n):
                qc.x(j)
    else:
        neg_list = [0]*(2**n)
        for i in range(L, 2**n):
            print(neg_list)
            pos1 = find_one_positions(i)
            if neg_list[i] == 0:
                if len(pos1) == 1:
                    qc.p(math.pi, pos1[0])
                else:
                    qc.mcp(math.pi, pos1[1:], pos1[0])
                for m in range(2**n):
                    pos1m = find_one_positions(m)
                    if all(elem in pos1m for elem in pos1):
                        print(m)
                        neg_list[m] = 1 - neg_list[m]

    return qc
'''


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

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

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    """
    Return an n-qubit circuit that multiplies the amplitudes of the basis
    states |0>, |1>, …, |L-1> (little-endian integers) by −1 and leaves all
    other amplitudes unchanged.  Global phase is ignored by the judge, so
    nothing has to be done for L = 0 or L = 2**n.
    The construction keeps the depth well below the required limit (≤ 50
    for n ≤ 10).
    """
    qc = QuantumCircuit(n)

    total = 1 << n
    if L == 0 or L == total:            # nothing to mark or global phase only
        return qc

    # Work from the most-significant bit (n−1) down to the least-significant.
    #
    # If the j-th bit of L is 1 we have to flip the sign of all states whose
    #   – more-significant bits equal the prefix of L, and
    #   – bit j equals 0.
    #
    # To realise a *positive* multi-controlled phase, every control must be
    # in state |1>.  Hence, before applying the gate
    #   – every control qubit whose prefix bit is 0 is flipped with X,
    #   – the (would-be) target qubit j is also flipped so that the states
    #     with bit j = 0 are mapped to |1>.
    # After the controlled-Z the temporary X’s are undone.
    #
    for j in range(n - 1, -1, -1):              # j = MSB … LSB
        if ((L >> j) & 1) == 0:                 # skip if that bit of L is 0
            continue

        # More-significant qubits (the prefix)
        prefix = list(range(j + 1, n))
        flipped_prefix = []                     # remember X’s to undo later

        # 1) Bring every “0” of the prefix to “1” (positive control)
        for k in prefix:
            if ((L >> k) & 1) == 0:             # L’s prefix bit is 0
                qc.x(k)
                flipped_prefix.append(k)

        # 2) Flip target qubit j so that ‘bit j = 0’ → |1>
        qc.x(j)

        # 3) Apply the multi-controlled Z  (all controls are now |1>)
        if prefix:                              # at least one control
            mcz = ZGate().control(len(prefix))  # all-ones ctrl_state
            qc.append(mcz, prefix + [j])
        else:                                   # j is the MSB → plain Z
            qc.z(j)

        # 4) Undo the X on the target qubit
        qc.x(j)

        # 5) Restore the prefix qubits that were flipped
        for k in flipped_prefix:
            qc.x(k)

    return qc
'''


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

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

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

    return qc
'''


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

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

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

    return qc
'''


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

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

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

    rec(n, L, [])

    return qc
'''


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

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

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

    rec(n, L, [])

    return qc
'''


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        w = (1 << (n - i - 1))
        if L & (1 << i) == 0:
            controls = []
            for k in range(i):
                controls.append(k)
            qc.mcp(math.pi, controls, i)
            qc.x(i)
    for i in reverse(range(n)):
        w = (1 << (n - i - 1))
        if L & (1 << i) == 0:
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        w = (1 << (n - i - 1))
        if L & (1 << i) == 0:
            controls = []
            for k in range(i):
                controls.append(k)
            qc.mcp(math.pi, controls, i)
            qc.x(i)
    for i in reverse(range(n)):
        w = (1 << (n - i - 1))
        if L & (1 << i) == 0:
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        w = (1 << (n - i - 1))
        if L & (1 << i) == 0:
            controls = []
            for k in range(i):
                controls.append(k)
            if len(controls) > 0:
                qc.mcp(math.pi, controls, i)
            qc.x(i)
    for i in reversed(range(n)):
        w = (1 << (n - i - 1))
        if L & (1 << i) == 0:
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if L & (1 << i) == 0:
            controls = []
            for k in range(i):
                controls.append(n-k-1)
            if len(controls) > 0:
                qc.mcp(math.pi, controls, n-i-1)
            qc.x(n-i-1)
    for i in reversed(range(n)):
        if L & (1 << i) == 0:
            qc.x(n-i-1)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    return qc
'''


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

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

    def f(l: int, r: int, d: int):
        # qc.barrier()
        # print(l, r, d, li)
        if d < 0 or r - l == 0:
            return
        N = 2 ** d
        if r - l < N:
            li.append(d)
            f(l, r, d - 1)
        else:
            if d == n - 1:
                qc.x(d)
                qc.z(d)
                qc.x(d)
                f(l + N, r, d - 1)
            else:
                if len(li) > 0:
                    qc.x(li)
                qc.x(d)
                qc.append(ZGate().control(n - d - 1), range(d, n))
                if len(li) > 0:
                    qc.x(li)
                qc.x(d)
                f(l + N, r, d - 1)

    li = list()
    f(0, L, 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)

    def f(l: int, r: int, d: int):
        # qc.barrier()
        # print(l, r, d, li)
        if d < 0 or r - l == 0:
            return
        N = 2 ** d
        if r - l < N:
            li.append(d)
            f(l, r, d - 1)
        else:
            if d == n - 1:
                qc.x(d)
                qc.z(d)
                qc.x(d)
                f(l + N, r, d - 1)
            else:
                if len(li) > 0:
                    qc.x(li)
                qc.x(d)
                qc.append(ZGate().control(n - d - 1), range(d, n))
                if len(li) > 0:
                    qc.x(li)
                qc.x(d)
                f(l + N, r, d - 1)

    li = list()
    f(0, L, n - 1)
    # |1111>を反転する
    if 2 ** n == L:
        if n == 1:
            qc.z(0)
        else:
            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:
    theta = 2*math.pi
    for i in range(L-1):
        qc.ry(theta, i)
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for l in range(L):
        for i in range(n):
            # check if i-th bit of l is 0 or 1
            if not ((l >> i) & 1):
                qc.x(i)
        if n == 1:
            qc.z(0)
        else:
            # apply multiple controlled Z gate
            qc.append(ZGate().control(n - 1), range(n))
        for i in range(n):
            if not ((l >> i) & 1):
                qc.x(i)
    return qc
'''


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

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

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

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

    qc.x(n-1)

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

    qc.x(n-1)

    return qc
'''


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

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

    if L == 1:
        return qc

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

    L -= 1

    for i in reversed(range(n)):
        if L & (1 << i) == 0:
            continue

        ctrl = 0
        for j in range(i + 1, n):
            ctrl |= 1 << (n - 1 - j)

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

    return qc
'''


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

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

    if L == 1:
        return qc

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

    L -= 1

    for i in reversed(range(1, n)):
        if L & (1 << i) == 0:
            continue

        ctrl = 0
        for j in range(i + 1, n):
            ctrl |= 1 << (n - 1 - j)

        print(
            f"nqubits={n-1}, ctrl={ctrl}, qubits={list(reversed(range(i - 1, n)))}")

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

    return qc
'''


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

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

    if L == 1:
        return qc

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

    L -= 1

    for i in reversed(range(1, n)):
        if L & (1 << i) == 0:
            continue

        ctrl = 0
        for j in range(i + 1, n):
            if L & (1 << j) != 0:
                ctrl |= 1 << (n - 1 - j)

        print(
            f"nqubits={n-i}, ctrl={ctrl}, qubits={list(reversed(range(i - 1, n)))}")

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

    ctrl = 0
    for j in range(1, n):
        if L & (1 << j) != 0:
            ctrl |= 1 << (n - 1 - j)

    if L & 1 != 0:
        qc.x(0)
    qc.append(ZGate().control(n-1, None, ctrl), reversed(range(n)))
    if L & 1 != 0:
        qc.x(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:

    if L == 1:
        return qc

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

    L -= 1

    for i in reversed(range(1, n)):
        if L & (1 << i) == 0:
            continue

        ctrl = 0
        for j in range(i + 1, n):
            if L & (1 << j) != 0:
                ctrl |= 1 << (n - 1 - j)

        print(
            f"nqubits={n-i}, ctrl={ctrl}, qubits={list(reversed(range(i - 1, n)))}")

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

    ctrl = 0
    for j in range(1, n):
        if L & (1 << j) != 0:
            ctrl |= 1 << (n - 1 - j)

    if L & 1 == 0:
        qc.x(0)
    print(
        f"nqubits={n-1}, ctrl={ctrl}, qubits={list(reversed(range(i - 1, n)))}")
    qc.append(ZGate().control(n-1, None, ctrl), reversed(range(n)))
    if L & 1 == 0:
        qc.x(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:

    qc.h(range(n))

    if L == 1:
        qc.x(n-1)
        qc.append(ZGate().control(n-1, None, 0), range(n))
        qc.x(n-1)
        return qc

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

    L -= 1

    for i in reversed(range(1, n)):
        if L & (1 << i) == 0:
            continue

        ctrl = 0
        for j in range(i + 1, n):
            if L & (1 << j) != 0:
                ctrl |= 1 << (n - 1 - j)

        print(
            f"nqubits={n-i}, ctrl={ctrl}, qubits={list(reversed(range(i - 1, n)))}, target={i - 1}")

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

    ctrl = 0
    for j in range(1, n):
        if L & (1 << j) != 0:
            ctrl |= 1 << (n - 1 - j)

    qc.x(0)
    print(
        f"nqubits={n-1}, ctrl={ctrl}, qubits={list(reversed(range(n)))}")
    qc.append(ZGate().control(n-1, None, ctrl), reversed(range(n)))
    qc.x(0)

    if L & 1 != 0:
        print(
            f"nqubits={n-1}, ctrl={ctrl}, qubits={list(reversed(range(n)))}")
        qc.append(ZGate().control(n-1, None, ctrl), reversed(range(n)))

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate
# from qiskit import Aer, transpile, execute

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

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

    if L == 1:
        qc.x(n-1)
        qc.append(ZGate().control(n-1, None, 0), range(n))
        qc.x(n-1)
        return qc

    L -= 1

    for i in reversed(range(1, n)):
        if L & (1 << i) == 0:
            continue

        ctrl = 0
        for j in range(i + 1, n):
            if L & (1 << j) != 0:
                ctrl |= 1 << (n - 1 - j)

        print(
            f"nqubits={n-i}, ctrl={ctrl}, qubits={list(reversed(range(i - 1, n)))}, target={i - 1}")

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

    ctrl = 0
    for j in range(1, n):
        if L & (1 << j) != 0:
            ctrl |= 1 << (n - 1 - j)

    qc.x(0)
    print(
        f"nqubits={n-1}, ctrl={ctrl}, qubits={list(reversed(range(n)))}")
    qc.append(ZGate().control(n-1, None, ctrl), reversed(range(n)))
    qc.x(0)

    if L & 1 != 0:
        print(
            f"nqubits={n-1}, ctrl={ctrl}, qubits={list(reversed(range(n)))}")
        qc.append(ZGate().control(n-1, None, ctrl), reversed(range(n)))

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate
# from qiskit import Aer, transpile, execute

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

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

    if L == 1:
        qc.x(n-1)
        qc.append(ZGate().control(n-1, None, 0), range(n))
        qc.x(n-1)
        return qc

    L -= 1

    for i in reversed(range(1, n)):
        if L & (1 << i) == 0:
            continue

        ctrl = 0
        for j in range(i + 1, n):
            if L & (1 << j) != 0:
                ctrl |= 1 << (n - 1 - j)

        print(
            f"nqubits={n-i}, ctrl={ctrl}, qubits={list(reversed(range(i - 1, n)))}, target={i - 1}")

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

    ctrl = 0
    for j in range(1, n):
        if L & (1 << j) != 0:
            ctrl |= 1 << (n - 1 - j)

    qc.x(0)
    print(
        f"nqubits={n-1}, ctrl={ctrl}, qubits={list(reversed(range(n)))}")
    qc.append(ZGate().control(n-1, None, ctrl), reversed(range(n)))
    qc.x(0)

    if L & 1 != 0:
        print(
            f"nqubits={n-1}, ctrl={ctrl}, qubits={list(reversed(range(n)))}")
        qc.append(ZGate().control(n-1, None, ctrl), reversed(range(n)))

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    #For each group
    for i in range(n):
        #if ith bit of l is 0, no ZGate
        if not ((L >> i) & 1):
            continue
        #subset[i] < l[i] so subset[i] == 0
        qc.x(i)

        #get ready for CZGate
        for j in range(i+1, n):
            #if j (>i) th bit of l is 0, flip bit
            if not ((L >> j) & 1):
                qc.x(j)

        #For last subset which has no control bit
        if i == n - 1:
            qc.z(i)
        else:
            #Apply CZGate that is in the subset
            qc.append(ZGate().control(n - i - 1), range(i, n))

        #Reverse the preparation
        qc.x(i)
        for j in range(i+1, n):
            #if j (>i) th bit of l is 0, flip bit
            if not ((L >> j) & 1):
                qc.x(j)
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if (L>>(n-1))%2==0:
        qc.x(n-1)
    else:
        qc.x(n-1)
        qc.z(n-1)
        qc.x(n-1)
    for i in range(n-2,-1,-1):
        if (L>>i)%2==0:
            qc.x(i)
        else:
            qc.x(i)
            qc.mcp(math.pi, list(range(i+1,n)), i)
            qc.x(i)
    for i in range(n):
        if (L>>i)%2==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)
  if L == (1 << n):
    for i in range(n):
      qc.z(i)
  else:
    for i in range(n):
      qc.x(i)
    for i in range(n - 1, -1, -1):
      if ((L >> i) & 1) == 1:
        print(i)
        if i == n - 1:
          qc.z(n - 1)
        else:
          qc.append(ZGate().control(n - 1 - i), range(i, n))
        qc.x(i)
    for i in range(n):
      if ((L >> i) & 1) == 0:
        qc.x(i)
  return qc
'''


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


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


'''python
from qiskit import QuantumCircuit

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

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

    j=0
    while j<n:
        print("l1=",l)
        print("j1=",j)
        if l%2==0:
            l=l//2
            j+=1
            continue
        l=l-1
        # j+=1
        print("l=",l)
        print("j=",j)
        for i in range(n-j):
            if not (1<<i)&l:
                qc.x(i+j)
                # print("i=",i)
        print(n,j)
        if j+1<n:
            qc.h(j)
            print(list(range(j+1,n)),j)
            qc.mcx(list(range(j+1,n)),j)
            qc.h(j)
        else:
            qc.z(j)
        for i in range(n-j):
            if not (1<<i)&l:
                qc.x(i+j)
                # print("i=",i)
    return qc
'''


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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
   qc = QuantumCircuit(n)
   ctrlarg = []
   if n != 1:
      strL = bin(L)
      strL = strL[2:]
      while len(strL) < n:
         strL = '0'  + strL
      print(strL)
      for i in range(len(strL)):
         if i > 0:
            print(i)
            for ind in range(len(ctrlarg)):
               if ctrlarg[ind] == '0':
                  qc.x(ind)
            # if ctrlarg[i-1] == '1':
            #    qc.x(i)
            qc.append(ZGate().control(i), qargs=[j for j in range(i+1)])
            for ind in range(len(ctrlarg)):
               if ctrlarg[ind] == '0':
                  qc.x(ind)
            # if ctrlarg[i-1] == '1':
            #    qc.x(i)
            ctrlarg.append(strL[i])
            if '1' not in strL[i+1:]:
               break
         else:
            print(i)
            qc.z(0)
            ctrlarg.append(strL[i])

   else:
      if L == 2:
         qc.z(0)

   # Write your code here:

   return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
   qc = QuantumCircuit(n)
   ctrlarg = []
   if n != 1:
      strL = bin(L)
      strL = strL[2:]
      while len(strL) < n:
         strL = '0'  + strL
      print(strL)
      for i in range(len(strL)):
         if i > 0:
            print(i)
            for ind in range(len(ctrlarg)):
               if ctrlarg[ind] == '0':
                  qc.x(ind)
            if ctrlarg[i-1] == '1':
               qc.x(i)
            qc.append(ZGate().control(i), qargs=[j for j in range(i+1)])
            for ind in range(len(ctrlarg)):
               if ctrlarg[ind] == '0':
                  qc.x(ind)
            if ctrlarg[i-1] == '1':
               qc.x(i)
            ctrlarg.append(strL[i])
            if '1' not in strL[i+1:]:
               break
         else:
            print(i)
            qc.z(0)
            ctrlarg.append(strL[i])
            if '1' not in strL[i+1:]:
               break

   else:
      if L == 2:
         qc.z(0)

   # Write your code here:

   return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
   qc = QuantumCircuit(n)
   ctrlarg = []
   if n != 1:
      strL = bin(L)
      strL = strL[2:]
      while len(strL) < n:
         strL = '0'  + strL
      print(strL)
      qc.h(range(n))
      for i in range(len(strL)):
         if i > 0:
            print(i)
            for ind in range(len(ctrlarg)):
               if ctrlarg[ind] == '0':
                  qc.x(ind)
            if ctrlarg[i-1] == '1':
               qc.x(i)
            qc.append(ZGate().control(i), qargs=[j for j in range(i+1)])
            for ind in range(len(ctrlarg)):
               if ctrlarg[ind] == '0':
                  qc.x(ind)
            if ctrlarg[i-1] == '1':
               qc.x(i)
            ctrlarg.append(strL[i])
            if '1' not in strL[i+1:]:
               break
         else:
            print(i)
            qc.z(0)
            ctrlarg.append(strL[i])
            if '1' not in strL[i+1:]:
               break

   else:
      if L == 2:
         qc.z(0)

   for i in range(n//2):
        qc.swap(i, n - 1 - i)

   # Write your code here:

   return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
   qc = QuantumCircuit(n)
   ctrlarg = []
   if n != 1:
      strL = bin(L)
      strL = strL[2:]
      while len(strL) < n:
         strL = '0'  + strL
      print(strL)
      for i in range(len(strL)):
         if i > 0:
            print(i)
            for ind in range(len(ctrlarg)):
               if ctrlarg[ind] == '0':
                  qc.x(ind)
            if ctrlarg[i-1] == '1':
               qc.x(i)
            qc.append(ZGate().control(i), qargs=[j for j in range(i+1)])
            for ind in range(len(ctrlarg)):
               if ctrlarg[ind] == '0':
                  qc.x(ind)
            if ctrlarg[i-1] == '1':
               qc.x(i)
            ctrlarg.append(strL[i])
            if '1' not in strL[i+1:]:
               break
         else:
            print(i)
            qc.z(0)
            ctrlarg.append(strL[i])
            if '1' not in strL[i+1:]:
               break

   else:
      if L == 2:
         qc.z(0)

   for i in range(n//2):
        qc.swap(i, n - 1 - i)

   # Write your code here:

   return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
   qc = QuantumCircuit(n)
   ctrlarg = []
   if n != 1 and 2**n != L:
      strL = bin(L)
      strL = strL[2:]
      while len(strL) < n:
         strL = '0'  + strL
      print(strL)
      for i in range(len(strL)):
         if i > 0:
            print(i)
            for ind in range(len(ctrlarg)):
               if ctrlarg[ind] == '0':
                  qc.x(ind)
            if ctrlarg[i-1] == '1':
               qc.x(i)
            qc.append(ZGate().control(i), qargs=[j for j in range(i+1)])
            for ind in range(len(ctrlarg)):
               if ctrlarg[ind] == '0':
                  qc.x(ind)
            if ctrlarg[i-1] == '1':
               qc.x(i)
            ctrlarg.append(strL[i])
            if '1' not in strL[i+1:]:
               break
         else:
            print(i)
            qc.z(0)
            ctrlarg.append(strL[i])
            if '1' not in strL[i+1:]:
               break
   elif 2**n == L:
      qc.x(0)
      qc.x(0)
   else:
      if L == 2:
         qc.z(0)

   # for i in range(n//2):
   #      qc.swap(i, n - 1 - i)

   # Write your code here:

   return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
   qc = QuantumCircuit(n)
   ctrlarg = []
   if n != 1 and 2**n != L:
      strL = bin(L)
      strL = strL[2:]
      while len(strL) < n:
         strL = '0'  + strL
      print(strL)
      #qc.h(range(n))
      for i in range(len(strL)):
         if i > 0:
            print(i)
            for ind in range(len(ctrlarg)):
               if ctrlarg[ind] == '0':
                  qc.x(ind)
            if ctrlarg[i-1] == '1':
               qc.x(i)
            qc.append(ZGate().control(i), qargs=[j for j in range(i+1)])
            for ind in range(len(ctrlarg)):
               if ctrlarg[ind] == '0':
                  qc.x(ind)
            if ctrlarg[i-1] == '1':
               qc.x(i)
            ctrlarg.append(strL[i])
            if '1' not in strL[i+1:]:
               break
         else:
            print(i)
            qc.z(0)
            ctrlarg.append(strL[i])
            if '1' not in strL[i+1:]:
               break
   elif 2**n == L:
      qc.x(0)
      qc.x(0)
   else:
      if L == 2:
         qc.z(0)

   for i in range(n//2):
        qc.swap(i, n - 1 - i)

   # Write your code here:

   return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
   qc = QuantumCircuit(n)
   ctrlarg = []
   for i in range(n//2):
        qc.swap(i, n - 1 - i)
   if n != 1 and 2**n != L:
      strL = bin(L)
      strL = strL[2:]
      while len(strL) < n:
         strL = '0'  + strL
      print(strL)

      for i in range(len(strL)):
         if i > 0:
            print(i)
            for ind in range(len(ctrlarg)):
               if ctrlarg[ind] == '0':
                  qc.x(ind)
            if ctrlarg[i-1] == '1':
               qc.x(i)
            qc.append(ZGate().control(i), qargs=[j for j in range(i+1)])
            for ind in range(len(ctrlarg)):
               if ctrlarg[ind] == '0':
                  qc.x(ind)
            if ctrlarg[i-1] == '1':
               qc.x(i)
            ctrlarg.append(strL[i])
            if '1' not in strL[i+1:]:
               break
         else:
            print(i)
            qc.z(0)
            ctrlarg.append(strL[i])
            if '1' not in strL[i+1:]:
               break
   elif 2**n == L:
      qc.x(0)
      qc.x(0)
   else:
      if L == 2:
         qc.z(0)

   for i in range(n//2):
        qc.swap(i, n - 1 - i)

   # Write your code here:

   return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
   qc = QuantumCircuit(n)
   ctrlarg = []
   if n != 1 and 2**n != L:
      strL = bin(L)
      strL = strL[2:]
      while len(strL) < n:
         strL = '0'  + strL
      print(strL)

      for i in range(n//2):
         qc.swap(i, n - 1 - i)

      for i in range(len(strL)):
         if i > 0:
            print(i)
            for ind in range(len(ctrlarg)):
               if ctrlarg[ind] == '0':
                  qc.x(ind)
            if ctrlarg[i-1] == '1':
               qc.x(i)
            qc.append(ZGate().control(i), qargs=[j for j in range(i+1)])
            for ind in range(len(ctrlarg)):
               if ctrlarg[ind] == '0':
                  qc.x(ind)
            if ctrlarg[i-1] == '1':
               qc.x(i)
            ctrlarg.append(strL[i])
            if '1' not in strL[i+1:]:
               break
         else:
            print(i)
            qc.z(0)
            ctrlarg.append(strL[i])
            if '1' not in strL[i+1:]:
               break
      for i in range(n//2):
         qc.swap(i, n - 1 - i)
   elif 2**n == L and n != 1:
      qc.x(0)
      qc.x(0)
   else:
      if L == 2:
         qc.z(0)

   # Write your code here:

   return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
   qc = QuantumCircuit(n)
   ctrlarg = []
   for i in range(n//2):
        qc.swap(i, n - 1 - i)
   if n != 1 and 2**n != L:
      strL = bin(L)
      strL = strL[2:]
      while len(strL) < n:
         strL = '0'  + strL
      print(strL)

      for i in range(len(strL)):
         if i > 0:
            print(i)
            for ind in range(len(ctrlarg)):
               if ctrlarg[ind] == '0':
                  qc.x(ind)
            if ctrlarg[i-1] == '1':
               qc.x(i)
            qc.append(ZGate().control(i), qargs=[j for j in range(i+1)])
            for ind in range(len(ctrlarg)):
               if ctrlarg[ind] == '0':
                  qc.x(ind)
            if ctrlarg[i-1] == '1':
               qc.x(i)
            ctrlarg.append(strL[i])
            if '1' not in strL[i+1:]:
               break
         else:
            print(i)
            qc.z(0)
            ctrlarg.append(strL[i])
            if '1' not in strL[i+1:]:
               break
   elif 2**n == L and n != 1:
      qc.x(0)
      qc.x(0)
   else:
      if L == 2:
         qc.z(0)

   for i in range(n//2):
        qc.swap(i, n - 1 - i)

   # Write your code here:

   return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
   qc = QuantumCircuit(n)
   ctrlarg = []
   for i in range(n//2):
        qc.swap(i, n - 1 - i)
   if n != 1 and 2**n != L:
      strL = bin(L)
      strL = strL[2:]
      while len(strL) < n:
         strL = '0'  + strL
      print(strL)

      for i in range(len(strL)):
         if i > 0:
            print(i)
            for ind in range(len(ctrlarg)):
               if ctrlarg[ind] == '0':
                  qc.x(ind)
            if ctrlarg[i-1] == '1':
               qc.x(i)
            qc.append(ZGate().control(i), qargs=[j for j in range(i+1)])
            for ind in range(len(ctrlarg)):
               if ctrlarg[ind] == '0':
                  qc.x(ind)
            if ctrlarg[i-1] == '1':
               qc.x(i)
            ctrlarg.append(strL[i])
            if '1' not in strL[i+1:]:
               break
         else:
            print(i)
            qc.z(0)
            ctrlarg.append(strL[i])
            if '1' not in strL[i+1:]:
               break
   elif 2**n == L and n != 1:
      qc.x(0)
      qc.x(0)
   else:
      if L == 1:
         qc.z(0)

   for i in range(n//2):
        qc.swap(i, n - 1 - i)

   # Write your code here:

   return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    l=[]
    for i in range(n-1,-1,-1):
        l.append(i)
        if (1<<i)<=L:
            qc.x(i)
            qc.append(GlobalPhaseGate(phase=math.pi).control(len(l)), l)
            qc.x(i)
            L-=(1<<i)
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    l=[]
    for i in range(n-1,-1,-1):
        l.append(i)
        if (1<<i)<=L:
            qc.x(i)
            qc.append(GlobalPhaseGate(phase=math.pi).control(len(l)), l)
            qc.x(i)
            L-=(1<<i)
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    l=[]
    t=[]
    for i in range(n-1,-1,-1):
        j=i
        l.append(j)
        if (1<<i)>=L:
            qc.append(GlobalPhaseGate(phase=math.pi).control(len(l)), l)
            qc.x(j)
            t.append(j)
        else:
            L-=(1<<i)

    for j in t:
        qc.x(j)

    return qc
'''


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

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

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

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

    print(flip)
    if flip[0]:
      qc.x(n-1)
      qc.z(n-1)
      qc.x(n-1)

    for i in range(1,n):
      if flip[i]:
        for j in range(i-1):
          if not flip[j]:
            qc.x(n-1-j)
        qc.x(n-1-i)
        qc.append(ZGate().control(i), range(n-1-i,n))
        for j in range(i-1):
          if not flip[j]:
            qc.x(n-1-j)
        qc.x(n-1-i)

    return qc
'''


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

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

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

    if L == 2**n:
      return qc

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

    print(flip)
    if flip[0]:
      qc.x(n-1)
      qc.z(n-1)
      qc.x(n-1)

    for i in range(1,n):
      if flip[i]:
        for j in range(i-1):
          if not flip[j]:
            qc.x(n-1-j)
        qc.x(n-1-i)
        qc.append(ZGate().control(i), range(n-1-i,n))
        for j in range(i-1):
          if not flip[j]:
            qc.x(n-1-j)
        qc.x(n-1-i)

    return qc
'''


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

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

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

    if L == 2**n:
      return qc

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

    print(flip)
    if flip[0]:
      qc.x(n-1)
      qc.z(n-1)
      qc.x(n-1)

    for i in range(1,n):
      if flip[i]:
        for j in range(i):
          if not flip[j]:
            qc.x(n-1-j)
        qc.x(n-1-i)
        qc.append(ZGate().control(i), range(n-1-i,n))
        for j in range(i):
          if not flip[j]:
            qc.x(n-1-j)
        qc.x(n-1-i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n - 1, -1, -1):
        if (((L - 1) >> i) & 1):
            qc.x(i)
            if i == n - 1:
                qc.z(i)
            else:
                controls = list(range(i + 1, n))
                cz_gate = ZGate().control(len(controls))
                qc.append(cz_gate, controls + [i])
            qc.x(i)
        else:
            qc.x(i)

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

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

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n - 1, -1, -1):
        if (((L - 1) >> i) & 1):
            qc.x(i)
            if i == n - 1:
                qc.z(i)
            else:
                controls = list(range(i + 1, n))
                cz_gate = ZGate().control(len(controls))
                qc.append(cz_gate, controls + [i])
            qc.x(i)
        else:
            qc.x(i)

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

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

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n - 1, -1, -1):
        if (((L - 1) >> i) & 1):
            qc.x(i)
            if i == n - 1:
                qc.z(i)
            else:
                controls = list(range(i + 1, n))
                cz_gate = ZGate().control(len(controls))
                qc.append(cz_gate, controls + [i])
            qc.x(i)
        else:
            qc.x(i)

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

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

    return qc
'''


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

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

    for i in range(n-1,-1,-1):

        lst = []
        for j in range(i+1,n):
            lst.append(j)
            if (L>>j)&1:
                pass
            else:
                qc.x(j)

        if len(lst)==0:
            if (L>>i)&1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
        else:
            if (L>>i)&1:
                qc.x(i)
                qc.append(MCMT('z', len(lst), 1), lst+[i])
                qc.x(i)

        for j in range(i+1,n):
            lst.append(j)
            if (L>>j)&1:
                pass
            else:
                qc.x(j)
    return qc
'''


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

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

    for i in range(n-1,-1,-1):

        lst = []
        for j in range(i+1,n):
            lst.append(j)
            if (L>>j)&1:
                pass
            else:
                qc.x(j)

        if len(lst)==0:
            if (L>>i)&1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
        else:
            if (L>>i)&1:
                qc.x(i)
                qc.append(MCMT('z', len(lst), 1), lst+[i])
                qc.x(i)

        for j in range(i+1,n):
            lst.append(j)
            if (L>>j)&1:
                pass
            else:
                qc.x(j)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    import math
    from qiskit.circuit.library import MCMT
    # for i in range(n):
    #     qc.h(i)

    for i in range(n-1,-1,-1):

        lst = []
        for j in range(i+1,n):
            lst.append(j)
            if (L>>j)&1:
                pass
            else:
                qc.x(j)

        if len(lst)==0:
            if (L>>i)&1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
        else:
            if (L>>i)&1:
                qc.x(i)
                qc.append(MCMT('z', len(lst), 1), lst+[i])
                qc.x(i)

        for j in range(i+1,n):
            lst.append(j)
            if (L>>j)&1:
                pass
            else:
                qc.x(j)
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.h(i)
    ato = []
    for i in range(n-1,-1,-1):
        qc.barrier()
        lst = []
        for j in range(i+1,n):
            lst.append(j)
            # if (L>>j)&1:
            #     pass
            # else:
            #     qc.x(j)

        if len(lst)==0:
            if (L>>i)&1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)
        else:
            if (L>>i)&1:
                qc.x(i)
                qc.append(MCMT('z', len(lst), 1), lst+[i])
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)

        # for j in range(i+1,n):
        #     lst.append(j)
            # if (L>>j)&1:
            #     pass
            # else:
            #     qc.x(j)
        for i in ato:
            qc.x(i)
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # for i in range(n):
    #     qc.h(i)
    ato = []
    for i in range(n-1,-1,-1):
        # qc.barrier()
        lst = []
        for j in range(i+1,n):
            lst.append(j)
            # if (L>>j)&1:
            #     pass
            # else:
            #     qc.x(j)

        if len(lst)==0:
            if (L>>i)&1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)
        else:
            if (L>>i)&1:
                qc.x(i)
                qc.append(MCMT('z', len(lst), 1), lst+[i])
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)

        # for j in range(i+1,n):
        #     lst.append(j)
            # if (L>>j)&1:
            #     pass
            # else:
            #     qc.x(j)
        for i in ato:
            qc.x(i)
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # for i in range(n):
    #     qc.h(i)
    ato = []
    for i in range(n-1,-1,-1):
        # qc.barrier()
        lst = []
        for j in range(i+1,n):
            lst.append(j)
            # if (L>>j)&1:
            #     pass
            # else:
            #     qc.x(j)

        if len(lst)==0:
            if (L>>i)&1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)
        else:
            if (L>>i)&1:
                qc.x(i)
                qc.append(MCMT('z', len(lst), 1), lst+[i])
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)

        # for j in range(i+1,n):
        #     lst.append(j)
            # if (L>>j)&1:
            #     pass
            # else:
            #     qc.x(j)
    for i in ato:
        qc.x(i)
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # for i in range(n):
    #     qc.h(i)
    ato = []
    for i in range(n-1,-1,-1):
        lst = [0]
        qc.append(MCMT('z', len(lst), 1), lst+[i])
    #     # qc.barrier()
    #     lst = []
    #     for j in range(i+1,n):
    #         lst.append(j)
    #         # if (L>>j)&1:
    #         #     pass
    #         # else:
    #         #     qc.x(j)

    #     if len(lst)==0:
    #         if (L>>i)&1:
    #             qc.x(i)
    #             qc.z(i)
    #             qc.x(i)
    #         else:
    #             qc.x(i)
    #             ato.append(i)
    #     else:
    #         if (L>>i)&1:
    #             qc.x(i)
    #             qc.append(MCMT('z', len(lst), 1), lst+[i])
    #             qc.x(i)
    #         else:
    #             qc.x(i)
    #             ato.append(i)

    #     # for j in range(i+1,n):
    #     #     lst.append(j)
    #         # if (L>>j)&1:
    #         #     pass
    #         # else:
    #         #     qc.x(j)
    # for i in ato:
    #     qc.x(i)
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    lst = [0]
    qc.append(MCMT('z', len(lst), 1), lst+[1])
    # for i in range(n):
    #     qc.h(i)
    # ato = []
    # for i in range(n-1,-1,-1):
    #     lst = [0]
    #     qc.append(MCMT('z', len(lst), 1), lst+[i])
    #     # qc.barrier()
    #     lst = []
    #     for j in range(i+1,n):
    #         lst.append(j)
    #         # if (L>>j)&1:
    #         #     pass
    #         # else:
    #         #     qc.x(j)

    #     if len(lst)==0:
    #         if (L>>i)&1:
    #             qc.x(i)
    #             qc.z(i)
    #             qc.x(i)
    #         else:
    #             qc.x(i)
    #             ato.append(i)
    #     else:
    #         if (L>>i)&1:
    #             qc.x(i)
    #             qc.append(MCMT('z', len(lst), 1), lst+[i])
    #             qc.x(i)
    #         else:
    #             qc.x(i)
    #             ato.append(i)

    #     # for j in range(i+1,n):
    #     #     lst.append(j)
    #         # if (L>>j)&1:
    #         #     pass
    #         # else:
    #         #     qc.x(j)
    # for i in ato:
    #     qc.x(i)
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # for i in range(n):
    #     qc.h(i)
    ato = []
    for i in range(n-1,-1,-1):
        # qc.barrier()
        lst = []
        for j in range(i+1,n):
            lst.append(j)

        if len(lst)==0:
            if (L>>i)&1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)
        else:
            if (L>>i)&1:
                qc.x(i)
                qc.append(MCPhaseGate(math.pi, len(lst)), lst + [i])
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)

    for i in ato:
        qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math
from qiskit.circuit.library import MCPhaseGate
def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # for i in range(n):
    #     qc.h(i)
    ato = []
    for i in range(n-1,-1,-1):
        # qc.barrier()
        lst = []
        for j in range(i+1,n):
            lst.append(j)

        if len(lst)==0:
            if (L>>i)&1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)
        else:
            if (L>>i)&1:
                qc.x(i)
                qc.append(MCPhaseGate(math.pi, len(lst)), lst + [i])
                qc.x(i)
            else:
                qc.x(i)
                ato.append(i)

    for i in ato:
        qc.x(i)
    return qc
'''


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

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

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


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

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

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


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

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


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

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

    if qc.depth() <= 50:
      return qc
    qc = QuantumCircuit(n)
    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
from qiskit.circuit.library import ZGate

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

    if qc.depth() <= 50 and ok == 1:
      return qc
    qc = QuantumCircuit(n)
    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
from qiskit.circuit.library import ZGate

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

    qc.z(0)
    qc.x(0)
    qc.z(0)
    qc.x(0)
    dp = [-1 for _ in range(2**n)]
    for i in range(2**n):
        if i >= L:
            if dp[i] == -1:
                array = []
                for j in range(n):
                    if i & (1 << j) != 0:
                        array.append(j)
                if len(array) == 1:
                    qc.z(array[0])
                else:
                    qc.append(ZGate().control(len(array) - 1), array)
                for j in range(2**n):
                    if j > i:
                        ok = True
                        for k in range(n):
                            if ((i & (1 << k)) != 0) and ((j & (1 << k)) == 0):
                                ok = False
                        if ok:
                            dp[j] *= -1

    #qc.append(ZGate(), [1])
    #qc.append(ZGate().control(1), [0, 1])

    return qc
'''


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

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

    qc.x(0)
    qc.z(0)
    qc.x(0)
    dp = [-1 for _ in range(2**n)]
    for i in range(2**n):
        if i % 2 == 1:
            dp[i] = 1
    for i in range(2**n):
        if i >= L:
            if dp[i] == -1:
                array = []
                for j in range(n):
                    if i & (1 << j) != 0:
                        array.append(j)
                if len(array) == 1:
                    qc.z(array[0])
                else:
                    qc.append(ZGate().control(len(array) - 1), array)
                for j in range(2**n):
                    if j > i:
                        ok = True
                        for k in range(n):
                            if ((i & (1 << k)) != 0) and ((j & (1 << k)) == 0):
                                ok = False
                        if ok:
                            dp[j] *= -1

    #qc.append(ZGate(), [1])
    #qc.append(ZGate().control(1), [0, 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)

    L -= 1

    s0 = 1
    if not (L >> (n - 1)) & 1:
        qc.x(n - 1)
        qc.z(n - 1)
        qc.x(n - 1)
        s0 = 0

    s = ''
    for i in range(n - 2, -1, -1):
        if not (L >> i) & 1:
            if s0 == 0:
                qc.x(n - 1)
            qc.append(ZGate().control(n - i - 1, ctrl_state=s+'1'), range(i, n))
            if s0 == 0:
                qc.x(n - 1)
        s += str((L >> i) & 1)
    #print(s)

    #qc.append(ZGate().control(n - 1, ctrl_state='01'), range(n))
    #qc.z(n - 1)
    #qc.x(n - 1)
    #qc.append(ZGate().control(n - 2, ctrl_state='11'), range(1, n))
    #qc.x(n - 1)

    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 QuantumCircuit

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

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

        qc.x(i)

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

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


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

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

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

        qc.x(i)

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

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


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

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

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

        qc.x(i)

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


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.rz(2*math.pi, i)
    qc.append(ZGate().control(n - 1), 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:
    for i in range(n):
        qc.rz(2*math.pi, i)
    qc.append(ZGate().control(n - 1), 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:
    qc.rz(2*math.pi, 0)
    qc.append(ZGate().control(n - 1), range(n))
    return qc
'''


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

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


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

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


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

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