'''python
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import ZGate
import math
"""
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)
    k = 45
    for _ in range(k):
        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, QuantumRegister
from qiskit.circuit.library import ZGate
import math
"""
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)
    k = 30
    for _ in range(k):
        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, QuantumRegister
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(n**(-0.5)))**2 for r in range(30)]
    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, QuantumRegister
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(30)]
    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, 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 flipzero(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    for i in range(n):
        if i == 0:
            qc.z(0)
        else:
            qc.append(ZGate().control(i), range(i+1))
        qc.x(i)

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

    return qc

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

    qc.compose(flipzero(n), inplace=True)

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

    return qc

def solve(n: int, Uf: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    qc.x(n)
    qc.h(n)
    for i in range(n):
        qc.h(i)

    r = pi / 4 * sqrt(2 ** n)
    num = int(round(r))
    if n == 2:
        num = 1

    for _ in range(num):
        qc.compose(Uf, inplace=True)
        qc.compose(Us(n), inplace=True)

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

    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 flipzero(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

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

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

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

    return qc

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

    qc.compose(flipzero(n), inplace=True)

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

    return qc

def solve(n: int, Uf: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    qc.x(n)
    qc.h(n)
    for i in range(n):
        qc.h(i)

    r = pi / 4 * sqrt(2 ** n)
    num = int(round(r))
    if n == 2:
        num = 1

    for _ in range(num):
        qc.compose(Uf, inplace=True)
        qc.compose(Us(n), inplace=True)

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

    return qc
'''


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

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

    # Step 1: Initialize ancilla qubit y to |-> state for phase-flip conversion
    qc.x(y[0])
    qc.h(y[0])

    # Step 2: Create initial superposition on x register
    qc.h(x)

    # Step 3: Calculate optimal number of iterations
    # For single target: M=1, N=2^n
    # optimal_iterations ≈ π/4 * sqrt(N)
    N = 2**n
    optimal_iterations = round(math.pi / 4 * math.sqrt(N))

    # Step 4: Apply Grover operator optimal_iterations times
    for _ in range(optimal_iterations):
        # Phase-flip oracle: apply the given bit-flip oracle
        # Since y is in |-> state, bit-flip becomes phase-flip
        qc.compose(o, inplace=True)

        # Diffusion operator (amplitude amplification about average)
        # 1. Apply H to all x qubits
        qc.h(x)

        # 2. Apply X to all x qubits (to flip |0...0> to |1...1>)
        qc.x(x)

        # 3. Apply multi-controlled Z gate (phase flip |1...1> state)
        if n == 1:
            qc.z(x[0])
        elif n == 2:
            qc.cz(x[0], x[1])
        else:
            # Multi-controlled Z gate using ancilla
            # For n>2, we need to implement multi-controlled Z
            # Using the fact that MCZ = MCX with target in |-> state
            qc.h(x[n-1])  # Convert last qubit to |+/-⟩ basis

            # Multi-controlled X gate on last qubit
            if n == 3:
                qc.ccx(x[0], x[1], x[n-1])
            else:
                # For larger n, implement using decomposition
                # This is a simplified approach for the contest
                controls = list(range(n-1))
                qc.mcx(controls, x[n-1])

            qc.h(x[n-1])  # Convert back

        # 4. Apply X to all x qubits (flip back)
        qc.x(x)

        # 5. Apply H to all x qubits
        qc.h(x)

    return qc
'''


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

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

    # Step 1: Initialize uniform superposition on x qubits
    for i in range(n):
        qc.h(x[i])

    # Step 2: Initialize ancilla qubit y in |1> state for phase oracle
    qc.x(y[0])
    qc.h(y[0])

    # Step 3: Calculate optimal number of iterations
    # For 1 marked state out of 2^n states: approximately π√(2^n)/4
    num_iterations = int(np.pi * np.sqrt(2**n) / 4)
    if num_iterations == 0:
        num_iterations = 1

    # Step 4: Grover iterations
    for _ in range(num_iterations):
        # Apply oracle O
        qc.compose(o, inplace=True)

        # Apply diffusion operator (inversion about average)
        # H gates
        for i in range(n):
            qc.h(x[i])

        # X gates (flip all qubits)
        for i in range(n):
            qc.x(x[i])

        # Multi-controlled Z gate (phase flip on |111...1>)
        if n == 1:
            qc.z(x[0])
        elif n == 2:
            qc.cz(x[0], x[1])
        else:
            # Multi-controlled Z using ancilla-free implementation
            qc.h(x[n-1])
            for i in range(n-1):
                qc.cx(x[i], x[n-1])
            qc.h(x[n-1])

        # X gates (flip back)
        for i in range(n):
            qc.x(x[i])

        # H gates
        for i in range(n):
            qc.h(x[i])

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

"""
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:
    qc.h(range(n))
    for i in range(int(0.8 * n)):
        qc.compose(o, inplace=True)
        qc.z(y)
        qc.compose(o, inplace=True)
        qc.h(range(n))
        qc.x(list(range(n)))
        qc.mcp(np.pi, list(range(n-1)), n-1)
        qc.x(list(range(n)))
        qc.h(range(n))
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
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:
    qc.h(range(n))
    for i in range(int(0.8 * n)):
        qc.compose(o, inplace=True)
        qc.z(y)
        qc.compose(o, inplace=True)
        qc.h(range(n))
        qc.x(list(range(n)))
        qc.mcp(np.pi, list(range(n-1)), n-1)
        qc.x(list(range(n)))
        qc.h(range(n))
    return qc
'''


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

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

iter = [4, 4, 11, 9, 2, 2, 11, 7, 7]

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.h(range(iter[n - 2]))
    for i in range(int(0.8 * n)):
        qc.compose(o, inplace=True)
        qc.z(y)
        qc.compose(o, inplace=True)
        qc.h(range(n))
        qc.x(list(range(n)))
        qc.mcp(np.pi, list(range(n-1)), n-1)
        qc.x(list(range(n)))
        qc.h(range(n))
    return qc
'''


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

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

iter = [4, 4, 11, 9, 2, 2, 11, 7, 7]

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.h(range(n))
    for i in range(iter[n - 2]):
        qc.compose(o, inplace=True)
        qc.z(y)
        qc.compose(o, inplace=True)
        qc.h(range(n))
        qc.x(list(range(n)))
        qc.mcp(np.pi, list(range(n-1)), n-1)
        qc.x(list(range(n)))
        qc.h(range(n))
    return qc
'''


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

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

