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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.x(0)
    angle = [0] * (n+1)

    for i in range(n+1):
        if i==0:
            pass
        else:
            angle[i]=np.arccos(1/np.sqrt(i))

    for i in range(n-2):
        qc.cry(angle[n-i]*2,i,i+1)

    qc.cx(n-2,n-3)
    qc.ch(n-2,n-1)
    qc.cx(n-1,n-2)

    for i in range(n+1):
        if i<4:
            pass
        else:
            for j in range(i-1):
                qc.cx(n-j,n-i)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.x(0)
    angle = [0] * (n+1)

    for i in range(n+1):
        if i==0:
            pass
        else:
            angle[i]=np.arccos(1/np.sqrt(i))

    for i in range(n-2):
        qc.cry(angle[n-i]*2,i,i+1)

    qc.cx(n-2,n-3)
    qc.ch(n-2,n-1)
    qc.cx(n-1,n-2)

    for i in range(n+1):
        if i<4:
            pass
        else:
            for j in range(i-1):
                qc.cx(n-1-j,n-i)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.x(0)
    angle = [0] * (n+1)

    for i in range(n+1):
        if i==0:
            pass
        else:
            angle[i]=np.arccos(1/np.sqrt(i))

    for i in range(n-2):
        qc.cry(angle[n-i]*2,i,i+1)

    qc.cx(n-2,n-3)
    qc.ch(n-2,n-1)
    qc.cx(n-1,n-2)

    for i in range(n+1):
        if i<4:
            pass
        else:
            for j in range(i-1):
                qc.cx(n-i+1+j,n-i)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n==2:
        qc.h(0)
        qc.x(0)
        qc.cx(0,1)
        qc.x(0)
        return qc
    else:
        qc.x(0)
        angle = [0] * (n+1)

        for i in range(n+1):
            if i==0:
                pass
            else:
                angle[i]=np.arccos(1/np.sqrt(i))

        for i in range(n-2):
            qc.cry(angle[n-i]*2,i,i+1)

        qc.cx(n-2,n-3)
        qc.ch(n-2,n-1)
        qc.cx(n-1,n-2)

        for i in range(n+1):
            if i<4:
                pass
            else:
                for j in range(i-1):
                    qc.cx(n-1-j,n-i)
        return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for i in range (1, n):
        qc.cx(0, 1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    theta = 2 * atan(sqrt(n - 1))
    qc.ry(theta, 0)

    for i in range(1, n):
        theta = 2 * atan(sqrt(n - i - 1) / (i + 1))
        qc.cry(theta, i - 1, i)

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

    return qc
'''


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

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

    theta = 2 * atan(sqrt(n - 1))
    qc.ry(theta, 0)

    for i in range(1, n):
        theta = 2 * atan(sqrt(n - i - 1) / (i + 1))
        qc.cry(theta, i - 1, i)

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

    return qc
'''


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

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

    theta = 2 * atan(sqrt(n - 1))
    qc.ry(theta, 0)

    for i in range(1, n):
        theta = 2 * atan(sqrt(n - i - 1) / (i + 1))
        qc.cry(theta, i - 1, i)

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

    qc.x(0)

    return qc
'''


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

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

    theta = 2 * atan(sqrt(n - 1))
    qc.ry(theta, 0)

    for i in range(1, n-1):
        theta = 2 * atan(sqrt(n - i - 1))
        qc.cry(theta, i - 1, i)

    for i in range(0, n-1):
        qc.cx(n-2-i, n-i -1)

    qc.x(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.ry(2 * math.acos(math.sqrt(2/3) ), 0)
    for i in range(1, n):
        qc.cry(2 * math.acos(math.sqrt((n-(i+1)) / (n-(i+1)+1)) ), 0,i)

    return qc
'''


'''python
def solve(n: int) -> QuantumCircuit:

    qc = QuantumCircuit(n)
    qc.ry(2 * math.acos(math.sqrt((n - 1) / n)), 0)

    for i in range(1, n):
        qc.cry(2 * math.acos(math.sqrt((n - i) / (n - i + 1))), 0, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        theta = math.acos(1 / math.sqrt(n - i))
        qc.ry(2 * theta, i)

        # Apply controlled NOTs to ensure only one qubit is in the |1⟩ state per state
        if i < n - 1:
            qc.cx(i, i + 1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for i in range(1,n):
        qc.cx(0,i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for i in range(1,n):
        qc.cx(0,i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
            qc.u3(np.pi/2, 0, np.pi, i)  # 量子ビットを|+⟩状態にする
            qc.u1(np.pi/2, i)  # 位相を調整して|1/sqrt(n)⟩状態にする
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
            qc.u3(np.pi/2, 0, np.pi, i)  # 量子ビットを|+⟩状態にする
            qc.u1(np.pi/2, i)  # 位相を調整して|1/sqrt(n)⟩状態にする
    return qc
'''


'''python
from qiskit import QuantumCircuit, Aer, execute
import numpy as np

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

    # すべての量子ビットを|0⟩状態に初期化
    for i in range(n):
        qc.initialize([1, 0], i)

    # one-hot状態を生成
    for i in range(n):
        qc.u3(np.pi/2, 0, np.pi, i)  # 量子ビットを|+⟩状態にする
        qc.u1(np.pi/2, i)  # 位相を調整して|1/sqrt(n)⟩状態にする
'''


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

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

    # すべての量子ビットを|0⟩状態に初期化
    for i in range(n):
        qc.initialize([1, 0], i)

    # one-hot状態を生成
    for i in range(n):
        qc.u3(math.pi/2, 0, math.pi, i)  # 量子ビットを|+⟩状態にする
        qc.u1(math.pi/2, i)  # 位相を調整して|1/sqrt(n)⟩状態にする
'''


'''python
from qiskit import QuantumCircuit
import math

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

    # すべての量子ビットを|0⟩状態に初期化
    for i in range(n):
        qc.initialize([1, 0], i)

    # one-hot状態を生成
    for i in range(n):
        qc.u3(math.pi/2, 0, math.pi, i)  # 量子ビットを|+⟩状態にする
        qc.u1(mat.pi/2, i)  # 位相を調整して|1/sqrt(n)⟩状態にする
'''


'''python
from qiskit import QuantumCircuit
import math

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

    # すべての量子ビットを|0⟩状態に初期化
    for i in range(n):
        qc.initialize([1, 0], i)

    # one-hot状態を生成
    for i in range(n):
        qc.u3(math.pi/2, 0, math.pi, i)  # 量子ビットを|+⟩状態にする
        qc.u1(math.pi/2, i)  # 位相を調整して|1/sqrt(n)⟩状態にする
'''


'''python
from qiskit import QuantumCircuit

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

    for i in range(n):
        for j in range(i+1, n):
            qc.cp(np.pi / n, i, j)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    for i in range(n):
        for j in range(i+1, n):
            qc.cp(math.pi / n, i, j)

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

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.ry(2 * np.arccos(np.sqrt(1/n)), 0)

    for i in range(1, n):
        theta = 2 * np.arccos(np.sqrt(1 / (n - i)))
        qc.cx(i - 1, i)
        qc.ry(theta, i)
        qc.cx(i - 1, i)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.x(0)

    for i in range(n-1):
        theta = 2 * np.arccos(np.sqrt(1 / (n - i)))
        qc.ry(theta, i+1)
        qc.cx(i, i-1)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.x(0)

    for i in range(n-1):
        theta = 2 * np.arccos(np.sqrt(1 / (n - i)))
        qc.ry(theta, i+1)
        qc.cx(i+1,i)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.x(0)

    for i in range(n-1):
        theta = 2 * np.arccos(np.sqrt(1 / (n - i)))
        qc.ry(theta, i+1)
        qc.cx(i,i+1)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.x(0)

    for i in range(n-1):
        theta = 2 * np.arccos(np.sqrt(1 / (n - i)))
        qc.cry(theta,i, i+1)
        qc.cx(i+1,i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.h(0)
    qc.x(0)
    for i in range(n):
        qc.x(i)
        qc.cx(i,i+1)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    a = (1 / n) ** 0.5
    qc.ry(2 * math.acos(a), 0)
    for i in range(n - 2):
        a = (1 / (n - 1 - i)) ** 0.5
        qc.cry(2 * math.acos(a), i, i + 1)
    for i in range(1, n - 1)[::-1]:
        for j in range(i):
            qc.cx(i, j)
    qc.x(n - 1)
    for i in range(n - 1):
        qc.cx(i, n - 1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    qc.h(0)

    for i in range(1, n):
        qc.ch(i-1, i)
        qc.cx(i, i-1)

    return qc
'''


'''python
from math import (
    pi,
    # degrees,
    # radians,
    asin,
    acos,
    # atan2,
    sqrt,
    # sin,
    # cos,
    # tan
)
import numpy as np
from qiskit import QuantumCircuit, QuantumRegister

# from qiskit.circuit.library.standard_gates import (
#     C3XGate,
#     C3SXGate,
#     C4XGate,
#     CCXGate,
#     DCXGate,
#     CHGate,
#     CPhaseGate,
#     CRXGate,
#     CRYGate,
#     CRZGate,
#     CSwapGate,
#     CSXGate,
#     CUGate,
#     CU1Gate,
#     CU3Gate,
#     CXGate,
#     CYGate,
#     CZGate,
#     CCZGate,
#     HGate,
#     IGate,
#     MCPhaseGate,
#     PhaseGate,
#     RCCXGate,
#     RC3XGate,
#     RXGate,
#     RXXGate,
#     RYGate,
#     RYYGate,
#     RZGate,
#     RZZGate,
#     RZXGate,
#     XXMinusYYGate,
#     XXPlusYYGate,
#     ECRGate,
#     SGate,
#     SdgGate,
#     CSGate,
#     CSdgGate,
#     SwapGate,
#     iSwapGate,
#     SXGate,
#     SXdgGate,
#     TGate,
#     TdgGate,
#     UGate,
#     U1Gate,
#     U2Gate,
#     U3Gate,
#     XGate,
#     YGate,
#     ZGate,
# )

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(0)
    for i in range(1, n):
        qc.cry(asin(sqrt((n - i) / (n - i + 1))) * 2, i - 1, i)
    for i in range(1, n):
        qc.cx(i, i - 1)

    return qc
'''


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

def F_gate(circ,i,j,n,k) :
  theta = np.arccos(np.sqrt(1/(n-k+1)))
  circ.ry(-theta,j)
  circ.cz(i,j)
  circ.ry(theta,j)

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.x(n - 1)
    for i in range(n - 1):
        F_gate(qc, n-i-1, n-i-2, n, i+1)

    for i in range(n - 1):
        qc.cx(n-i-2, n-i-1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    theta = 2 * np.arccos(1 / 2)

    qc.ry(theta, 0)
    qc.cry(theta, 0, 1)
    qc.cry(theta, 0, 2)
    qc.cry(theta, 0, 3)

    return qc
'''


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

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

    theta = 2 * np.arccos(1 / 2)

    qc.ry(theta, 0)
    qc.cry(theta, 0, 1)
    qc.cry(theta, 0, 2)
    qc.cry(theta, 0, 3)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(0)
    for i in reversed(range(1,n)):
        qc.cry(2 * math.atan(math.sprt(n - i)), i - 1, i)
        qc.cx(i - 1,i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(0)
    for i in reversed(range(1,n)):
        qc.cry(2 * math.atan(math.sprt(n - i)), i - 1, i)
        qc.cx(i,i - 1)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(0)
    for i in range(n - 1):
        qc.cry(2 * math.atan(math.sprt(n - i)), i, i + 1)
        qc.cx(i + 1,i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    theta = [2 * math.atan(math.sqrt(i)) for i in range(n - 1, 0, -1)]
    qc.x(0)
    for i in range(n - 1):
        qc.cry(theta[i], i, i + 1)
        qc.cx(i + 1,i)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1):
        qc.x(i)
    theta = math.acos(math.sqrt((n-1)/(n))) * 2
    qc.ry(theta, 0)
    for i in range(1, n-1):
        theta = math.acos(math.sqrt((n-i-1)/(n-i))) * 2
        qc.append(RYGate(theta).control(i), [_ for _ in range(i + 1)])
    qc.mcx([_ for _ in range(n - 1)], n - 1)
    for i in range(n-1):
        qc.x(i)
    qc.z(n - 1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if i == 0:
            qc.h(i)
        else:
            qc.append(HGate().control(i), list(range(i+1)))
        qc.x(i)
    for i in range(n):
        qc.x(i)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if i == 0:
            qc.h(i)
        else:
            qc.append(HGate().control(i), list(range(i+1)))
        qc.x(i)
    for i in range(n):
        qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(0)
    for _ in range(0, n):
        qc.cry(2 * math.atan(math.sqrt(n - _ - 1)), _, _ + 1)
        qc.cx(_ + 1, _)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(0)
    for _ in range(0, n):
        qc.cry(2 * math.atan(math.sqrt(n - _ - 1)), _, _ + 1)
        qc.cx(_ + 1, _)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(0)
    for _ in range(n - 1):
        qc.cry(2 * math.atan(math.sqrt(n - _ - 1)), _, _ + 1)
        qc.cx(_ + 1, _)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    # Paso 1: Aplicar Ry al primer qubit
    theta = 2 * math.acos(1 / math.sqrt(n))
    qc.ry(theta, 0)

    # Paso 2: Aplicar puertas controladas de Hadamard desde el primer qubit a los demás
    for i in range(1, n):
        qc.ch(0, i)

    # Paso 3: Aplicar una cadena de puertas CNOT entre los qubits 1 a n-1
    for i in range(1, n-1):
        qc.cx(i, i+1)

    # Paso 4: Aplicar puertas CNOT desde el primer qubit a los demás
    for i in range(1, n):
        qc.cx(0, i)

    # Paso 5: Aplicar una puerta X al primer qubit
    qc.x(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math as m

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

    circuit.x(0)

    m = m.floor(n/2)

    qubitT = 1

    def _w_state_circuit(circuit, nn, mm, qubitC):

        if nn == 0 and mm == 1:
            pass #do nothing in this case
        elif nn == 1 and mm == 2: #case (1,2)
            circuit.cu3(m.pi/2, 0, 0, q[qubitC], q[qubitT])
            circuit.cx(q[qubitT], q[qubitC])
            qubitT = qubitT + 1
        else: #otherwise
            theta = 2*np.arccos(m.sqrt(nn/mm))
            circuit.cu3(theta, 0, 0, q[qubitC], q[qubitT])
            circuit.cx(q[qubitT], q[qubitC])

            qubitTRecurse = qubitT #saving target qubit index, used as control qubit for lower child
            qubitT = qubitT + 1

            a = m.floor(nn/2)
            b = m.floor(mm/2)
            c = m.ceil(nn/2)
            d = m.ceil(mm/2)

            if a == 1 and b == 1: #upper child (1,1) => (1,2) became upper child
                circuit = _w_state_circuit(circuit, 1, 2, qubitC)
                #there is no lower child
            elif c == 1 and d == 1: #lower child (1,1) => (1,2) became lower child
                circuit = _w_state_circuit(circuit, 1, 2, qubitTRecurse)
                #there is no upper child
            else:
                #upper child
                circuit = _w_state_circuit(circuit, a, b, qubitC)
                #lower child
                circuit = _w_state_circuit(circuit, c, d, qubitTRecurse)

            return circuit

    qc = _w_state_circuit(qc, m, n, 0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math as m

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

    circuit.x(0)

    qubitT = 1

    def _w_state_circuit(circuit, nn, mm, qubitC):

        if nn == 0 and mm == 1:
            pass #do nothing in this case
        elif nn == 1 and mm == 2: #case (1,2)
            circuit.cu3(m.pi/2, 0, 0, q[qubitC], q[qubitT])
            circuit.cx(q[qubitT], q[qubitC])
            qubitT = qubitT + 1
        else: #otherwise
            theta = 2*np.arccos(m.sqrt(nn/mm))
            circuit.cu3(theta, 0, 0, q[qubitC], q[qubitT])
            circuit.cx(q[qubitT], q[qubitC])

            qubitTRecurse = qubitT #saving target qubit index, used as control qubit for lower child
            qubitT = qubitT + 1

            a = m.floor(nn/2)
            b = m.floor(mm/2)
            c = m.ceil(nn/2)
            d = m.ceil(mm/2)

            if a == 1 and b == 1: #upper child (1,1) => (1,2) became upper child
                circuit = _w_state_circuit(circuit, 1, 2, qubitC)
                #there is no lower child
            elif c == 1 and d == 1: #lower child (1,1) => (1,2) became lower child
                circuit = _w_state_circuit(circuit, 1, 2, qubitTRecurse)
                #there is no upper child
            else:
                #upper child
                circuit = _w_state_circuit(circuit, a, b, qubitC)
                #lower child
                circuit = _w_state_circuit(circuit, c, d, qubitTRecurse)

            return circuit

    qc = _w_state_circuit(qc, m.floor(n/2), n, 0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math as m

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

    circuit.x(0)

    qubitT = 1

    def _w_state_circuit(circuit, nn, mm, qubitC):

        if nn == 0 and mm == 1:
            pass #do nothing in this case
        elif nn == 1 and mm == 2: #case (1,2)
            circuit.cu3(m.pi/2, 0, 0, qubitC, qubitT)
            circuit.cx(qubitT, qubitC)
            qubitT = qubitT + 1
        else: #otherwise
            theta = 2*np.arccos(m.sqrt(nn/mm))
            circuit.cu3(theta, 0, 0, qubitC, qubitT)
            circuit.cx(qubitT, qubitC)

            qubitTRecurse = qubitT #saving target qubit index, used as control qubit for lower child
            qubitT = qubitT + 1

            a = m.floor(nn/2)
            b = m.floor(mm/2)
            c = m.ceil(nn/2)
            d = m.ceil(mm/2)

            if a == 1 and b == 1: #upper child (1,1) => (1,2) became upper child
                circuit = _w_state_circuit(circuit, 1, 2, qubitC)
                #there is no lower child
            elif c == 1 and d == 1: #lower child (1,1) => (1,2) became lower child
                circuit = _w_state_circuit(circuit, 1, 2, qubitTRecurse)
                #there is no upper child
            else:
                #upper child
                circuit = _w_state_circuit(circuit, a, b, qubitC)
                #lower child
                circuit = _w_state_circuit(circuit, c, d, qubitTRecurse)

            return circuit

    qc = _w_state_circuit(qc, m.floor(n/2), n, 0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math as m

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

    qc.x(0)

    qubitT = 1

    def _w_state_circuit(circuit, nn, mm, qubitC):

        if nn == 0 and mm == 1:
            pass #do nothing in this case
        elif nn == 1 and mm == 2: #case (1,2)
            circuit.cu3(m.pi/2, 0, 0, qubitC, qubitT)
            circuit.cx(qubitT, qubitC)
            qubitT = qubitT + 1
        else: #otherwise
            theta = 2*np.arccos(m.sqrt(nn/mm))
            circuit.cu3(theta, 0, 0, qubitC, qubitT)
            circuit.cx(qubitT, qubitC)

            qubitTRecurse = qubitT #saving target qubit index, used as control qubit for lower child
            qubitT = qubitT + 1

            a = m.floor(nn/2)
            b = m.floor(mm/2)
            c = m.ceil(nn/2)
            d = m.ceil(mm/2)

            if a == 1 and b == 1: #upper child (1,1) => (1,2) became upper child
                circuit = _w_state_circuit(circuit, 1, 2, qubitC)
                #there is no lower child
            elif c == 1 and d == 1: #lower child (1,1) => (1,2) became lower child
                circuit = _w_state_circuit(circuit, 1, 2, qubitTRecurse)
                #there is no upper child
            else:
                #upper child
                circuit = _w_state_circuit(circuit, a, b, qubitC)
                #lower child
                circuit = _w_state_circuit(circuit, c, d, qubitTRecurse)

            return circuit

    qc = _w_state_circuit(qc, m.floor(n/2), n, 0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math as m

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

    qc.x(0)

    qubitT = 1

    def _w_state_circuit(circuit, nn, mm, qubitC):

        if nn == 0 and mm == 1:
            pass #do nothing in this case
        elif nn == 1 and mm == 2: #case (1,2)
            circuit.cu(m.pi/2, 0, 0, qubitC, qubitT)
            circuit.cx(qubitT, qubitC)
            qubitT = qubitT + 1
        else: #otherwise
            theta = 2*np.arccos(m.sqrt(nn/mm))
            circuit.cu(theta, 0, 0, qubitC, qubitT)
            circuit.cx(qubitT, qubitC)

            qubitTRecurse = qubitT #saving target qubit index, used as control qubit for lower child
            qubitT = qubitT + 1

            a = m.floor(nn/2)
            b = m.floor(mm/2)
            c = m.ceil(nn/2)
            d = m.ceil(mm/2)

            if a == 1 and b == 1: #upper child (1,1) => (1,2) became upper child
                circuit = _w_state_circuit(circuit, 1, 2, qubitC)
                #there is no lower child
            elif c == 1 and d == 1: #lower child (1,1) => (1,2) became lower child
                circuit = _w_state_circuit(circuit, 1, 2, qubitTRecurse)
                #there is no upper child
            else:
                #upper child
                circuit = _w_state_circuit(circuit, a, b, qubitC)
                #lower child
                circuit = _w_state_circuit(circuit, c, d, qubitTRecurse)

            return circuit

    qc = _w_state_circuit(qc, m.floor(n/2), n, 0)

    return qc
'''


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

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

    qc.x(0)

    def _w_state_circuit(circuit, nn, mm, qubitC):
        global qubitT

        if nn == 0 and mm == 1:
            pass #do nothing in this case
        elif nn == 1 and mm == 2: #case (1,2)
            circuit.cu(m.pi/2, 0, 0, 0, qubitC, qubitT)
            circuit.cx(qubitT, qubitC)
            qubitT = qubitT + 1
        else: #otherwise
            theta = 2*np.arccos(m.sqrt(nn/mm))
            circuit.cu(theta, 0, 0, 0, qubitC, qubitT)
            circuit.cx(qubitT, qubitC)

            qubitTRecurse = qubitT #saving target qubit index, used as control qubit for lower child
            qubitT = qubitT + 1

            a = m.floor(nn/2)
            b = m.floor(mm/2)
            c = m.ceil(nn/2)
            d = m.ceil(mm/2)

            if a == 1 and b == 1: #upper child (1,1) => (1,2) became upper child
                circuit = _w_state_circuit(circuit, 1, 2, qubitC)
                #there is no lower child
            elif c == 1 and d == 1: #lower child (1,1) => (1,2) became lower child
                circuit = _w_state_circuit(circuit, 1, 2, qubitTRecurse)
                #there is no upper child
            else:
                #upper child
                circuit = _w_state_circuit(circuit, a, b, qubitC)
                #lower child
                circuit = _w_state_circuit(circuit, c, d, qubitTRecurse)

        return circuit

    global qubitT
    qubitT = 1
    qc = _w_state_circuit(qc, m.floor(n/2), n, 0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    # Paso 1: Aplicar Ry al primer qubit
    theta = 2 * math.acos(1 / math.sqrt(n))
    qc.ry(theta, 0)

    # Paso 2: Aplicar puertas controladas de Hadamard desde el primer qubit a los demás
    if n >= 3:
        for i in range(1, n):
            qc.ch(0, i)

        # Paso 3: Aplicar una cadena de puertas CNOT entre los qubits 1 a n-1
        for i in range(1, n-1):
            qc.cx(i, i+1)

    # Paso 4: Aplicar puertas CNOT desde el primer qubit a los demás
    for i in range(1, n):
        qc.cx(0, i)

    # Paso 5: Aplicar una puerta X al primer qubit
    qc.x(0)

    return qc
'''


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

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

    for i in range(n - 1):
        theta = 2 * acos(1 / sqrt(n - i))
        qc.cry(theta, i, i + 1)
        qc.cx(i + 1, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector
import numpy as np

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

    desired_state = np.zeros(2**n)
    for i in range(n):
        desired_state[1 << i] = 1 / np.sqrt(n)

    qc.initialize(desired_state)

    return qc
'''


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

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

    qc.append(HGate(), [0])

    for i in range(1, n):
        qc.append(CXGate(), [0, i])

    qc.global_phase = np.arccos(1 / np.sqrt(n))

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    # Create the superposition state
    for i in range(n):
        qc.x(i)          # Flip the i-th qubit to |1>
        qc.h(i)          # Apply Hadamard to create superposition
        qc.x(i)          # Flip back to |0>

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
	qc = QuantumCircuit(n)
	qc.x(0)

	def solve_for(l, r):
		if r - l == 1:
			return
		m = l + r >> 1
		theta = 2 * acos(sqrt(m - l) / sqrt(r - l))
		qc.cry(theta, l, m)
		qc.cx(m, l)
		solve_for(l, m)
		solve_for(m, r)

	solve_for(0, n)
	return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    x =  math.sqrt(1/3)
    y =  math.sqrt(2/3)

    theta = 2 * math.atan(y/x)
    qc.ry(theta, 1)
    qc.ch(1,2)
    qc.x(0)
    qc.cx(1,0)
    qc.cx(2,1)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    x =  math.sqrt(1/3)
    y =  math.sqrt(2/3)

    theta = 2 * math.atan(y/x)
    qc.ry(theta, 1)
    qc.ch(1,2)
    qc.x(0)
    qc.cx(1,0)
    qc.cx(2,1)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n, 1, -1):
        x =  math.sqrt(1/i)
        y =  math.sqrt(i-1/i)
        theta = 2 * math.atan(y/x)
        qc.ry(theta, 1)

    qc.x(n-1)
    qc.cx(0,n-1)
    for i in range(n-1):
        qc.cx(i+1,i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n, 1, -1):
        x =  math.sqrt(1/i)
        y =  math.sqrt(i-1/i)
        theta = 2 * math.atan(y/x)
        qc.ry(theta, 1)

    qc.x(0)
    qc.cx(1,0)
    qc.cx(2,1)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n, 1, -1):
        x =  math.sqrt(1/i)
        y =  math.sqrt(i-1/i)
        theta = 2 * math.atan(y/x)
        qc.ry(theta, n-i)

    qc.x(n-1)
    qc.cx(0,n-1)
    for i in range(n-1):
        qc.cx(i+1,i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n, 1, -1):
        x =  math.sqrt(1/i)
        y =  math.sqrt(i-1/i)
        theta = 2 * math.atan(y/x)
        qc.ry(theta, n - i)

    qc.x(n-1)
    qc.cx(0,n-1)
    for i in range(n-1):
        qc.cx(i+1,i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n, 1, -1):
        x =  math.sqrt(1/i)
        y =  math.sqrt(i-1/i)
        theta = 2 * math.atan(y/x)
        if i == n:
            qc.ry(theta, n - i)
        else:
            qc.cry(theta, n-i-1, n - i)

    qc.x(n-1)
    qc.cx(0,n-1)
    for i in range(n-1):
        qc.cx(i+1,i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n, 2, -1):
        x =  math.sqrt(1/i)
        y =  math.sqrt(i-1/i)
        theta = 2 * math.atan(y/x)
        if i == n:
            qc.ry(theta, n - i)
        else:
            qc.cry(theta, n-i-1, n - i)

    qc.x(n-1)
    qc.cx(0,n-1)
    for i in range(n-1):
        qc.cx(i+1,i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n, 1, -1):
        x =  math.sqrt(1/i)
        y =  math.sqrt(i-1/i)
        theta = 2 * math.atan(y/x)
        if i == n:
            qc.ry(theta, n - i)
        else:
            qc.cry(theta, n-i-1, n - i)

    qc.x(n-1)
    qc.cx(0,n-1)
    for i in range(n-2):
        qc.cx(i+1,i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n, 1, -1):
        x =  math.sqrt(1/i)
        y =  math.sqrt(i-1/i)
        theta = 2 * math.atan(y/x)
        if i == n:
            qc.ry(theta, n - i)
        else:
            qc.cry(theta, n-i-1, n - i)

    qc.x(n-1)
    qc.cx(0,n-1)
    for i in range(n-):
        qc.cx(i+1,i)

    return qc

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


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    x =  math.sqrt(1/3)
    y =  math.sqrt(2/3)
    theta = 2 * math.atan(y/x)
    qc.ry(theta, 0)
    x =  math.sqrt(1/2)
    y =  math.sqrt(1/2)
    theta = 2 * math.atan(y/x)
    qc.cry(theta, 0, 1)

    qc.x(2)
    qc.cx(0,2)
    qc.cx(1,0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    x =  math.sqrt(1/3)
    y =  math.sqrt(2/3)
    theta = 2 * math.atan(y/x)
    qc.ry(theta, 0)
    x =  math.sqrt(1/2)
    y =  math.sqrt(1/2)
    theta = 2 * math.atan(y/x)
    qc.cry(theta, 0, 1)

    qc.x(2)
    qc.cx(0,2)
    qc.cx(1,0,)

    return qc

    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        x =  math.sqrt(1/n-i)
        y =  math.sqrt(n-i-1/n-i)
        theta = 2 * math.atan(y/x)
        if i == 0:
            qc.ry(theta, i)
        else:
            qc.cry(theta, i-1, i)

    qc.x(n-1)
    qc.cx(0,n-1)
    for i in range(n-2):
        qc.cx(i+1,i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:

    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        x =  math.sqrt(1/n-i)
        y =  math.sqrt(n-i-1/n-i)
        theta = 2 * math.atan(y/x)
        if i == 0:
            qc.ry(theta, i)
        else:
            qc.cry(theta, i-1, i)

    qc.x(n-1)
    qc.cx(0,n-1)
    for i in range(n-2):
        qc.cx(i+1,i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n-1):
        x =  math.sqrt(1/n-i)
        y =  math.sqrt(n-i-1/n-i)
        theta = 2 * math.atan(y/x)
        if i == 0:
            qc.ry(theta, i)
        else:
            qc.cry(theta, i-1, i)

    qc.x(n-1)
    qc.cx(0,n-1)
    for i in range(n-2):
        qc.cx(i+1,i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range( n - 1 ):
        x =  math.sqrt(1/ (n-i))
        y =  math.sqrt((n-i-1)/(n-i))
        theta = 2 * math.atan(y/x)
        if i == 0:
            qc.ry(theta, i)
        else:
            qc.cry(theta, i-1, i)

    qc.x(n-1)
    qc.cx(0,n-1)
    for i in range(n-2):
        qc.cx(i+1,i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    theta 2*math.atan(math.sqrt(n-1))
    qc.ry(theta,0)
    for i in range(0,n-1):
        theta = 2 * math.atan(math.sqrt(n-i))
        qc.cry(theta,i,i+1)
        qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    theta 2 * math.atan(math.sqrt(n-1))
    qc.ry(theta,0)
    for i in range(n-1):
        theta = 2 * math.atan(math.sqrt(n-i-1))
        qc.cry(theta,i,i+1)
        qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    theta 2 * math.atan(math.sqrt(n-1))
    qc.ry(theta,0)
    for i in range(n-1):
        theta = 2 * math.atan(math.sqrt(n-i-))
        qc.cry(theta,[i],i+1)
        qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    theta = 2 * math.atan(math.sqrt(n-1))
    qc.ry(theta,0)

    for i in range(n-1):
        theta = 2 * math.atan(math.sqrt(n-i-1))
        qc.cry(theta,i,i+1)
        qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    theta = 2 * math.atan(math.sqrt(n-1))
    qc.ry(theta,0)

    for i in range(n-1):
        theta = 2 * math.atan(math.sqrt(n-i-2))
        qc.cry(theta,i,i+1)
        qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    theta = 2 * math.atan(math.sqrt(n-1))
    qc.ry(theta,0)

    for i in range(n-2):
        theta = 2 * math.atan(math.sqrt(n-i-2))
        qc.cry(theta,i,i+1)
        qc.x(i)
    qc.ch(n-2,n-1)
    qc.x(n-2)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    theta = 2 * math.atan(math.sqrt(n-1))
    qc.ry(theta,0)

    for i in range(n-2):
        theta = 2 * math.atan(math.sqrt(n-i-2))
        qc.cry(theta,i,i+1)
    for i in range(n-1)[::-1]:
        qc.cx(i+1,i)
    qc.x(0)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    theta = 2 * math.atan(math.sqrt(n-1))
    qc.ry(theta,0)

    for i in range(n-2):
        theta = 2 * math.atan(math.sqrt(n-i-2))
        qc.cry(theta,i,i+1)

    qc.(n-2,n-1)
    for i in range(n-2)[::-1]:
        qc.cx(i,i+1)
    qc.x(0)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    theta = 2 * math.atan(math.sqrt(n-1))
    qc.ry(theta,0)

    for i in range(n-2):
        theta = 2 * math.atan(math.sqrt(n-i-2))
        qc.cry(theta,i,i+1)

    qc.cx(n-2,n-1)
    for i in range(n-2)[::-1]:
        qc.cx(i,i+1)
    qc.x(0)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    # Rotación a cada qubit
    theta = 2 * acos(1 / sqrt(n))  # Angulo
    for qubit in range(n):
        qc.ry(theta, qubit)

    return qc
'''


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

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

    # Aplicamos la puerta CNOT
    for qubit in range(1, n):
        qc.cx(0, qubit)

    return qc
'''


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

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

    # Step 1: Apply Ry rotation on the first qubit
    angle = 2 * np.arcsin(np.sqrt(1.0 / n))
    qc.ry(angle, 0)

    # Step 2: Apply controlled Ry rotations on remaining qubits
    for i in range(1, n):
        angle = 2 * np.arcsin(np.sqrt(1.0 / (n - i)))

        # Apply a multi-controlled Ry gate
        qc.mcry(angle, list(range(i)), i)

    return qc
'''


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

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

    # Step 1: Apply Ry rotation on the first qubit
    angle = 2 * np.arcsin(np.sqrt(1.0 / n))
    qc.ry(angle, 0)

    # Step 2: Apply controlled Ry rotations on remaining qubits
    for i in range(1, n):
        angle = 2 * np.arcsin(np.sqrt(1.0 / (n - i)))

        # Apply a multi-controlled phase gate
        mc_phase_gate = MCPhaseGate(angle, num_ctrl_qubits=i)
        qc.append(mc_phase_gate, list(range(i)) + [i])

    return qc
'''


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

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

    # Step 1: Apply Ry rotation on the first qubit
    angle = 2 * np.arcsin(np.sqrt(1.0 / n))
    qc.ry(angle, 0)

    # Step 2: Apply controlled Ry rotations on remaining qubits
    for i in range(1, n):
        angle = 2 * np.arcsin(np.sqrt(1.0 / (n - i)))
        qc.mcz(angle, list(range(i)), i)
    return qc
'''


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

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

    # Step 1: Apply Ry rotation on the first qubit
    angle = 2 * np.arcsin(np.sqrt(1.0 / n))
    qc.ry(angle, 0)

    # Step 2: Apply controlled Ry rotations on remaining qubits
    for i in range(1, n):
        angle = 2 * np.arcsin(np.sqrt(1.0 / (n - i)))
        qc.mcrz(angle, list(range(i)), i)
    return qc
'''


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

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

    for k in range(n):
        theta = 2 * math.asin(1 / math.sqrt(n - k))
        if k == 0:
            qc.ry(theta, 0)
        else:
            # Apply X to qubits 0..k-1
            for i in range(k):
                qc.x(i)
            # Create a controlled Ry gate with k controls (all must be 1)
            cry_gate = RYGate(theta).control(k, ctrl_state='1'*k)
            qc.append(cry_gate, list(range(k)) + [k])
            # Apply X again to qubits 0..k-1
            for i in range(k):
                qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

from qiskit import QuantumCircuit, QuantumRegister
import numpy as np
import math

def solve() -> QuantumCircuit:
    # qc = QuantumCircuit(3)z
    # # Write your code here:
    # q = QuantumRegister(3)

    def F_gate(circ,q,i,j,n,k) :
        theta = np.arccos(np.sqrt(1/(n-k+1)))
        circ.ry(-theta,q[j])
        circ.cz(q[i],q[j])
        circ.ry(theta,q[j])
        circ.barrier(q[i])
    # Define the cxrv gate which uses reverse CNOT instead of CNOT
    def  cxrv(circ,q,i,j) :
        circ.h(q[i])
        circ.h(q[j])
        circ.cx(q[j],q[i])
        circ.h(q[i])
        circ.h(q[j])
        circ.barrier(q[i],q[j])

    # 3-qubit W state
    flag_qx2 = True

    n = 4
    q_w = QuantumRegister(n)

    qc = QuantumCircuit(q_w)

    qc.x(q_w[2]) #start is |100>
    F_gate(qc,q_w,2,1,3,1) # Applying F12
    F_gate(qc,q_w,1,0,3,2) # Applying F23

    if flag_qx2 : # option ibmqx2
        qc.cx(q_w[1],q_w[2]) # cNOT 21
        qc.cx(q_w[0],q_w[1]) # cNOT 32

    else :        # option ibmqx4
        cxrv(qc,q_w,1,2)
        cxrv(qc,q_w,0,1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

from qiskit import QuantumCircuit, QuantumRegister
import numpy as np
import math

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

    qc = QuantumCircuit(q_w)

    def F_gate(circ,q,i,j,n,k) :
        theta = np.arccos(np.sqrt(1/(n-k+1)))
        circ.ry(-theta,q[j])
        circ.cz(q[i],q[j])
        circ.ry(theta,q[j])
        circ.barrier(q[i])
    # Define the cxrv gate which uses reverse CNOT instead of CNOT
    def  cxrv(circ,q,i,j) :
        circ.h(q[i])
        circ.h(q[j])
        circ.cx(q[j],q[i])
        circ.h(q[i])
        circ.h(q[j])
        circ.barrier(q[i],q[j])

    # 4-qubit W state
    # n = 4
    # q = QuantumRegister(n)
    # c = ClassicalRegister(n)
    # W_states = QuantumCircuit(q,c)

    qc.x(q[3]) #start is |1000>
    F_gate(qc,q,3,2,4,1) # Applying F12
    F_gate(qc,q,2,1,4,2) # Applying F23
    F_gate(qc,q,1,0,4,3) # Applying F34
    cxrv(qc,q,2,3) # cNOT 21
    qc.cx(q[1],q[2]) # cNOT 32
    qc.cx(q[0],q[1]) # cNOT 43
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    for i in range(0, n-1):
        theta = math.atan(math.sqrt(n-i-1)) * 2
        qc.cry(theta, i, i+1)
        qc.cx(i+1, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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


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

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

    theta = 2*atan(sqrt(n-1))
    qc.ry(theta, 0)
    for i in range(1, n-1):
        theta = 2*atan(sqrt(n-1-i))
        qc.cry(theta, i-1, i)
    for i in range(0, n-1):
        qc.cx(n-i-2, n-i-1)
    qc.x(0)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.ry(math.asin(1/math.sqrt(n))*2, 0)
    for i in range(1, n):
        for j in range(i):
            qc.x(j)
        qc.append(RYGate(math.asin(1/math.sqrt(n-i))*2).control(i), range(i+1))
        for j in range(i):
            qc.x(j)
    return qc

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


'''python
from qiskit import QuantumCircuit
import math

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

    # Write your code here:
    theta_list=[0,0]
    for i in range(2,n+1):
        theta_list.append(4*math.atan(math.sqrt(i*(i-1))/(i+math.sqrt(i))))

    qc.ry(theta_list[n],0)
    for i in range(n-2):
        qc.cry(theta_list[n-i-1],i,i+1)
    for i in range(n-1)[::-1]:
        qc.cx(i,i+1)
    qc.x(0)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    qc.h(0)

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

    qc.cx(n - 1)
    qc.cx(0, n - 1)

    for i in range(1, n - 1)
        qc.cx(i, i - 1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    qc.h(0)

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

    qc.x(n - 1)
    qc.cx(0, n - 1)

    for i in range(1, n - 1)
        qc.cx(i, i - 1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    qc.h(0)

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

    qc.x(n - 1)
    qc.cx(0, n - 1)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    qc.h(0)

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

    qc.x(n - 1)
    qc.cx(0, n - 1)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    th = 2 * math.atan(math.sqrt(n - 1))
    qc.ry(th, 0)

    for i in range(n - 1):
        th = 2 * math.atan(math.sqrt(n - 2 - i))
        qc.cry(th, i, i + 1)

    qc.x(n - 1)
    qc.cx(0, n - 1)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    th = 2 * math.atan(math.sqrt(n - 1))
    qc.ry(th, 0)

    for i in range(n - 1):
        th = 2 * math.atan(math.sqrt(n - 2 - i))
        qc.cry(th, i, i + 1)

    qc.x(n - 1)
    qc.cx(0, n - 1)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1):
        p = math.sqrt(1/(n-i))
        qc.ry(2*math.acos(p), i)
        qc.cx(i, i+1)
        qc.x(i)

    qc.ch(n-1, n)
    qc.cx(n, n-1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1):
        p = math.sqrt(1/(n-i))
        qc.ry(2*math.acos(p), i)
        qc.cx(i, i+1)
        qc.x(i)

    qc.ch(n-2, n-1)
    qc.cx(n-1, n-2)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1):
        p = math.sqrt(1/(n-i))
        qc.ry(2*math.acos(p), i)
        qc.cx(i, i+1)
        qc.x(i)

    # qc.ch(n-2, n-1)
    # qc.cx(n-, n-2)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1):
        p = math.sqrt(1/(n-i))
        qc.ry(2*math.acos(p), i)
        qc.cx(i, i+1)
        qc.x(i)

    qc.ch(n-2, n-1)
    qc.cx(n-, n-2)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1):
        p = math.sqrt(1/(n-i))
        qc.ry(2*math.acos(p), i)
        qc.cx(i, i+1)
        qc.x(i)

    qc.ch(n-2, n-1)
    qc.cx(n-1, n-2)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    theta = 2 * math.acos(1 / math.sqrt(n))

    qc.x(n - 1)
    qc.ry(theta, 0)

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

    qc.cx(0, n - 1)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    'theta = 2 * math.acos(1 / math.sqrt(n))'

    qc.x(n - 1)
    qc.ry(theta, 0)

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

    qc.cx(0, n - 1)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:

    qc = QuantumCircuit(n)

    theta = 2 * math.acos(1 / math.sqrt(n))

    qc.x(n - 1)

    qc.ry(theta, 0)

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

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

    return qc
'''


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

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

    qc.x(0)
    split(qc, 0, n)

    return qc

def split(qc, stIncl, edExcl):
    if stIncl + 1 == edExcl:
        return

    mid = (stIncl + edExcl) // 2
    left = mid - stIncl
    right = edExcl - mid
    print(f'{left=} {right=}')

    angle = 2*math.atan(math.sqrt(right/left))
    qc.cry(angle, stIncl, mid)
    qc.cx(mid, stIncl)

    split(qc, stIncl, mid)
    split(qc, mid, edExcl)

    return qc
'''


'''python
from qiskit import QuantumCircuit

import math

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

    theta = [2 * math.atan(math.sqrt(i)) for i in range(n - 1, 0, -1)]

    qc.x(0)

    for i in range(n - 1):
        qc.cry(theta[i], i, i + 1)
        qc.cx(i + 1, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from math import asin

def move(qc, f, t, ratio):
    theta = 2 * asin(ratio**0.5)
    qc.cry(theta, f, t)
    qc.cx(t, f)

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

    qc.x(0)

    parent = [None]
    for i in range(1, n):
        for r in (8, 4, 2, 1):
            if i & r:
                parent.append(i - r)
                break

    # print(parent)

    ww = [0] * n
    for i in range(n - 1, -1, -1):
        ww[i] += 1 / n
        if parent[i] is not None:
            ww[parent[i]] += ww[i]

    # print(ww)

    xx = [0] * n
    xx[0] = 1

    for i in range(1, n):
        p = parent[i]
        move(qc, p, i, ww[i] / xx[p])
        xx[p] -= ww[i]
        xx[i] = ww[i]

    return qc
'''


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

"""
You can apply oracle as follows:
qc.compose(o, inplace=True)
"""

def diffusion_oracle(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.h(i)
        qc.x(i)
    qc.append(ZGate().control(n - 1), range(n))
    for i in range(n):
        qc.x(i)
        qc.h(i)
    return qc

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.x(0)

    count = 1

    # queue = [(a, b, control bit of CRy), ...]
    queue = [(n // 2, n, 0)]

    # breadth first search
    while len(queue):
        a, b, control = queue.pop(0)

        if a == 0:
            continue

        theta = 2 * math.atan(math.sqrt((b - a) / a))

        qc.cry(theta, control, count)
        qc.cx(count, control)

        queue.append(((b // 2) // 2, b // 2, control))
        queue.append((math.ceil(b / 2) // 2, math.ceil(b / 2), count))

        count += 1

    return qc

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

    n = 3
    qc = solve(n)
    sv = Statevector(qc)
    print(sv)
    print(qc)
    print(f"{qc.depth() = }")
    # sv = Statevector.from_label('01000')
    # print(sv.evolve(qc))
'''


'''python
from qiskit import QuantumCircuit
import math

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

    qc.x(0)

    for i in range(1, n):
        qc.cry(2 * math.acos(1.0 / math.sqrt(n - i + 1)), i - 1, i)

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

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(0)
    for i in range(n-1):
        t = 2*asin(sqrt(1/(n-i)))
        qc.cry(t, 0, i+1)
        qc.cx(i+1, 0)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(0)
    for i in range(n-1):
        t = 2*asin(sqrt(1/(n-i)))
        qc.cry(t, 0, i+1)
        qc.cx(i+1, 0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(0)
    for i in range(n-1):
        print(i)
        theta = 2 * math.atan(math.sqrt(n - i - 1))
        print(math.cos(theta/2))
        qc.cry(theta, i, i+1)
        qc.cx(i+1, i)
    return qc
'''


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

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

    qc.x(0)
    for i in range(n - 1):
        theta = 2 * numpy.arccos(math.sqrt(1/(n - i)))
        qc.cry(theta, i, i + 1)

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

    return qc
'''


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

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

    theta = 2 * np.arcsin(np.sqrt(1/3))
    qc.ry(theta, 0)
    qc.cx(0, 1)
    qc.cx(0, 2)

    theta = 2 * np.arcsin(np.sqrt(1/n))
    qc.ry(theta, 0)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    # 回転角 θ を計算 (θ = 2 * arcsin(√(1/n)))
    theta = 2 * math.asin(math.sqrt(1/n))

    # 第1量子ビットに Ry ゲートを適用して重ね合わせを作成
    qc.ry(theta, 0)

    # 第1量子ビットが 1 のとき、他の量子ビットにエンタングルメントを作成
    for i in range(1, n):
        qc.cx(0, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    qc.x(0)
    for i in range(n-1):
        theta = 2 * math.acos((n-i)**-0.5)
        qc.cry(theta, i, i+1)
        qc.cx(i+1, 0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    qc.x(0)
    for i in range(n-1):
        theta = 2 * math.acos((n-i)**-0.5)
        qc.cry(theta, i, i+1)
        qc.cx(i+1, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(0)
    for i in range(1,n):
        theta = math.atan(1/math.sqrt(n-i))*2
        qc.cry(theta, 0, i)
        qc.cx(i,0)
    qc.x(0)
    for i in range(1,n):
        qc.cx(i,0)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(0)
    for i in range(1,n):
        theta = math.atan(math.sqrt(n-i))*2
        qc.cry(theta, i - 1, i)
        qc.cx(i,i - 1)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(0)
    for i in range(1,n):
        theta = math.atan(math.sqrt(n-i))*2
        qc.cry(theta, i-1, i)
        qc.cx(i,i-1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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

    qc.ry(2 * math.acos(1 / math.sqrt(n)), 0)

    for i in range(1, n):
        angle = 2 * math.acos(math.sqrt((n - i) / (n - i + 1)))
        qc.cry(angle, i - 1, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    qc.ry(2 * math.acos(1 / math.sqrt(n)), 0)

    for i in range(1, n):
        angle = 2 * math.acos(math.sqrt((n - i) / (n - i + 1)))
        qc.cry(angle, i - 1, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import mat

def F_gate(circ,q,i,j,n,k) :
    theta = math.acos(math.sqrt(1/(n-k+1)))
    circ.ry(-theta,q[j])
    circ.cz(q[i],q[j])
    circ.ry(theta,q[j])
    circ.barrier(q[i])

def  cxrv(circ,q,i,j) :
    circ.h(q[i])
    circ.h(q[j])
    circ.cx(q[j],q[i])
    circ.h(q[i])
    circ.h(q[j])
    circ.barrier(q[i],q[j])

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


'''python
from qiskit import QuantumCircuit, QuantumRegister
import math

def F_gate(circ,q,i,j,n,k) :
    theta = math.acos(math.sqrt(1/(n-k+1)))
    circ.ry(-theta,q[j])
    circ.cz(q[i],q[j])
    circ.ry(theta,q[j])
    circ.barrier(q[i])

def  cxrv(circ,q,i,j) :
    circ.h(q[i])
    circ.h(q[j])
    circ.cx(q[j],q[i])
    circ.h(q[i])
    circ.h(q[j])
    circ.barrier(q[i],q[j])

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


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1):
        qc.ry(math.asin(1/math.sqrt(n-i)), i)
        qc.x(i)
        qc.cx(i,i+1)
        qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1):
        qc.ry(2*math.asin(1/math.sqrt(n-i)), i)
        qc.x(i)
        qc.cx(i,i+1)
        qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.ry(2*math.asin(1/math.sqrt(n)), 0)
    for i in range(n-1):
        qc.x(i)
        qc.cry(2*math.asin(1/math.sqrt(n-i-1)),i,i+1)
        qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    import numpy as np
    qc.ry(2 * np.arccos(1 / np.sqrt(n)), 0)
    for i in range(n-1):
        qc.ch(0, i+1)
        qc.cx(n-i-1, n-i-2)
    qc.x(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    import numpy as np
    def theta(k):
        return np.arccos(np.sqrt(1 / (n-k+1)))

    for k in range(n-1):
        qc.ry(- theta(k), k+1)
        qc.cz(k, k+1)
        qc.ry(theta(k), k+1)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.r(2*math.acos(1.0/math.sqrt(n)), math.pi/2, 0)
    for i in range(n-2):
      qc.crx(2*math.acos(1.0/math.sqrt(n-i-1)), i, i+1)
      qc.cp(math.pi/2, i, i+1)
    for i in range(n-1):
      qc.cx(n-2-i, n-1-i)
    qc.x(0)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    qc.x(0)
    for i in range(n-1):
        theta = 2 * math.acos(1 / math.sqrt(n - i))
        qc.cry(theta, i, i+1)
        qc.cx(i+1, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def calc(n):
    return math.acos(1/n**(1/2))

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        #前側をすべて1に
        for j in range(i):
            qc.x(j)
        theta = 2*calc(n-i)
        if i >= 1:
            qc.mcry(theta, list(range(i)), i+1)
        else:
            qc.ry(theta, 0)
        for j in range(i+1):
            qc.x(j)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def calc(n):
    return math.acos(1/n**(1/2))

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        #前側をすべて1に
        for j in range(i):
            qc.x(j)
        theta = 2*calc(n-i)
        if i >= 1:
            qc.mcry(theta, list(range(i)), i)
        else:
            qc.ry(theta, 0)
        for j in range(i+1):
            qc.x(j)
    return qc
'''


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

def calc(n):
    return math.acos(1/n**(1/2))

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        #前側をすべて1に
        for j in range(i):
            qc.x(j)
        theta = 2*calc(n-i)
        if i >= 1:
            ccry = RYGate(theta).control(num_ctrl_qubits=i, label=None)
            #qc.mcry(theta, list(range(i)), i)
            qc.append(ccry, list(range(i+1)))
        else:
            qc.ry(theta, 0)
        for j in range(i+1):
            qc.x(j)
    return qc
'''


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

def calc(n):
    return math.acos(1/n**(1/2))

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        #前側をすべて1に
        for j in range(i):
            qc.x(j)
        theta = 2*calc(n-i)
        if i >= 1:
            ccry = RYGate(theta).control(num_ctrl_qubits=i, label=None)
            #qc.mcry(theta, list(range(i)), i)
            qc.append(ccry, list(range(i+1)))
        else:
            qc.ry(theta, 0)
    for j in range(i+1):
        qc.x(j)
    return qc
'''


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

def calc(n):
    return math.acos(1/n**(1/2))

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        #前側0をすべて1に
        for j in range(i):
            qc.x(j)
        theta = 2*calc(n-i)
        if i >= 1:
            ccry = RYGate(theta).control(num_ctrl_qubits=i, label=None)
            #qc.mcry(theta, list(range(i)), i)
            qc.append(ccry, list(range(i+1)))
        else:
            qc.ry(theta, 0)
        for j in range(i):
            qc.x(j)
    return qc
'''


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

def calc(n):
    return math.acos(1/n**(1/2))

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1):
        #前側0をすべて1に
        for j in range(i):
            qc.x(j)
        theta = 2*calc(n-i)
        if i >= 1:
            ccry = RYGate(theta).control(num_ctrl_qubits=i, label=None)
            #qc.mcry(theta, list(range(i)), i)
            qc.append(ccry, list(range(i+1)))
        else:
            qc.ry(theta, 0)
        #0->1にしてたやつを戻す
        for j in range(i):
            qc.x(j)
    for i in range(n-1):
        qc.x(i)
    qc.mcx(list(range(n-1)), n-1)
    for i in range(n-1):
        qc.x(i)
    return qc
'''


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

def calc(n):
    return math.acos(1/n**(1/2))

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1):
        #前側0をすべて1に
        for j in range(i):
            qc.x(j)
        theta = 2*calc(n-i)
        if i >= 1:
            ccry = RYGate(theta).control(num_ctrl_qubits=i, label=None)
            #qc.mcry(theta, list(range(i)), i)
            qc.append(ccry, list(range(i+1)))
        else:
            qc.ry(theta, 0)
        qc.x(i)
        #0->1にしてたやつを戻す
        for j in range(i):
            qc.x(j)
    for i in range(n-1):
        qc.x(i)
    qc.mcx(list(range(n-1)), n-1)
    for i in range(n-1):
        qc.x(i)
    return qc
'''


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

def calc(n):
    return math.acos(1/n**(1/2))

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1):
        #前側0をすべて1に
        for j in range(i):
            qc.x(j)
        theta = 2*calc(n-i)
        if i >= 1:
            ccry = RYGate(theta).control(num_ctrl_qubits=i, label=None)
            #qc.mcry(theta, list(range(i)), i)
            qc.append(ccry, list(range(i+1)))
            qc.mcx(list(range(i)), i)
        else:
            qc.ry(theta, 0)
            qc.x(0)

        #0->1にしてたやつを戻す
        for j in range(i):
            qc.x(j)
    for i in range(n-1):
        qc.x(i)
    qc.mcx(list(range(n-1)), n-1)
    for i in range(n-1):
        qc.x(i)
    return qc
'''


'''python
import math
from qiskit import QuantumCircuit

def theta(k: int) -> float:
    return math.asin(math.sqrt((k-1)/k)) * 2

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.ry(theta(n), 0)
    for i in range(1, n):
        qc.x(i-1)
        qc.cry(theta(n-i), i-1, i)
        qc.x(i-1)
    return qc

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


'''python
import math
from qiskit import QuantumCircuit

def theta(k: int) -> float:
    return math.acos(math.sqrt((k-1)/k)) * 2

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.ry(theta(n), 0)
    for i in range(1, n):
        qc.cry(theta(n-i), i-1, i)
    for i in range(n):
        qc.x(i)
    return qc

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


'''python
import math
from qiskit import QuantumCircuit

def theta(k: int) -> float:
    return math.asin(math.sqrt((k-1)/k)) * 2

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

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


'''python
import math
from qiskit import QuantumCircuit

def theta(k: int) -> float:
    return math.asin(math.sqrt((k-1)/k)) * 2

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

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


'''python
import math
from qiskit import QuantumCircuit

def theta(k: int) -> float:
    return math.acos(math.sqrt((k-1)/k)) * 2

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

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


'''python
import math
from qiskit import QuantumCircuit

def theta(k: int) -> float:
    return math.acos(math.sqrt((k-1)/k)) * 2

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

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


'''python
import math
from qiskit import QuantumCircuit

def theta(k: int) -> float:
    return math.asin(math.sqrt((k-1)/k)) * 2

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

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


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    prob_amp = np.sqrt(1 / n)
    rot_ang = 2 * np.arccos(prob_amp)

    qc.x(0)

    for i in range(n - 1):
        comp_amp = np.sqrt(1 - i / n)
        rot_ang = 2 * np.arccos(prob_amp / (comp_amp))
        qc.cry(rot_ang, i, i + 1)
        qc.cx(i + 1, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    prob_amp = np.sqrt(1/n)
    rot_ang = 2*np.arccos(prob_amp)

    qc_w = QuantumCircuit(n)

    # probability redistribution
    qc_w.ry(rot_ang,0)

    for i in range(1,n-1):
        comp_amp = np.sqrt(1-i/n)
        rot_ang = 2*np.arccos(prob_amp/(comp_amp))
        qc_w.cry(rot_ang,i-1,i)

    # state reshuffling
    for i in range(n-1, 0, -1):
        qc_w.cx(i-1,i)

    qc_w.x(0)

    return qc_w
'''


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

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

    prob_amp = np.sqrt(1/n)
    rot_ang = 2*np.arccos(prob_amp)

    qc_w = QuantumCircuit(n)

    # probability redistribution
    qc_w.ry(rot_ang,0)

    for i in range(1,n-1):
        comp_amp = np.sqrt(1-i/n)
        rot_ang = 2*np.arccos(prob_amp/(comp_amp))
        qc_w.cry(rot_ang,i-1,i)

    # state reshuffling
    for i in range(n-1, 0, -1):
        qc_w.cx(i-1,i)

    qc_w.x(0)

    return qc_w
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(0)
    theta = [2 * math.atan(math.sqrt(i)) range(n - 1,0,-1)]
    for i in range(n - 1):
        qc.cry(theta[i],i,i + 1)
        qc.cx(i + 1,i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(0)
    theta = [2 * math.atan(math.sqrt(i)) for i in range(n - 1,0,-1)]
    for i in range(n - 1):
        qc.cry(theta[i],i,i + 1)
        qc.cx(i + 1,i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-2):
        theta = math.atan(math.sqrt(i-1))
        qc.ry(theta,i)
        qc.ch(i,i+1)
        qc.ccx(i,i+1,i+2)
        qc.cx(i,i+1)
        qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-2):
        theta = math.atan(math.sqrt(i-1))
        qc.ry(theta,i)
        qc.ch(i,i+1)
        qc.ccx(i,i+1,i+2)
        qc.cx(i,i+1)
        qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-3):
        theta = math.atan(math.sqrt(i-1))
        qc.ry(theta,i)
        qc.ch(i,i+1)
        qc.ccx(i,i+1,i+2)
        qc.cx(i,i+1)
        qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-2):
        x = math.sqrt(i-1)
        theta = math.atan(x)
        qc.ry(theta,i)
        qc.ch(i,i+1)
        qc.ccx(i,i+1,i+2)
        qc.cx(i,i+1)
        qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1):
        x = math.sqrt(i-1)
        theta = math.atan(x)
        qc.ry(theta,i)
        qc.ch(i,i+1)
        qc.ccx(i,i+1,i+2)
        qc.cx(i,i+1)
        qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-2):
        x = math.sqrt(i+1)
        theta = math.atan(x)
        qc.ry(theta,i)
        qc.ch(i,i+1)
        qc.ccx(i,i+1,i+2)
        qc.cx(i,i+1)
        qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-2):
        x = math.sqrt(i+2)
        theta = math.atan(x)
        qc.ry(theta,i)
        qc.ch(i,i+1)
        qc.ccx(i,i+1,i+2)
        qc.cx(i,i+1)
        qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-2):
        x = math.sqrt(n-i-2)
        theta = math.atan(x)
        qc.ry(theta,i)
        qc.ch(i,i+1)
        qc.ccx(i,i+1,i+2)
        qc.cx(i,i+1)
        qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-2):
        x = math.sqrt(n-i-1)
        theta = math.atan(x)
        qc.ry(theta,i)
        qc.ch(i,i+1)
        qc.ccx(i,i+1,i+2)
        qc.cx(i,i+1)
        qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    for i in range(n):
        theta = 2*math.acos(math.sqrt(1/(n-i)))
        qc.cry(theta, 0, i+1)
        qc.cx(i+1, 0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    for i in range(n-1):
        theta = 2*math.acos(math.sqrt(1/(n-i)))
        qc.cry(theta, 0, i+1)
        qc.cx(i+1, 0)

    return qc

solve(3).draw('mpl')
'''


'''python
from qiskit import QuantumCircuit
import math

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

    for i in range(n-1):
        theta = 2*math.acos(math.sqrt(1/(n-i)))
        qc.cry(theta, 0, i+1)
        qc.cx(i+1, 0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    for i in range(n-1):
        theta = 2*math.acos(math.sqrt((n-i-1)/(n-i)))
        qc.cry(theta, 0, i+1)
        qc.cx(i+1, 0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    stheta = 2 * atan(math.sqrt(n-1))
    qc.ry(stheta,0)
    for i in range(1,n-1):
        theta = 2*atan(math.sqrt(n-i-1))
        qc.cry(theta,i-1,i)
    #qc.x(n-1)
    for i in range(1,n):
        qc.cx(i,i-1)
    qc.x(n-1)
    for i in range(0,n-1):
        qc.cx(i,n-1)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    stheta = 2 * math.atan(math.sqrt(n-1))
    qc.ry(stheta,0)
    for i in range(1,n-1):
        theta = 2*math.atan(math.sqrt(n-i-1))
        qc.cry(theta,i-1,i)
    #qc.x(n-1)
    for i in range(1,n):
        qc.cx(i,i-1)
    qc.x(n-1)
    for i in range(0,n-1):
        qc.cx(i,n-1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    import math
    theta = 2 * math.atan(math.sqrt(2))
    qc.ry(theta, n - 1)
    qc.ch(n - 1, 0)
    for i in range(1, n - 1):
        qc.x(0)
        qc.x(n - 1)
        qc.ccx(0, n - 1, i)
        qc.x(n - 1)
        qc.x(0)

    qc.cx(0, n - 1)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    theta = 2 * math.acos(math.sqrt(1/n))
    qc.ry(theta, 0)
    for i in range(1, n):
        theta_i = 2 * math.acos(math.sqrt(1/(n-i)))
        qc.cry(theta_i, i-1, i)
        qc.cx(i, i-1)
    for i in range(0, n-1):
        qc.cx(i, n-1)
    qc.x(n-1)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    levels = int(np.ceil(np.log2(n)))
    qc.x(0)

    box = [0 for _ in range(n)]
    box[0] = n

    for level in range(levels):
        tmp = 2**level
        for i in range(tmp):
            parent = box[i]
            if parent == 1:
                continue

            left = parent//2
            right = parent - left

            prob_amp = np.sqrt(left / parent)
            rot_ang = 2 * np.arccos(prob_amp)

            for j in range(tmp, n):
                if box[j] == 0:
                    bridge = j
                    break

            qc.cry(rot_ang, i, bridge)
            qc.cx(bridge, i)

            box[i] = left
            box[bridge] = right

    return qc
'''


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

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

    qc.x(0)
    def f(l: int, r: int) -> None:
        if r - l == 1:
            return
        mid = (l + r) // 2
        qc.cry(2 * acos(sqrt((mid-l)/(r-l))), l, mid)
        qc.cx(mid, l)
        f(l, mid)
        f(mid, r)
    f(0, n)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n < 9:
      qc.ry(2*math.acos(1.0/math.sqrt(n)), 0)
      for i in range(n-2):
        qc.cry(2*math.acos(1.0/math.sqrt(n-i-1)), i, i+1)
      for i in range(n-1):
        qc.cry(-math.pi, n-2-i, n-1-i)
      qc.ry(-math.pi, 0)
    else:
      qc.ry(2*math.acos(math.sqrt(8.0/n)), 0)
      qc.cx(0, 8)
      qc.x(0)
      for i in range(7):
        qc.cry(2*math.acos(1.0/math.sqrt(8-i-1)), i, i+1)
      for i in range(7):
        qc.cry(-math.pi, i+1, i)
      for i in range(n-9):
        qc.cry(2*math.acos(1.0/math.sqrt(n-8-i)), i+8, i+9)
      for i in range(n-9):
        qc.cry(-math.pi, i+9, i+8)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n < 9:
      qc.ry(2*math.acos(1.0/math.sqrt(n)), 0)
      for i in range(n-2):
        qc.cry(2*math.acos(1.0/math.sqrt(n-i-1)), i, i+1)
      for i in range(n-1):
        qc.cry(-math.pi, n-2-i, n-1-i)
      qc.ry(-math.pi, 0)
    else:
      qc.ry(2*math.acos(math.sqrt(8.0/n)), 0)
      qc.cx(0, 8)
      qc.x(0)
      for i in range(7):
        qc.cry(2*math.acos(1.0/math.sqrt(8-i)), i, i+1)
      for i in range(7):
        qc.cry(-math.pi, i+1, i)
      for i in range(n-9):
        qc.cry(2*math.acos(1.0/math.sqrt(n-8-i)), i+8, i+9)
      for i in range(n-9):
        qc.cry(-math.pi, i+9, i+8)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1):
        qc.x(i)
    theta = math.acos(math.sqrt((n-1)/(n))) * 2
    qc.ry(theta, 0)
    for i in range(1, n-1):
        theta = math.acos(math.sqrt((n-i-1)/(n-i))) * 2
        qc.append(RYGate(theta).control(i), [_ for _ in range(i + 1)])
    qc.mcx([_ for _ in range(n - 1)], n - 1)
    for i in range(n-1):
        qc.x(i)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1):
        qc.x(i)
    theta = math.acos(math.sqrt((n-1)/(n))) * 2
    qc.ry(theta, 0)
    for i in range(1, n-1):
        theta = math.acos(math.sqrt((n-i-1)/(n-i))) * 2
        qc.append(RYGate(theta).control(i), [_ for _ in range(i + 1)])
    qc.mcx([_ for _ in range(n - 1)], n - 1)
    for i in range(n-1):
        qc.x(i)
    qc.z(n - 1)
    return qc
'''


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

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

    qc.x(0)
    split(qc, 0, n)

    return qc

def split(qc, stIncl, edExcl):
    if stIncl + 1 == edExcl:
        return

    mid = (stIncl + edExcl) // 2
    left = mid - stIncl
    right = edExcl - mid
    print(f'{left=} {right=}')

    angle = 2*math.atan(math.sqrt(right/left))
    qc.cry(angle, stIncl, mid)
    qc.cx(mid, stIncl)

    split(qc, stIncl, mid)
    split(qc, mid, edExcl)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    qc.x(0)

    for i in range(1, n):
        qc.cry(2 * math.acos(1.0 / math.sqrt(n - i + 1)), i - 1, i)

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

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n==2:
        qc.h(0)
        qc.x(0)
        qc.cx(0,1)
        qc.x(0)
        return qc
    else:
        qc.x(0)
        angle = [0] * (n+1)

        for i in range(n+1):
            if i==0:
                pass
            else:
                angle[i]=np.arccos(1/np.sqrt(i))

        for i in range(n-2):
            qc.cry(angle[n-i]*2,i,i+1)

        qc.cx(n-2,n-3)
        qc.ch(n-2,n-1)
        qc.cx(n-1,n-2)

        for i in range(n+1):
            if i<4:
                pass
            else:
                for j in range(i-1):
                    qc.cx(n-1-j,n-i)
        return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(0)
    theta = [2 * math.atan(math.sqrt(i)) for i in range(n - 1,0,-1)]
    for i in range(n - 1):
        qc.cry(theta[i],i,i + 1)
    for i in range(n - 1):
        qc.cx(i + 1,i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
impoort math

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

    # 各量子ビットに対して重ね合わせを作成
    for i in range(n):
        # 回転角度を計算 (θ = 2 * arcsin(1/sqrt(n)))
        theta = 2 * math.asin(1 / math.sqrt(n))
        qc.ry(theta, i)

    # 全ての量子ビットに制御をかけてエンタングルメントを作成
    for i in range(1, n):
        qc.cx(0, i)

    return qc
'''


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

def calc(n):
    return math.acos(1/n**(1/2))

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1):
        #前側0をすべて1に
        for j in range(i):
            qc.x(j)
        theta = 2*calc(n-i)
        if i >= 1:
            ccry = RYGate(theta).control(num_ctrl_qubits=i, label=None)
            #qc.mcry(theta, list(range(i)), i)
            qc.append(ccry, list(range(i+1)))
            qc.mcx(list(range(i)), i)
        else:
            qc.ry(theta, 0)
            qc.x(0)

        #0->1にしてたやつを戻す
        for j in range(i):
            qc.x(j)
    for i in range(n-1):
        qc.x(i)
    qc.mcx(list(range(n-1)), n-1)
    for i in range(n-1):
        qc.x(i)
    return qc
'''


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

def calc(n):
    return math.acos(((n-1)/n)**(1/2))

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1):
        #前側0をすべて1に
        theta = 2*calc(n-i)
        #print("theta", theta, math.cos(theta/2))
        if i >= 1:
            ccry = RYGate(theta).control(num_ctrl_qubits=i, label=None)
            #qc.mcry(theta, list(range(i)), i)
            qc.append(ccry, list(range(i+1)))
        else:
            qc.ry(theta, 0)
        qc.x(i)

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

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

    return qc
'''


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

def calc(n):
    return math.acos((1/n)**(1/2))

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1):
        #前側0をすべて1に
        theta = 2*calc(n-i)
        #print("theta", theta, math.cos(theta/2))
        if i >= 1:
            ccry = RYGate(theta).control(num_ctrl_qubits=i, label=None)
            #qc.mcry(theta, list(range(i)), i)
            qc.append(ccry, list(range(i+1)))
        else:
            qc.ry(theta, 0)

    for i in range(n-1):
        qc.mcx(list(range(n-i-1)), n-i-1)
    qc.x(0)
    return qc
'''


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

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

    prob_amp = np.sqrt(1/n)
    rot_ang = 2*np.arccos(prob_amp)

    qc_w = QuantumCircuit(n)

    # probability redistribution
    qc_w.ry(rot_ang,0)

    for i in range(1,n-1):
        comp_amp = np.sqrt(1-i/n)
        rot_ang = 2*np.arccos(prob_amp/(comp_amp))
        qc_w.cry(rot_ang,i-1,i)

    # state reshuffling
    for i in range(n-1, 0, -1):
        qc_w.cx(i-1,i)

    qc_w.x(0)

    return qc_w
'''


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

def solve(n: int) -> QuantumCircuit:
    qc_w = QuantumCircuit(n)

    # Define the target amplitude and initial rotation angle
    prob_amp = np.sqrt(1 / n)
    initial_rot_ang = 2 * np.arccos(prob_amp)

    # Start with an X gate on the first qubit (initial step remains the same)
    qc_w.x(0)

    # Apply controlled rotations in parallel stages to reduce depth
    for stage in range(0, n-1, 2):
        # Controlled rotation for even indexed pairs
        if stage < n - 1:
            comp_amp = np.sqrt(1 - stage / n)
            rot_ang = 2 * np.arccos(prob_amp / comp_amp)
            qc_w.cry(rot_ang, stage, stage + 1)
            qc_w.cx(stage + 1, stage)

    for stage in range(1, n-1, 2):
        # Controlled rotation for odd indexed pairs
        if stage < n - 1:
            comp_amp = np.sqrt(1 - stage / n)
            rot_ang = 2 * np.arccos(prob_amp / comp_amp)
            qc_w.cry(rot_ang, stage, stage + 1)
            qc_w.cx(stage + 1, stage)

    return qc_w
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    prob_amp = np.sqrt(1 / n)
    rot_ang = 2 * np.arccos(prob_amp)

    qc.x(0)

    for i in range(n - 1):
        comp_amp = np.sqrt(1 - i / n)
        rot_ang = 2 * np.arccos(prob_amp / (comp_amp))
        qc.cry(rot_ang, i, i + 1)
        qc_.cx(i + 1, i)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    prob_amp = np.sqrt(1 / n)

    qc.x(0)

    for i in range(n - 1):
        comp_amp = np.sqrt(1 - i / n)
        rot_ang = 2 * np.arccos(prob_amp / (comp_amp))
        qc.cry(rot_ang, i, i + 1)
        qc.cx(i + 1, i)

    return qc
'''


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

def apply_w_state(qc, qubits):
    # ベースケース: 1量子ビットの場合、初期状態 |1> を作成
    if len(qubits) == 1:
        qc.x(qubits[0])
        return

    # 左右に分割
    mid = len(qubits) // 2
    left = qubits[:mid]
    right = qubits[mid:]

    # 左側の状態を|1>と|0>の重ね合わせに回転
    theta = 2 * np.arccos(np.sqrt(len(left) / len(qubits)))
    qc.ry(theta, left[0])
    qc.cx(left[0], right[0])

    # 再帰的に左右に対してW状態を構築
    apply_w_state(qc, left)
    apply_w_state(qc, right)

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qubits = list(range(n))
    apply_w_state(qc, qubits)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    prob_amp = np.sqrt(1 / n)
    rot_ang = 2 * np.arccos(prob_amp)

    qc.x(0)

    def dichotomy_tree(qc, start, size):
        if size == 1:
            return
        left_size = size // 2
        right_size = size - left_size

        qc.cry(2 * np.arccos(np.sqrt(left_size / size)), start, start + left_size)

        dichotomy_tree(qc, start, left_size)
        dichotomy_tree(qc, start + left_size, right_size)

    dichotomy_tree(qc, 0, n)

    return qc
'''


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

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

    def dichotomy_tree(qc, start, size):
        if size == 1:
            return
        left_size = size // 2
        right_size = size - left_size

        qc.cry(2 * np.arccos(np.sqrt(left_size / size)), start, start + left_size)

        dichotomy_tree(qc, start, left_size)
        dichotomy_tree(qc, start + left_size, right_size)

    dichotomy_tree(qc, 0, n)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    levels = int(np.ceil(np.log2(n)))
    for level in range(levels):
        step = 2 ** level
        for i in range(0, n - step, 2 * step):
            if i + step < n:
                qc.cry(2 * np.arccos(1 / np.sqrt(2)), i, i + step)
                qc.cx(i, i + step)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    levels = int(np.ceil(np.log2(n)))
    qc.h(0)

    for level in range(levels):
        step = 2 ** level
        for i in range(0, n - step, 2 * step):
            if i + step < n:
                theta = 2 * np.arccos(1 / np.sqrt(level + 2))
                qc.cry(theta, i, i + step)
                qc.cx(i, i + step)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    levels = int(np.ceil(np.log2(n)))
    qc.h(0)

    for level in range(levels):
        step = 2 ** level
        for i in range(0, n - step, 2 * step):
            if i + step < n:
                theta = 2 * np.arccos(1 / np.sqrt(level + 2))
                qc.cry(theta, i, i + step)
                qc.cx(i + step, i)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    levels = int(np.ceil(np.log2(n)))
    qc.x(0)

    box = [0 for _ in range(n)]
    box[0] = n

    for level in range(levels):
        tmp = 2**level
        for i in range(tmp):
            parent = box[i]
            if parent == 1:
                continue
            left = parent//2
            right = parent - left
            prob_amp = np.sqrt(left / parent)
            rot_ang = 2 * np.arccos(prob_amp)
            for j in range(tmp, n):
                if box[j] == 0:
                    bridge = j
                    break
            qc.cry(rot_ang, i, bridge)
            qc.cx(bridge, i)
            box[i] = left
            box[bridge] = right

    return qc
'''


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

def F_gate(circ,q,i,j,n,k) :
    theta = math.acos(math.sqrt(1/(n-k+1)))
    circ.ry(-theta,q[j])
    circ.cz(q[i],q[j])
    circ.ry(theta,q[j])
    circ.barrier(q[i])

def  cxrv(circ,q,i,j) :
    circ.h(q[i])
    circ.h(q[j])
    circ.cx(q[j],q[i])
    circ.h(q[i])
    circ.h(q[j])
    circ.barrier(q[i],q[j])

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


'''python
from qiskit import QuantumCircuit, QuantumRegister
import math

def F_gate(circ,q,i,j,n,k) :
    theta = math.acos(math.sqrt(1/(n-k+1)))
    circ.ry(-theta,q[j])
    circ.cz(q[i],q[j])
    circ.ry(theta,q[j])
    circ.barrier(q[i])

def  cxrv(circ,q,i,j) :
    circ.h(q[i])
    circ.h(q[j])
    circ.cx(q[j],q[i])
    circ.h(q[i])
    circ.h(q[j])
    circ.barrier(q[i],q[j])

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


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.ry(math.asin(1/math.sqrt(n))*2, 0)
    for i in range(1, n):
        for j in range(i):
            qc.x(j)
        qc.append(RYGate(math.asin(1/math.sqrt(n-i))*2).control(i), range(i+1))
        for j in range(i):
            qc.x(j)
    return qc

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


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.x(i)
    qc.ry(math.asin(-1/math.sqrt(n))*2, 0)
    for i in range(1, n-1):
        qc.append(RYGate(math.asin(-1/math.sqrt(n-i))*2).control(i), range(i+1))
    qc.mcx(list(range(n-1)), n-1)
    for i in range(n):
        qc.x(i)
    # print(qc.depth())
    return qc

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


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(0)
    for i in range(1,n):
        theta = math.atan(math.sqrt(n-i))*2
        qc.cry(theta, i-1, i)
        qc.cx(i,i-1)
    return qc
'''