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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        cnt_0 = (L >> (i+1)) + 1
        cnt_1 = L >> (i+1)
        theta = math.acos(math.sqrt(cnt_0 / (cnt_0 + cnt_1))) * 2
        if L >> i & 1 ==  1:
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 1:
                    qc.append(HGate().control(i-j, ctrl_state='0'+same), list(range(i-1, j-1, -1)) + [i])
                same = str(nxt) + same
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 0:
                    qc.append(RYGate(theta).control(i-j, ctrl_state='1'+same), list(range(i-1, j-1, -1)) + [i])
                same = str(nxt) + same
            if i == 0:
                qc.ry(theta, 0)
            else:
                qc.append(RYGate(theta).control(i, ctrl_state=same), list(range(i-1, -1, -1)) + [i])
        else:
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 1:
                    qc.append(RYGate(theta).control(i-j, ctrl_state='0'+same), list(range(i-1, j-1, -1)) + [i])
                same = str(nxt) + same
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 0:
                    qc.append(HGate().control(i-j, ctrl_state='1'+same), list(range(i-1, j-1, -1)) + [i])
                same = str(nxt) + same
            if i == 0:
                qc.h(0)
            else:
                qc.append(HGate().control(i, ctrl_state=same), list(range(i-1, -1, -1)) + [i])

    return qc

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


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        cnt_0 = (L >> (i+1)) + 1
        cnt_1 = L >> (i+1)
        theta = math.acos(math.sqrt(cnt_0 / (cnt_0 + cnt_1))) * 2
        if L >> i & 1 ==  1:
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 1:
                    qc.append(HGate().control(i-j, ctrl_state=same+'0'), range(j, i+1))
                same += str(nxt)
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 0:
                    qc.append(RYGate(theta).control(i-j, ctrl_state=same+'1'), range(j, i+1))
                same += str(nxt)
            if i == 0:
                qc.ry(theta, 0)
            else:
                qc.append(RYGate(theta).control(i, ctrl_state=same), range(i+1))
        else:
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 1:
                    qc.append(RYGate(theta).control(i-j, ctrl_state=same+'0'), range(j, i+1))
                same += str(nxt)
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 0:
                    qc.append(HGate().control(i-j, ctrl_state=same+'1'), range(j, i+1))
                same += str(nxt)
            if i == 0:
                qc.h(0)
            else:
                qc.append(HGate().control(i, ctrl_state=same), range(i+1))

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    strings = ['']
    for i in range(n):
        strings.append(strings[-1] + str(L >> i & 1))
    # Write your code here:
    for i in range(n):
        cnt_0 = (L >> (i+1)) + 1
        cnt_1 = L >> (i+1)
        theta = math.acos(math.sqrt(cnt_0 / (cnt_0 + cnt_1))) * 2
        if L >> i & 1 ==  1:
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 1:
                    qc.append(HGate().control(i-j, ctrl_state=same+'0'), range(j, i+1))
                    same += '1'
                else:
                    same += '0'
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 0:
                    qc.append(RYGate(theta).control(i-j, ctrl_state=same+'1'), range(j, i+1))
                    same += '0'
                else:
                    same += '1'
            if i == 0:
                qc.ry(theta, 0)
            else:
                qc.append(RYGate(theta).control(i, ctrl_state=same), range(i+1))
        else:
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 1:
                    qc.append(RYGate(theta).control(i-j, ctrl_state=same+'0'), range(j, i+1))
                    same += '1'
                else:
                    same += '0'
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 0:
                    qc.append(HGate().control(i-j, ctrl_state=same+'1'), range(j, i+1))
                    same += '0'
                else:
                    same += '1'
            if i == 0:
                qc.h(0)
            else:
                qc.append(HGate().control(i, ctrl_state=same), range(i+1))

    return qc