iter = [10, 4, 8, 2, 2, 2, 11, 7, 7]

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.h(range(n))
    for i in range(iter[n - 2]):
        qc.compose(o, inplace=True)
        qc.z(y)
        qc.compose(o, inplace=True)
        qc.h(range(n))
        qc.x(list(range(n)))
        qc.mcp(np.pi, list(range(n-1)), n-1)
        qc.x(list(range(n)))
        qc.h(range(n))
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
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:
    qc.h(range(n))
    for i in range(int(3.14 / 4 * 3 * n)):
        qc.compose(o, inplace=True)
        qc.z(y)
        qc.compose(o, inplace=True)
        qc.h(range(n))
        qc.x(list(range(n)))
        qc.mcp(np.pi, list(range(n-1)), n-1)
        qc.x(list(range(n)))
        qc.h(range(n))
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
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:
    qc.h(range(n))
    for i in range(int(3.14 / 4 * 5 * n)):
        qc.compose(o, inplace=True)
        qc.z(y)
        qc.compose(o, inplace=True)
        qc.h(range(n))
        qc.x(list(range(n)))
        qc.mcp(np.pi, list(range(n-1)), n-1)
        qc.x(list(range(n)))
        qc.h(range(n))
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
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:
    qc.h(range(n))
    for i in range(int(3.14 / 4 * 4 * n)):
        qc.compose(o, inplace=True)
        qc.z(y)
        qc.compose(o, inplace=True)
        qc.h(range(n))
        qc.x(list(range(n)))
        qc.mcp(np.pi, list(range(n-1)), n-1)
        qc.x(list(range(n)))
        qc.h(range(n))
    return qc
'''


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

"""
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:
    iter = 1
    for i in range(20):
        s = (iter + 1)* 2 * math.asin(1 / math.sqrt(n))
        for j in range(6):
            if abs(s - (j + 1 / 2) * math.pi) < 0.1: break
        iter += 1
    qc.h(range(n))
    for i in range(iter):
        qc.compose(o, inplace=True)
        qc.z(y)
        qc.compose(o, inplace=True)
        qc.h(range(n))
        qc.x(list(range(n)))
        qc.mcp(np.pi, list(range(n-1)), n-1)
        qc.x(list(range(n)))
        qc.h(range(n))
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
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:
    qc.h(range(n))
    for i in range(int(0.8 * (2 ** n))):
        qc.compose(o, inplace=True)
        qc.z(y)
        qc.compose(o, inplace=True)
        qc.h(range(n))
        qc.x(list(range(n)))
        qc.mcp(np.pi, list(range(n-1)), n-1)
        qc.x(list(range(n)))
        qc.h(range(n))
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
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:
    qc.h(range(n))
    for i in range(int(0.8 * np.sqrt(2 ** n))):
        qc.compose(o, inplace=True)
        qc.z(y)
        qc.compose(o, inplace=True)
        qc.h(range(n))
        qc.x(list(range(n)))
        qc.mcp(np.pi, list(range(n-1)), n-1)
        qc.x(list(range(n)))
        qc.h(range(n))
    return qc
'''


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

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

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

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

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

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

    return qc

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

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

    return qc
'''


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

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

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

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

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

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

    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.h(x)

    init_prob = math.sqrt(1/(2**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
        if curr_prob > target_prob:
            break

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

        repeat += 1

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

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

def diffuser(n):
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.h(i)
    for i in range(n):
        qc.x(i)

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

    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:
    niter = math.floor(math.pi/4  * math.sqrt(2**n))
    diff = diffuser(n)
    for i in range(n):
        qc.h(i)
    for i in range(niter):
        qc.compose(o, inplace=True)
        qc.z(y)
        qc.compose(o, inplace=True)
        qc.compose(diff, inplace=True)
    return qc
'''


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

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

def diffuser(n):
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.h(i)
    for i in range(n):
        qc.x(i)

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

    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:
    niter = math.floor(math.pi/4  * math.sqrt(2**n))
    diff = diffuser(n)
    for i in range(n):
        qc.h(i)
    for i in range(niter):
        qc.compose(o, inplace=True)
        qc.z(y)
        qc.compose(o, inplace=True)
        qc.compose(diff, inplace=True)
    return qc
'''


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

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

def diffuser(n):
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.h(i)

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

    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:
    niter = math.floor(math.pi/4  * math.sqrt(2**n))
    diff = diffuser(n)
    for i in range(n):
        qc.h(i)
    for i in range(niter):
        qc.compose(o, inplace=True)
        qc.z(y)
        qc.compose(o, inplace=True)
        qc.compose(diff, inplace=True)
    return qc
'''


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

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

def diffuser(n):
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    for i in range(n):
        qc.h(i)
    for i in range(n):
        qc.x(i)

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

    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:
    niter = math.floor(math.pi/4  * math.sqrt(2**n))
    diff = diffuser(n)
    for i in range(n):
        qc.h(i)
    for i in range(niter):
        qc.compose(o, inplace=True)
        qc.z(y)
        qc.compose(o, inplace=True)
        qc.compose(diff, inplace=True)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

"""
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:

    return qc
'''


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

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

def b2(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(o, inplace=True)
    qc.z(y[0])
    qc.compose(o, 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.h(0)
    qc.mcx(list(range(1, n)), 0)
    qc.h(0)
    for i in range(n):
        qc.x(i)
    qc.append(GlobalPhaseGate(math.pi))
    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:
    for i in range(n):
        qc.h(i)
    t = math.asin(1/math.sqrt(2**n))
    for i in range(1, 100):
        qc.compose(b2(n, o), inplace=True)
        qc.compose(b5(n), list(range(n)), inplace=True)
        if math.sin((2*i+1)*t)**2>=0.9:
            break
    return qc
'''


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

"""
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:

    qc.h(range(n))

    for _ in range(int(math.pi / 4 * math.sqrt(2 ** n))):
        qc.compose(o, inplace=True)
        qc.z(n)
        qc.compose(o, inplace=True)

        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
import math

