'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    """
    Implementa un circuito cuántico que prepara el estado |psi⟩ tal que
    la probabilidad de medir |L⟩ es al menos 0.9.

    Parámetros:
    - n: Número de qubits
    - o: Oráculo O como QuantumCircuit de profundidad 1 que actúa sobre n qubits

    Retorna:
    - qc: QuantumCircuit que implementa la operación deseada
    """
    # Inicializar el circuito cuántico con n qubits
    qc = QuantumCircuit(n)

    # Definir el qubit auxiliar como el último qubit (n-1)
    y = n - 1

    # Paso 1: Preparar el qubit auxiliar y en el estado |-> = (|0⟩ - |1⟩)/sqrt(2)
    qc.x(y)      # Aplica X para convertir |0⟩ en |1⟩
    qc.h(y)      # Aplica Hadamard para obtener |->

    # Paso 2: Aplicar puertas Hadamard a todos los qubits excepto el auxiliar para crear una superposición uniforme
    if n > 1:
        qc.h(range(n - 1))
    else:
        qc.h(y)  # Si n=1, aplicar H al único qubit

    # Calcular el número óptimo de iteraciones de Grover
    # Número total de estados
    N = 2**n
    # Número de soluciones (k=1, ya que L es único)
    k = 1
    # Calcular el ángulo theta
    theta = math.asin(math.sqrt(k / N))
    # Número óptimo de iteraciones
    optimal_iterations = int(math.floor((math.pi / (4 * theta)) - 0.5))

    # Estimar la profundidad por iteración
    # Cada iteración incluye:
    # - Oráculo O: profundidad 1
    # - Difusor: profundidad aproximada (dependiendo de n)
    # Para simplificar, asumimos una profundidad por iteración de 3n + 3
    depth_per_iteration = 3 * n + 3
    max_depth = 75
    initial_depth = 2 + (n - 1 if n > 1 else 1)  # X y H en y qubit, H en otros qubits

    # Calcular el número máximo de iteraciones sin exceder la profundidad
    remaining_depth = max_depth - initial_depth
    if remaining_depth < depth_per_iteration:
        iterations = 1  # Al menos una iteración
    else:
        iterations = min(optimal_iterations, remaining_depth // depth_per_iteration)

    # Asegurarse de que al menos se realice una iteración
    iterations = max(1, iterations)

    # Definir el operador de difusión (diffuser)
    def diffuser_operator(n_qubits):
        """
        Crea el operador de difusión para el Algoritmo de Grover.
        """
        diffuser = QuantumCircuit(n_qubits, name='Diffuser')
        diffuser.h(range(n_qubits))
        diffuser.x(range(n_qubits))

        # Aplicar la puerta Z multi-controlada
        if n_qubits == 1:
            diffuser.z(0)
        elif n_qubits == 2:
            diffuser.cz(0, 1)
        else:
            diffuser.h(n_qubits - 1)
            diffuser.mcx(list(range(n_qubits - 1)), n_qubits - 1)  # Puerta Toffoli multi-controlada
            diffuser.h(n_qubits - 1)

        diffuser.x(range(n_qubits))
        diffuser.h(range(n_qubits))

        return diffuser.to_gate()

    diffuser_gate = diffuser_operator(n)

    # Aplicar iteraciones de Grover
    for _ in range(iterations):
        # Paso 3: Aplicar el oráculo O
        qc.compose(o, qubits=range(n), inplace=True)

        # Paso 4: Aplicar el operador de difusión
        qc.append(diffuser_gate, qargs=range(n), inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    """
    Implementa un circuito cuántico que prepara el estado |psi⟩ tal que
    la probabilidad de medir |L⟩ es al menos 0.9.

    Parámetros:
    - n: Número de qubits
    - o: Oráculo O como QuantumCircuit de profundidad 1 que actúa sobre n qubits

    Retorna:
    - qc: QuantumCircuit que implementa la operación deseada
    """
    # Definir el registro cuántico
    x = QuantumCircuit(n)
    qc = QuantumCircuit(n)

    # Paso 1: Aplicar puertas Hadamard a todos los qubits para crear una superposición uniforme
    qc.h(range(n))

    # Paso 2: Determinar el número óptimo de iteraciones de Grover
    # Número total de estados
    N = 2**n
    # Número de soluciones (k=1, ya que L es único)
    k = 1
    # Calcular el ángulo theta
    theta = math.asin(math.sqrt(k / N))
    # Número óptimo de iteraciones
    optimal_iterations = int(math.floor((math.pi / (4 * theta)) - 0.5))

    # Estimar la profundidad por iteración
    # Cada iteración incluye:
    # - Oráculo O: profundidad 1
    # - Difusor: profundidad aproximada 4 (H, X, MCX, etc.)
    # Total por iteración: ~5
    depth_per_iteration = 5
    max_depth = 75
    # Calcular el número máximo de iteraciones sin exceder la profundidad
    max_iterations = min(optimal_iterations, max_depth // depth_per_iteration)
    # Asegurarse de que al menos se realice una iteración
    iterations = max(1, max_iterations)

    # Aplicar iteraciones de Grover
    for _ in range(iterations):
        # Paso 3: Aplicar el oráculo O
        qc.compose(o, qubits=range(n), inplace=True)

        # Paso 4: Aplicar el operador de difusión
        qc.h(range(n))
        qc.x(range(n))

                      # Aplicar una puerta Z multi-controlada
        qc.h(n-1)
        qc.mcx(list(range(n-1)), n-1)  # Puerta Toffoli multi-controlada
        qc.h(n-1)

        qc.x(range(n))
        qc.h(range(n))

    return qc
'''


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

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    """
    Implementa un circuito cuántico que prepara el estado |psi⟩ tal que
    la probabilidad de medir |L⟩ es al menos 0.9.

    Parámetros:
    - n: Número de qubits
    - o: Oráculo O como QuantumCircuit de profundidad 1 que actúa sobre n qubits y 1 auxiliar

    Retorna:
    - qc: QuantumCircuit que implementa la operación deseada
    """
    # Definir los registros cuánticos
    x = QuantumRegister(n, 'x')  # Qubits para |x⟩
    y = QuantumRegister(1, 'y')  # Qubit auxiliar
    qc = QuantumCircuit(x, y)

    # Paso 1: Preparar el qubit auxiliar en el estado |-> = (|0⟩ - |1⟩)/sqrt(2)
    qc.x(y[0])      # Aplicar puerta X para |1⟩
    qc.h(y[0])      # Aplicar puerta Hadamard para |->

    # Paso 2: Aplicar puertas Hadamard a todos los qubits de x para crear una superposición uniforme
    qc.h(x)

    # Calcular el número óptimo de iteraciones de Grover
    N = 2**n
    k = 1  # Número de soluciones (único L)
    theta = math.asin(math.sqrt(k / N))
    optimal_iterations = int(math.floor((math.pi / (4 * theta)) - 0.5))

    # Estimar la profundidad por iteración
    # Cada iteración incluye:
    # - Oráculo O: profundidad 1
    # - Difusor: aproximadamente 4n + 2 (puertas H, X, MCX, etc.)
    # Suponemos una profundidad por iteración de 5n para mayor seguridad
    depth_per_iteration = 5 * n
    max_depth = 75
    initial_depth = 2 + n  # X and H on y qubit and H on x qubits

    # Calcular el número máximo de iteraciones sin exceder la profundidad
    remaining_depth = max_depth - initial_depth
    if remaining_depth < depth_per_iteration:
        iterations = 1  # Al menos una iteración
    else:
        iterations = min(optimal_iterations, remaining_depth // depth_per_iteration)

    # Asegurarse de que al menos se realice una iteración
    iterations = max(1, iterations)

    # Definir el operador de difusión (diffuser) sin usar to_gate()
    def apply_diffuser(circuit, qubits):
        """
        Aplica el operador de difusión de Grover al circuito dado sobre los qubits especificados.
        """
        # Paso 1: Aplicar puertas Hadamard a todos los qubits
        circuit.h(qubits)

        # Paso 2: Aplicar puertas X a todos los qubits
        circuit.x(qubits)

        # Paso 3: Aplicar la puerta Z multi-controlada
        if len(qubits) == 1:
            # Caso n=1: Aplicar puerta Z directamente
            circuit.z(qubits[0])
        elif len(qubits) == 2:
            # Caso n=2: Aplicar puerta CZ
            circuit.cz(qubits[0], qubits[1])
        else:
            # Caso n > 2: Implementar una puerta Z multi-controlada
            # Convertir la última qubit a Z mediante MCX y H
            circuit.h(qubits[-1])
            circuit.mcx(qubits[:-1], qubits[-1])  # Puerta Toffoli multi-controlada
            circuit.h(qubits[-1])

        # Paso 4: Aplicar puertas X a todos los qubits
        circuit.x(qubits)

        # Paso 5: Aplicar puertas Hadamard a todos los qubits
        circuit.h(qubits)

    # Aplicar iteraciones de Grover
    for _ in range(iterations):
        # Paso 3: Aplicar el oráculo O
        qc.compose(o, qubits=x[:] + [y[0]], inplace=True)

        # Paso 4: Aplicar el operador de difusión
        apply_diffuser(qc, x)

    # Paso 5: Medir los qubits de x (opcional, dependiendo de la evaluación)
    # qc.measure_all()

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    # Apply Hadamard gates to all qubits
    for qubit in range(n):
        qc.h(qubit)

    # Apply the oracle circuit
    qc.compose(o, inplace=True)

    # Apply the inverse quantum Fourier transform
    for qubit in range(n):
        for j in range(qubit):
            qc.cu1(-2 * math.pi / 2**(qubit - j), j, qubit)
        qc.h(qubit)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:-
    for i in range(n):
        qc.h(i)
    row = [math.sin((2*r+1)*math.asin(2**(-n/2)))**2 for r in range(10)]
    r = np.argmax(row)
    for _ in range(r):
        qc.append(o,qargs = range(n+1))
        qc.z(n)
        qc.append(o,qargs = range(n+1))
        for i in range(n):
            qc.h(i)
        for i in range(n):
            qc.x(i)
        qc.append(ZGate().control(n-1),qargs = range(n))
        for i in range(n):
            qc.x(i)
        for i in range(n):
            qc.h(i)
    return qc
'''


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

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

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:-
    for i in range(n):
        qc.h(i)
    row = [math.sin((2*r+1)*math.asin(2**(-n/2)))**2 for r in range(10)]
    r = np.argmax(row)
    for _ in range(r):
        qc.append(o,qargs = range(n))
        for i in range(n):
            qc.h(i)
        for i in range(n):
            qc.x(i)
        qc.append(ZGate().control(n-1),qargs = range(n))
        for i in range(n):
            qc.x(i)
        for i in range(n):
            qc.h(i)
    return qc
'''


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

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

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:-
    for i in range(n):
        qc.h(i)
    row = [math.sin((2*r+1)*math.asin(2**(-n/2)))**2 for r in range(10)]
    r = np.argmax(row)
    for _ in range(r):
        qc.append(o,qargs = range(n))
        for i in range(n):
            qc.h(i)
        for i in range(n):
            qc.x(i)
        qc.append(ZGate().control(n-1),qargs = range(n))
        for i in range(n):
            qc.x(i)
        for i in range(n):
            qc.h(i)
    return qc
'''


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

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

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n <= 7:
        return grover(n,o)
    else:
        for i in range(n):
            qc.h(i)
        for _ in range(2):
            qc.compose(o)
            qc.compose(uniform_one_hot)
            for i in range(n):
                qc.x(i)
            qc.compose(ZGate().control(n-1))
            for i in range(n):
                qc.x(i)
            qc.compose(uniform_one_hot)
        return qc

    return qc

def grover(n: int, o: QuantumCircuit) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:-
    for i in range(n):
        qc.h(i)
    row = [math.sin((2*r+1)*math.asin(2**(-n/2)))**2 for r in range(10)]
    r = np.argmax(row)
    for _ in range(r):
        qc.append(o,qargs = range(n))
        for i in range(n):
            qc.h(i)
        for i in range(n):
            qc.x(i)
        qc.append(ZGate().control(n-1),qargs = range(n))
        for i in range(n):
            qc.x(i)
        for i in range(n):
            qc.h(i)
    return qc

def uniform_one_hot(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    n_code = bin(n)[2:]
    m = [0]
    for i in range(len(n_code)):
        if n_code[i] == "1":
            m.append(m[-1]+2**(len(n_code)-i-1))
    qc.x(0)
    for i in range(1,len(m)-1):
        split_one_hot(qc,m[i-1],m[i],(m[i]-m[i-1])/(n-m[i-1]))
    for j in range(1,len(m)):
        for k in range(1,m[j]-m[j-1]):
            split_one_hot(qc,m[j-1]+k-2**int(math.log2(k)),m[j-1]+k,1/2)

    return qc
def split_one_hot(qc,m1,m2,l):
    print(m1,m2,l)
    qc.cry(2*math.acos(math.sqrt(l)),m1,m2)
    qc.cx(m2,m1)
'''


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

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

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n <= 7:
        return grover(n,o)
    else:
        for i in range(n):
            qc.h(i)
        for _ in range(2):
            qc.compose(o)
            qc.compose(uniform_one_hot(n))
            for i in range(n):
                qc.x(i)
            qc.compose(ZGate().control(n-1))
            for i in range(n):
                qc.x(i)
            qc.compose(uniform_one_hot(n))
        return qc

def grover(n: int, o: QuantumCircuit) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:-
    for i in range(n):
        qc.h(i)
    row = [math.sin((2*r+1)*math.asin(2**(-n/2)))**2 for r in range(10)]
    r = np.argmax(row)
    for _ in range(r):
        qc.append(o,qargs = range(n))
        for i in range(n):
            qc.h(i)
        for i in range(n):
            qc.x(i)
        qc.append(ZGate().control(n-1),qargs = range(n))
        for i in range(n):
            qc.x(i)
        for i in range(n):
            qc.h(i)
    return qc

def uniform_one_hot(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    n_code = bin(n)[2:]
    m = [0]
    for i in range(len(n_code)):
        if n_code[i] == "1":
            m.append(m[-1]+2**(len(n_code)-i-1))
    qc.x(0)
    for i in range(1,len(m)-1):
        split_one_hot(qc,m[i-1],m[i],(m[i]-m[i-1])/(n-m[i-1]))
    for j in range(1,len(m)):
        for k in range(1,m[j]-m[j-1]):
            split_one_hot(qc,m[j-1]+k-2**int(math.log2(k)),m[j-1]+k,1/2)

    return qc
def split_one_hot(qc,m1,m2,l):
    print(m1,m2,l)
    qc.cry(2*math.acos(math.sqrt(l)),m1,m2)
    qc.cx(m2,m1)
'''


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

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

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n <= 7:
        return grover(n,o)
    else:
        for i in range(n):
            qc.h(i)
        for _ in range(2):
            qc.compose(o)
            qc.compose(uniform_one_hot(n))
            for i in range(n):
                qc.x(i)
            qc.compose(ZGate().control(n-1))
            for i in range(n):
                qc.x(i)
            qc.compose(uniform_one_hot(n).inverse())
        return qc

def grover(n: int, o: QuantumCircuit) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:-
    for i in range(n):
        qc.h(i)
    row = [math.sin((2*r+1)*math.asin(2**(-n/2)))**2 for r in range(10)]
    r = np.argmax(row)
    for _ in range(r):
        qc.append(o,qargs = range(n))
        for i in range(n):
            qc.h(i)
        for i in range(n):
            qc.x(i)
        qc.append(ZGate().control(n-1),qargs = range(n))
        for i in range(n):
            qc.x(i)
        for i in range(n):
            qc.h(i)
    return qc

def uniform_one_hot(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    n_code = bin(n)[2:]
    m = [0]
    for i in range(len(n_code)):
        if n_code[i] == "1":
            m.append(m[-1]+2**(len(n_code)-i-1))
    qc.x(0)
    for i in range(1,len(m)-1):
        split_one_hot(qc,m[i-1],m[i],(m[i]-m[i-1])/(n-m[i-1]))
    for j in range(1,len(m)):
        for k in range(1,m[j]-m[j-1]):
            split_one_hot(qc,m[j-1]+k-2**int(math.log2(k)),m[j-1]+k,1/2)

    return qc
def split_one_hot(qc,m1,m2,l):
    print(m1,m2,l)
    qc.cry(2*math.acos(math.sqrt(l)),m1,m2)
    qc.cx(m2,m1)
'''


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

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

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n <= 7:
        return grover(n,o)
    else:
        for i in range(n):
            qc.h(i)
        for _ in range(2):
            qc.compose(o)
            qc.compose(uniform_one_hot(n).inverse())
            for i in range(n):
                qc.x(i)
            qc.compose(ZGate().control(n-1))
            for i in range(n):
                qc.x(i)
            qc.compose(uniform_one_hot(n))
        return qc

def grover(n: int, o: QuantumCircuit) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:-
    for i in range(n):
        qc.h(i)
    row = [math.sin((2*r+1)*math.asin(2**(-n/2)))**2 for r in range(10)]
    r = np.argmax(row)
    for _ in range(r):
        qc.append(o,qargs = range(n))
        for i in range(n):
            qc.h(i)
        for i in range(n):
            qc.x(i)
        qc.append(ZGate().control(n-1),qargs = range(n))
        for i in range(n):
            qc.x(i)
        for i in range(n):
            qc.h(i)
    return qc

def uniform_one_hot(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    n_code = bin(n)[2:]
    m = [0]
    for i in range(len(n_code)):
        if n_code[i] == "1":
            m.append(m[-1]+2**(len(n_code)-i-1))
    qc.x(0)
    for i in range(1,len(m)-1):
        split_one_hot(qc,m[i-1],m[i],(m[i]-m[i-1])/(n-m[i-1]))
    for j in range(1,len(m)):
        for k in range(1,m[j]-m[j-1]):
            split_one_hot(qc,m[j-1]+k-2**int(math.log2(k)),m[j-1]+k,1/2)

    return qc
def split_one_hot(qc,m1,m2,l):
    print(m1,m2,l)
    qc.cry(2*math.acos(math.sqrt(l)),m1,m2)
    qc.cx(m2,m1)
'''


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

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

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n <= 7:
        return grover(n,o)
    else:
        for i in range(n):
            qc.h(i)
        for _ in range(2):
            qc.compose(o, inplace=True)
            qc.compose(uniform_one_hot(n).inverse(), inplace=True)
            for i in range(n):
                qc.x(i)
            qc.compose(ZGate().control(n-1), inplace=True)
            for i in range(n):
                qc.x(i)
            qc.compose(uniform_one_hot(n), inplace=True)
        return qc

def grover(n: int, o: QuantumCircuit) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:-
    for i in range(n):
        qc.h(i)
    row = [math.sin((2*r+1)*math.asin(2**(-n/2)))**2 for r in range(10)]
    r = np.argmax(row)
    for _ in range(r):
        qc.append(o,qargs = range(n))
        for i in range(n):
            qc.h(i)
        for i in range(n):
            qc.x(i)
        qc.append(ZGate().control(n-1),qargs = range(n))
        for i in range(n):
            qc.x(i)
        for i in range(n):
            qc.h(i)
    return qc

def uniform_one_hot(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    n_code = bin(n)[2:]
    m = [0]
    for i in range(len(n_code)):
        if n_code[i] == "1":
            m.append(m[-1]+2**(len(n_code)-i-1))
    qc.x(0)
    for i in range(1,len(m)-1):
        split_one_hot(qc,m[i-1],m[i],(m[i]-m[i-1])/(n-m[i-1]))
    for j in range(1,len(m)):
        for k in range(1,m[j]-m[j-1]):
            split_one_hot(qc,m[j-1]+k-2**int(math.log2(k)),m[j-1]+k,1/2)

    return qc
def split_one_hot(qc,m1,m2,l):
    print(m1,m2,l)
    qc.cry(2*math.acos(math.sqrt(l)),m1,m2)
    qc.cx(m2,m1)
'''


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

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

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n <= 7:
        return grover(n,o)
    else:
        for i in range(n):
            qc.h(i)
        for _ in range(2):
            qc.compose(o, inplace=True)
            qc.compose(uniform_one_hot(n).inverse(), inplace=True)
            for i in range(n):
                qc.x(i)
            qc.compose(ZGate().control(n-1), inplace=True)
            for i in range(n):
                qc.x(i)
            qc.compose(uniform_one_hot(n), inplace=True)
        return qc

def grover(n: int, o: QuantumCircuit) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:-
    for i in range(n):
        qc.h(i)
    row = [math.sin((2*r+1)*math.asin(2**(-n/2)))**2 for r in range(10)]
    r = np.argmax(row)
    for _ in range(r):
        qc.compose(o, inplace=True)
        for i in range(n):
            qc.h(i)
        for i in range(n):
            qc.x(i)
        qc.compose(ZGate().control(n-1), inplace=True)
        for i in range(n):
            qc.x(i)
        for i in range(n):
            qc.h(i)
    return qc

def uniform_one_hot(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    n_code = bin(n)[2:]
    m = [0]
    for i in range(len(n_code)):
        if n_code[i] == "1":
            m.append(m[-1]+2**(len(n_code)-i-1))
    qc.x(0)
    for i in range(1,len(m)-1):
        split_one_hot(qc,m[i-1],m[i],(m[i]-m[i-1])/(n-m[i-1]))
    for j in range(1,len(m)):
        for k in range(1,m[j]-m[j-1]):
            split_one_hot(qc,m[j-1]+k-2**int(math.log2(k)),m[j-1]+k,1/2)

    return qc
def split_one_hot(qc,m1,m2,l):
    print(m1,m2,l)
    qc.cry(2*math.acos(math.sqrt(l)),m1,m2)
    qc.cx(m2,m1)
'''


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

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

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n <= 7:
        return grover(n,o)
    else:
        for i in range(n):
            qc.h(i)
        for _ in range(2):
            qc.compose(o, inplace=True)
            qc.compose(uniform_one_hot(n).inverse(), inplace=True)
            qc.compose(reflect(n), inplace=True)
            qc.compose(uniform_one_hot(n), inplace=True)
        return qc

def grover(n: int, o: QuantumCircuit) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:-
    for i in range(n):
        qc.h(i)
    row = [math.sin((2*r+1)*math.asin(2**(-n/2)))**2 for r in range(10)]
    r = np.argmax(row)
    for _ in range(r):
        qc.compose(o, inplace=True)
        for i in range(n):
            qc.h(i)
        qc.compose(reflect(n), inplace=True)
        for i in range(n):
            qc.h(i)
    return qc

def reflect(n:int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(range(n))
    qc.compose(ZGate().control(n-1), inplace=True)
    qc.x(range(n))
    return qc

def uniform_one_hot(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    n_code = bin(n)[2:]
    m = [0]
    for i in range(len(n_code)):
        if n_code[i] == "1":
            m.append(m[-1]+2**(len(n_code)-i-1))
    qc.x(0)
    for i in range(1,len(m)-1):
        split_one_hot(qc,m[i-1],m[i],(m[i]-m[i-1])/(n-m[i-1]))
    for j in range(1,len(m)):
        for k in range(1,m[j]-m[j-1]):
            split_one_hot(qc,m[j-1]+k-2**int(math.log2(k)),m[j-1]+k,1/2)

    return qc
def split_one_hot(qc,m1,m2,l):
    print(m1,m2,l)
    qc.cry(2*math.acos(math.sqrt(l)),m1,m2)
    qc.cx(m2,m1)
'''


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

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

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n <= 7:
        return grover(n,o)
    else:
        qc.compose(uniform_one_hot(n), inplace=True)
        for _ in range(2):
            qc.compose(o, inplace=True)
            qc.compose(uniform_one_hot(n).inverse(), inplace=True)
            qc.compose(reflect(n), inplace=True)
            qc.compose(uniform_one_hot(n), inplace=True)
        return qc

def grover(n: int, o: QuantumCircuit) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:-
    for i in range(n):
        qc.h(i)
    row = [math.sin((2*r+1)*math.asin(2**(-n/2)))**2 for r in range(10)]
    r = np.argmax(row)
    for _ in range(r):
        qc.compose(o, inplace=True)
        for i in range(n):
            qc.h(i)
        qc.compose(reflect(n), inplace=True)
        for i in range(n):
            qc.h(i)
    return qc

def reflect(n:int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.x(range(n))
    qc.compose(ZGate().control(n-1), inplace=True)
    qc.x(range(n))
    return qc

def uniform_one_hot(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    n_code = bin(n)[2:]
    m = [0]
    for i in range(len(n_code)):
        if n_code[i] == "1":
            m.append(m[-1]+2**(len(n_code)-i-1))
    qc.x(0)
    for i in range(1,len(m)-1):
        split_one_hot(qc,m[i-1],m[i],(m[i]-m[i-1])/(n-m[i-1]))
    for j in range(1,len(m)):
        for k in range(1,m[j]-m[j-1]):
            split_one_hot(qc,m[j-1]+k-2**int(math.log2(k)),m[j-1]+k,1/2)

    return qc
def split_one_hot(qc,m1,m2,l):
    print(m1,m2,l)
    qc.cry(2*math.acos(math.sqrt(l)),m1,m2)
    qc.cx(m2,m1)
'''


'''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 split_state(qc: QuantumCircuit, targets: list[int], proportions: list[float]):
    sum_p = sum(proportions)
    proportions = [p / sum_p for p in proportions]
    for i in range(1, len(targets)):
        qc.cry(
            asin(sqrt((1 - sum(proportions[:i])) / (1 - sum(proportions[: i - 1]))))
            * 2,
            targets[i - 1],
            targets[i],
        )
    for i in range(1, len(targets)):
        qc.cx(targets[i], targets[i - 1])

def solve_main(qc: QuantumCircuit, i0: int, i1: int):
    if i1 - i0 <= 1:
        return
    i = (i0 + i1) // 2
    split_state(qc, [i0, i], [len(range(i0, i)), len(range(i, i1))])
    solve_main(qc, i0, i)
    solve_main(qc, i, i1)

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

    return qc

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

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

    qc.compose(prep_qc, inplace=True)

    theta_0 = asin(np.sqrt(1 / n))
    K = int(np.floor((pi / 2 - theta_0) / theta_0 / 2))
    for _ in range(K):
        qc.compose(o, inplace=True)

        qc.compose(prep_qc.inverse(), inplace=True)
        qc.x(range(n))
        qc.mcp(pi, list(range(n - 1)), n - 1)
        qc.x(range(n))
        qc.compose(prep_qc, inplace=True)

    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 split_state(qc: QuantumCircuit, targets: list[int], proportions: list[float]):
    sum_p = sum(proportions)
    proportions = [p / sum_p for p in proportions]
    for i in range(1, len(targets)):
        qc.cry(
            asin(sqrt((1 - sum(proportions[:i])) / (1 - sum(proportions[: i - 1]))))
            * 2,
            targets[i - 1],
            targets[i],
        )
    for i in range(1, len(targets)):
        qc.cx(targets[i], targets[i - 1])

def solve_main(qc: QuantumCircuit, i0: int, i1: int):
    if i1 - i0 <= 1:
        return
    i = (i0 + i1) // 2
    split_state(qc, [i0, i], [len(range(i0, i)), len(range(i, i1))])
    solve_main(qc, i0, i)
    solve_main(qc, i, i1)

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

    return qc

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

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

    qc.compose(prep_qc, inplace=True)

    theta_0 = asin(sqrt(1 / n))
    K = int(np.floor((pi / 2 + theta_0) / theta_0 / 2))
    for _ in range(K):
        qc.compose(o, inplace=True)

        qc.compose(prep_qc.inverse(), inplace=True)
        qc.x(range(n))
        qc.mcp(pi, list(range(n - 1)), n - 1)
        qc.x(range(n))
        qc.compose(prep_qc, inplace=True)

    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 split_state(qc: QuantumCircuit, targets: list[int], proportions: list[float]):
    sum_p = sum(proportions)
    proportions = [p / sum_p for p in proportions]
    for i in range(1, len(targets)):
        qc.cry(
            asin(sqrt((1 - sum(proportions[:i])) / (1 - sum(proportions[: i - 1]))))
            * 2,
            targets[i - 1],
            targets[i],
        )
    for i in range(1, len(targets)):
        qc.cx(targets[i], targets[i - 1])

def solve_main(qc: QuantumCircuit, i0: int, i1: int):
    if i1 - i0 <= 1:
        return
    i = (i0 + i1) // 2
    split_state(qc, [i0, i], [len(range(i0, i)), len(range(i, i1))])
    solve_main(qc, i0, i)
    solve_main(qc, i, i1)

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

    return qc

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

def solve_7(n: int, o: QuantumCircuit) -> QuantumCircuit:
    qc = QuantumCircuit(8)
    prep_qc = prep_A6(8)
    # Write your code here:

    qc.compose(prep_qc, inplace=True)

    theta_0 = asin(sqrt(1 / 8))
    K = int(np.round((pi / 2 - theta_0) / theta_0 / 2))
    for _ in range(K):
        qc.compose(o, inplace=True)

        qc.compose(prep_qc.inverse(), inplace=True)
        qc.x(range(8))
        qc.mcp(pi, list(range(8 - 1)), 8 - 1)
        qc.x(range(8))
        qc.compose(prep_qc, inplace=True)

    return qc

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    if n == 7:
        return solve_7(n, o)
    qc = QuantumCircuit(n)
    prep_qc = prep_A6(n)
    # Write your code here:

    qc.compose(prep_qc, inplace=True)

    theta_0 = asin(sqrt(1 / n))
    K = int(np.round((pi / 2 - theta_0) / theta_0 / 2))
    for _ in range(K):
        qc.compose(o, inplace=True)

        qc.compose(prep_qc.inverse(), inplace=True)
        qc.x(range(n))
        qc.mcp(pi, list(range(n - 1)), n - 1)
        qc.x(range(n))
        qc.compose(prep_qc, inplace=True)

    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 split_state(qc: QuantumCircuit, targets: list[int], proportions: list[float]):
    sum_p = sum(proportions)
    proportions = [p / sum_p for p in proportions]
    for i in range(1, len(targets)):
        qc.cry(
            asin(sqrt((1 - sum(proportions[:i])) / (1 - sum(proportions[: i - 1]))))
            * 2,
            targets[i - 1],
            targets[i],
        )
    for i in range(1, len(targets)):
        qc.cx(targets[i], targets[i - 1])

def solve_main(qc: QuantumCircuit, i0: int, i1: int):
    if i1 - i0 <= 1:
        return
    i = (i0 + i1) // 2
    split_state(qc, [i0, i], [len(range(i0, i)), len(range(i, i1))])
    solve_main(qc, i0, i)
    solve_main(qc, i, i1)

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

    return qc

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

def solve_2(n: int, o: QuantumCircuit) -> QuantumCircuit:
    qc = QuantumCircuit(4)
    prep_qc = prep_A6(4)
    # Write your code here:

    qc.compose(prep_qc, inplace=True)

    theta_0 = asin(sqrt(1 / 4))
    K = int(np.round((pi / 2 - theta_0) / theta_0 / 2))
    for _ in range(K):
        qc.compose(o, inplace=True)

        qc.compose(prep_qc.inverse(), inplace=True)
        qc.x(range(4))
        qc.mcp(pi, list(range(4 - 1)), 4 - 1)
        qc.x(range(4))
        qc.compose(prep_qc, inplace=True)

    return qc

def solve_7(n: int, o: QuantumCircuit) -> QuantumCircuit:
    qc = QuantumCircuit(8)
    prep_qc = prep_A6(8)
    # Write your code here:

    qc.compose(prep_qc, inplace=True)

    theta_0 = asin(sqrt(1 / 8))
    K = int(np.round((pi / 2 - theta_0) / theta_0 / 2))
    for _ in range(K):
        qc.compose(o, inplace=True)

        qc.compose(prep_qc.inverse(), inplace=True)
        qc.x(range(8))
        qc.mcp(pi, list(range(8 - 1)), 8 - 1)
        qc.x(range(8))
        qc.compose(prep_qc, inplace=True)

    return qc

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    if n == 2:
        return solve_2(n, o)
    if n == 7:
        return solve_7(n, o)
    qc = QuantumCircuit(n)
    prep_qc = prep_A6(n)
    # Write your code here:

    qc.compose(prep_qc, inplace=True)

    theta_0 = asin(sqrt(1 / n))
    K = int(np.round((pi / 2 - theta_0) / theta_0 / 2))
    for _ in range(K):
        qc.compose(o, inplace=True)

        qc.compose(prep_qc.inverse(), inplace=True)
        qc.x(range(n))
        qc.mcp(pi, list(range(n - 1)), n - 1)
        qc.x(range(n))
        qc.compose(prep_qc, inplace=True)

    return qc
'''


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

def psi(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

    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

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

    qc.append(GlobalPhaseGate(math.pi))
    qc.x(range(n))

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

    qc.x(range(n))

    return qc

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

    qc.compose(psi(n).inverse(), inplace=True)
    qc.compose(refl(n), inplace=True)
    qc.compose(psi(n), inplace=True)
    return qc

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

    qc.x(y)
    qc.h(y)
    qc.compose(psi(n), x, inplace=True)

    init_prob = math.sqrt(1/n)
    init_angle = math.acos(init_prob)
    target_prob = 0.9
    repeat = 0

    while True:
        angle = (2*repeat+1) * init_angle
        curr_prob = math.cos(angle)**2
        print(f'{repeat=} {curr_prob=}')
        if curr_prob > target_prob:
            break

        qc.compose(o, inplace=True)
        qc.compose(psi(n).inverse(), inplace=True)
        qc.compose(refl(n), inplace=True)
        qc.compose(psi(n), inplace=True)

        repeat += 1

    qc.h(y)
    qc.x(y)

    return qc
'''


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

def psi(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

    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

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

    qc.append(GlobalPhaseGate(math.pi))
    qc.x(range(n))

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

    qc.x(range(n))

    return qc

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

    qc.compose(psi(n).inverse(), inplace=True)
    qc.compose(refl(n), inplace=True)
    qc.compose(psi(n), inplace=True)
    return qc

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

    qc.x(y)
    qc.h(y)
    qc.compose(psi(n), x, inplace=True)

    init_prob = math.sqrt(1/n)
    init_angle = math.acos(init_prob)
    target_prob = 0.9
    repeat = 0

    for _ in range(2):
        angle = (2*repeat+1) * init_angle
        curr_prob = math.cos(angle)**2
        print(f'{repeat=} {curr_prob=}')
        if curr_prob > target_prob:
            break

        qc.compose(o, inplace=True)
        qc.compose(psi(n).inverse(), inplace=True)
        qc.compose(refl(n), inplace=True)
        qc.compose(psi(n), inplace=True)

        repeat += 1

    qc.h(y)
    qc.x(y)

    return qc
'''


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

def psi(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

    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

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

    qc.append(GlobalPhaseGate(math.pi))
    qc.x(range(n))

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

    qc.x(range(n))

    return qc

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

    qc.compose(psi(n).inverse(), inplace=True)
    qc.compose(refl(n), inplace=True)
    qc.compose(psi(n), inplace=True)
    return qc

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

    qc.x(y)
    qc.h(y)
    qc.compose(psi(n), x, inplace=True)

    init_prob = math.sqrt(1/n)
    init_angle = math.acos(init_prob)
    target_prob = 0.9
    repeat = 0

    for _ in range(3):
        angle = (2*repeat+1) * init_angle
        curr_prob = math.cos(angle)**2
        print(f'{repeat=} {curr_prob=}')
        if curr_prob > target_prob:
            break

        qc.compose(o, inplace=True)
        qc.compose(psi(n).inverse(), inplace=True)
        qc.compose(refl(n), inplace=True)
        qc.compose(psi(n), inplace=True)

        repeat += 1

    qc.h(y)
    qc.x(y)

    return qc
'''


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

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

    angle = math.acos(1/16)
    qc.ry(2*math.acos(math.sqrt(1-math.cos(angle)*n)), 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

    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

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

    qc.append(GlobalPhaseGate(math.pi))
    qc.x(range(n))

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

    qc.x(range(n))

    return qc

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

    qc.compose(psi(n).inverse(), inplace=True)
    qc.compose(refl(n), inplace=True)
    qc.compose(psi(n), inplace=True)
    return qc

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.x(y)
    qc.h(y)
    qc.compose(psi(n), x, inplace=True)

    init_prob = math.sqrt(1/16)
    init_angle = math.acos(init_prob)
    target_prob = 0.9
    repeat = 0

    while True:
        angle = (2*repeat+1) * init_angle
        curr_prob = math.cos(angle)**2
        print(f'{repeat=} {curr_prob=}')
        if curr_prob > target_prob:
            break

        qc.compose(o, inplace=True)
        qc.compose(psi(n).inverse(), inplace=True)
        qc.compose(refl(n), inplace=True)
        qc.compose(psi(n), inplace=True)

        repeat += 1

    qc.h(y)
    qc.x(y)

    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 w_state(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

def reflect_w_state(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.compose(w_state(n).inverse(), inplace=True)
    qc.x(range(n))
    qc.append(ZGate().control(n - 1), range(n))
    qc.x(range(n))
    qc.compose(w_state(n), inplace=True)
    return qc

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

    theta = math.asin(1 / (n) ** 0.5)
    iterations = math.floor(math.pi / (4 * theta))
    # print(theta, iterations)
    qc.compose(w_state(n), inplace=True)
    reflect = reflect_w_state(n)
    for _ in range(iterations):
        qc.compose(o, inplace=True)
        qc.z(y)
        qc.compose(o.inverse(), inplace=True)
        qc.compose(reflect, inplace=True)

    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 w_state(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

def reflect_w_state(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.compose(w_state(n).inverse(), inplace=True)
    qc.x(range(n))
    qc.append(ZGate().control(n - 1), range(n))
    qc.x(range(n))
    qc.compose(w_state(n), inplace=True)
    return qc

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

    theta = math.asin(1 / n ** 0.5)
    iterations = math.floor(math.pi / (4 * theta))
    # print(f"{theta = } {iterations = }")
    qc.compose(w_state(n), inplace=True)
    reflect = reflect_w_state(n)
    for _ in range(iterations):
        qc.compose(o, inplace=True)
        qc.z(y)
        qc.compose(o.inverse(), inplace=True)
        qc.compose(reflect, inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    qr = QuantumRegister(n, 'q')
    anc = QuantumRegister(1, 'ancilla')
    qc = QuantumCircuit(qr, anc)

    qc.h(qr)

    qc.x(anc)
    qc.h(anc)

    qc.compose(o, qubits=qr[:] + anc[:], inplace=True)

    qc.h(qr)
    qc.x(qr)
    qc.h(qr[-1])
    qc.mct(qr[:-1], qr[-1])
    qc.h(qr[-1])
    qc.x(qr)
    qc.h(qr)

    qc = qc.remove_final_measurements(inplace=False)

    final_qc = QuantumCircuit(n)
    final_qc.compose(qc, qubits=range(n), inplace=True)

    return final_qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    ancilla = n - 1
    qc.x(ancilla)
    qc.h(ancilla)

    qc.h(range(n-1))
    qc.compose(o, inplace=True)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    qc.h(range(n))

    qc.compose(o, inplace=True)

    qc.h(range(n))
    qc.x(range(n))
    qc.mcz(list(range(n-1)), n-1)
    qc.x(range(n))
    qc.h(range(n))

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.h(i)
    k = 75 // (2*n+4)
    for _ in range(k):
        qc.compose(o, inplace=True)
        qc.h(range(n))
        qc.x(range(n))
        qc.h(n-1)
        qc.mcx(list(range(n-1)), n-1)
        qc.h(n-1)
        qc.x(range(n))
        qc.h(range(n))
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library.standard_gates import GlobalPhaseGate, ZGate, RYGate
import math

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

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

    if n==7:
        qc.ry(math.asin(-1/math.sqrt(n+1))*2, 0)
        for i in range(1, n):
            qc.append(RYGate(math.asin(-1/math.sqrt(n-i+1))*2).control(i), range(i+1))
        # qc.mcx(list(range(n-1)), n-1)
    else:
        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)
    return qc

def b7(n: int, T: list[float]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.compose(a5(n, T).inverse(), inplace=True)
    for i in range(n):
        qc.x(i)
    qc.append(ZGate().control(n-1), list(range(n)))
    for i in range(n):
        qc.x(i)
    # qc.append(GlobalPhaseGate(math.pi))
    qc.compose(a5(n, T), inplace=True)
    return qc

# def b5(n: int) -> QuantumCircuit:
#     qc = QuantumCircuit(n)
#     # init = [0]*(2**n)
#     # init[2] = 1
#     # qc.initialize(init)
#     # Write your code here:
#     for i in range(n):
#         qc.h(i)
#     for i in range(n):
#         qc.x(i)
#     qc.append(ZGate().control(n-1), list(range(n)))
#     for i in range(n):
#         qc.x(i)
#     for i in range(n):
#         qc.h(i)
#     return qc

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(a5(n), inplace=True)

    t = math.asin(1/math.sqrt(n))
    for i in range(1, 100):
        qc.compose(o, inplace=True)
        qc.compose(b7(n), inplace=True)
        if math.sin((2*i+1)*t)**2>=0.9:
            break
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library.standard_gates import GlobalPhaseGate, ZGate, RYGate
import math

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

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

    if n==7:
        qc.ry(math.asin(-1/math.sqrt(n+1))*2, 0)
        for i in range(1, n):
            qc.append(RYGate(math.asin(-1/math.sqrt(n-i+1))*2).control(i), range(i+1))
        # qc.mcx(list(range(n-1)), n-1)
    else:
        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)
    return qc

def b7(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.compose(a5(n).inverse(), inplace=True)
    for i in range(n):
        qc.x(i)
    qc.append(ZGate().control(n-1), list(range(n)))
    for i in range(n):
        qc.x(i)
    # qc.append(GlobalPhaseGate(math.pi))
    qc.compose(a5(n), inplace=True)
    return qc

# def b5(n: int) -> QuantumCircuit:
#     qc = QuantumCircuit(n)
#     # init = [0]*(2**n)
#     # init[2] = 1
#     # qc.initialize(init)
#     # Write your code here:
#     for i in range(n):
#         qc.h(i)
#     for i in range(n):
#         qc.x(i)
#     qc.append(ZGate().control(n-1), list(range(n)))
#     for i in range(n):
#         qc.x(i)
#     for i in range(n):
#         qc.h(i)
#     return qc

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(a5(n), inplace=True)

    t = math.asin(1/math.sqrt(n))
    for i in range(1, 100):
        qc.compose(o, inplace=True)
        qc.compose(b7(n), inplace=True)
        if math.sin((2*i+1)*t)**2>=0.9:
            break
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library.standard_gates import GlobalPhaseGate, ZGate, RYGate
import math

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

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

    if n==7:
        qc.ry(math.asin(-1/math.sqrt(n+1))*2, 0)
        for i in range(1, n):
            qc.append(RYGate(math.asin(-1/math.sqrt(n-i+1))*2).control(i), range(i+1))
        # qc.mcx(list(range(n-1)), n-1)
    else:
        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)
    return qc

def b7(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.compose(a5(n).inverse(), inplace=True)
    for i in range(n):
        qc.x(i)
    qc.append(ZGate().control(n-1), list(range(n)))
    for i in range(n):
        qc.x(i)
    # qc.append(GlobalPhaseGate(math.pi))
    qc.compose(a5(n), inplace=True)
    return qc

# def b5(n: int) -> QuantumCircuit:
#     qc = QuantumCircuit(n)
#     # init = [0]*(2**n)
#     # init[2] = 1
#     # qc.initialize(init)
#     # Write your code here:
#     for i in range(n):
#         qc.h(i)
#     for i in range(n):
#         qc.x(i)
#     qc.append(ZGate().control(n-1), list(range(n)))
#     for i in range(n):
#         qc.x(i)
#     for i in range(n):
#         qc.h(i)
#     return qc

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(a5(n), inplace=True)

    t = math.asin(1/math.sqrt(n))
    if n==7:
        t = math.asin(1/math.sqrt(n+1))
    for i in range(1, 100):
        qc.compose(o, inplace=True)
        qc.compose(b7(n), inplace=True)
        if math.sin((2*i+1)*t)**2>=0.9:
            break
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library.standard_gates import GlobalPhaseGate, ZGate, RYGate
import math

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

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

    if n==7:
        qc.ry(math.asin(-1/math.sqrt(n+1))*2, 0)
        for i in range(1, n):
            qc.append(RYGate(math.asin(-1/math.sqrt(n-i+1))*2).control(i), range(i+1))
        # qc.mcx(list(range(n-1)), n-1)
    else:
        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)
    return qc

def b7(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.compose(a5(n).inverse(), inplace=True)
    for i in range(n):
        qc.x(i)
    qc.append(ZGate().control(n-1), list(range(n)))
    for i in range(n):
        qc.x(i)
    # qc.append(GlobalPhaseGate(math.pi))
    qc.compose(a5(n), inplace=True)
    return qc

# def b5(n: int) -> QuantumCircuit:
#     qc = QuantumCircuit(n)
#     # init = [0]*(2**n)
#     # init[2] = 1
#     # qc.initialize(init)
#     # Write your code here:
#     for i in range(n):
#         qc.h(i)
#     for i in range(n):
#         qc.x(i)
#     qc.append(ZGate().control(n-1), list(range(n)))
#     for i in range(n):
#         qc.x(i)
#     for i in range(n):
#         qc.h(i)
#     return qc

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    assert n!=10
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(a5(n), inplace=True)

    t = math.asin(1/math.sqrt(n))
    if n==7:
        t = math.asin(1/math.sqrt(n+1))
    for i in range(1, 100):
        qc.compose(o, inplace=True)
        qc.compose(b7(n), inplace=True)
        if math.sin((2*i+1)*t)**2>=0.9:
            break
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library.standard_gates import GlobalPhaseGate, ZGate, RYGate
import math

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

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

    if n==7 or n==2:
        qc.ry(math.asin(-1/math.sqrt(n+1))*2, 0)
        for i in range(1, n):
            qc.append(RYGate(math.asin(-1/math.sqrt(n-i+1))*2).control(i), range(i+1))
        # qc.mcx(list(range(n-1)), n-1)
    else:
        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)
    return qc

def b7(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.compose(a5(n).inverse(), inplace=True)
    for i in range(n):
        qc.x(i)
    qc.append(ZGate().control(n-1), list(range(n)))
    for i in range(n):
        qc.x(i)
    # qc.append(GlobalPhaseGate(math.pi))
    qc.compose(a5(n), inplace=True)
    return qc

# def b5(n: int) -> QuantumCircuit:
#     qc = QuantumCircuit(n)
#     # init = [0]*(2**n)
#     # init[2] = 1
#     # qc.initialize(init)
#     # Write your code here:
#     for i in range(n):
#         qc.h(i)
#     for i in range(n):
#         qc.x(i)
#     qc.append(ZGate().control(n-1), list(range(n)))
#     for i in range(n):
#         qc.x(i)
#     for i in range(n):
#         qc.h(i)
#     return qc

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(a5(n), inplace=True)

    t = math.asin(1/math.sqrt(n))
    if n==7 or n==2:
        t = math.asin(1/math.sqrt(n+1))
    for i in range(1, 100):
        qc.compose(o, inplace=True)
        qc.compose(b7(n), inplace=True)
        if math.sin((2*i+1)*t)**2>=0.9:
            break
    return qc
'''


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

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

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

    for i in range(n):
        qc.append(HGate(), [i])

    qc.compose(o, inplace=True)

    for i in range(n):
        qc.append(HGate(), [i])

    for i in range(n):
        qc.append(XGate(), [i])

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

    for i in range(n):
        qc.append(XGate(), [i])

    for i in range(n):
        qc.append(HGate(), [i])

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
    qc.y(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
    qc.x(0)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
    qc.x(₁)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
    qc.x(₀)

    return qc
'''


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

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
    for _ in range(2*10**4+1):
        qc.x(0)

    return qc

# from qiskit import Aer, execute

# def simulate(qc: QuantumCircuit):
#     simulator = Aer.get_backend("statevector_simulator")
#     statevector = execute(qc, simulator).result().get_statevector(qc)
#     print(statevector)

# if __name__ == "__main__":
#     simulate(solve())
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    qc.x(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)

    qc.x(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    qc.x();

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    qc.x(0);

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
    qc.g(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    # Create quantum circuit with 1 qubit
    qc = QuantumCircuit(1)

    # Apply X gate to flip from |0⟩ to |1⟩
    qc.x(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
    qc.x(0)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
    qc.c(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
    qc.xc(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
    qc.X(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    qc.x(0)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
qc.g(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
    qc.g(0)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
    qc.g(1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Apply the Pauli-X gate to flip the state from |0> to |1>
    qc.x(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
    qc.g(1)
    qc.data
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)

    # Write your code here:
    qc.x(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
    qc.h(0)
    qc.cx(1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
    qc.h(0)
    qc.cx(0)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
    qc.x(0)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    qc.x(0)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    qc.x(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
    qc.append(XGate())

    return qc
'''


'''python
from qiskit import QuantumCircuit, XGate

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
    qc.append(XGate())

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
circuit = QuantumCircuit(2)
circuit.h(0)
circuit.cx(0, 1)
circuit.ry(-3. * np.pi / 4., 1)

circuit.draw('mpl')
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    qc.x(1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


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

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

    return qc

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


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

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

    return qc

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


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # 量子ビット0番にXゲートを適用
    qc.x(0)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
	qc = QuantumCircuit(1)
	qc.x(0)
	return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
    qc.x(0)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.swap(0, 1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.cx(0, 1)
    qc.cx(1,0)
    qc.cx(0,1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.cx(0,1)
    qc.cx(1,0)
    qc.cx(0,1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.x(0)
    qc.cx(0,1)
    qc.cx(1,0)
    qc.cx(0,1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.h(0)
    qc.cx(0,1)

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


'''python
from qiskit import QuantumCircuit

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

    qc.cx(0,1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    qc.h(0)
    qc.h(1)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)

    # Step 1: CNOT from qubit 0 to qubit 1
    qc.cx(0, 1)

    # Step 2: CNOT from qubit 1 to qubit 0
    qc.cx(1, 0)

    # Step 3: CNOT from qubit 0 to qubit 1 again
    qc.cx(0, 1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)

    # Write your code here:
    qc.cx(0, 1)
    qc.cx(1, 0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)

    # Write your code here:
    qc.cx(0, 1)
    qc.cx(1, 0)
    qc.cx(0, 1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.cx(0, 1)
    qc.cx(1, 0)
    qc.cx(0, 1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.cx(0,1)
    qc.cx(1,0)
    qc.cx(0,1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    qc.cx(0,1)
    qc.cx(1,0)
    qc.cx(0,1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    qc.x(0)
    qc.x(1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    # Apply X gates to initialize qubits
    qc.x(0)
    qc.x(1)

    # Implement SWAP using CX gates
    qc.cx(0, 1)
    qc.cx(1, 0)
    qc.cx(0, 1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    # Implement SWAP using CX gates
    qc.cx(0, 1)
    qc.cx(1, 0)
    qc.cx(0, 1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)

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

    return qc
'''


'''python
import math
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    qc.cx(0,1)
    qc.cx(1,0)
    qc.cx(0,1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    qc.cx(0,1)
    qc.cx(1,0)
    qc.cx(0,1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    #     qc.cx(0, 1)
    # qc.cx(1, 0)
    # qc.cx(0, 1)
    return qc
'''


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

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.swap(0, 1)
    return qc
'''


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

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.cx(0, 1)
    qc.cx(1, 0)
    qc.cx(0, 1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.cx(0, 1)
    qc.cx(1, 0)
    qc.cx(0, 1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.cx(0,1)
    qc.cx(1,0)
    qc.cx(0,1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.swap(0, 1)
    return qc
'''


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

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)

    # For debugging
    # initial_state = [1/np.sqrt(2),0,  1/np.sqrt(2),0]
    # qc.initialize(initial_state, [0, 1])
    # Write your code here:
    qc.cx(0,1)
    qc.cx(1,0)
    qc.cx(0,1)
    return qc

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


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

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)

    # For debugging
    # initial_state = [1/np.sqrt(2),0,  1/np.sqrt(2),0]
    # qc.initialize(initial_state, [0, 1])
    # Write your code here:
    qc.cx(0,1)
    qc.cx(1,0)
    qc.cx(0,1)
    return qc

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


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.cx(0, 1)
    qc.cx(1, 0)
    qc.cx(0, 1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    # Create quantum circuit with 2 qubits
    qc = QuantumCircuit(2)

    # Implement SWAP using 3 CNOT gates
    qc.cx(0, 1)  # First CNOT
    qc.cx(1, 0)  # Second CNOT
    qc.cx(0, 1)  # Third CNOT

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.swap(0,1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.swap(0,1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.iswap(0,1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.iswap(0,1)
    qc.p(0)
    qc.z(0)
    qc.p(1)
    qc.z(1)
    return qc
'''


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

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.iswap(0,1)
    qc.p(3*pi/2, 0)
    qc.p(3*pi/2, 1)
    return qc
'''


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

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.iswap(0,1)
    qc.p(3*pi/2, 0)
    qc.p(3*pi/2, 1)
    qc.cz(0,1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.cx(0,1)
    qc.cx(1,0)
    qc.cx(0,1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    qc.swap(0, 1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    qc.cx(0,1)
    qc.cx(1,0)
    qc.cx(0,1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    qc.cx(0,1)
    qc.cx(1,0)
    qc.cx(0,1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    return qc
'''


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

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

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

    return qc

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


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.cx(0, 1)
    qc.cx(1, 0)
    qc.cx(0, 1)

    return qc
'''


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

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

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.cx(0, 1)
    qc.cx(1, 0)
    qc.cx(0, 1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.cx(0,1)
    qc.cx(1,0)
    qc.cx(0,1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.cx(0, 1)
    qc.cx(1, 0)
    qc.cx(0, 1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.swap(0, 1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.swap(0,2)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.swap(0,1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    qc.cx(0,1)
    qc.cx(1,0)
    qc.cx(0,1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # 以下三次 CNOT 就可实现 SWAP
    qc.cx(0, 1)
    qc.cx(1, 0)
    qc.cx(0, 1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
	qc.cx(0, 1)
	qc.cx(1, 0)
    qc.cx(0, 1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.cx(0,1)
    qc.cx(1,0)
    qc.cx(0,1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.cx(1,0)
    qc.cx(0,1)
    qc.cx(1,0)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.quantum_info import Statevector

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.cx(0, 1)
    qc.cx(1, 0)
    qc.cx(0, 1)
    return qc

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


'''python
from qiskit import QuantumCircuit

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.x(0)
    qc.cx(0,1)
    qc.cx(1,0)
    qc.cx(0,1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.cx(0,1)
    qc.cx(1,0)
    qc.cx(0,1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    qc.cx(0, 1)
    qc.cx(1, 0)
    qc.cx(0, 1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.h(0)
    qc.h(1)
    qc.cx(0,1)
    qc.cx(1,0)
    qc.cx(0,1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    qc.cx(0, 1)
    qc.cx(1, 0)
    qc.cx(0, 1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.cx(0, 1)
    qc.cx(1, 0)
    qc.cx(0, 1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.x(1)
    qc.cx(0,1)
    qc.cx(1,0)
    qc.cx(0,1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.x(0)
    qc.x(1)
    qc.cx(0,1)
    qc.cx(1,0)
    qc.cx(0,1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.h(0)
    qc.h(1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    qc.cx(0,1)
    qc.cx(1,0)
    qc.cx(0,1)
    # Write your code here:

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.swap()
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.swap(1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.swap(1, 2)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.swap(0, 1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.cx(0, 1)
    qc.cz(0, 1)
    qc.cx(0, 1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.cx(0, 1)
    qc.cx(1, 0)
    qc.cx(0, 1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.cx(0,1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.cx(0,1)
    qc.cx(1,0)
    qc.cx(0,1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.swap(0, 1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
	qc = QuantumCircuit(2)
	qc.cx(0, 1)
	qc.cx(1, 0)
	qc.cx(0, 1)
	return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    # Write your code here:
    qc.cx(0, 1)
    qc.cx(1, 0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int) -> QuantumCircuit:
    # Create quantum circuit with n qubits
    qc = QuantumCircuit(n)

    # Implement cyclic right shift using CNOT gates
    # We'll do this from right to left to maintain the correct ordering
    for i in range(n-1, -1, -1):
        # Target is current position, control is previous position (cyclically)
        prev = (i - 1) % n
        qc.cx(prev, i)

    # Now uncompute the extra copies, going left to right
    for i in range(n):
        # Target is current position, control is previous position (cyclically)
        prev = (i - 1) % n
        qc.cx(prev, i)

    # Finally, apply X gates to swap 0s and 1s where needed
    for i in range(n):
        qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int) -> QuantumCircuit:
    # Create quantum circuit with n qubits
    qc = QuantumCircuit(n)

    # Implement cyclic right shift using CNOT gates only
    # For right shift, we need to implement the shift in reverse order
    # to avoid overwriting values we still need
    for i in range(n-1):
        qc.cx(i, i+1)

    # Final CNOT to wrap around
    qc.cx(n-1, 0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int) -> QuantumCircuit:
    # Create quantum circuit with n qubits
    qc = QuantumCircuit(n)

    # Implement cyclic right shift
    # We need three CNOTs to swap without using SwapGate
    for i in range(n-1, -1, -1):
        next_i = (i + 1) % n
        # These three CNOTs implement the swap operation
        qc.cx(i, next_i)
        qc.cx(next_i, i)
        qc.cx(i, next_i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    # Perform the cyclic shift using CNOT and X gates
    for i in range(n - 1, 0, -1):
        qc.cx(i - 1, i)
        qc.cx(i, i - 1)
        qc.cx(i - 1, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    # Implement the right shift using CX gates
    for i in range(n-1):
        qc.cx(i, i+1)
        qc.cx(i+1, i)
        qc.cx(i, i+1)

    # Swap the first and last qubits
    qc.cx(0, n-1)
    qc.cx(n-1, 0)
    qc.cx(0, n-1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.cx(0,n-1)
    qc.cx(n-1,0)
    qc.cx(0,n-1)
    for i in range(n-1,1,-1):
        qc.cx(i,i-1)
        qc.cx(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:
    for i in range(n-1):
        swap(qc, i, i+1)
        print(i,i+1)
    if not n==2:
        swap(qc, n-1, 0)
        print(n-1,0)

    return qc

def swap(qc, a, b):
    qc.cx(a, b)
    qc.cx(b, a)
    qc.cx(a, b)
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1):
        swap(qc, i, i+1)
    if not n==2:
        swap(qc, n-1, 0)

    return qc

def swap(qc, a, b):
    qc.cx(a, b)
    qc.cx(b, a)
    qc.cx(a, b)
'''


'''python
from qiskit import QuantumCircuit

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

    if n != 2:
        qc.cx(n-1, 0)
        qc.cx(0, n-1)
        qc.cx(n-1, 0)

    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):
        swap(qc, n-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-1):
        swap(qc, n-1, i)

    return qc

def swap(qc, a, b):
    qc.cx(a, b)
    qc.cx(b, a)
    qc.cx(a, b)
'''


'''python
from qiskit import QuantumCircuit

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def my_swap(qc, q1, q2):
    qc.cx(q1, q2)
    qc.cx(q2, q1)
    qc.cx(q1, q2)

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in reversed(range(n-1)):
    	my_swap(qc, 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):
        cx.(n-i, 0)
        cx.(0, n-i)
        cx.(n-i,0)

    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):
        qc.cx(n-i, 0)
        qc.cx(0, n-i)
        qc.cx(n-i,0)

    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-1):
        qc.cx(n-i, 0)
        qc.cx(0, n-i)
        qc.cx(n-i,0)

    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):
        qc.cx(n-1, i)
        qc.cx(i, n-1)
        qc.cx(n-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 - 1):
        qc.x(n - i - 1, n - i - 2)
        qc.x(n - i - 2, n - i - 1)
        qc.x(n - i - 1, n - i - 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):
        qc.cx(n - i - 1, n - i - 2)
        qc.cx(n - i - 2, n - i - 1)
        qc.cx(n - i - 1, n - i - 2)
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    qc.cx(0,n-1)
    qc.cx(n-1,0)
    qc.cx(0,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(reversed(1, n):
        qc.cx(i-2,i-1)
        qc.cx(i-1,i-2)
        qc.cx(i-2,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(reversed(1, n+1):
        qc.cx(i-2,i-1)
        qc.cx(i-1,i-2)
        qc.cx(i-2,i-1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


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

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

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

    def swap(a, b):
        qc.cx(a, b)
        qc.cx(b, a)
        qc.cx(a, b)

    # Write your code here:
    def cyc(L, R):
        if R - L == 1:
            return
        mid = (L + R) // 2
        swap(mid - 1, R - 1)
        cyc(L, mid)
        cyc(mid, R)

    cyc(0, n)

    return qc
'''