solve(10, 999)
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    strings = ['']
    for i in range(n):
        strings.append(strings[-1] + str(L >> i & 1))
    # Write your code here:
    for i in range(n):
        cnt_0 = (L >> (i+1)) + 1
        cnt_1 = L >> (i+1)
        theta = math.acos(math.sqrt(cnt_0 / (cnt_0 + cnt_1))) * 2
        if L >> i & 1 ==  1:
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 1:
                    qc.append(HGate().control(i-j, ctrl_state=same+'0'), range(j, i+1))
                    same += '1'
                else:
                    same += '0'
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 0:
                    qc.append(RYGate(theta).control(i-j, ctrl_state=same+'1'), range(j, i+1))
                    same += '0'
                else:
                    same += '1'
            if i == 0:
                qc.ry(theta, 0)
            else:
                qc.append(RYGate(theta).control(i, ctrl_state=same), range(i+1))
        else:
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 1:
                    qc.append(RYGate(theta).control(i-j, ctrl_state=same+'0'), range(j, i+1))
                    same += '1'
                else:
                    same += '0'
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 0:
                    qc.append(HGate().control(i-j, ctrl_state=same+'1'), range(j, i+1))
                    same += '0'
                else:
                    same += '1'
            if i == 0:
                qc.h(0)
            else:
                qc.append(HGate().control(i, ctrl_state=same), range(i+1))

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    strings = ['']
    for i in range(n):
        strings.append(strings[-1] + str(L >> i & 1))
    # Write your code here:
    for i in range(n):
        cnt_0 = (L >> (i+1)) + 1
        cnt_1 = L >> (i+1)
        theta = math.acos(math.sqrt(cnt_0 / (cnt_0 + cnt_1))) * 2
        if L >> i & 1 ==  1:
            same = 0
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 1:
                    qc.append(HGate().control(i-j, ctrl_state=same << 1), range(j, i+1))
                    same = same << 1 | 1
                else:
                    same = same << 1
            same = 0
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 0:
                    qc.append(RYGate(theta).control(i-j, ctrl_state=same << 1 | 1), range(j, i+1))
                    same = same << 1
                else:
                    same = same << 1 | 1
            if i == 0:
                qc.ry(theta, 0)
            else:
                qc.append(RYGate(theta).control(i, ctrl_state=same), range(i+1))
        else:
            same = 0
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 1:
                    qc.append(RYGate(theta).control(i-j, ctrl_state=same << 1), range(j, i+1))
                    same = same << 1 | 1
                else:
                    same = same << 1
            same = 0
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 0:
                    qc.append(HGate().control(i-j, ctrl_state=same << 1 | 1), range(j, i+1))
                    same = same << 1
                else:
                    same = same << 1 | 1
            if i == 0:
                qc.h(0)
            else:
                qc.append(HGate().control(i, ctrl_state=same), range(i+1))

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        cnt_0 = (L >> (i+1)) + 1
        cnt_1 = L >> (i+1)
        theta = math.acos(math.sqrt(cnt_0 / (cnt_0 + cnt_1))) * 2
        if L >> i & 1 ==  1:
            same = 0
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 1:
                    qc.append(HGate().control(i-j, ctrl_state=same << 1), range(j, i+1))
                    same = same << 1 | 1
                else:
                    same = same << 1
            same = 0
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 0:
                    qc.append(RYGate(theta).control(i-j, ctrl_state=same << 1 | 1), range(j, i+1))
                    same = same << 1
                else:
                    same = same << 1 | 1
            if i == 0:
                qc.ry(theta, 0)
            else:
                qc.append(RYGate(theta).control(i, ctrl_state=same), range(i+1))
        else:
            same = 0
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 1:
                    qc.append(RYGate(theta).control(i-j, ctrl_state=same << 1), range(j, i+1))
                    same = same << 1 | 1
                else:
                    same = same << 1
            same = 0
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 0:
                    qc.append(HGate().control(i-j, ctrl_state=same << 1 | 1), range(j, i+1))
                    same = same << 1
                else:
                    same = same << 1 | 1
            if i == 0:
                qc.h(0)
            else:
                qc.append(HGate().control(i, ctrl_state=same), range(i+1))

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        cnt_0 = (L >> (i+1)) + 1
        cnt_1 = L >> (i+1)
        theta = math.acos(math.sqrt(cnt_0 / (cnt_0 + cnt_1))) * 2
        if L >> i & 1 ==  1:
            same = 0
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 1:
                    qc.append(HGate().control(i-j, ctrl_state=same << 1), list(range(j, i+1)))
                    same = same << 1 | 1
                else:
                    same = same << 1
            same = 0
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 0:
                    qc.append(RYGate(theta).control(i-j, ctrl_state=same << 1 | 1), list(range(j, i+1)))
                    same = same << 1
                else:
                    same = same << 1 | 1
            if i == 0:
                qc.ry(theta, 0)
            else:
                qc.append(RYGate(theta).control(i, ctrl_state=same), list(range(i+1)))
        else:
            same = 0
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 1:
                    qc.append(RYGate(theta).control(i-j, ctrl_state=same << 1), list(range(j, i+1)))
                    same = same << 1 | 1
                else:
                    same = same << 1
            same = 0
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 0:
                    qc.append(HGate().control(i-j, ctrl_state=same << 1 | 1), list(range(j, i+1)))
                    same = same << 1
                else:
                    same = same << 1 | 1
            if i == 0:
                qc.h(0)
            else:
                qc.append(HGate().control(i, ctrl_state=same), list(range(i+1)))

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    if L == (1 << n):
        qc = QuantumCircuit(n)
        for i in range(n):
            qc.h(i)
        return qc
    if L == 1:
        qc = QuantumCircuit(n)
        return qc
    qc = QuantumCircuit(n)
    strings = ['']
    for i in range(n):
        strings.append(strings[-1] + str(L >> i & 1))
    # Write your code here:
    for i in range(n):
        cnt_0 = (L >> (i+1)) + 1
        cnt_1 = L >> (i+1)
        theta = math.acos(math.sqrt(cnt_0 / (cnt_0 + cnt_1))) * 2
        if L >> i & 1 ==  1:
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 1:
                    qc.append(HGate().control(i-j, ctrl_state=same+'0'), range(j, i+1))
                    same += '1'
                else:
                    same += '0'
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 0:
                    qc.append(RYGate(theta).control(i-j, ctrl_state=same+'1'), range(j, i+1))
                    same += '0'
                else:
                    same += '1'
            if i == 0:
                qc.ry(theta, 0)
            else:
                qc.append(RYGate(theta).control(i, ctrl_state=same), range(i+1))
        else:
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 1:
                    qc.append(RYGate(theta).control(i-j, ctrl_state=same+'0'), range(j, i+1))
                    same += '1'
                else:
                    same += '0'
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 0:
                    qc.append(HGate().control(i-j, ctrl_state=same+'1'), range(j, i+1))
                    same += '0'
                else:
                    same += '1'
            if i == 0:
                qc.h(0)
            else:
                qc.append(HGate().control(i, ctrl_state=same), range(i+1))

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    for i in range(0, n+1):
        if L == (1 << i):
            qc = QuantumCircuit(n)
            for j in range(i):
                qc.h(j)
            return qc
    if L == 1:
        qc = QuantumCircuit(n)
        return qc
    qc = QuantumCircuit(n)
    strings = ['']
    for i in range(n):
        strings.append(strings[-1] + str(L >> i & 1))
    # Write your code here:
    for i in range(n):
        cnt_0 = (L >> (i+1)) + 1
        cnt_1 = L >> (i+1)
        theta = math.acos(math.sqrt(cnt_0 / (cnt_0 + cnt_1))) * 2
        if L >> i & 1 ==  1:
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 1:
                    qc.append(HGate().control(i-j, ctrl_state=same+'0'), range(j, i+1))
                    same += '1'
                else:
                    same += '0'
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 0:
                    qc.append(RYGate(theta).control(i-j, ctrl_state=same+'1'), range(j, i+1))
                    same += '0'
                else:
                    same += '1'
            if i == 0:
                qc.ry(theta, 0)
            else:
                qc.append(RYGate(theta).control(i, ctrl_state=same), range(i+1))
        else:
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 1:
                    qc.append(RYGate(theta).control(i-j, ctrl_state=same+'0'), range(j, i+1))
                    same += '1'
                else:
                    same += '0'
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 0:
                    qc.append(HGate().control(i-j, ctrl_state=same+'1'), range(j, i+1))
                    same += '0'
                else:
                    same += '1'
            if i == 0:
                qc.h(0)
            else:
                qc.append(HGate().control(i, ctrl_state=same), range(i+1))

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    for i in range(0, n+1):
        if L == (1 << i):
            qc = QuantumCircuit(n)
            for j in range(i):
                qc.h(j)
            return qc
    if L == 1:
        qc = QuantumCircuit(n)
        return qc
    qc = QuantumCircuit(n)
    l = [0] * n
    for i in range(n):
        l[i] = L >> i & 1
    # Write your code here:
    for i in range(n):
        cnt_0 = (L >> (i+1)) + 1
        cnt_1 = L >> (i+1)
        theta = math.acos(math.sqrt(cnt_0 / (cnt_0 + cnt_1))) * 2
        if l[i] == 1:
            same = ''
            for j in range(i-1, -1, -1):
                if l[j] == 1:
                    qc.append(HGate().control(i-j, ctrl_state=same+'0'), range(j, i+1))
                    same += '1'
                else:
                    same += '0'
            same = ''
            for j in range(i-1, -1, -1):
                if l[j] == 0:
                    qc.append(RYGate(theta).control(i-j, ctrl_state=same+'1'), range(j, i+1))
                    same += '0'
                else:
                    same += '1'
            if i == 0:
                qc.ry(theta, 0)
            else:
                qc.append(RYGate(theta).control(i, ctrl_state=same), range(i+1))
        else:
            same = ''
            for j in range(i-1, -1, -1):
                if l[j] == 1:
                    qc.append(RYGate(theta).control(i-j, ctrl_state=same+'0'), range(j, i+1))
                    same += '1'
                else:
                    same += '0'
            same = ''
            for j in range(i-1, -1, -1):
                nxt = l[j]
                if nxt == 0:
                    qc.append(HGate().control(i-j, ctrl_state=same+'1'), range(j, i+1))
                    same += '0'
                else:
                    same += '1'
            if i == 0:
                qc.h(0)
            else:
                qc.append(HGate().control(i, ctrl_state=same), range(i+1))

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    for i in range(0, n+1):
        if L == (1 << i):
            qc = QuantumCircuit(n)
            for j in range(i):
                qc.h(j)
            return qc
    if L == 1:
        qc = QuantumCircuit(n)
        return qc
    qc = QuantumCircuit(n)
    l = [0] * n
    for i in range(n):
        l[i] = L >> i & 1
    # Write your code here:
    for i in range(n):
        qc.h(i)
        cnt_0 = (L >> (i+1)) + 1
        cnt_1 = L >> (i+1)
        theta = math.acos(math.sqrt(cnt_0 / (cnt_0 + cnt_1))) * 2 - math.pi
        if l[i] == 1:
            same = ''
            for j in range(i-1, -1, -1):
                if l[j] == 0:
                    qc.append(RYGate(theta).control(i-j, ctrl_state=same+'1'), range(j, i+1))
                    same += '0'
                else:
                    same += '1'
            if i == 0:
                qc.ry(theta, 0)
            else:
                qc.append(RYGate(theta).control(i, ctrl_state=same), range(i+1))
        else:
            same = ''
            for j in range(i-1, -1, -1):
                if l[j] == 1:
                    qc.append(RYGate(theta).control(i-j, ctrl_state=same+'0'), range(j, i+1))
                    same += '1'
                else:
                    same += '0'

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    for i in range(0, n+1):
        if L == (1 << i):
            qc = QuantumCircuit(n)
            for j in range(i):
                qc.h(j)
            return qc
    if L == 1:
        qc = QuantumCircuit(n)
        return qc
    qc = QuantumCircuit(n)
    l = [0] * n
    for i in range(n):
        l[i] = L >> i & 1
    # Write your code here:
    for i in range(n):
        qc.h(i)
        cnt_0 = (L >> (i+1)) + 1
        cnt_1 = L >> (i+1)
        theta = math.acos(math.sqrt(cnt_0 / (cnt_0 + cnt_1))) * 2 - math.pi / 2
        if l[i] == 1:
            same = ''
            for j in range(i-1, -1, -1):
                if l[j] == 0:
                    qc.append(RYGate(theta).control(i-j, ctrl_state=same+'1'), range(j, i+1))
                    same += '0'
                else:
                    same += '1'
            if i == 0:
                qc.ry(theta, 0)
            else:
                qc.append(RYGate(theta).control(i, ctrl_state=same), range(i+1))
        else:
            same = ''
            for j in range(i-1, -1, -1):
                if l[j] == 1:
                    qc.append(RYGate(theta).control(i-j, ctrl_state=same+'0'), range(j, i+1))
                    same += '1'
                else:
                    same += '0'

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    if L == 1:
        return qc
    else:
        k = math.ceil(math.log2(L))
        qc.h(range(k))

        return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(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(math.ceil(math.log2(n))):
        qc.h(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(math.ceil(math.log2(L))):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    l = 1
    m = 0
    while l < L:
        l *= 2
        m += 1
    for i in range(m):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    return qc
'''


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

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

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

    if L != 2**n:
        angle = 2 * np.pi / (2**n)
        for i in range(L, 2**n):
            qc.p(-angle * i, range(n))
        qc.append(QFT(n, inverse=True), range(n))

        for i in range(L, 2**n):
            qc.p(angle * i, range(n))

        qc.append(QFT(n), 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:
    num_qubits_for_superposition = math.ceil(math.log2(L))
    for i in range(num_qubits_for_superposition):
        qc.h(i)

    if 2**num_qubits_for_superposition > L:
        for i in range(L, 2**num_qubits_for_superposition):
            binary_form = format(i, '0' + str(num_qubits_for_superposition) + 'b')[::-1]
            controls = [j for j, bit in enumerate(binary_form) if bit == '1']

            if controls:
                qc.mcp(math.pi, controls, num_qubits_for_superposition)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    if 2**num_qubits_for_superposition > L:
        additional_qubit = num_qubits_for_superposition
        qc.h(additional_qubit)

        for i in range(L, 2**num_qubits_for_superposition):
            binary_form = format(i, '0' + str(num_qubits_for_superposition) + 'b')[::-1]
            controls = [j for j, bit in enumerate(binary_form) if bit == '1']

            if controls:
                qc.mcrx(math.pi, controls, additional_qubit)

        qc.h(additional_qubit)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    if L == 1:
        return qc

    k = math.ceil(math.log2(L))
#    print("k:",str(k))
    qc.h(range(k))

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    bit_position = []
    for i in range(n):
        if (MX>>i)&1:
            bit_position.append(i)
    bit_position.reverse()
    sz = L
    for i in range(len(bit_position)):
        pos = bit_position[i]
        prob = (1<<pos)/sz
        print(pos, prob, sz, 1<<pos)
        theta = math.acos(math.sqrt(prob))
        if i == 0:
            qc.ry(2.0 * theta,pos)
        else:
            qc.cry(2.0 * theta,bit_position[i-1],pos)
        sz -= (1<<pos)

    bit_position.reverse()
    for i in range(len(bit_position)-1):
        pos = bit_position[i]
        if pos == 0:
            continue
        print(pos, (1<<pos))
        custom = HGate().control(2,ctrl_state="10")
        for j in range(pos):
            print(j,pos,bit_position[i+1])
            qc.append(custom,[pos,bit_position[i+1],j])

    qc.ch(bit_position[-1],range(bit_position[-1]),ctrl_state=0)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if L==1:
        return qc
    if n==1:
        qc.h(0)
        return qc
    MX = L-1
    bit_position = []
    for i in range(n):
        if (MX>>i)&1:
            bit_position.append(i)
    bit_position.reverse()
    # print(bit_position)
    sz = L
    for i in range(len(bit_position)):
        pos = bit_position[i]
        prob = (1<<pos)/sz
        # print(pos, prob, sz, 1<<pos)
        theta = math.acos(math.sqrt(prob))
        if i == 0:
            qc.ry(2.0 * theta,pos)
        else:
            qc.cry(2.0 * theta,bit_position[i-1],pos)
        sz -= (1<<pos)

    bit_position.reverse()
    for i in range(len(bit_position)-1):
        pos = bit_position[i]
        if pos == 0:
            continue
        custom = HGate().control(2,ctrl_state="10")
        for j in range(pos):
            qc.append(custom,[pos,bit_position[i+1],j])
    if bit_position[-1] != 0:
        qc.ch(bit_position[-1],range(bit_position[-1]),ctrl_state=0)
    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if L==1:
        return qc
    if n==1:
        qc.h(0)
        return qc
    MX = L-1
    bit_position = []
    for i in range(n):
        if (MX>>i)&1:
            bit_position.append(i)
    bit_position.reverse()
    # print(bit_position)
    sz = L
    for i in range(len(bit_position)):
        pos = bit_position[i]
        prob = (1<<pos)/sz
        # print(pos, prob, sz, 1<<pos)
        theta = math.acos(math.sqrt(prob))
        if i == 0:
            qc.ry(2.0 * theta,pos)
        else:
            qc.cry(2.0 * theta,bit_position[i-1],pos)
        sz -= (1<<pos)

    bit_position.reverse()
    for i in range(len(bit_position)-1):
        pos = bit_position[i]
        if pos == 0:
            continue
        custom = HGate().control(2,ctrl_state="10")
        for j in range(pos):
            qc.append(custom,[pos,bit_position[i+1],j])
    if bit_position[-1] != 0:
        qc.ch(bit_position[-1],range(bit_position[-1]),ctrl_state=0)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L == 1:
        pass
    else:
        cnt = math.ceil(math.log2(L))
        for i in range(cnt):
            qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

# from qiskit.quantum_info import Statevector

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

# if __name__ == "__main__":
#     qc = solve(3, 3)
#     print(Statevector(qc))
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    l = 0
    while (2**l<L):
        l += 1
    for i in range(l):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    lis = []
    k = L
    tmp = 0
    while(k>0):
        if k%2:
            lis = lis + [tmp]
        tmp += 1
        k = k //2
    m = 2 ** (lis[0])
    if len(lis) == 1:
        for i in range(lis[0]):
            qc.h(i)
        return qc

    for i in range(1,len(lis)):
        qc.x(lis[i])
    if lis[0] > 0:
        for i in range(lis[0]):
            qc.h(i)
    qc.ry(-2*math.acos(math.sqrt(m / L)), lis[1])
    qc.x(lis[1])
    for i in range(lis[0],lis[1]):
        qc.ch(lis[1], i)
    qc.x(lis[1])
    for i in range(1,len(lis) - 1):
        qc.x(lis[i])
        qc.cry(-2 * math.acos(math.sqrt(2 ** lis[i] / (L - m))), lis[i], lis[i+1])
        qc.x(lis[i])
        qc.x(lis[i+1])
        for j in range(lis[i],lis[i+1]):
            qc.ch(lis[i+1],j)
        qc.x(lis[i+1])
        m = m + 2 ** (lis[i])
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    lis = []
    k = L
    tmp = 0
    while(k>0):
        if k%2:
            lis = lis + [tmp]
        tmp += 1
        k = k //2
    m = 2 ** (lis[0])
    if len(lis) == 1:
        for i in range(lis[0]):
            qc.h(i)
        return qc

    for i in range(1,len(lis)):
        qc.x(lis[i])
    if lis[0] > 0:
        for i in range(lis[0]):
            qc.h(i)
    qc.ry(-2*math.acos(math.sqrt(m / L)), lis[1])
    qc.x(lis[1])
    for i in range(lis[0],lis[1]):
        qc.ch(lis[1], i)
    qc.x(lis[1])
    for i in range(1,len(lis) - 1):
        qc.x(lis[i])
        qc.cry(-2 * math.acos(math.sqrt(2 ** lis[i] / (L - m))), lis[i], lis[i+1])
        qc.x(lis[i])
        qc.x(lis[i+1])
        for j in range(lis[i],lis[i+1]):
            qc.ch(lis[i+1],j)
        qc.x(lis[i+1])
        m = m + 2 ** (lis[i])
    return qc
'''


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

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

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

    for i in range(n):
        qc = less_than(qc,n,L)
        for i in range(n):
            qc.h(i)

        qc = less_than(qc,n,2**n)

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

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

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

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

    return qc

def less_than(qc,n,L):
    l = L

    lst = []

    if l == 2**n:
        l = l//2;

    while (l > 0):
        if l & 1 == 1:
            lst.append(1)
        else:
            lst.append(0)
        l = l >> 1

    lst.reverse()

    k = [0 for i in range(n - len(lst))]
    lst = k + lst

    for i in range(len(lst)):
        if lst[i] == 1:
            qc.x(n-i-1)
            if i > 0:
                qc.append(ZGate().control(i), range(n-i-1,n))
            else:
                qc.z(n-i-1)
            qc.x(n-i-1)
        else:
            qc.x(n-i-1)

    for i in range(len(lst)):
        if lst[i] == 0:
            qc.x(n-i-1)

    if L == 2**n:
        qc.z(n-1)

    return qc
'''


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

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(int(math.sqrt(2**n))):
        qc = less_than(qc,n,L)
        for i in range(n):
            qc.h(i)

        qc = less_than(qc,n,2**n)

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

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

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

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

    return qc

def less_than(qc,n,L):
    l = L

    lst = []

    if l == 2**n:
        l = l//2;

    while (l > 0):
        if l & 1 == 1:
            lst.append(1)
        else:
            lst.append(0)
        l = l >> 1

    lst.reverse()

    k = [0 for i in range(n - len(lst))]
    lst = k + lst

    for i in range(len(lst)):
        if lst[i] == 1:
            qc.x(n-i-1)
            if i > 0:
                qc.append(ZGate().control(i), range(n-i-1,n))
            else:
                qc.z(n-i-1)
            qc.x(n-i-1)
        else:
            qc.x(n-i-1)

    for i in range(len(lst)):
        if lst[i] == 0:
            qc.x(n-i-1)

    if L == 2**n:
        qc.z(n-1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for i in range(L_keta):
        qc.h(i)
    return qc
'''


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

def append_cry(qc, angle, ctrl_lst, tgt):
    if ctrl_lst:
        # 制御量子ビットがある場合、制御RYゲートを追加
        qc.append(RYGate(angle).control(len(ctrl_lst)), ctrl_lst + [tgt])
    else:
        # 制御量子ビットがない場合、通常のRYゲートを追加
        qc.ry(angle, tgt)

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    dq = [(0,n,L)]
    while dq:
        x,m,l = dq.pop(0)
        #print(x,m,l)
        if l == 0 or m == 0:
            continue
        left = min(2**(m-1),l)
        right = l-left
        # 処理
        for j in range(m,n):
            if not (x>>j)&1:
                qc.x(j)
        append_cry(qc, 2*math.acos((left/l)**0.5),[_ for _ in range(m,n)],m-1)
        for j in range(m,n):
            if not (x>>j)&1:
                qc.x(j)
        # 追加
        #continue
        dq.append((x,m-1,left))
        dq.append((x+2**(m-1),m-1,right))

    return qc
'''


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

def append_cry(qc, angle, ctrl_lst, tgt):
    if ctrl_lst:
        # 制御量子ビットがある場合、制御RYゲートを追加
        qc.append(RYGate(angle).control(len(ctrl_lst)), ctrl_lst + [tgt])
    else:
        # 制御量子ビットがない場合、通常のRYゲートを追加
        qc.ry(angle, tgt)

def append_ch(qc, ctrl_lst, tgt):
    if ctrl_lst:
        # 制御量子ビットがある場合、制御Hゲートを追加
        qc.append(HGate().control(len(ctrl_lst)), ctrl_lst + [tgt])
    else:
        # 制御量子ビットがない場合、通常のHゲートを追加
        qc.h(tgt)

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    dq = [(0,n,L)]
    while dq:
        x,m,l = dq.pop(0)
        #print(x,m,l)
        if l == 0 or m == 0:
            continue

        if l == 2**m:
            for j in range(m,n):
                if not (x>>j)&1:
                    qc.x(j)
            for j in range(m):
                append_ch(qc,[_ for _ in range(m,n)],j)
            for j in range(m,n):
                if not (x>>j)&1:
                    qc.x(j)
            continue

        left = min(2**(m-1),l)
        right = l-left

        # 処理2
        for j in range(m,n):
            if not (x>>j)&1:
                qc.x(j)
        append_cry(qc, 2*math.acos((left/l)**0.5),[_ for _ in range(m,n)],m-1)
        for j in range(m,n):
            if not (x>>j)&1:
                qc.x(j)
        # 追加2
        dq.append((x,m-1,left))
        dq.append((x+2**(m-1),m-1,right))

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import PhaseGate
import mat

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

    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.p(math.pi / 3, i)
        else:
            qc.append(PhaseGate(math.pi / 3).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

def Rs(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.x(range(n))
    if n == 1:
        qc.p(math.pi / 3, 0)
    else:
        qc.append(PhaseGate(math.pi / 3).control(n - 1), range(n))
    qc.x(range(n))
    return qc

def U(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.h(range(max(1, math.ceil(math.log2(L)))))
    return qc

def Um(n: int, L: int, m: int, u: QuantumCircuit, r_t: QuantumCircuit, r_s: QuantumCircuit) -> QuantumCircuit:
    if m == 0:
        return U(n, L)
    u_m_1 = Um(n, L, m - 1, u, r_t, r_s)
    return u_m_1.compose(r_t).compose(u_m_1.inverse()).compose(r_s).compose(u_m_1)

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    u = U(n, L)
    r_t = Rt(n, L)
    r_s = Rs(n)
    um = Um(n, L, 2, u, r_t, r_s)
    return um
'''


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

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

    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.p(math.pi / 3, i)
        else:
            qc.append(PhaseGate(math.pi / 3).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

def Rs(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.x(range(n))
    if n == 1:
        qc.p(math.pi / 3, 0)
    else:
        qc.append(PhaseGate(math.pi / 3).control(n - 1), range(n))
    qc.x(range(n))
    return qc

def U(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.h(range(max(1, math.ceil(math.log2(L)))))
    return qc

def Um(n: int, L: int, m: int, u: QuantumCircuit, r_t: QuantumCircuit, r_s: QuantumCircuit) -> QuantumCircuit:
    if m == 0:
        return U(n, L)
    u_m_1 = Um(n, L, m - 1, u, r_t, r_s)
    return u_m_1.compose(r_t).compose(u_m_1.inverse()).compose(r_s).compose(u_m_1)

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    u = U(n, L)
    r_t = Rt(n, L)
    r_s = Rs(n)
    um = Um(n, L, 2, u, r_t, r_s)
    return um
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import HGate, RYGate
import math
# from qiskit.quantum_info import Statevector

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

    v = 0
    for i in range(n-1, -1, -1):
        if (L>>i)&1:
            for j in range(n-1, i, -1):
                if ((v>>j)&1)==0:
                    qc.x(j)
            if (v^(1<<i))<L:
                if i==n-1:
                    qc.ry(math.acos(math.sqrt(2**i/(L-v)))*2, i)
                else:
                    qc.append(RYGate(theta=math.acos(math.sqrt(2**i/(L-v)))*2).control(n-i-1), list(range(i+1, n))+[i])
            qc.x(i)
            for j in range(i):
                qc.append(HGate().control(n-i), list(range(i, n))+[j])
            for j in range(n-1, i-1, -1):
                if ((v>>j)&1)==0:
                    qc.x(j)
            v ^= (1<<i)
    # print(qc.depth())
    return qc

# if __name__ == "__main__":
#     qc = solve(10, 1023)
    # print(Statevector(qc))
'''


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

def Rt(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.p(math.pi / 3, i)
        else:
            qc.append(PhaseGate(math.pi / 3).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

def Rs(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    qc.x(range(n))

    if n == 1:
        qc.p(math.pi / 3, 0)
    else:
        qc.append(PhaseGate(math.pi / 3).control(n - 1), range(n))

    qc.x(range(n))

    return qc

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

    if m == 0:
        qc.h(range(n))
        return qc

    u = U(m - 1, n, L)

    qc.compose(u, inplace=True)
    qc.compose(Rt(n, L), inplace=True)
    qc.compose(u.inverse(), inplace=True)
    qc.compose(Rs(n), inplace=True)
    qc.compose(u, inplace=True)

    return qc

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

    k = math.ceil(math.log2(L))
    if k == 0:
        return qc

    m = 2
    qc.compose(U(m, k, L), inplace=True)

    return qc
'''


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

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

    if L == (1 << n):
        for i in range(n):
            qc.h(i)
        return qc

    target_L = 0
    x_lst = []
    for digit in range(n):
        target_R = target_L + (1 << (n - digit - 1))
        if L > target_R:
            theta = math.asin(math.sqrt((L - target_R) / (L - target_L))) * 2
            if(digit == 0):
                qc.ry(theta, n-1)
            else:
                for i in x_lst: qc.x(i)
                qc.append(RYGate(theta).control(digit), range(n - digit - 1, n)[::-1])
                for i in x_lst: qc.x(i)

            for i in x_lst: qc.x(i)
            qc.x(n - digit - 1)
            for i in range(n - digit - 1):
                qc.append(HGate().control(digit + 1), list(range(n - digit - 1, n)) + [i])
            for i in x_lst: qc.x(i)
            qc.x(n - digit - 1)

            target_L += (1 << (n - digit - 1))
        else:
            x_lst.append(n - digit - 1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    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):
        for i in range(n):
            qc.h(i)
        return qc
    flip_ind = []
    left = L
    for i in reversed(range(n)):
        if L&(1<<i) :
            if i == n-1:
                qc.ry(2*math.acos(math.sqrt((1<<i)/L)),i)
                qc.x(i)
                for j in range(i):
                    qc.ch(i,j)
                qc.x(i)
            else:
                for y in flip_ind:
                    qc.x(y)
                qc.mcry(2*math.acos(math.sqrt((1<<i)/left)),list(range(i+1,n)),i)
                qc.x(i)
                for j in range(i):
                    qc.mcrx(math.pi,list(range(i+1,n)),i)
                    qc.mcrz(math.pi,list(range(i+1,n)),i)
                    qc.mcrx(math.pi,list(range(i+1,n)),i)
                qc.x(i)
                for y in flip_ind:
                    qc.x(y)
            left -= (1<<i)
        else:
            flip_ind.append(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L == (1<<n):
        for i in range(n):
            qc.h(i)
        return qc
    flip_ind = []
    left = L
    for i in reversed(range(n)):
        if L&(1<<i) :
            if i == n-1:
                qc.ry(2*math.acos(math.sqrt((1<<i)/L)),i)
                qc.x(i)
                for j in range(i):
                    qc.ch(i,j)
                qc.x(i)
            else:
                for y in flip_ind:
                    qc.x(y)
                qc.mcry(2*math.acos(math.sqrt((1<<i)/left)),list(range(i+1,n)),i)
                qc.x(i)
                for j in range(i):
                    qc.mcrx(math.pi/2,list(range(i,n)),j)
                    qc.mcrz(math.pi/2,list(range(i,n)),j)
                    qc.mcrx(math.pi/2,list(range(i,n)),j)
                qc.x(i)
                for y in flip_ind:
                    qc.x(y)
            left -= (1<<i)
        else:
            flip_ind.append(i)

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L == (1<<n):
        for i in range(n):
            qc.h(i)
        return qc
    flip_ind = []
    left = L
    for i in reversed(range(n)):
        if L&(1<<i) :
            if i == n-1:
                qc.ry(2*math.acos(math.sqrt((1<<i)/L)),i)
                qc.x(i)
                for j in range(i):
                    qc.ch(i,j)
                qc.x(i)
            else:
                for y in flip_ind:
                    qc.x(y)
                MCMT.mcry(qc,theta=2*math.acos(math.sqrt((1<<i)/left)),q_controls=list(range(i+1,n)),q_target=i)
                qc.x(i)
                for j in range(i):
                    MCMT.mcrx(qc,theta=math.pi/2,q_controls=list(range(i,n)),q_target=j)
                    MCMT.mcrz(qc,lam=math.pi/2,q_controls=list(range(i,n)),q_target=j)
                    MCMT.mcrx(qc,theta=math.pi/2,q_controls=list(range(i,n)),q_target=j)
                    # qc += MCMT.mcrx(math.pi/2,list(range(i,n)),j)
                    # qc += MCMT.mcrz(math.pi/2,list(range(i,n)),j)
                    # qc += MCMT.mcrx(math.pi/2,list(range(i,n)),j)
                qc.x(i)
                for y in flip_ind:
                    qc.x(y)
            left -= (1<<i)
        else:
            flip_ind.append(i)

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L == (1<<n):
        for i in range(n):
            qc.h(i)
        return qc
    flip_ind = []
    left = L
    for i in reversed(range(n)):
        if L&(1<<i) :
            if i == n-1:
                qc.ry(2*math.acos(math.sqrt((1<<i)/L)),i)
                qc.x(i)
                for j in range(i):
                    qc.ch(i,j)
                qc.x(i)
            else:
                for y in flip_ind:
                    qc.x(y)
                MCMT.mcry(qc,theta=2*math.acos(math.sqrt((1<<i)/left)),q_controls=list(range(i+1,n)),q_target=i,use_basis_gates=True)
                qc.x(i)
                for j in range(i):
                    MCMT.mcrx(qc,theta=math.pi/2,q_controls=list(range(i,n)),q_target=j,use_basis_gates=True)
                    MCMT.mcrz(qc,lam=math.pi/2,q_controls=list(range(i,n)),q_target=j,use_basis_gates=True)
                    MCMT.mcrx(qc,theta=math.pi/2,q_controls=list(range(i,n)),q_target=j,use_basis_gates=True)
                    # qc += MCMT.mcrx(math.pi/2,list(range(i,n)),j)
                    # qc += MCMT.mcrz(math.pi/2,list(range(i,n)),j)
                    # qc += MCMT.mcrx(math.pi/2,list(range(i,n)),j)
                qc.x(i)
                for y in flip_ind:
                    qc.x(y)
            left -= (1<<i)
        else:
            flip_ind.append(i)

    return qc
'''


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

import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L == (1<<n):
        for i in range(n):
            qc.h(i)
        return qc
    flip_ind = []
    left = L
    for i in reversed(range(n)):
        if L&(1<<i) :
            if i == n-1:
                qc.ry(2*math.acos(math.sqrt((1<<i)/left)),i)
                qc.x(i)
                for j in range(i):
                    qc.ch(i,j)
                qc.x(i)
            else:
                for y in flip_ind:
                    qc.x(y)
                qc.append(RYGate(2*math.acos(math.sqrt((1<<i)/left))).control(n-1-i), list(reversed(range(i,n))))
                qc.x(i)
                for j in range(i):
                    qc.append(HGate().control(n-i), list(reversed(range(i,n)))+[j])
                qc.x(i)
                for y in flip_ind:
                    qc.x(y)
            left -= (1<<i)
        else:
            flip_ind.append(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve_rec(qc: QuantumCircuit, n: int, bit: int, L: int) -> None:
    if bit < 0:
        return
    if L < (1<<bit):
        qc.x(bit)
        solve_rec(qc, n, bit-1, L)
        qc.x(bit)
    else:
        qc.r(math.pi/2, 0, bit)
        qc.mcp(-2*math.acos(math.sqrt((1<<bit)/L)), list(range(bit+1,n)), bit)
        qc.r(-math.pi/2, 0, bit)
        qc.x(bit)
        for i in range(bit):
            qc.r(math.pi/2, 0, i)
            qc.mcp(math.pi/2, list(range(bit,n)), i)
            qc.r(-math.pi/2, 0, i)
        qc.x(bit)
        solve_rec(qc, n, bit-1, L-(1<<bit))

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n == 1 and L == 2:
        qc.h(0)
    if n == 1:
        return qc
    if L < (1<<(n-1)):
        qc.x(n-1)
        solve_rec(qc, n, n-2, L)
        qc.x(n-1)
    else:
        qc.r(2*math.acos(math.sqrt((1<<(n-1))/L)), math.pi/2, n-1)
        qc.x(n-1)
        for i in range(n-1):
            qc.ch(n-1, i)
        qc.x(n-1)
        solve_rec(qc, n, n-2, L-(1<<(n-1)))
    return qc
'''


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

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

    target = [True]*l + [False]*(2**n-l)
    build(n, qc, target, '')

    return qc

def build(n, qc, array, prefix):
    depth = len(prefix)

    if all(array):
        for idx in range(depth):
            if prefix[idx] == '0':
               qc.x(idx)

        mch(qc, list(range(0, depth)), list(range(depth, n)))

        for idx in range(depth):
            if prefix[idx] == '0':
                qc.x(idx)

        return

    half = len(array) // 2

    lcount = sum(array[0:half])
    rcount = sum(array[half:len(array)])

    if rcount != 0:
        div = math.sqrt(lcount / (lcount+rcount))

        for idx in range(0, depth):
            if prefix[idx] == '0':
                qc.x(idx)

        mcry(qc, -2*math.acos(div), list(range(0, depth)), [depth])

        for idx in range(0, depth):
            if prefix[idx] == '0':
                qc.x(idx)

    build(n, qc, array[0:half], prefix + '0')

    if rcount != 0:
        build(n, qc, array[half:len(array)], prefix + '1')

def mcry(qc, theta, control, target):
    gate = qisl.RYGate(theta)
    if len(control) != 0:
        gate = gate.control(len(control))

    for v in target:
        qc.append(gate, control + [v])

def mch(qc, control, target):
    gate = qisl.HGate().control(len(control))

    for v in target:
        qc.append(gate, control + [v])
'''


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

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

    target = [True]*l + [False]*(2**n-l)
    build(n, qc, target, '')

    return qc

def build(n, qc, array, prefix):
    depth = len(prefix)

    if all(array):
        for idx in range(depth):
            if prefix[idx] == '0':
               qc.x(idx)

        mch(qc, list(range(0, depth)), list(range(depth, n)))

        for idx in range(depth):
            if prefix[idx] == '0':
                qc.x(idx)

        return

    half = len(array) // 2

    lcount = sum(array[0:half])
    rcount = sum(array[half:len(array)])

    if rcount != 0:
        div = math.sqrt(lcount / (lcount+rcount))

        for idx in range(0, depth):
            if prefix[idx] == '0':
                qc.x(idx)

        mcry(qc, 2*math.acos(div), list(range(0, depth)), [depth])

        for idx in range(0, depth):
            if prefix[idx] == '0':
                qc.x(idx)

    build(n, qc, array[0:half], prefix + '0')

    if rcount != 0:
        build(n, qc, array[half:len(array)], prefix + '1')

def mcry(qc, theta, control, target):
    gate = qisl.RYGate(theta)
    if len(control) != 0:
        gate = gate.control(len(control))

    for v in target:
        qc.append(gate, control + [v])

def mch(qc, control, target):
    gate = qisl.HGate()
    if len(control) != 0:
        gate = gate.control(len(control))

    for v in target:
        qc.append(gate, control + [v])
'''


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

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

    target = [True]*l + [False]*(2**n-l)
    build(n, qc, target, '')

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

    return qc

def build(n, qc, array, prefix):
    depth = len(prefix)

    if all(array):
        for idx in range(depth):
            if prefix[idx] == '0':
               qc.x(idx)

        mch(qc, list(range(0, depth)), list(range(depth, n)))

        for idx in range(depth):
            if prefix[idx] == '0':
                qc.x(idx)

        return

    half = len(array) // 2

    lcount = sum(array[0:half])
    rcount = sum(array[half:len(array)])

    if rcount != 0:
        div = math.sqrt(lcount / (lcount+rcount))

        for idx in range(0, depth):
            if prefix[idx] == '0':
                qc.x(idx)

        mcry(qc, 2*math.acos(div), list(range(0, depth)), [depth])

        for idx in range(0, depth):
            if prefix[idx] == '0':
                qc.x(idx)

    build(n, qc, array[0:half], prefix + '0')

    if rcount != 0:
        build(n, qc, array[half:len(array)], prefix + '1')

def mcry(qc, theta, control, target):
    gate = qisl.RYGate(theta)
    if len(control) != 0:
        gate = gate.control(len(control))

    for v in target:
        qc.append(gate, control + [v])

def mch(qc, control, target):
    gate = qisl.HGate()
    if len(control) != 0:
        gate = gate.control(len(control))

    for v in target:
        qc.append(gate, control + [v])
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if L==1:
        return qc
    if n==1:
        qc.h(0)
        return qc
    MX = L-1
    bit_position = []
    for i in range(n):
        if (MX>>i)&1:
            bit_position.append(i)
    bit_position.reverse()
    # print(bit_position)
    sz = L
    for i in range(len(bit_position)):
        pos = bit_position[i]
        prob = (1<<pos)/sz
        # print(pos, prob, sz, 1<<pos)
        theta = math.acos(math.sqrt(prob))
        if i == 0:
            qc.ry(2.0 * theta,pos)
        else:
            qc.cry(2.0 * theta,bit_position[i-1],pos)
        sz -= (1<<pos)

    bit_position.reverse()
    for i in range(len(bit_position)-1):
        pos = bit_position[i]
        if pos == 0:
            continue
        custom = HGate().control(2,ctrl_state="10")
        for j in range(pos):
            qc.append(custom,[pos,bit_position[i+1],j])
    if bit_position[-1] != 0:
        qc.ch(bit_position[-1],range(bit_position[-1]),ctrl_state=0)
    return qc
'''


'''python
import math

from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import ZGate
from qiskit.circuit.library import HGate
from qiskit.circuit.library import YGate
from qiskit.circuit.library import RYGate

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

def debug(qc):
    qc.save_statevector()
    simulator = AerSimulator(method='statevector')
    circ = transpile(qc, simulator)
    result = simulator.run(circ).result()
    statevector = result.get_statevector(circ)
    # plot_state_city(statevector, title='Bell state', filename='statevector.png')
    print(statevector)
"""

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    L -= 1
    src = 0
    ## [src] が非零
    ## [0, src) が完成
    ## 1点 src だけ係数未完成
    for i in range(n):
        qc.x(i)
    for i in range(n - 1, -1, -1):
        if L & (1 << i):
            theta = 2 * math.atan2(math.sqrt(1 + (L & ((1 << i) - 1))), math.sqrt(1 << i))
            if n == 1:
                qc.ry(theta, i)
            else:
                qc.append(RYGate(theta).control(n - 1), [k for k in range(n) if k != i] + [i])
            for j in range(i):
                qc.append(HGate().control(n - i), list(range(i, n)) + [j])
            qc.x(i)

    for i in range(n):
        if not (L & (1 << i)):
            qc.x(i)
    print(qc.depth())
    return qc

solve(10, 1023)
'''


'''python
import math

from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import ZGate
from qiskit.circuit.library import HGate
from qiskit.circuit.library import YGate
from qiskit.circuit.library import RYGate

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

def debug(qc):
    qc.save_statevector()
    simulator = AerSimulator(method='statevector')
    circ = transpile(qc, simulator)
    result = simulator.run(circ).result()
    statevector = result.get_statevector(circ)
    # plot_state_city(statevector, title='Bell state', filename='statevector.png')
    print(statevector)
"""

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    L -= 1
    src = 0
    ## [src] が非零
    ## [0, src) が完成
    ## 1点 src だけ係数未完成
    for i in range(n):
        qc.x(i)
    for i in range(n - 1, -1, -1):
        if L & (1 << i):
            theta = 2 * math.atan2(math.sqrt(1 + (L & ((1 << i) - 1))), math.sqrt(1 << i))
            if n == 1:
                qc.ry(theta, i)
            else:
                qc.append(RYGate(theta).control(n - 1), [k for k in range(n) if k != i] + [i])
            for j in range(i):
                qc.append(HGate().control(n - i), list(range(i, n)) + [j])
            qc.x(i)

    for i in range(n):
        if not (L & (1 << i)):
            qc.x(i)
    print(qc.depth())
    return qc
'''


'''python
import math

from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import ZGate
from qiskit.circuit.library import HGate
from qiskit.circuit.library import YGate
from qiskit.circuit.library import RYGate

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

def debug(qc):
    qc.save_statevector()
    simulator = AerSimulator(method='statevector')
    circ = transpile(qc, simulator)
    result = simulator.run(circ).result()
    statevector = result.get_statevector(circ)
    # plot_state_city(statevector, title='Bell state', filename='statevector.png')
    print(statevector)
"""

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    L -= 1
    src = 0
    ## [src] が非零
    ## [0, src) が完成
    ## 1点 src だけ係数未完成
    for i in range(n):
        qc.x(i)
    for i in range(n - 1, -1, -1):
        if L & (1 << i):
            theta = 2 * math.atan2(math.sqrt(1 + (L & ((1 << i) - 1))), math.sqrt(1 << i))
            if n == 1:
                qc.ry(theta, i)
            else:
                qc.append(RYGate(theta).control(n - 1), [k for k in range(n) if k != i] + [i])
            for j in range(i):
                qc.append(HGate().control(n - i), list(range(i, n)) + [j])
            qc.x(i)

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


'''python
import math

from qiskit import QuantumCircuit
#from qiskit.circuit.library import ZGate
from qiskit.circuit.library import HGate
#from qiskit.circuit.library import YGate
from qiskit.circuit.library import RYGate

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

def debug(qc):
    qc.save_statevector()
    simulator = AerSimulator(method='statevector')
    circ = transpile(qc, simulator)
    result = simulator.run(circ).result()
    statevector = result.get_statevector(circ)
    # plot_state_city(statevector, title='Bell state', filename='statevector.png')
    print(statevector)
"""

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    L -= 1
    src = 0
    ## [src] が非零
    ## [0, src) が完成
    ## 1点 src だけ係数未完成
    for i in range(n):
        qc.x(i)
    for i in range(n - 1, -1, -1):
        if L & (1 << i):
            theta = 2 * math.atan2(math.sqrt(1 + (L & ((1 << i) - 1))), math.sqrt(1 << i))
            if n == 1:
                qc.ry(theta, i)
            else:
                qc.append(RYGate(theta).control(n - 1), [k for k in range(n) if k != i] + [i])
            for j in range(i):
                qc.append(HGate().control(n - i), list(range(i, n)) + [j])
            qc.x(i)

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


'''python
import math

from qiskit import QuantumCircuit
from qiskit.circuit.library import HGate
from qiskit.circuit.library import RYGate

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

def debug(qc):
    qc.save_statevector()
    simulator = AerSimulator(method='statevector')
    circ = transpile(qc, simulator)
    result = simulator.run(circ).result()
    statevector = result.get_statevector(circ)
    # plot_state_city(statevector, title='Bell state', filename='statevector.png')
    print(statevector)
"""

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    L -= 1
    src = 0
    ## [src] が非零
    ## [0, src) が完成
    ## 1点 src だけ係数未完成
    for i in range(n):
        qc.x(i)
    for i in range(n - 1, -1, -1):
        if L & (1 << i):
            theta = 2 * math.atan2(math.sqrt(1 + (L & ((1 << i) - 1))), math.sqrt(1 << i))
            if n == 1 or n == i + 1:
                qc.ry(theta, i)
            else:
                qc.append(RYGate(theta).control(n - i - 1), list(range(i + 1, n)) + [i])
            for j in range(i):
                qc.append(HGate().control(n - i), list(range(i, n)) + [j])
            qc.x(i)

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

    return qc
'''


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

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

    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.p(math.pi / 3, i)
        else:
            qc.append(PhaseGate(math.pi / 3).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

def Rs(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.x(range(n))
    if n == 1:
        qc.p(math.pi / 3, 0)
    else:
        qc.append(PhaseGate(math.pi / 3).control(n - 1), range(n))
    qc.x(range(n))
    return qc

def U(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.h(range(max(1, math.ceil(math.log2(L)))))
    return qc

def Um(n: int, L: int, m: int, u: QuantumCircuit, r_t: QuantumCircuit, r_s: QuantumCircuit) -> QuantumCircuit:
    if m == 0:
        return U(n, L)
    u_m_1 = Um(n, L, m - 1, u, r_t, r_s)
    return u_m_1.compose(r_t).compose(u_m_1.inverse()).compose(r_s).compose(u_m_1)

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    u = U(n, L)
    r_t = Rt(n, L)
    r_s = Rs(n)
    um = Um(n, L, 2, u, r_t, r_s)
    return um
'''


'''python
from qiskit import QuantumCircuit
import math

def oracle(qc: QuantumCircuit, n:int, L:int):

    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

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

    loop_count = -1
    for p in range(100):
        expected = math.sin((2 * loop_count + 1) * math.asin(math.sqrt(L) / math.sqrt(1 << (itr+1)))) ** 2
        if expected > 0.996:
            loop_count = p
            break

    # grover loop
    for t in range(loop_count):
        qc = oracle(qc,itr+1,L)
        for p in range(itr + 1):
            qc.h(p)

        qc.rz(math.pi * 2,0)
        for t in range(itr + 1):
            qc.x(t)

        data = []
        for t in range(itr):
            data.append(itr - t)

        qc.mcp(lam=math.pi,control_qubits=data, target_qubit=0)
        for t in range(itr + 1):
            qc.x(t)
        for p in range(itr + 1):
            qc.h(p)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def oracle(qc: QuantumCircuit, n:int, L:int):

    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

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

    loop_count = -1
    for p in range(1000):
        expected = math.sin((2 * loop_count + 1) * math.asin(math.sqrt(L) / math.sqrt(1 << (itr+1)))) ** 2
        if expected > 0.998:
            loop_count = p
            break

    # grover loop
    for t in range(loop_count):
        qc = oracle(qc,itr+1,L)
        for p in range(itr + 1):
            qc.h(p)

        qc.rz(math.pi * 2,0)
        for t in range(itr + 1):
            qc.x(t)

        data = []
        for t in range(itr):
            data.append(itr - t)

        qc.mcp(lam=math.pi,control_qubits=data, target_qubit=0)
        for t in range(itr + 1):
            qc.x(t)
        for p in range(itr + 1):
            qc.h(p)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def oracle(qc: QuantumCircuit, n:int, L:int):

    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

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

    loop_count = -1
    for p in range(1000):
        loop_count = p
        expected = math.sin((2 * loop_count + 1) * math.asin(math.sqrt(L) / math.sqrt(1 << (itr+1)))) ** 2
        if expected > 0.998:
            loop_count = p
            break

    # grover loop
    for t in range(loop_count):
        qc = oracle(qc,itr+1,L)
        for p in range(itr + 1):
            qc.h(p)

        qc.rz(math.pi * 2,0)
        for t in range(itr + 1):
            qc.x(t)

        data = []
        for t in range(itr):
            data.append(itr - t)

        qc.mcp(lam=math.pi,control_qubits=data, target_qubit=0)
        for t in range(itr + 1):
            qc.x(t)
        for p in range(itr + 1):
            qc.h(p)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def oracle(qc: QuantumCircuit, n:int, L:int):

    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

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

    qc = QuantumCircuit(n)
    target_itr = 0
    for i in range(n):
        if L >= (1 << i):
            target_itr = i + 1

    loop_count = -1
    itr = -1
    for p in range(1000):
        for qubit in range(target_itr, n):
            expected = math.sin((2 * p + 1) * math.asin(math.sqrt(L) / math.sqrt(1 << (qubit)))) ** 2
            if expected >= 0.996:
                loop_count = p
                itr = qubit - 1
                break
        if loop_count != -1:
            break

    # grover loop
    for t in range(loop_count):
        qc = oracle(qc,itr+1,L)
        for p in range(itr + 1):
            qc.h(p)

        qc.rz(math.pi * 2,0)
        for t in range(itr + 1):
            qc.x(t)

        data = []
        for t in range(itr):
            data.append(itr - t)

        qc.mcp(lam=math.pi,control_qubits=data, target_qubit=0)
        for t in range(itr + 1):
            qc.x(t)
        for p in range(itr + 1):
            qc.h(p)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def oracle(qc: QuantumCircuit, n:int, L:int):

    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

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

    loop_count = -1
    for p in range(1000):
        loop_count = p
        expected = math.sin((2 * loop_count + 1) * math.asin(math.sqrt(L) / math.sqrt(1 << (itr+1)))) ** 2
        if expected > 0.995:
            loop_count = p
            break

    # grover loop
    for t in range(loop_count):
        qc = oracle(qc,itr+1,L)
        for p in range(itr + 1):
            qc.h(p)

        qc.rz(math.pi * 2,0)
        for t in range(itr + 1):
            qc.x(t)

        data = []
        for t in range(itr):
            data.append(itr - t)

        qc.mcp(lam=math.pi,control_qubits=data, target_qubit=0)
        for t in range(itr + 1):
            qc.x(t)
        for p in range(itr + 1):
            qc.h(p)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def oracle(qc: QuantumCircuit, n:int, L:int):

    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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n == 1 or L ==1:
        return qc
    itr = 1
    for i in range(n):
        if L >= (1 << i) or True:
            itr = n-1
            qc.h(i)

    loop_count = -1
    for i in range(1000):
        expected = math.sin((2 * i + 1) * math.asin(math.sqrt(L) / math.sqrt(1 << (itr+1)))) ** 2
        if expected > 0.995:
            loop_count = i
            break

    # grover loop
    for t in range(loop_count):
        qc = oracle(qc,itr+1,L)
        for p in range(itr + 1):
            qc.h(p)

        qc.rz(math.pi * 2,0)
        for t in range(itr + 1):
            qc.x(t)

        data = []
        for t in range(itr):
            data.append(itr - t)
        if itr == 0:
            qc.z(0)
        else:
            qc.mcp(lam=math.pi,control_qubits=data, target_qubit=0)
        for t in range(itr + 1):
            qc.x(t)
        for p in range(itr + 1):
            qc.h(p)
    results = execute(qc, backend=Aer.get_backend('statevector_simulator'), shots=1).result()
    data = results.get_statevector()
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def oracle(qc: QuantumCircuit, n:int, L:int):

    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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n == 1 or L ==1:
        return qc
    itr = 1
    for i in range(n):
        if L >= (1 << i) or True:
            itr = n-1
            qc.h(i)

    loop_count = -1
    for i in range(1000):
        expected = math.sin((2 * i + 1) * math.asin(math.sqrt(L) / math.sqrt(1 << (itr+1)))) ** 2
        if expected > 0.995:
            loop_count = i
            break

    # grover loop
    for t in range(loop_count):
        qc = oracle(qc,itr+1,L)
        for p in range(itr + 1):
            qc.h(p)

        qc.rz(math.pi * 2,0)
        for t in range(itr + 1):
            qc.x(t)

        data = []
        for t in range(itr):
            data.append(itr - t)
        if itr == 0:
            qc.z(0)
        else:
            qc.mcp(lam=math.pi,control_qubits=data, target_qubit=0)
        for t in range(itr + 1):
            qc.x(t)
        for p in range(itr + 1):
            qc.h(p)
    #results = execute(qc, backend=Aer.get_backend('statevector_simulator'), shots=1).result()
    #data = results.get_statevector()
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def oracle(qc: QuantumCircuit, n:int, L:int):

    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

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n == 1 or L ==1:
        return qc
    itr = 1
    for i in range(n):
        if L >= (1 << i) or True:
            itr = n-1
            qc.h(i)

    loop_count = -1
    for i in range(10000):
        expected = math.sin((2 * i + 1) * math.asin(math.sqrt(L) / math.sqrt(1 << (itr+1)))) ** 2
        if expected > 0.995:
            loop_count = i
            break

    # grover loop
    for t in range(loop_count):
        qc = oracle(qc,itr+1,L)
        for p in range(itr + 1):
            qc.h(p)

        qc.rz(math.pi * 2,0)
        for t in range(itr + 1):
            qc.x(t)

        data = []
        for t in range(itr):
            data.append(itr - t)
        if itr == 0:
            qc.z(0)
        else:
            qc.mcp(lam=math.pi,control_qubits=data, target_qubit=0)
        for t in range(itr + 1):
            qc.x(t)
        for p in range(itr + 1):
            qc.h(p)
    #results = execute(qc, backend=Aer.get_backend('statevector_simulator'), shots=1).result()
    #data = results.get_statevector()
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def prepare_uniform_superposition(qc, n, L):
    # We need to apply Hadamard gates to create a uniform superposition.
    # However, we don't want to apply it to all qubits if L is not a power of 2.
    for i in range(n):
        if L > 1 << i:
            qc.h(i)

    # If L is not a power of 2, we need to correct the amplitudes.
    if not math.isclose(L, 2 ** round(math.log2(L))):
        angle = 2 * math.acos(math.sqrt(L / 2 ** n))
        for i in range(n):
            # Apply controlled rotation to correct the amplitude of states that are not part of the L states
            qc.cu(angle, 0, 0, 0, i, range(i+1, n))

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

# Example usage:
n = 3
L = 3
qc = solve(n, L)
print(qc)
'''


'''python
from qiskit import QuantumCircuit
import math

def prepare_uniform_superposition(qc, n, L):
    # We need to apply Hadamard gates to create a uniform superposition.
    # However, we don't want to apply it to all qubits if L is not a power of 2.
    for i in range(n):
        if L > 1 << i:
            qc.h(i)

    # If L is not a power of 2, we need to correct the amplitudes.
    if not math.isclose(L, 2 ** round(math.log2(L))):
        angle = 2 * math.acos(math.sqrt(L / 2 ** n))
        for i in range(n):
            # Apply controlled rotation to correct the amplitude of states that are not part of the L states
            qc.cu(angle, 0, 0, 0, i, range(i+1, n))

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


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

def Rt(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-:
            qc.p(math.pi/3,i)
        else:
            qc.append(PhaseGate(math.pi/3).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

def Rs(n:int)->QuantumCircuit:
    qc=QuantumCircuit(n)
    qc.x(range(n))
    if n==1:
        qc.p(math.pi/3,0)
    else:
        qc.append(PhaseGate(math.pi/3).control(n-1),range(n))

    qc.x(range(n))

    return qc

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

    if m==0:
        qc.h(range(m))
        return qc

    u=U(m-1,n,L)

    qc.compose(u,inplace=True)
    qc.compose(Rt(n,L),inplace=True)
    qc.compose(u.inverse(),inplace=True)
    qc.compose(Rs(n),inplace=True)
    qc.compose(u,inplace=True)

    return qc

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    k=math.ceil(math.log2(L))
    if k==0:
        return qc
    m=2
    qc.compose(U(m,k,L),inplace=True)
    return qc
'''


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

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

    # Write your code here:

    # 左n bitが1になっている物の位相を反転
    def reverse(qubits,qc,start):
        if qubits-start>1:
            qc.append(ZGate().control(qubits - 1 - start), range(start,qubits))
        else:
            qc.z(start)

    # ある値をall_1に変更する操作
    # 左何bit目から操作するか指定
    def to_calcable(qubits,qc,n,start):
        for i in range(start,qubits):
            if not (n&(1<<i)):
                qc.x(i)

    d={}
    for i in range(1<<n):
        tmp=""
        for j in bin(i)[2:].rjust(n,"0"):
            tmp+=j
            d.setdefault(tmp,0)

    for i in range(L):
        s=bin(i)[2:].rjust(n,"0")
        tmp=""
        for i in s:
            tmp+=i
            d[tmp]+=1

    # 指定したbit(ゼロ状態)が0になる確率をratioにする

    # 指定したcontroll bitがすべて1の場合にbit(ゼロ状態)が0になる確率をratioにする
    def crotate(qc,bits,p0):
        theta=math.acos(p0*2-1)
        if len(bits)==1:
            qc.ry(theta,bits[0])
        else:
            qc.append(RYGate(theta).control(len(bits)-1),bits)

    def dfs(prefix,bit,qc,n):
        # print("call",prefix,bit)
        prefix0=prefix+"0"
        prefix1=prefix+"1"
        v=int(prefix.ljust(n,"0"),2)

        if bit==-1 or d.get(prefix0)+d.get(prefix1)==0:return
        if prefix=="":
            p0=d[prefix0]/(d[prefix0]+d[prefix1])
            to_calcable(n,qc,v,0)
            crotate(qc,[bit],p0)
            to_calcable(n,qc,v,0)
            dfs(prefix0,bit-1,qc,n)
            dfs(prefix1,bit-1,qc,n)
        elif bit>=0:
            # print(prefix,d[prefix+"0"],d[prefix+"1"])
            p0=d[prefix0]/(d[prefix0]+d[prefix1])
            if p0==0.5:
                to_calcable(n,qc,v,0)
                for b in range(bit,-1,-1):
                    crotate(qc,list(range(n-1,bit,-1))+[b],p0)
                to_calcable(n,qc,v,0)
            else:
                to_calcable(n,qc,v,0)
                crotate(qc,list(range(n-1,bit-1,-1)),p0)
                to_calcable(n,qc,v,0)
                dfs(prefix0,bit-1,qc,n)
                dfs(prefix1,bit-1,qc,n)

    dfs("",n-1,qc,n)
    # depth = qc.depth()
    # print("量子回路の深さ:", depth)
    return qc
'''


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

def append_control_gate(qc: QuantumCircuit, gate, target_qubit: int, ctrl_state: int, controlled_qubits)->QuantumCircuit:
	if len(controlled_qubits) == 0:
		qc.append(gate, [target_qubit])
	else:
		qc.append(gate.control(len(controlled_qubits), ctrl_state = ctrl_state), controlled_qubits + [target_qubit])
	return qc

def solve(n: int, L: int)->QuantumCircuit:
	qc = QuantumCircuit(n)
	if L == (1 << n):
		for i in range(n):
			qc.h(i)
		return qc
	mask = (1 << n) - 1
	ctrl_state = 0
	controlled_qubits = []
	for bit in range(n - 1, -1, -1):
		if L >> bit & 1:
			left, right = 1 << bit, L - (1 << bit)
			theta = acos(sqrt(left) / sqrt(left + right)) * 2
			append_control_gate(qc, RYGate(theta), bit, ctrl_state, controlled_qubits)
			for bit_next in range(bit - 1, -1, -1):
				append_control_gate(qc, HGate(), bit_next, ctrl_state, controlled_qubits + [bit])
			L ^= 1 << bit
			ctrl_state |= 1 << len(controlled_qubits)
		controlled_qubits += [bit]
	return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    """
    Prepare an n-qubit state such that |0>,|1>,...,|L-1> have equal
    probability amplitudes and the total probability of these states
    is (ideally) 1.  The construction is exact – amplitudes of all
    states i >= L are 0 – therefore the success probability certainly
    exceeds 1-5e-3.
    """
    qc = QuantumCircuit(n)

    # -----------------------------------------------------------------
    #  Helper routines
    # -----------------------------------------------------------------
    def apply_controlled_ry(theta: float, target: int, ctrl_bits):
        """
        Apply RY(theta) to `target` qubit, controlled on the qubits
        in `ctrl_bits`.  `ctrl_bits` is a list of tuples
        (qubit_index , value) where value is 0 or 1, telling whether
        we condition on |0> or |1>.  If value == 0 we surround the
        control qubit with X gates.
        """
        pre_x = []
        controls = []

        # turn controls that should be in |0> into |1> by X, remember them
        for q, val in ctrl_bits:
            if val == 0:
                qc.x(q)
                pre_x.append(q)
            controls.append(q)

        if len(controls) == 0:
            qc.ry(theta, target)
        else:
            gate = RYGate(theta).control(len(controls))
            qc.append(gate, controls + [target])

        # restore the negated control qubits
        for q in reversed(pre_x):
            qc.x(q)

    def apply_force_one(target: int, ctrl_bits):
        """
        Deterministically set `target` qubit to |1> on the sub-space
        specified by `ctrl_bits` (same format as above).
        Implemented with a multi-controlled X.
        """
        pre_x = []
        controls = []
        for q, val in ctrl_bits:
            if val == 0:
                qc.x(q)
                pre_x.append(q)
            controls.append(q)

        if len(controls) == 0:
            qc.x(target)
        else:
            gate = XGate().control(len(controls))
            qc.append(gate, controls + [target])

        for q in reversed(pre_x):
            qc.x(q)

    # -----------------------------------------------------------------
    #  Recursive construction
    # -----------------------------------------------------------------
    selected = list(range(L))                      # indices that must get weight
    qubits_order = list(range(n))                  # use little-endian: 0 (LSB) … n-1 (MSB)

    def build(indices, bit_pos, ctrl_bits):
        """
        `indices` : list of basis indices still to realise
        `bit_pos` : current qubit to branch on (0 … n-1)
        `ctrl_bits` : list of previously fixed qubits (for controls)
        """
        if len(indices) <= 1 or bit_pos >= n:
            # Either a single state left or no more qubits to process.
            if len(indices) == 1:
                target_state = indices[0]
                # If the remaining qubit (bit_pos) values have to be 1,
                # set them explicitly.
                for b in range(bit_pos, n):
                    desired = (target_state >> b) & 1
                    if desired == 1:
                        apply_force_one(b, ctrl_bits)
            return

        target_qubit = qubits_order[bit_pos]
        zero_branch = [i for i in indices if ((i >> bit_pos) & 1) == 0]
        one_branch  = [i for i in indices if ((i >> bit_pos) & 1) == 1]

        if len(zero_branch) == 0:
            # All states require this qubit to be |1>
            apply_force_one(target_qubit, ctrl_bits)
        elif len(one_branch) == 0:
            # All states require |0>, nothing to do
            pass
        else:
            # Both 0 and 1 appear – split amplitudes accordingly
            theta = 2 * math.atan2(math.sqrt(len(one_branch)),
                                   math.sqrt(len(zero_branch)))
            apply_controlled_ry(theta, target_qubit, ctrl_bits)

        # Recurse on the sub-branches
        if len(zero_branch) > 0:
            build(zero_branch, bit_pos + 1, ctrl_bits + [(target_qubit, 0)])
        if len(one_branch) > 0:
            build(one_branch,  bit_pos + 1, ctrl_bits + [(target_qubit, 1)])

    # -----------------------------------------------------------------
    #  Kick-off the recursion
    # -----------------------------------------------------------------
    if L > 0:
        build(selected, 0, [])

    return qc
'''


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

def solve(n: int, L: int) -> QuantumCircuit:
    """
    Returns a QuantumCircuit acting on `n` qubits that prepares the state
        |ψ> = (1/√L) Σ_{i=0}^{L-1} |i>
    with little-endian integer encoding.

    The construction keeps the circuit depth low by
      • using simple Hadamards when L is a power of two;
      • otherwise employing `initialize` only on the minimum number of qubits
        needed (m = ⌈log₂ L⌉), leaving the remaining qubits in |0>.
    """
    qc = QuantumCircuit(n)

    # Trivial case  ───────
    if L == 1:
        # The initial |0⋯0⟩ is already the desired state.
        return qc

    # Power-of-two case  ──
    if (L & (L - 1)) == 0:          # checks if L is a power of two
        k = int(math.log2(L))       # number of qubits that need Hadamards
        for qubit in range(k):      # apply to least-significant qubits
            qc.h(qubit)
        return qc

    # General case  ───────
    # Work only on m = ⌈log₂ L⌉ least-significant qubits.
    m = math.ceil(math.log2(L))
    vec_len = 1 << m                # 2**m
    state_vec = np.zeros(vec_len, dtype=complex)
    amplitude = 1 / math.sqrt(L)
    state_vec[:L] = amplitude       # equal amplitudes for |0⟩ … |L-1⟩

    # Initialize those m qubits; higher qubits remain in |0⟩.
    target_qubits = list(range(m))  # little-endian ordering
    qc.initialize(state_vec, target_qubits)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library.standard_gates import HGate
import math
import numpy as np

def solve(n: int, L: int) -> QuantumCircuit:
    """
    Return a circuit that prepares (up to global phase)

        |ψ⟩ = 1/√L · Σ_{k=0}^{L-1} |k⟩_little-endian

    on an n-qubit register, with total depth ≤ 1000 for every
    (n ≤ 10, 1 ≤ L ≤ 2^n).

    Strategy
    --------
    •  If  L  is a power of two, apply Hadamards to the
       lowest  log₂(L)  qubits – finished (depth = 1).

    •  Otherwise, write  L = 2^m + r  with 0 < r < 2^m.
       Use qubit  m  as a “flag”:

          |0⟩⊗|u_{2^m}⟩   (2^m states)
                   ──┐
          |1⟩⊗|u_{r}⟩    (r   states)

       where  |u_t⟩  is the uniform superposition of  t
       computational-basis states.

       Preparation steps
       1)  R_y(θ) on the flag with
              tan(θ/2) = √(r / 2^m)
           sets the correct weight ratio between the two
           branches so that every populated basis state
           finally has amplitude 1/√L.

       2)  Put the first  m  qubits into |u_{2^m}⟩ with a
           layer of Hadamards (depth +1).

       3)  For flag = 1 we undo those Hadamards (controlled-H,
           one per qubit) so that the branch is back in
           |0…0⟩_m.

       4)  If  r > 1 we attach a controlled “initialize”
           gate that turns |0…0⟩ into |u_r⟩ on the first m
           qubits.  The initialise acts on ≤ 9 qubits
           (m ≤ 9) so its decomposed depth is ≤ 510;
           overall circuit depth stays well below 1000.

       Remaining (unused) qubits, if any, stay in |0⟩,
       hence contribute only zero amplitudes.

    All gates are taken from the permitted standard gate set.
    """
    qc = QuantumCircuit(n)

    # Trivial case -----------------------------------------------------------
    if L == 1:
        # Already in |0…0>, nothing to do.
        return qc

    # Power-of-two case ------------------------------------------------------
    if (L & (L - 1)) == 0:
        k = int(math.log2(L))          # number of Hadamards needed
        for q in range(k):
            qc.h(q)
        return qc

    # General case  L = 2^m + r  (0 < r < 2^m) ------------------------------
    m = int(math.floor(math.log2(L)))  # largest power-of-two exponent
    base = 1 << m                      # 2^m
    r = L - base                       # remainder  (1 … 2^m - 1)
    flag = m                           # use qubit m as the flag

    # 1. amplitude ratio between the two branches
    theta = 2 * math.atan(math.sqrt(r / base))
    qc.ry(theta, flag)

    # 2. make the |0>-flag branch uniform over 2^m states
    for q in range(m):
        qc.h(q)

    # 3. for flag = 1, revert those Hadamards (H is its own inverse)
    ch_gate = HGate().control(1)
    for q in range(m):
        qc.append(ch_gate, [flag, q])

    # 4. prepare |u_r> when flag = 1   (skip if r == 1, because the
    #    branch is already |0…0>)
    if r > 1:
        vec = np.zeros(base, dtype=complex)
        vec[:r] = 1 / math.sqrt(r)     # amplitudes for |0>…|r-1>
        prep = QuantumCircuit(m)
        prep.initialize(vec, list(range(m)))
        cprep = prep.to_gate().control(1)
        qc.append(cprep, [flag] + list(range(m)))

    return qc
'''


'''python
from qiskit import QuantumCircuit
from math import acos, sqrt

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)
    M = L
    # Write your code here:
    ls = [i for i in range(n) if get_bit(M, i)]
    if len(ls) == 0:
        for i in range(n):
            qc.h(i)
        return qc
    for l in ls[1:]:
        qc.x(l)

    l0 = ls[0]
    if len(ls) == 1:
        print(l0)
        for i in range(l0):
            qc.h(i)
        return qc

    l1 = ls[1]
    M0 = 2 ** l0
    if l0 > 0:
        for i in range(l0):
            qc.h(i)
    theta0 = -2 * acos(sqrt(M0 / M))
    qc.ry(theta0, l1)

    qc.x(l1)
    for i in range(l0, l1):
        qc.ch(l1, i)
    qc.x(l1)

    k = len(ls) - 1

    Mm = M0

    for m in range(1, k):
        theta_m = -2 * acos(sqrt(2 ** ls[m] / (M - Mm)))
        qc.x(ls[m])
        qc.cry(theta_m, ls[m], ls[m+1])
        qc.x(ls[m])
        qc.x(ls[m+1])
        for i in range(ls[m], ls[m+1]):
            qc.ch(ls[m+1], i)
        qc.x(ls[m+1])
        Mm = Mm + 2 ** ls[m]

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    lis = []
    k = L
    tmp = 0
    while(k>0):
        if k%2:
            lis = lis + [tmp]
        tmp += 1
        k = k //2
    m = 2 ** (lis[0])
    if len(lis) == 1:
        for i in range(lis[0]):
            qc.h(i)
        return qc

    for i in range(1,len(lis)):
        qc.x(lis[i])
    if lis[0] > 0:
        for i in range(lis[0]):
            qc.h(i)
    qc.ry(-2*math.acos(math.sqrt(m / L)), lis[1])
    qc.x(lis[1])
    for i in range(lis[0],lis[1]):
        qc.ch(lis[1], i)
    qc.x(lis[1])
    for i in range(1,len(lis) - 1):
        qc.x(lis[i])
        qc.cry(-2 * math.acos(math.sqrt(2 ** lis[i] / (L - m))), lis[i], lis[i+1])
        qc.x(lis[i])
        qc.x(lis[i+1])
        for j in range(lis[i],lis[i+1]):
            qc.ch(lis[i+1],j)
        qc.x(lis[i+1])
        m = m + 2 ** (lis[i])
    return qc
'''