"""
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:

    qc.h(range(n))

    k = math.ceil(math.pi / 4 * math.sqrt(2 ** n))
    for _ in range(k):
        qc.compose(o, inplace=True)
        qc.z(n)
        qc.compose(o, inplace=True)

        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
import math
from qiskit.circuit.library.standard_gates import MCPhaseGate

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

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    # 注意: 今回の与えられたオラクル O は，x が L であるときに y を反転させるので，
    # y–レジスタを (|0⟩–|1⟩)/√2 にしておくと，O の作用は
    #   |x⟩ (|0⟩–|1⟩)/√2 → { –|x⟩ (|0⟩–|1⟩)/√2   (x = L)
    #                           |x⟩ (|0⟩–|1⟩)/√2    (x ≠ L) }
    # となり，Grover オラクルとして利用できます．
    #
    # また，最終的に (x,y) = (L,L) を得るためには，x–レジスタが |L⟩ (ほぼ)になった後，
    # little–エンディアンで x[0]（最下位ビット）の値に合わせて y を補正します．
    #
    # （なお，本問題では y–レジスタは 1 量子ビットであるため，
    # 　最終的に |L⟩|L⟩ とできるのは L ∈ {0,1} であると解釈しています．）

    # x: n 量子ビット，y: 1 量子ビット
    x = QuantumRegister(n, 'x')
    y = QuantumRegister(1, 'y')
    qc = QuantumCircuit(x, y)

    # --- 1. 初期状態準備 ---
    # x–レジスタを一様重ね合わせに
    qc.h(x)
    # y–レジスタを |0⟩ から (|0⟩–|1⟩)/√2 にする： X → H
    qc.x(y)
    qc.h(y)

    # --- 2. Grover 反復 ---
    # 1 個の解があるとき，θ = arcsin(1/√(2^n))
    theta = math.asin(1 / math.sqrt(2**n))
    # 最適回数（四捨五入）:
    r = int(round((math.pi / (4 * theta)) - 0.5))

    for _ in range(r):
        # (a) オラクルの適用
        qc.compose(o, qubits=x[:] + y[:], inplace=True)
        # (b) 拡散演算子 (inversion–about–the–mean) を x–レジスタに作用
        qc.h(x)
        qc.x(x)
        # ここで，|11…1⟩にのみ位相 –1 を付与する多重制御位相ゲートを MCPhaseGate で実装．
        # x のうち，先頭 n–1 ビットを制御，最後のビットをターゲットとして働かせる．
        qc.append(MCPhaseGate(math.pi, num_ctrl_qubits=n-1),
                  qargs=list(x[:-1]) + [x[-1]])
        qc.x(x)
        qc.h(x)

    # --- 3. y–レジスタの補正 ---
    # 現在，x–レジスタはほぼ |L⟩，y–レジスタは依然 (|0⟩–|1⟩)/√2 のままです．
    # ここで，little–エンディアンでは x[0] が L の最下位ビットとなるので，
    # その値に合わせて y を |L⟩ に変換します．
    #
    # 具体的には，
    # ・まず y に H を作用すると (|0⟩–|1⟩)/√2 は |1⟩ になります．
    qc.h(y)
    # ・x[0] の値が 0 の場合にのみ y を反転させるように，x[0] を反転してから制御付き X を適用，
    # 　その後，x[0] を元に戻します．
    qc.x(x[0])
    qc.cx(x[0], y[0])
    qc.x(x[0])

    return qc
'''


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

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

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    # x: n量子ビットレジスタ（little‐エンディアンで整数をエンコード）
    # y: 1量子ビットレジスタ
    x = QuantumRegister(n, 'x')
    y = QuantumRegister(1, 'y')
    qc = QuantumCircuit(x, y)

    # 1. 初期状態の準備
    qc.h(x)    # x‐レジスタを一様重ね合わせ状態に
    qc.x(y)    # y を |0⟩→|1⟩に変換
    qc.h(y)    # y を Hadamard して (|0⟩ - |1⟩)/√2 に

    # 2. Grover 反復回数の設定
    theta = math.asin(1 / math.sqrt(2**n))
    # r = round( (pi/(4θ)) - 0.5 ) 　と同値
    r = int(round((math.pi / (4 * theta)) - 0.5))

    # 3. Grover 反復
    for _ in range(r):
        # (a) オラクル O の適用
        qc.compose(o, inplace=True)  # 注意：ここは問題文の「qc.compose(o, inplace=True)」に従う
        # (b) 拡散演算子（inversion-about-the-mean）を x‐レジスタに作用
        qc.h(x)
        qc.x(x)
        qc.append(MCPhaseGate(math.pi, num_ctrl_qubits=n-1), qargs=list(x[:-1]) + [x[-1]])
        qc.x(x)
        qc.h(x)

    # 4. y‐レジスタの補正
    # 現在、x‐レジスタはほぼ |L⟩、y‐レジスタは (|0⟩ - |1⟩)/√2 のまま．
    # まず Hadamard で y を |1⟩に変換
    qc.h(y)
    # x[0]（LSB）が L の下位ビットと一致するはずなので、x[0] の値が 0 の場合にのみ y を反転する操作を実現するために
    qc.x(x[0])
    qc.cx(x[0], y[0])
    qc.x(x[0])

    return qc
'''


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

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

    # Step 1: Prepare superposition on the n qubits
    for i in range(n):
        qc.h(x[i])  # Apply Hadamard gate to each qubit in the x register

    # Step 2: Apply the oracle
    qc.compose(o, inplace=True)  # Apply the oracle O

    # Step 3: Measurement (not included in the circuit but will be done after)
    # We will measure the x register to get the value of L
    qc.measure_all()  # Measure all qubits

    return qc
'''


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

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

    # Apply Hadamard gate to all qubits
    for i in range(n):
        qc.h(x[i])

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

    # Apply Hadamard gate to all qubits again
    for i in range(n):
        qc.h(x[i])

    # Measure all qubits
    qc.measure_all()

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

"""
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:
    qc.h(x)
    qc.compose(o, inplace=True)
    qc.p(math.pi,y)
    qc.compose(o, inplace=True)
    for i in range(n):
        qc.h(i)
    for i in range(n):
        qc.x(i)
    qc.mcp(math.pi,list(range(n-1)),n-1)
    for i in range(n):
        qc.x(i)
    for i in range(n):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import math
"""
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:
    qc.h()
    qc.compose(o, inplace=True)
    qc.p(math.pi,y)
    qc.compose(o, inplace=True)
    for i in range(n):
        qc.h(i)
    for i in range(n):
        qc.x(i)
    qc.mcp(math.pi,list(range(n-1)),n-1)
    for i in range(n):
        qc.x(i)
    for i in range(n):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import math
"""
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:
    qc.h(x)
    qc.compose(o, inplace=True)
    qc.p(math.pi,y)
    qc.compose(o, inplace=True)
    for i in range(n):
        qc.h(i)
    for i in range(n):
        qc.x(i)
    qc.mcp(math.pi,list(range(n-1)),n-1)
    for i in range(n):
        qc.x(i)
    for i in range(n):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import math
"""
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:
    qc.h(x)
    for j in range(20):
        qc.compose(o, inplace=True)
        qc.p(math.pi,y)
        qc.compose(o, inplace=True)
        for i in range(n):
            qc.h(i)
        for i in range(n):
            qc.x(i)
        qc.mcp(math.pi,list(range(n-1)),n-1)
        for i in range(n):
            qc.x(i)
        for i in range(n):
            qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import math
"""
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:
    qc.h(x)
    for j in range(50):
        qc.compose(o, inplace=True)
        qc.p(math.pi,y)
        qc.compose(o, inplace=True)
        for i in range(n):
            qc.h(i)
        for i in range(n):
            qc.x(i)
        qc.mcp(math.pi,list(range(n-1)),n-1)
        for i in range(n):
            qc.x(i)
        for i in range(n):
            qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import math
"""
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:
    qc.h(x)
    for j in range(30):
        qc.compose(o, inplace=True)
        qc.p(math.pi,y)
        qc.compose(o, inplace=True)
        for i in range(n):
            qc.h(i)
        for i in range(n):
            qc.x(i)
        qc.mcp(math.pi,list(range(n-1)),n-1)
        for i in range(n):
            qc.x(i)
        for i in range(n):
            qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import math
"""
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:
    qc.h(x)
    for j in range(40):
        qc.compose(o, inplace=True)
        qc.p(math.pi,y)
        qc.compose(o, inplace=True)
        for i in range(n):
            qc.h(i)
        for i in range(n):
            qc.x(i)
        qc.mcp(math.pi,list(range(n-1)),n-1)
        for i in range(n):
            qc.x(i)
        for i in range(n):
            qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import math
"""
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:
    qc.h(x)
    for j in range(40):
        qc.compose(o, inplace=True)
        qc.p(math.pi,y)
        qc.compose(o, inplace=True)
        qc.h(x)
        qc.x(x)
        qc.mcp(math.pi,list(range(n-1)),n-1)
        qc.x(x)
        qc.h()

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import math
"""
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:
    qc.h(x)
    for j in range(40):
        qc.compose(o, inplace=True)
        qc.p(math.pi,y)
        qc.compose(o, inplace=True)
        qc.h(x)
        qc.x(x)
        qc.mcp(math.pi,list(range(n-1)),n-1)
        qc.x(x)
        qc.h(x)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import math
"""
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:
    qc.h(x)
    for j in range(35):
        qc.compose(o, inplace=True)
        qc.p(math.pi,y)
        qc.compose(o, inplace=True)
        qc.h(x)
        qc.x(x)
        qc.mcp(math.pi,list(range(n-1)),n-1)
        qc.x(x)
        qc.h(x)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import math
"""
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:
    qc.h(x)
    for j in range(34):
        qc.compose(o, inplace=True)
        qc.p(math.pi,y)
        qc.compose(o, inplace=True)
        qc.h(x)
        qc.x(x)
        qc.mcp(math.pi,list(range(n-1)),n-1)
        qc.x(x)
        qc.h(x)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import math
"""
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:
    qc.h(x)
    for j in range(33):
        qc.compose(o, inplace=True)
        qc.p(math.pi,y)
        qc.compose(o, inplace=True)
        qc.h(x)
        qc.x(x)
        qc.mcp(math.pi,list(range(n-1)),n-1)
        qc.x(x)
        qc.h(x)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import math
"""
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:
    qc.h(x)
    for j in range(32):
        qc.compose(o, inplace=True)
        qc.p(math.pi,y)
        qc.compose(o, inplace=True)
        qc.h(x)
        qc.x(x)
        qc.mcp(math.pi,list(range(n-1)),n-1)
        qc.x(x)
        qc.h(x)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import math
"""
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:
    qc.h(x)
    for j in range(31):
        qc.compose(o, inplace=True)
        qc.p(math.pi,y)
        qc.compose(o, inplace=True)
        qc.h(x)
        qc.x(x)
        qc.mcp(math.pi,list(range(n-1)),n-1)
        qc.x(x)
        qc.h(x)

    return qc
'''


'''python
from math import ceil,floor,acos,sqrt,pi
from qiskit import QuantumCircuit,QuantumRegister

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.h(x)
    a=1 / sqrt(2**n)
    r=round(pi/4/asin(a)-1/2)
    for j in range(r):
        qc.compose(o, inplace=True)
        qc.p(pi,y)
        qc.compose(o, inplace=True)
        qc.h(x)
        qc.x(x)
        qc.mcp(math.pi,list(range(n-1)),n-1)
        qc.x(x)
        qc.h(x)

    return qc
'''


'''python
from math import ceil,floor,acos,sqrt,pi
from qiskit import QuantumCircuit,QuantumRegister

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.h(x)
    a=1 / sqrt(2**n)
    r=round(pi/4/asin(a)-1/2)
    for j in range(r):
        qc.compose(o, inplace=True)
        qc.p(pi,y)
        qc.compose(o, inplace=True)
        qc.h(x)
        qc.x(x)
        qc.mcp(pi,list(range(n-1)),n-1)
        qc.x(x)
        qc.h(x)

    return qc
'''


'''python
from math import ceil,floor,asin,sqrt,pi
from qiskit import QuantumCircuit,QuantumRegister

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.h(x)
    a=1 / sqrt(2**n)
    r=round(pi/4/asin(a)-1/2)
    for j in range(r):
        qc.compose(o, inplace=True)
        qc.p(pi,y)
        qc.compose(o, inplace=True)
        qc.h(x)
        qc.x(x)
        qc.mcp(pi,list(range(n-1)),n-1)
        qc.x(x)
        qc.h(x)

    return qc
'''


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

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

    qc.compose(o, qubits=[*x, y[0]], inplace=True)
    qc.z(y[0])
    qc.compose(o, qubits=[*x, y[0]], inplace=True)

    for t in range(10):

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

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

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

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

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

    return qc
'''


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

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

    qc.compose(o, qubits=[*x, y[0]], inplace=True)
    qc.z(y[0])
    qc.compose(o, qubits=[*x, y[0]], inplace=True)

    for t in range(100):

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

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

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

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

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

    return qc
'''


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

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

    qc.compose(o, qubits=[*x, y[0]], inplace=True)
    qc.z(y[0])
    qc.compose(o, qubits=[*x, y[0]], inplace=True)

    for t in range(40):

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

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

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

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

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

    return qc
'''


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

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

    T = 40
    for t in range(T):
        qc.compose(o, qubits=[*x, y[0]], inplace=True)
        qc.z(y[0])
        qc.compose(o, qubits=[*x, y[0]], inplace=True)

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

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

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

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

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

    return qc
'''


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

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

    T = 30
    for t in range(T):
        qc.compose(o, qubits=[*x, y[0]], inplace=True)
        qc.z(y[0])
        qc.compose(o, qubits=[*x, y[0]], inplace=True)

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

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

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

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

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

    return qc
'''


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

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

    amp1 = -(2 ** (-n))
    amp2 = 2 ** (-n)

    T = 1
    for t in range(T):
        qc.compose(o, qubits=[*x, y[0]], inplace=True)
        qc.z(y[0])
        qc.compose(o, qubits=[*x, y[0]], inplace=True)

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

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

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

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

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

        amp1, amp2 = (math.sqrt(2 ** (-n)) - 1) * amp1 + (2 ** n - 1) * math.sqrt(2 ** (-n)) * amp2, math.sqrt(2 ** (-n)) * amp1 + (math.sqrt(2 ** (-n)) - 1) * amp2 + (2 ** n - 2) * math.sqrt(2 ** (-n)) * amp2

#        print(amp1)
#        print(amp2)

        prob = amp1 * amp1 / (amp1 * amp1 + amp2 * amp2 * (2 ** n - 1))
#        print(prob)
        if prob > 0.92:
            break

    return qc
'''


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

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

    amp1 = -math.sqrt(2 ** (-n))
    amp2 = math.sqrt(2 ** (-n))

    T = 30
    for t in range(T):
        qc.compose(o, qubits=[*x, y[0]], inplace=True)
        qc.z(y[0])
        qc.compose(o, qubits=[*x, y[0]], inplace=True)

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

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

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

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

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

        amp1, amp2 = (2 ** (-n) - 1) * amp1 + (2 ** n - 1) * 2 ** (-n) * amp2, math.sqrt(2 ** (-n)) * amp1 + (2 ** (-n) - 1) * amp2 + (2 ** n - 2) * math.sqrt(2 ** (-n)) * amp2

        prob = amp1 * amp1 / (amp1 * amp1 + amp2 * amp2 * (2 ** n - 1))
        if prob > 0.92:
            break

    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 preparation(targets: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(len(targets))
    qc.h(targets)
    return qc

def diffusion(qc, targets: list[int]):
    o = preparation(targets)

    qc.compose(o.inverse(), inplace=True)
    qc.x(targets)
    qc.mcp(pi, targets[:-1], targets[-1])
    qc.x(targets)
    qc.compose(o, inplace=True)

"""
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:

    qc.compose(preparation(list(range(n))), inplace=True)
    qc.h(y)

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

    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 preparation(targets: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(len(targets))
    qc.h(targets)
    return qc

def diffusion(qc, targets: list[int]):
    o = preparation(targets)

    qc.compose(o.inverse(), inplace=True)
    qc.x(targets)
    qc.mcp(pi, targets[:-1], targets[-1])
    qc.x(targets)
    qc.compose(o, inplace=True)

"""
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:

    qc.compose(preparation(list(range(n))), inplace=True)
    qc.h(y)

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

    qc.h(y)
    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,
# )

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

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

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

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

        qc.h(range(n))
        qc.x(range(n))
        qc.mcp(pi, range(n - 1), n - 1)
        qc.x(range(n))
        qc.h(range(n))

    qc.h(y)
    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,
# )

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

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

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

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

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

    qc.h(y)
    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,
# )

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

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

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

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

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

    qc.h(y)
    qc.x(y)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
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:

    qc.h(range(n))  # メインの n 量子ビット
    qc.h(n)  # 補助ビット

    qc.compose(o, inplace=True)

    qc.h(range(n))  # Hゲートを適用
    qc.x(range(n))  # Xゲートを適用
    qc.h(n-1)  # 最後のビットにHゲートを適用
    qc.mcx(list(range(n-1)), n-1)  # マルチ制御Xゲート (制御は n-1 ビット)
    qc.h(n-1)  # 再びHゲートを適用
    qc.x(range(n))  # Xゲートを適用
    qc.h(range(n))  # 再びHゲートを適用

    return qc
'''


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

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

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

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

    theta = math.asin(1 / (2 ** n) ** 0.5)
    iterations = math.pi / (4 * theta)
    for i in range(n):
        qc.h(x[i])
    for _ in range(iterations):
        qc.compose(o, inplace=True)
        qc.z(y)
        qc.compose(o.inverse(), inplace=True)
        qc.compose(diffusion_oracle(), inplace=True)

    return qc
'''


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

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

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

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

    theta = math.asin(1 / (2 ** n) ** 0.5)
    iterations = math.pi / (4 * theta)
    for i in range(n):
        qc.h(x[i])
    for _ in range(iterations):
        qc.compose(o, inplace=True)
        qc.z(y)
        qc.compose(o.inverse(), inplace=True)
        qc.compose(diffusion_oracle(), inplace=True)

    return qc
'''


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

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

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

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

    theta = math.asin(1 / (2 ** n) ** 0.5)
    iterations = math.floor(math.pi / (4 * theta))
    # print(theta, iterations)
    for i in range(n):
        qc.h(x[i])
    diff = diffusion_oracle(n)
    for _ in range(iterations):
        qc.compose(o, inplace=True)
        qc.z(y)
        qc.compose(o.inverse(), inplace=True)
        qc.compose(diff, inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

"""
You can apply oracle as follows:
qc.compose(o, inplace=True)
"""
def diffusion(n):
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)

    theta = np.pi
    controls = list(range(n-1))
    target = n-1

    qc.h(range(n))

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

    qc.mcp(theta, list(range(n-1)), n-1)

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

    qc.h(range(n))

    return qc

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    n = n
    rounds = np.round(np.pi/(4*(np.arcsin(np.sqrt(1/2**(n - 1)))))-1/2, 4)
    rounds_int = int(np.round(rounds))

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

    for _ in range(rounds_int-1):
        qc.compose(o, inplace=True)
        qc.compose(diffusion(n), inplace = True)

    qc.compose(o, inplace=True)

    qc.h(range(n))

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

    qc.mcp(np.pi, list(range(n)), y[0])

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

    qc.h(range(n))

    qc.h(y)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

"""
You can apply oracle as follows:
qc.compose(o, inplace=True)
"""
def diffusion(n):
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)

    theta = np.pi
    controls = list(range(n-1))
    target = n-1

    qc.h(range(n))

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

    qc.mcp(theta, list(range(n-1)), n-1)

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

    qc.h(range(n))

    return qc

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    n = n
    rounds = np.round(np.pi/(4*(np.arcsin(np.sqrt(1/2**(n - 1)))))-1/2, 4)
    rounds_int = int(np.round(rounds))

    qc.h(x)

    for _ in range(rounds_int-1):
        qc.compose(o, inplace=True)
        qc.compose(diffusion(n), inplace = True)

    qc.compose(o, inplace=True)

    qc.h(range(n))

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

    qc.mcx(list(range(n)), y[0])

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

    qc.h(range(n))

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

"""
You can apply oracle as follows:
qc.compose(o, inplace=True)
"""
def diffusion(n):
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)

    theta = np.pi
    controls = list(range(n-1))
    target = n-1

    qc.h(range(n))

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

    qc.mcp(theta, list(range(n-1)), n-1)

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

    qc.h(range(n))

    return qc

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    n = n
    rounds = np.round(np.pi/(4*(np.arcsin(np.sqrt(1/2**(n - 1)))))-1/2, 4)
    rounds_int = int(np.round(rounds))

    qc.h(x)

    for _ in range(rounds_int):
        qc.compose(o, inplace=True)
        qc.z(y[0])
        qc.compose(diffusion(n), inplace = True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

"""
You can apply oracle as follows:
qc.compose(o, inplace=True)
"""
def diffusion(n):
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)

    theta = np.pi
    controls = list(range(n-1))
    target = n-1

    qc.h(range(n))

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

    qc.mcp(theta, list(range(n-1)), n-1)

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

    qc.h(range(n))

    return qc

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    n = n
    rounds = np.round(np.pi/(4*(np.arcsin(np.sqrt(1/2**(n - 1)))))-1/2, 4)
    rounds_int = int(np.round(rounds))

    qc.h(x)

    for _ in range(rounds_int-):
        qc.compose(o, inplace=True)
        qc.z(y[0])
        qc.compose(o, inplace= True)
        qc.compose(diffusion(n), inplace = True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

"""
You can apply oracle as follows:
qc.compose(o, inplace=True)
"""
def diffusion(n):
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)

    theta = np.pi
    controls = list(range(n-1))
    target = n-1

    qc.h(range(n))

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

    qc.mcp(theta, list(range(n-1)), n-1)

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

    qc.h(range(n))

    return qc

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    n = n
    rounds = np.round(np.pi/(4*(np.arcsin(np.sqrt(1/2**(n - 1)))))-1/2, 4)
    rounds_int = int(np.round(rounds))

    qc.h(x)

    for _ in range(rounds_int-):
        qc.compose(o, inplace=True)
        qc.z(y[0])
        #qc.compose(o, inplace= True)
        qc.compose(diffusion(n), inplace = True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

"""
You can apply oracle as follows:
qc.compose(o, inplace=True)
"""
def diffusion(n):
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)

    theta = np.pi
    controls = list(range(n-1))
    target = n-1

    qc.h(range(n))

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

    qc.mcp(theta, list(range(n-1)), n-1)

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

    qc.h(range(n))

    return qc

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    n = n
    rounds = np.round(np.pi/(4*(np.arcsin(np.sqrt(1/2**(n - 1)))))-1/2, 4)
    rounds_int = int(np.round(rounds))

    qc.h(x)

    for _ in range(rounds_int):
        qc.compose(o, inplace=True)
        qc.z(y[0])
        qc.compose(o, inplace = True)
        qc.compose(diffusion(n), inplace = True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

"""
You can apply oracle as follows:
qc.compose(o, inplace=True)
"""
def diffusion(n):
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)

    theta = np.pi
    controls = list(range(n-1))
    target = n-1

    qc.h(range(n))

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

    qc.mcp(theta, list(range(n-1)), n-1)

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

    qc.h(range(n))

    return qc

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    n = n
    rounds = np.round(np.pi/(4*(np.arcsin(np.sqrt(1/2**(n - 1)))))-1/2, 4)
    rounds_int = int(np.round(rounds))

    qc.h(x)

    for _ in range(rounds_int):
        qc.compose(o, inplace=True)
        qc.z(y[0])
        qc.compose(o, inplace = True)
        qc.compose(diffusion(n), inplace = True)

    qc.compose(o, inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

"""
You can apply oracle as follows:
qc.compose(o, inplace=True)
"""
def diffusion(n):
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)

    theta = np.pi
    controls = list(range(n-1))
    target = n-1

    qc.h(range(n))

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

    qc.mcp(theta, list(range(n-1)), n-1)

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

    qc.h(range(n))

    return qc

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    n = n
    rounds = np.round(np.pi/(4*(np.arcsin(np.sqrt(1/2**(n - 1)))))-1/2, 4)
    rounds_int = int(np.round(rounds))

    qc.h(x)
    qc.h(y[0])

    for _ in range(rounds_int):
        qc.compose(o, inplace=True)
        qc.z(y[0])
        qc.compose(diffusion(n), inplace = True)

    qc.compose(o, inplace=True)

    qc.h(y[0])

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

"""
You can apply oracle as follows:
qc.compose(o, inplace=True)
"""
def diffusion(n):
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)

    theta = np.pi
    controls = list(range(n-1))
    target = n-1

    qc.h(range(n))

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

    qc.mcp(theta, list(range(n-1)), n-1)

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

    qc.h(range(n))

    return qc

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    n = n
    rounds = np.round(np.pi/(4*(np.arcsin(np.sqrt(1/2**(n - 1)))))-1/2, 4)
    rounds_int = int(np.round(rounds))

    qc.h(x)
    qc.h(y[0])

    for _ in range(rounds_int):
        qc.compose(o, inplace=True)
        qc.z(y[0])
        qc.compose(diffusion(n), inplace = True)

    qc.h(y[0])

    return qc
'''


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

def b4(qc: QuantumCircuit) -> None:
    n = qc.num_qubits
    for i in range(n):
        qc.x(i)
    mcz = ZGate().control(n - 1)
    qc.append(mcz, range(n))

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

def b5(qc: QuantumCircuit) -> None:
    n = qc.num_qubits
    # Write your code here:
    for i in range(n):
        qc.h(i)
    b4(qc)
    for i in range(n):
        qc.h(i)

def calc_r(n: int) -> int:
    val = pi * sqrt(2**n) / 4
    for r in range(1, 1000):
        if r > val:
            break
    return r - 1

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.x(n)
    for i in range(n + 1):
        qc.h(i)
    r = calc_r(n)
    for i in range(r):
        qc.compose(o, inplace=True)
        b5(qc)

    return qc
'''


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

def b4(qc: QuantumCircuit) -> None:
    n = qc.num_qubits
    for i in range(n):
        qc.x(i)
    mcz = ZGate().control(n - 1)
    qc.append(mcz, range(n))

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

def b5(qc: QuantumCircuit) -> None:
    n = qc.num_qubits
    # Write your code here:
    for i in range(n):
        qc.h(i)
    b4(qc)
    for i in range(n):
        qc.h(i)

def calc_r(n: int) -> int:
    val = pi * sqrt(2**n) / 4
    for r in range(1, 1000):
        if r > val:
            break
    return r - 1

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.x(n)
    for i in range(n + 1):
        qc.h(i)
    r = calc_r(n)
    for i in range(r):
        qc.compose(o, inplace=True)
        b5(qc)

    return qc
'''


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

def b4(n: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    for i in range(n):
        qc.x(i)
    mcz = ZGate().control(n - 1)
    qc.append(mcz, range(n))
    for i in range(n):
        qc.x(i)
    return qc

def b5(n: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(n):
        qc.h(i)
    qc.compose(b4(n), inplace=True)
    for i in range(n):
        qc.h(i)
    return qc

def calc_r(n: int) -> int:
    val = pi * sqrt(2**n) / 4
    for r in range(1, 1000):
        if r > val:
            break
    return r - 1

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.x(n)
    for i in range(n + 1):
        qc.h(i)
    r = calc_r(n)
    for i in range(r):
        qc.compose(o, inplace=True)
        qc.compose(b5, inplace=True)

    return qc
'''


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

# def oracle(n: int) -> QuantumCircuit:
#     x, y = QuantumRegister(n), QuantumRegister(1)
#     qc = QuantumCircuit(x, y)
#     for i in range(n):
#         qc.x(i)
#     mcz = ZGate().control(n - 1)
#     qc.append(mcz, range(n))
#     for i in range(n):
#         qc.x(i)
#     return qc

def b4(n: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    for i in range(n):
        qc.x(i)
    mcz = ZGate().control(n - 1)
    qc.append(mcz, range(n))
    for i in range(n):
        qc.x(i)
    return qc

def b5(n: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(n):
        qc.h(i)
    qc.compose(b4(n), inplace=True)
    for i in range(n):
        qc.h(i)
    return qc

def calc_r(n: int) -> int:
    val = pi * sqrt(2**n) / 4
    for r in range(1, 1000):
        if r > val:
            break
    return r - 1

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.x(n)
    for i in range(n + 1):
        qc.h(i)
    r = calc_r(n)
    for i in range(r):
        qc.compose(o, inplace=True)
        qc.compose(b5(n), inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

"""
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:

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

"""
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)
    for i in range(n):
        qc.h(x[i])
    qc.compose(o, 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,
# )

"""
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:

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
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:
    qc.compose(o, inplace=True)
    qc.cry(np.pi*2, n+1, 0)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import numpy as np
from qiskit.quantum_info import Statevector
"""
You can apply oracle as follows:
qc.compose(o, inplace=True)
"""

zero = Statevector([1,0])
zero_state = zero.tensor(zero) # or zero_state = Statevector([1,0,0,0])
projector = zero_state.to_operator()

def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(o, inplace=True)
    qc.cry(np.pi*2, n+1, 0)
    qc.h(n+1)
    qc.projector(n+1)

    return qc
'''


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

"""
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:

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

"""
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:
    qc.compose(o, inplace=True)
    qc.cz(y[0],x[0])
    qc.x(x[0])
    qc.cz(y[0],x[0])
    qc.x(x[0])
    qc.cx(x[0],y[0])
    qc.x(x[0])
    qc.cx(x[0],y[0])
    qc.x(x[0])
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

"""
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:
    qc.x(y[0])
    qc.h(y[0])
    qc.compose(o, inplace=True)
    qc.h(y[0])
    qc.x(y[0])
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

"""
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:

    # Paso 1: Aplicar el oráculo O
    qc.compose(o, qubits=x[:] + y[:], inplace=True)

    # Paso 2: Aplicar la puerta Z al qubit auxiliar y[0]
    qc.z(y[0])

    # Paso 3: Aplicar nuevamente el oráculo O
    qc.compose(o, qubits=x[:] + y[:], inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

"""
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:

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

    # Paso 2: Aplicar el oráculo O
    qc.compose(o, qubits=x[:] + y[:], inplace=True)

    # Paso 3: Deshacer la preparación del qubit auxiliar
    qc.h(y[0])  # Aplicar puerta Hadamard
    qc.x(y[0])  # Aplicar puerta X para regresar al estado |0⟩

    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, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.x(n)
    qc.h(n)
    qc.compose(o, inplace=True)
    qc.h(n)
    qc.x(n)

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

"""
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:
    qc.x(list(range(n)))
    qc.h(list(range(n)))
    qc.compose(o, inplace=True)
    qc.h(list(range(n)))
    qc.x(list(range(n)))
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

"""
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:
    qc.x(y)
    qc.h(y)
    qc.compose(o, inplace=True)
    qc.h(y)
    qc.x(y)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

"""
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:
    qc.x(y)
    qc.h(y)
    qc.compose(o, inplace=True)
    qc.h(y)
    qc.(y)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

"""
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:
    qc.x(y)
    qc.h(y)
    qc.compose(o, inplace=True)
    qc.h(y)
    qc.x(y)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

"""
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:

    qc.x(y[0])
    qc.h(y[0])

    qc.compose(o, inplace=True)

    qc.h(y[0])
    qc.x(y[0])

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

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

    qc.compose(o, inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    qc.x(y)

    qc.compose(o, inplace=True)

    qc.x(y)

    qc.mct(x, y)

    qc.x(y)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    qc.x(y)

    qc.compose(o, inplace=True)

    qc.x(y)
    qc.mct(x, y)
    qc.x(y)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

"""
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:
    qc.x(n)
    qc.h(n)
    qc.compose(o,inplace=True)
    qc.h(n)
    qc.x(n)

    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:
    """
    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 registros cuánticos
    x = QuantumRegister(n, 'x')  # Qubits para |x>
    y = QuantumRegister(1, 'y')  # Qubit auxiliar
    qc = QuantumCircuit(x, y)

    def diffuser(n):
        """
        Crea el operador de difusión (diffuser) para el Algoritmo de Grover.
        """
        qc = QuantumCircuit(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))

        # Convertir el circuito en una puerta
        diffuser_gate = qc.to_gate()
        diffuser_gate.name = "Diffuser"
        return diffuser_gate

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

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

    # Definir el operador de difusión
    diffuser_gate = diffuser(n)

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

    # Determinar el número de iteraciones permitidas por la restricción de profundidad
    # Estimación de profundidad por iteración:
    # - Oráculo O: profundidad 1
    # - H y X en y: profundidad 2
    # - Difusor: profundidad aproximadamente n (depende de la implementación)
    # Asumimos una profundidad total aproximada por iteración
    depth_per_iteration = 1 + 2 + 20  # Ajustar según n
    max_iterations = math.floor(75 / depth_per_iteration)
    iterations = min(optimal_iterations, max_iterations)
    iterations = max(1, iterations)  # Al menos una iteración

    # Aplicar el algoritmo 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 puertas H y X al qubit auxiliar para realizar phase kickback
        qc.h(y[0])
        qc.x(y[0])

        # Paso 5: Aplicar el operador de difusión
        qc.append(diffuser_gate, qargs=x[:], inplace=True)

    return qc
'''


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

def diffuser(n):
    """
    Crea el operador de difusión (diffuser) para el Algoritmo de Grover.
    """
    qc = QuantumCircuit(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))

    # Convertir el circuito en una puerta
    diffuser_gate = qc.to_gate()
    diffuser_gate.name = "Diffuser"
    return diffuser_gate

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 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])
    qc.h(y[0])

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

    # Definir el operador de difusión
    diffuser_gate = diffuser(n)

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

    # Determinar el número de iteraciones permitidas por la restricción de profundidad
    # Estimación de profundidad por iteración:
    # - Oráculo O: profundidad 1
    # - H y X en y: profundidad 2
    # - Difusor: profundidad aproximadamente n (depende de la implementación)
    # Asumimos una profundidad total aproximada por iteración
    depth_per_iteration = 1 + 2 + 20  # Ajustar según n
    max_iterations = math.floor(75 / depth_per_iteration)
    iterations = min(optimal_iterations, max_iterations)
    iterations = max(1, iterations)  # Al menos una iteración

    # Aplicar el algoritmo 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 puertas H y X al qubit auxiliar para realizar phase kickback
        qc.h(y[0])
        qc.x(y[0])

        # Paso 5: Aplicar el operador de difusión
        qc.append(diffuser_gate, qargs=x[:], inplace=True)

    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 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])
    qc.h(y[0])

    # Paso 2: Aplicar puertas Hadamard a todos los qubits 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 (k=1 ya que L es único)
    theta = math.asin(math.sqrt(k / N))
    optimal_iterations = int(math.floor((math.pi / (4 * theta)) - 0.5))

    # Determinar el número de iteraciones permitidas por la restricción de profundidad
    # Estimación de profundidad por iteración:
    # - Oráculo O: profundidad 1
    # - H y X en y: profundidad 2
    # - Difusor: profundidad aproximadamente n (depende de la implementación)
    # Asumimos una profundidad total aproximada por iteración
    depth_per_iteration = 1 + 2 + 20  # Ajustar según n
    max_iterations = math.floor(75 / depth_per_iteration)
    iterations = min(optimal_iterations, max_iterations)
    iterations = max(1, iterations)  # Al menos una iteración

    # Aplicar el algoritmo 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 puertas H y X al qubit auxiliar para realizar phase kickback
        qc.h(y[0])
        qc.x(y[0])

        # Paso 5: 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
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
        # Aplicar puertas Hadamard a todos los qubits
        qc.h(range(n))
        # Aplicar puertas X a todos los qubits
        qc.x(range(n))

        # Aplicar una puerta Z multi-controlada
        # Implementación de una puerta CCZ para n=2 o MCZ para n>2
        if n == 1:
            qc.z(0)
        elif n == 2:
            qc.cz(0, 1)
        else:
            # Aplicar Hadamard al último qubit
            qc.h(n-1)
            # Aplicar MCX con los primeros n-1 qubits como controles y el último como objetivo
            qc.mcx(list(range(n-1)), n-1)
            # Aplicar nuevamente Hadamard al último qubit
            qc.h(n-1)

        # Aplicar puertas X a todos los qubits
        qc.x(range(n))
        # Aplicar puertas Hadamard a todos los qubits
        qc.h(range(n))

    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
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(diffuser):
        """
        Crea el operador de difusión para el Algoritmo de Grover.
        """
        #diffuser = QuantumCircuit(n_qubits, name='Diffuser')
        diffuser.h(range(n))
        diffuser.x(range(n))

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

        diffuser.x(range(n))
        diffuser.h(range(n))

        #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))
        diffuser_operator(qc)

    return qc
'''