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

def solve(n: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(n + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(add_multi_mod(n,a,L),inplace=True)
    gcd,x,y = extended_euclidean(a,L)
    a_inv = x % L
    #print(a_inv)
    for i in range(n):
        qc.cx(i,n+i)
        qc.cx(n+i,i)
        qc.cx(i,n+i)
    qc.compose(add_multi_mod(n,L-a_inv,L),inplace=True)
    return qc

def extended_euclidean(a: int, b: int) -> tuple[int, int, int]:
    # when b == 0:
    if b == 0:
        return a, 1, 0

    # recursive step
    gcd, x1, y1 = extended_euclidean(b, a % b)

    # update x and y
    x = y1
    y = x1 - (a // b) * y1

    return gcd, x, y

def add_multi_mod(n: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(n + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(qft(n+1),qubits = list(range(n,2*n+1)), inplace=True)
    for i in range(n):
        qc.compose(add_mod(n,(a*2**i)%L,L).control(1),qubits = [i]+list(range(n,2*n+1)), inplace=True)
    qc.compose(qft(n+1).inverse(),qubits = list(range(n,2*n+1)), inplace=True)

    return qc

def add_mod(n: int, a: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    qc.compose(shift(n+1,2**n-L+a), inplace=True)
    qc.compose(qft(n+1).inverse(), inplace=True)
    qc.compose(pack(n,2**n-L+a,2**n+a), inplace=True)
    qc.compose(qft(n+1), inplace=True)
    qc.compose(shift(n+1,a), inplace=True)

    return qc

def pack(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    qc.compose(qft(n),qubits = list(range(n)), inplace=True)
    qc.compose(shift(n,-t).control(1),qubits = [n]+list(range(n)), inplace=True)
    qc.x(n)
    qc.compose(shift(n,-s).control(1),qubits = [n]+list(range(n)), inplace=True)
    qc.x(n)
    qc.compose(qft(n).inverse(),qubits = list(range(n)), inplace=True)
    return qc

def add(n,a):
    qc = QuantumCircuit(n)
    qc.compose(qft(n), inplace=True)
    qc.compose(shift(n,a), inplace=True)
    qc.compose(qft(n).inverse(), inplace=True)
    return qc

def qft(n):
    qc = QuantumCircuit(n)
    for i in range(n-1,-1,-1):
        qc.h(i)
        for j in range(i-1,-1,-1):
            qc.cp(2*math.pi/2**(i-j+1),j,i)
    for k in range(n//2):
        qc.swap(k,n-1-k)
    return qc

def iqft(n):
    qc = QuantumCircuit(n)
    for i in range(n-1,-1,-1):
        qc.h(i)
        for j in range(i-1,-1,-1):
            qc.cp(-2*math.pi/2**(i-j+1),j,i)
    for k in range(n//2):
        qc.swap(k,n-1-k)
    return qc

def shift(n,a):
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.p(2*math.pi*a/2**(n-i),i)
    return qc
'''


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

def solve(n: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(n + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(add_multi_mod(n,a,L),inplace=True)
    gcd,x,y = extended_euclidean(a,L)
    a_inv = x % L
    #print(a_inv)
    for i in range(n):
        qc.cx(i,n+i)
        qc.cx(n+i,i)
        qc.cx(i,n+i)
    qc.compose(add_multi_mod(n,L-a_inv,L),inplace=True)
    return qc

def extended_euclidean(a: int, b: int) -> tuple[int, int, int]:
    # when b == 0:
    if b == 0:
        return a, 1, 0

    # recursive step
    gcd, x1, y1 = extended_euclidean(b, a % b)

    # update x and y
    x = y1
    y = x1 - (a // b) * y1

    return gcd, x, y

def add_multi_mod(n: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(n + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(qft(n+1),qubits = list(range(n,2*n+1)), inplace=True)
    for i in range(n):
        qc.compose(add_mod(n,(a*2**i)%L,L).control(1),qubits = [i]+list(range(n,2*n+1)), inplace=True)
    qc.compose(qft(n+1).inverse(),qubits = list(range(n,2*n+1)), inplace=True)

    return qc

def add_mod(n: int, a: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    qc.compose(shift(n+1,2**n-L+a), inplace=True)
    qc.compose(qft(n+1).inverse(), inplace=True)
    qc.compose(pack(n,2**n-L+a,2**n+a), inplace=True)
    qc.compose(qft(n+1), inplace=True)
    qc.compose(shift(n+1,a), inplace=True)

    return qc

def pack(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    qc.compose(qft(n),qubits = list(range(n)), inplace=True)
    qc.compose(shift(n,-t).control(1),qubits = [n]+list(range(n)), inplace=True)
    qc.x(n)
    qc.compose(shift(n,-s).control(1),qubits = [n]+list(range(n)), inplace=True)
    qc.x(n)
    qc.compose(qft(n).inverse(),qubits = list(range(n)), inplace=True)
    return qc

def qft(n):
    qc = QuantumCircuit(n)
    for i in range(n-1,-1,-1):
        qc.h(i)
        for j in range(i-1,-1,-1):
            qc.cp(2*math.pi/2**(i-j+1),j,i)
    for k in range(n//2):
        qc.swap(k,n-1-k)
    return qc

def shift(n,a):
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.p(2*math.pi*a/2**(n-i),i)
    return qc
'''


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

def solve(n: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(n + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(add_multi_mod(n,a,L),inplace=True)
    gcd,x,y = extended_euclidean(a,L)
    a_inv = x % L
    #print(a_inv)
    for i in range(n):
        qc.swap(i,n+i)
    qc.compose(add_multi_mod(n,L-a_inv,L),inplace=True)
    return qc

def extended_euclidean(a: int, b: int) -> tuple[int, int, int]:
    # when b == 0:
    if b == 0:
        return a, 1, 0

    # recursive step
    gcd, x1, y1 = extended_euclidean(b, a % b)

    # update x and y
    x = y1
    y = x1 - (a // b) * y1

    return gcd, x, y

def add_multi_mod(n: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(n + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(qft(n+1),qubits = list(range(n,2*n+1)), inplace=True)
    for i in range(n):
        qc.compose(add_mod(n,(a*2**i)%L,L).control(1),qubits = [i]+list(range(n,2*n+1)), inplace=True)
    qc.compose(qft(n+1).inverse(),qubits = list(range(n,2*n+1)), inplace=True)

    return qc

def add_mod(n: int, a: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    qc.compose(shift(n+1,2**n-L+a), inplace=True)
    qc.compose(qft(n+1).inverse(), inplace=True)
    qc.compose(pack(n,2**n-L+a,2**n+a), inplace=True)
    qc.compose(qft(n+1), inplace=True)
    qc.compose(shift(n+1,a), inplace=True)

    return qc

def pack(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    qc.compose(qft(n),qubits = list(range(n)), inplace=True)
    qc.compose(shift(n,-t).control(1),qubits = [n]+list(range(n)), inplace=True)
    qc.x(n)
    qc.compose(shift(n,-s).control(1),qubits = [n]+list(range(n)), inplace=True)
    qc.x(n)
    qc.compose(qft(n).inverse(),qubits = list(range(n)), inplace=True)
    return qc

def qft(n):
    qc = QuantumCircuit(n)
    for i in range(n-1,-1,-1):
        qc.h(i)
        for j in range(i-1,-1,-1):
            qc.cp(2*math.pi/2**(i-j+1),j,i)
    for k in range(n//2):
        qc.swap(k,n-1-k)
    return qc

def shift(n,a):
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.p(2*math.pi*a/2**(n-i),i)
    return qc
'''


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

def solve(n: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(n + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(qft(n+1),qubits = list(range(n,2*n+1)), inplace=True)
    for i in range(n):
        b = (a*2**i)%L
        qc.compose(shift(n+1,2**n-L+b), inplace=True)
        qc.compose(qft(n+1).inverse(), inplace=True)
        qc.compose(pack(n,2**n-L+b,2**n+b), inplace=True)
        qc.compose(qft(n+1), inplace=True)
        qc.compose(shift(n+1,b), inplace=True)
    qc.compose(qft(n+1).inverse(),qubits = list(range(n,2*n+1)), inplace=True)
    gcd,x,y = extended_euclidean(a,L)
    a_inv = x % L
    #print(a_inv)
    for i in range(n):
        qc.swap(i,n+i)
    qc.compose(qft(n+1),qubits = list(range(n,2*n+1)), inplace=True)
    for i in range(n):
        b = ((L-a_inv)*2**i)%L
        qc.compose(shift(n+1,2**n-L+b), inplace=True)
        qc.compose(qft(n+1).inverse(), inplace=True)
        qc.compose(pack(n,2**n-L+b,2**n+b), inplace=True)
        qc.compose(qft(n+1), inplace=True)
        qc.compose(shift(n+1,b), inplace=True)
    qc.compose(qft(n+1).inverse(),qubits = list(range(n,2*n+1)), inplace=True)
    return qc

def extended_euclidean(a: int, b: int) -> tuple[int, int, int]:
    # when b == 0:
    if b == 0:
        return a, 1, 0

    # recursive step
    gcd, x1, y1 = extended_euclidean(b, a % b)

    # update x and y
    x = y1
    y = x1 - (a // b) * y1

    return gcd, x, y

def pack(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    qc.compose(qft(n),qubits = list(range(n)), inplace=True)
    qc.compose(shift(n,-t).control(1),qubits = [n]+list(range(n)), inplace=True)
    qc.x(n)
    qc.compose(shift(n,-s).control(1),qubits = [n]+list(range(n)), inplace=True)
    qc.x(n)
    qc.compose(qft(n).inverse(),qubits = list(range(n)), inplace=True)
    return qc

def qft(n):
    qc = QuantumCircuit(n)
    for i in range(n-1,-1,-1):
        qc.h(i)
        for j in range(i-1,-1,-1):
            qc.cp(2*math.pi/2**(i-j+1),j,i)
    for k in range(n//2):
        qc.swap(k,n-1-k)
    return qc

def shift(n,a):
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.p(2*math.pi*a/2**(n-i),i)
    return qc
'''


'''python
import math

from qiskit import QuantumCircuit, QuantumRegister

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

    for i in reversed(range(n)):
        qc.h(i)
        for j in reversed(range(i)):
            qc.cp(math.pi / 2 ** (i - j), j, i)

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

    return qc

def crot(n: int, a: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)

    for i in range(n):
        theta = 2 * math.pi * a * 2**i / 2**n
        qc.cp(theta, n, i)

    return qc

def cadd(n: int, a: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)

    qc.compose(qft(n), qubits=range(n), inplace=True)
    qc.compose(crot(n, a), qubits=range(n + 1), inplace=True)
    qc.compose(qft(n).inverse(), qubits=range(n), inplace=True)

    return qc

def ccrot(n: int, a: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 2)

    for i in range(n):
        theta = 2 * math.pi * a * 2**i / 2**n
        qc.mcp(theta, [n, n + 1], i)

    return qc

def ccadd(n: int, a: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 2)

    qc.compose(qft(n), qubits=range(n), inplace=True)
    qc.compose(ccrot(n, a), qubits=range(n + 2), inplace=True)
    qc.compose(qft(n).inverse(), qubits=range(n), inplace=True)

    return qc

def cpack(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 2)

    qc.compose(ccadd(n, 2 ** (n + 1) - t), qubits=range(n + 2), inplace=True)

    qc.x(n)
    qc.compose(ccadd(n, -s), qubits=range(n + 2), inplace=True)
    qc.x(n)

    return qc

def add_ax_mod(n: int, L: int, a: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(n + 1)
    qc = QuantumCircuit(x, y)

    for i in range(n):
        b = (2**i * a) % L
        targets = [*y, x[i]]
        qc.compose(cadd(n + 1, 2**n - L), qubits=targets, inplace=True)
        qc.compose(cadd(n + 1, b), qubits=targets, inplace=True)
        qc.compose(cpack(n, 2**n - L + b, 2**n + b), qubits=targets, inplace=True)
        qc.compose(cadd(n + 1, b), qubits=targets, inplace=True)

    return qc

def inv_mod(a: int, L: int) -> int:
    def extended_euclidean(a: int, b: int) -> tuple[int, int, int]:
        if b == 0:
            return a, 1, 0

        gcd, x1, y1 = extended_euclidean(b, a % b)

        x = y1
        y = x1 - (a // b) * y1

        return gcd, x, y

    _, x, _ = extended_euclidean(a, L)

    return x % L

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

    qc.compose(add_ax_mod(n, L, a), qubits=range(2 * n + 1), inplace=True)

    for i in range(n):
        qc.swap(x[i], y[i])

    b = -inv_mod(a, L) % L
    qc.compose(add_ax_mod(n, L, b), qubits=range(2 * n + 1), inplace=True)

    return qc
'''


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

def solve(n: int, m: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(2 * m + 1)
    qc = QuantumCircuit(x, y)

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

    # Step 2: Compute a^k mod L for each k in the superposition
    # We will use controlled operations to achieve this
    for k in range(2**n):
        # Calculate a^k mod L
        mod_result = pow(a, k, L)

        # Convert mod_result to binary and store in the second register
        for j in range(2 * m + 1):
            if (mod_result >> j) & 1:
                qc.x(y[j])  # Apply X gate to set the corresponding bit

    return qc
'''


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

def solve(n: int, m: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(2 * m + 1)
    qc = QuantumCircuit(x, y)

    # Apply Hadamard gates on the first n qubits
    for i in range(n):
        qc.h(x[i])

    # Apply controlled phase gates based on the value of k
    for k in range(2**n):
        binary_k = format(k, '0'+str(n)+'b')  # Convert k to binary string
        for i in range(n):
            if binary_k[i] == '0':
                qc.x(x[i])
        phase = (a**k) % L
        binary_phase = format(phase, '0'+str(2*m+1)+'b')  # Convert phase to binary string
        for i in range(2*m+1):
            if binary_phase[i] == '1':
                qc.cp(2*math.pi / (2**m), x, y[i])
        for i in range(n):
            if binary_k[i] == '0':
                qc.x(x[i])

    # Apply inverse QFT on the first n qubits
    qft_dagger = QFT(n, inverse=True)
    qc.append(qft_dagger, x)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, m: int, a: int, L: int) -> QuantumCircuit:
    """
    Create |0>_n |0>_(2m+1)  →  (1/√(2^n)) Σ_k |k> |a^k mod L>
    Registers are in little-endian order.
    """
    x = QuantumRegister(n, 'x')               # exponent register
    y = QuantumRegister(2 * m + 1, 'y')       # result register  (enough qubits for L)
    qc = QuantumCircuit(x, y)

    # 1) Prepare uniform superposition on |x>
    for i in range(n):
        qc.h(x[i])                            # |0…0> → (1/√2^n) Σ_k |k>

    # helper: multi-controlled X with an arbitrary number of controls
    def cX(ctrls, tgt):
        if len(ctrls) == 0:
            qc.x(tgt)
        elif len(ctrls) == 1:
            qc.cx(ctrls[0], tgt)
        else:
            qc.mcx(ctrls, tgt)                # built-in multi-controlled X

    # 2) Encode  a^k  (mod L)  into |y>  for every computational basis state |k>
    N = 1 << n                                # 2^n
    y_bits = 2 * m + 1                        # number of qubits in y
    for k in range(N):
        value = pow(a, k, L)                  # classical a^k  mod L
        if value == 0:
            continue                          # all target bits stay 0
        # list of control qubits (all qubits of register x)
        ctrls = [x[i] for i in range(n)]
        # temporarily flip controls whose bit in k is 0  (so all controls become |1>)
        zeros = [i for i in range(n) if ((k >> i) & 1) == 0]
        for i in zeros:
            qc.x(x[i])

        # for every 1-bit in 'value' flip corresponding qubit in y
        for bit in range(y_bits):
            if (value >> bit) & 1:
                cX(ctrls, y[bit])

        # undo the temporary flips
        for i in zeros:
            qc.x(x[i])

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, m: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(2 * m + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(n):
        qc.h(i)
    multipliers = doubling_powers(n,a,L)
    for i in range(n):
        qc.compose(multiply_mod(m,a,L).control(1),qubits = [i] + list(range(n,n+2*m+1)),inplace=True)
    return qc

def doubling_powers(n: int, a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    result.append(a)
    for i in range(1,n):
        result.append(result[-1]**2%L)
    return result

def multiply_mod(n: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(n + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(add_multi_mod(n,a,L),inplace=True)
    gcd,x,y = extended_euclidean(a,L)
    a_inv = x % L
    #print(a_inv)
    for i in range(n):
        qc.swap(i,n+i)
    qc.compose(add_multi_mod(n,L-a_inv,L),inplace=True)
    return qc

def extended_euclidean(a: int, b: int) -> tuple[int, int, int]:
    # when b == 0:
    if b == 0:
        return a, 1, 0

    # recursive step
    gcd, x1, y1 = extended_euclidean(b, a % b)

    # update x and y
    x = y1
    y = x1 - (a // b) * y1

    return gcd, x, y

def add_multi_mod(n: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(n + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(qft(n+1),qubits = list(range(n,2*n+1)), inplace=True)
    for i in range(n):
        qc.compose(add_mod(n,(a*2**i)%L,L),qubits = [i]+list(range(n,2*n+1)), inplace=True)
    qc.compose(qft(n+1).inverse(),qubits = list(range(n,2*n+1)), inplace=True)

    return qc

def add_mod(n: int, a: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 2)
    # Write your code here:
    qc.compose(shift(n+1,2**n-L+a).control(1), inplace=True)
    qc.compose(qft(n+1).inverse(),qubits =  list(range(1,n+2)), inplace=True)
    qc.compose(pack(n,2**n-L+a,2**n+a), inplace=True)
    qc.compose(qft(n+1),qubits =  list(range(1,n+2)), inplace=True)
    qc.compose(shift(n+1,a).control(1), inplace=True)

    return qc

def pack(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 2)
    # Write your code here:
    qc.compose(qft(n),qubits = list(range(1,n+1)), inplace=True)
    qc.compose(shift(n,-t).control(2),qubits = [0,n+1]+list(range(1,n+1)), inplace=True)
    qc.x(n+1)
    qc.compose(shift(n,-s).control(2),qubits = [0,n+1]+list(range(1,n+1)), inplace=True)
    qc.x(n+1)
    qc.compose(qft(n).inverse(),qubits = list(range(1,n+1)), inplace=True)
    return qc

def qft(n):
    qc = QuantumCircuit(n)
    for i in range(n-1,-1,-1):
        qc.h(i)
        for j in range(i-1,-1,-1):
            qc.cp(2*math.pi/2**(i-j+1),j,i)
    for k in range(n//2):
        qc.swap(k,n-1-k)
    return qc

def shift(n,a):
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.p(2*math.pi*a/2**(n-i),i)
    return qc
'''


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

def solve(n: int, m: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(2 * m + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(n):
        qc.h(i)
    multipliers = doubling_powers(n,a,L)
    for i in range(n):
        qc.compose(multiply_mod(m,a,L).control(1),qubits = [i] + list(range(n,n+2*m+1)),inplace=True)
    return qc

def doubling_powers(n: int, a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    result.append(a)
    for i in range(1,n):
        result.append(result[-1]**2%L)
    return result

def multiply_mod(n: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(n + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(add_multi_mod(n,a,L),inplace=True)
    gcd,x,y = extended_euclidean(a,L)
    a_inv = x % L
    #print(a_inv)
    for i in range(n):
        qc.swap(i,n+i)
    qc.compose(add_multi_mod(n,L-a_inv,L),inplace=True)
    return qc

def extended_euclidean(a: int, b: int) -> tuple[int, int, int]:
    # when b == 0:
    if b == 0:
        return a, 1, 0

    # recursive step
    gcd, x1, y1 = extended_euclidean(b, a % b)

    # update x and y
    x = y1
    y = x1 - (a // b) * y1

    return gcd, x, y

def add_multi_mod(n: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(n + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(qft(n+1),qubits = list(range(n,2*n+1)), inplace=True)
    for i in range(n):
        qc.compose(add_mod(n,(a*2**i)%L,L),qubits = [i]+list(range(n,2*n+1)), inplace=True)
    qc.compose(qft(n+1).inverse(),qubits = list(range(n,2*n+1)), inplace=True)

    return qc

def add_mod(n: int, a: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 2)
    # Write your code here:
    qc.compose(shift(n+1,2**n-L+a).control(1), inplace=True)
    qc.compose(qft(n+1).inverse(),qubits =  list(range(1,n+2)), inplace=True)
    qc.compose(pack(n,2**n-L+a,2**n+a), inplace=True)
    qc.compose(qft(n+1),qubits =  list(range(1,n+2)), inplace=True)
    qc.compose(shift(n+1,a).control(1), inplace=True)

    return qc

def pack(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 2)
    # Write your code here:
    qc.compose(qft(n),qubits = list(range(1,n+1)), inplace=True)
    qc.compose(shift(n,-t).control(2),qubits = [0,n+1]+list(range(1,n+1)), inplace=True)
    qc.x(n+1)
    qc.compose(shift(n,-s).control(2),qubits = [0,n+1]+list(range(1,n+1)), inplace=True)
    qc.x(n+1)
    qc.compose(qft(n).inverse(),qubits = list(range(1,n+1)), inplace=True)
    return qc

def qft(n):
    qc = QuantumCircuit(n)
    for i in range(n-1,-1,-1):
        qc.h(i)
        for j in range(i-1,-1,-1):
            qc.cp(2*math.pi/2**(i-j+1),j,i)
    for k in range(n//2):
        qc.swap(k,n-1-k)
    return qc

def shift(n,a):
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.p(2*math.pi*a/2**(n-i),i)
    return qc
'''


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

def solve(n: int, m: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(2 * m + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(n):
        qc.h(i)
    multipliers = doubling_powers(n,a,L)
    for i in range(n):
        qc.compose(multiply_mod(m,multipliers[i],L).control(1),qubits = [i] + list(range(n,n+2*m+1)),inplace=True)
    return qc

def doubling_powers(n: int, a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    result.append(a)
    for i in range(1,n):
        result.append(result[-1]**2%L)
    return result

def multiply_mod(n: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(n + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(add_multi_mod(n,a,L),inplace=True)
    gcd,x,y = extended_euclidean(a,L)
    a_inv = x % L
    #print(a_inv)
    for i in range(n):
        qc.swap(i,n+i)
    qc.compose(add_multi_mod(n,L-a_inv,L),inplace=True)
    return qc

def extended_euclidean(a: int, b: int) -> tuple[int, int, int]:
    # when b == 0:
    if b == 0:
        return a, 1, 0

    # recursive step
    gcd, x1, y1 = extended_euclidean(b, a % b)

    # update x and y
    x = y1
    y = x1 - (a // b) * y1

    return gcd, x, y

def add_multi_mod(n: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(n + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(qft(n+1),qubits = list(range(n,2*n+1)), inplace=True)
    for i in range(n):
        qc.compose(add_mod(n,(a*2**i)%L,L),qubits = [i]+list(range(n,2*n+1)), inplace=True)
    qc.compose(qft(n+1).inverse(),qubits = list(range(n,2*n+1)), inplace=True)

    return qc

def add_mod(n: int, a: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 2)
    # Write your code here:
    qc.compose(shift(n+1,2**n-L+a).control(1), inplace=True)
    qc.compose(qft(n+1).inverse(),qubits =  list(range(1,n+2)), inplace=True)
    qc.compose(pack(n,2**n-L+a,2**n+a), inplace=True)
    qc.compose(qft(n+1),qubits =  list(range(1,n+2)), inplace=True)
    qc.compose(shift(n+1,a).control(1), inplace=True)

    return qc

def pack(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 2)
    # Write your code here:
    qc.compose(qft(n),qubits = list(range(1,n+1)), inplace=True)
    qc.compose(shift(n,-t).control(2),qubits = [0,n+1]+list(range(1,n+1)), inplace=True)
    qc.x(n+1)
    qc.compose(shift(n,-s).control(2),qubits = [0,n+1]+list(range(1,n+1)), inplace=True)
    qc.x(n+1)
    qc.compose(qft(n).inverse(),qubits = list(range(1,n+1)), inplace=True)
    return qc

def qft(n):
    qc = QuantumCircuit(n)
    for i in range(n-1,-1,-1):
        qc.h(i)
        for j in range(i-1,-1,-1):
            qc.cp(2*math.pi/2**(i-j+1),j,i)
    for k in range(n//2):
        qc.swap(k,n-1-k)
    return qc

def shift(n,a):
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.p(2*math.pi*a/2**(n-i),i)
    return qc
'''


'''python
def solve(n: int, m: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(2 * m + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(n):
        qc.h(i)
    qc.x(n)
    multipliers = doubling_powers(n,a,L)
    for i in range(n):
        qc.compose(multiply_mod(m,multipliers[i],L).control(1),qubits = [i] + list(range(n,n+2*m+1)),inplace=True)
    return qc

def doubling_powers(n: int, a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    result.append(a)
    for i in range(1,n):
        result.append(result[-1]**2%L)
    return result

def multiply_mod(n: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(n + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(add_multi_mod(n,a,L),inplace=True)
    gcd,x,y = extended_euclidean(a,L)
    a_inv = x % L
    #print(a_inv)
    for i in range(n):
        qc.swap(i,n+i)
    qc.compose(add_multi_mod(n,L-a_inv,L),inplace=True)
    return qc

def extended_euclidean(a: int, b: int) -> tuple[int, int, int]:
    # when b == 0:
    if b == 0:
        return a, 1, 0

    # recursive step
    gcd, x1, y1 = extended_euclidean(b, a % b)

    # update x and y
    x = y1
    y = x1 - (a // b) * y1

    return gcd, x, y

def add_multi_mod(n: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(n + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(qft(n+1),qubits = list(range(n,2*n+1)), inplace=True)
    for i in range(n):
        qc.compose(add_mod(n,(a*2**i)%L,L),qubits = [i]+list(range(n,2*n+1)), inplace=True)
    qc.compose(qft(n+1).inverse(),qubits = list(range(n,2*n+1)), inplace=True)

    return qc

def add_mod(n: int, a: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 2)
    # Write your code here:
    qc.compose(shift(n+1,2**n-L+a).control(1), inplace=True)
    qc.compose(qft(n+1).inverse(),qubits =  list(range(1,n+2)), inplace=True)
    qc.compose(pack(n,2**n-L+a,2**n+a), inplace=True)
    qc.compose(qft(n+1),qubits =  list(range(1,n+2)), inplace=True)
    qc.compose(shift(n+1,a).control(1), inplace=True)

    return qc

def pack(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 2)
    # Write your code here:
    qc.compose(qft(n),qubits = list(range(1,n+1)), inplace=True)
    qc.compose(shift(n,-t).control(2),qubits = [0,n+1]+list(range(1,n+1)), inplace=True)
    qc.x(n+1)
    qc.compose(shift(n,-s).control(2),qubits = [0,n+1]+list(range(1,n+1)), inplace=True)
    qc.x(n+1)
    qc.compose(qft(n).inverse(),qubits = list(range(1,n+1)), inplace=True)
    return qc

def qft(n):
    qc = QuantumCircuit(n)
    for i in range(n-1,-1,-1):
        qc.h(i)
        for j in range(i-1,-1,-1):
            qc.cp(2*math.pi/2**(i-j+1),j,i)
    for k in range(n//2):
        qc.swap(k,n-1-k)
    return qc

def shift(n,a):
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.p(2*math.pi*a/2**(n-i),i)
    return qc
'''


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

def solve(n: int, m: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(2 * m + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(n):
        qc.h(i)
    qc.x(n)
    multipliers = doubling_powers(n,a,L)
    for i in range(n):
        qc.compose(multiply_mod(m,multipliers[i],L).control(1),qubits = [i] + list(range(n,n+2*m+1)),inplace=True)
    return qc

def doubling_powers(n: int, a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    result.append(a)
    for i in range(1,n):
        result.append(result[-1]**2%L)
    return result

def multiply_mod(n: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(n + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(add_multi_mod(n,a,L),inplace=True)
    gcd,x,y = extended_euclidean(a,L)
    a_inv = x % L
    #print(a_inv)
    for i in range(n):
        qc.swap(i,n+i)
    qc.compose(add_multi_mod(n,L-a_inv,L),inplace=True)
    return qc

def extended_euclidean(a: int, b: int) -> tuple[int, int, int]:
    # when b == 0:
    if b == 0:
        return a, 1, 0

    # recursive step
    gcd, x1, y1 = extended_euclidean(b, a % b)

    # update x and y
    x = y1
    y = x1 - (a // b) * y1

    return gcd, x, y

def add_multi_mod(n: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(n + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.compose(qft(n+1),qubits = list(range(n,2*n+1)), inplace=True)
    for i in range(n):
        qc.compose(add_mod(n,(a*2**i)%L,L),qubits = [i]+list(range(n,2*n+1)), inplace=True)
    qc.compose(qft(n+1).inverse(),qubits = list(range(n,2*n+1)), inplace=True)

    return qc

def add_mod(n: int, a: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 2)
    # Write your code here:
    qc.compose(shift(n+1,2**n-L+a).control(1), inplace=True)
    qc.compose(qft(n+1).inverse(),qubits =  list(range(1,n+2)), inplace=True)
    qc.compose(pack(n,2**n-L+a,2**n+a), inplace=True)
    qc.compose(qft(n+1),qubits =  list(range(1,n+2)), inplace=True)
    qc.compose(shift(n+1,a).control(1), inplace=True)

    return qc

def pack(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 2)
    # Write your code here:
    qc.compose(qft(n),qubits = list(range(1,n+1)), inplace=True)
    qc.compose(shift(n,-t).control(2),qubits = [0,n+1]+list(range(1,n+1)), inplace=True)
    qc.x(n+1)
    qc.compose(shift(n,-s).control(2),qubits = [0,n+1]+list(range(1,n+1)), inplace=True)
    qc.x(n+1)
    qc.compose(qft(n).inverse(),qubits = list(range(1,n+1)), inplace=True)
    return qc

def qft(n):
    qc = QuantumCircuit(n)
    for i in range(n-1,-1,-1):
        qc.h(i)
        for j in range(i-1,-1,-1):
            qc.cp(2*math.pi/2**(i-j+1),j,i)
    for k in range(n//2):
        qc.swap(k,n-1-k)
    return qc

def shift(n,a):
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.p(2*math.pi*a/2**(n-i),i)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import math
def solve(n: int, m: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(2 * m + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(n):
        qc.h(i)
    qc.x(n)
    multipliers = doubling_powers(n,a,L)
    for i in range(n):
        qc.compose(multiply_mod(m,multipliers[i],L),qubits = [i] + list(range(n,n+2*m+1)),inplace=True)
    return qc

def doubling_powers(n: int, a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    result.append(a)
    for i in range(1,n):
        result.append(result[-1]**2%L)
    return result

def multiply_mod(n: int, a: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(2*n+2)
    # Write your code here:
    qc.compose(add_multi_mod(n,a,L),inplace=True)
    gcd,x,y = extended_euclidean(a,L)
    a_inv = x % L
    #print(a_inv)
    for i in range(n):
        qc.cswap(0,i+1,n+i+1)
    qc.compose(add_multi_mod(n,L-a_inv,L),inplace=True)
    return qc

def extended_euclidean(a: int, b: int) -> tuple[int, int, int]:
    # when b == 0:
    if b == 0:
        return a, 1, 0

    # recursive step
    gcd, x1, y1 = extended_euclidean(b, a % b)

    # update x and y
    x = y1
    y = x1 - (a // b) * y1

    return gcd, x, y

def add_multi_mod(n: int, a: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(2*n+2)
    # Write your code here:
    qc.compose(qft(n+1),qubits = list(range(n+1,2*n+2)), inplace=True)
    for i in range(n):
        qc.compose(add_mod(n,(a*2**i)%L,L),qubits = [0,i+1]+list(range(n+1,2*n+2)), inplace=True)
    qc.compose(qft(n+1).inverse(),qubits = list(range(n+1,2*n+2)), inplace=True)

    return qc

def add_mod(n: int, a: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 3)
    # Write your code here:
    qc.compose(shift(n+1,2**n-L+a).control(2), inplace=True)
    qc.compose(qft(n+1).inverse(),qubits = list(range(2,n+3)), inplace=True)
    qc.compose(pack(n,2**n-L+a,2**n+a), inplace=True)
    qc.compose(qft(n+1),qubits = list(range(2,n+3)), inplace=True)
    qc.compose(shift(n+1,a).control(2), inplace=True)

    return qc

def pack(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 3)
    # Write your code here:
    qc.compose(qft(n),qubits = list(range(2,n+2)), inplace=True)
    qc.compose(shift(n,-t).control(3),qubits = [0,1,n+2]+list(range(2,n+2)), inplace=True)
    qc.x(n+2)
    qc.compose(shift(n,-s).control(3),qubits = [0,1,n+2]+list(range(2,n+2)), inplace=True)
    qc.x(n+2)
    qc.compose(qft(n).inverse(),qubits = list(range(2,n+2)), inplace=True)
    return qc

def qft(n):
    qc = QuantumCircuit(n)
    for i in range(n-1,-1,-1):
        qc.h(i)
        for j in range(i-1,-1,-1):
            qc.cp(2*math.pi/2**(i-j+1),j,i)
    for k in range(n//2):
        qc.swap(k,n-1-k)
    return qc

def shift(n,a):
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.p(2*math.pi*a/2**(n-i),i)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, m: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(2 * m + 1)
    qc = QuantumCircuit(x, y)

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

    # Step 2: For each basis state |k⟩, compute a^k mod L
    for k in range(n):
        # For each qubit in k's binary representation
        power = 2**k
        # If kth qubit is 1, multiply by a^(2^k) mod L
        controlled_modmult(qc, x[k], y, a**power % L, L, m)

    return qc

def controlled_modmult(qc, control, target_reg, factor, modulus, m):
    """
    Applies controlled modular multiplication:
    If control=1: |y⟩ → |y * factor mod modulus⟩
    """
    n_target = len(target_reg)

    # Convert factor to binary representation
    bin_factor = format(factor % modulus, f'0{n_target}b')

    # Apply CNOT gates based on binary representation of factor
    for i, bit in enumerate(reversed(bin_factor)):
        if bit == '1':
            qc.cx(control, target_reg[i])

    # Apply modular reduction
    # This is a simplified version - in practice, you'd need
    # a more sophisticated quantum modular arithmetic implementation
    # that handles the modular reduction in a reversible way
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, m: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(2 * m + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    def add_constant(qc, const, reg, controls):
        if const == 0:
            return
        n_reg = len(reg)
        for i in range(n_reg):
            if (const & (1 << i)):
                sub_reg = reg[i:]
                ctrl_list = controls[:]
                if len(ctrl_list) > 0:
                    qc.mcx(ctrl_list, sub_reg[0])
                else:
                    qc.x(sub_reg[0])
                for k in range(1, len(sub_reg)):
                    ctrl_list = controls + sub_reg[0:k]
                    qc.mcx(ctrl_list, sub_reg[k])

    def modular_add_constant(qc, const, reg, L, controls):
        if const == 0:
            return
        add_constant(qc, const, reg, controls)
        temp = (1 << len(reg)) - L
        add_constant(qc, temp, reg, controls)
        add_constant(qc, L, reg, controls + [reg[-1]])

    qc.h(x)
    qc.x(y[0])
    z_reg = y[0:m]
    aux_reg = y[m:2*m+1]
    for j in range(n):
        c = pow(a, 1 << j, L)
        if c == 0 or c == 1:
            continue
        inv = pow(c, -1, L)
        neg_inv = L - inv
        # CMULT c
        for bit in range(m):
            ctrl2 = z_reg[bit]
            e = (c * (1 << bit)) % L
            modular_add_constant(qc, e, aux_reg, L, [x[j], ctrl2])
        # cswap
        for bit in range(m):
            qc.cswap(x[j], z_reg[bit], aux_reg[bit])
        # CMULT neg_inv
        for bit in range(m):
            ctrl2 = z_reg[bit]
            e = (neg_inv * (1 << bit)) % L
            modular_add_constant(qc, e, aux_reg, L, [x[j], ctrl2])

    return qc
'''


'''python
from __future__ import annotations
from math import pi
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import QFT         # ∣x⟩↦∣x̃⟩ helper
# ------------------------------------------------------------------
# elementary arithmetic gadgets
# ------------------------------------------------------------------
def qft_add_const(qc: QuantumCircuit, q: list, c: int) -> None:
    """ |x⟩  → |x+c mod 2^k⟩   (no ancilla, k = len(q))
        little-endian QFT adder (Cuccaro, Draper 2000) """
    k = len(q)
    qc.append(QFT(k, do_swaps=False).to_gate(label='QFT'), q)
    for j in range(k):
        angle = 2*pi*((c % (1<<k)) >> j) / (1 << (j+1))
        if abs(angle) > 1e-12:
            qc.p(angle, q[j])
    qc.append(QFT(k, do_swaps=False).inverse().to_gate(label='QFT†'), q)

def add_const_mod(qc: QuantumCircuit,
                  tgt: list,                       # k  value qubits  (little-endian)
                  c: int,                          # classical const  (0 ≤ c < L)
                  L: int,                          # modulus
                  flag: int) -> None:              # *one* ancilla qubit inside tgt-register
    """ |x⟩|0⟩  → |(x+c) mod L⟩|0⟩
        *flag* is the MSB of *tgt*: we recycle it as the “overflow” bit.

        Steps   (all reversible):
        1.  x ← x+c                    (plain addition)
        2.  x-L?  set flag if x ≥ L    (comparator)
        3.  conditional −L             (controlled on flag)
        4.  uncompute comparator       (flag back to |0⟩)
    """
    k = len(tgt)-1                     # data bits 0…k-1, flag at k
    data = tgt[:k]                     # shorthand
    fbit = tgt[k]

    # 1. add c
    qft_add_const(qc, data, c)

    # 2. compute “x ≥ L”  (compare to L-1 in two’s complement):
    inv = ((1 << k) - L)               # actually add (2^k − L)
    qft_add_const(qc, data, inv)       # x ← x + 2^k - L   (mod 2^k)
    qc.x(data[-1])                     # MSB is 0  ↔  overflow  (‖ 2’s-complement sign)
    qc.cx(data[-1], fbit)              # copy sign into flag
    qc.x(data[-1])

    # 3. if overflow, add L back
    qft_add_const(qc, data, (L if c else 0))      # tidy up phase
    qc.cx(fbit, data[-1])
    qc.x(data[-1])
    qc.cx(data[-1], fbit)
    qft_add_const(qc, data, (1 << k) - L)         # undo comparator residue

    # 4. uncompute flag
    qft_add_const(qc, data, (1 << k) - c)         # restore original basis
    qc.cx(data[-1], fbit)                         # flag back to |0〉
    qft_add_const(qc, data, c)                    # data contains (x+c) mod L

def controlled_mul_const_mod(qc: QuantumCircuit,
                             ctl, tgt: list, const: int, L: int) -> None:
    """If ctl==1 multiply |y⟩ by fixed *const* modulo L   (textbook “double-and-add”).

       Uses the upper m ancilla qubits already available inside *tgt*.
    """
    k = (len(tgt) - 1) // 2            # k = m
    acc   = tgt[:k]                    # low  m  qubits – current value
    work  = tgt[k:2*k]                 # upper m qubits – scratch (starts/ends |0⟩)
    flag  = tgt[-1]                    # one shared ancilla

    # copy acc into work so that we can accumulate there
    for j in range(k):
        qc.cx(acc[j], work[j])

    for j in range(k):                 # multiply via “shift-add”
        bit_const = (const >> j) & 1
        if bit_const:
            # add (2^j mod L) * y   controlled on ctl & acc[j]
            cc = QuantumCircuit(1, name=f'×{const}·2^{j}')
            add_const_mod(qc, work + [flag], (const << j) % L, L, flag)
            for t in [*work, flag]:
                qc.cx(ctl, t).c_if(acc[j], 1)     # dual control: ctl & acc[j]

    # swap result into low register, clean ancilla
    for j in range(k):
        qc.cx(work[j], acc[j])
        qc.cx(acc[j], work[j])
        qc.cx(work[j], acc[j])

# ------------------------------------------------------------------
# complete solution
# ------------------------------------------------------------------
def solve(n: int, m: int, a: int, L: int) -> QuantumCircuit:
    """
    Prepares  (1/√2^n) Σ_k |k⟩ |a^k mod L⟩     with *real* modular arithmetic.
    Register layout (little-endian):
        x  : n          exponent qubits
        y  : m          value   qubits         –  current number   (< L ≤ 2^m)
             m          work    ancillae       –  scratch that returns to |0〉
             1          overflow flag
    """
    x = QuantumRegister(n,          'x')
    y = QuantumRegister(2 * m + 1,  'y')
    qc = QuantumCircuit(x, y)

    # |k〉 superposition
    qc.h(x)

    # initialise y to 1 (multiplicative identity)
    qc.x(y[0])

    # repeated-squaring table  a^{2^i} (mod L)
    powers = [pow(a, 1 << i, L) for i in range(n)]

    # for each exponent bit apply controlled modular-multiplication
    for i, const in enumerate(powers):
        if const != 1:                          # skip no-op factors
            controlled_mul_const_mod(qc, x[i], list(y), const, L)

    return qc
'''


'''python
from math import pi
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import QFT         # ∣x⟩↦∣x̃⟩ helper
# ------------------------------------------------------------------
# elementary arithmetic gadgets
# ------------------------------------------------------------------
def qft_add_const(qc: QuantumCircuit, q: list, c: int) -> None:
    """ |x⟩  → |x+c mod 2^k⟩   (no ancilla, k = len(q))
        little-endian QFT adder (Cuccaro, Draper 2000) """
    k = len(q)
    qc.append(QFT(k, do_swaps=False).to_gate(label='QFT'), q)
    for j in range(k):
        angle = 2*pi*((c % (1<<k)) >> j) / (1 << (j+1))
        if abs(angle) > 1e-12:
            qc.p(angle, q[j])
    qc.append(QFT(k, do_swaps=False).inverse().to_gate(label='QFT†'), q)

def add_const_mod(qc: QuantumCircuit,
                  tgt: list,                       # k  value qubits  (little-endian)
                  c: int,                          # classical const  (0 ≤ c < L)
                  L: int,                          # modulus
                  flag: int) -> None:              # *one* ancilla qubit inside tgt-register
    """ |x⟩|0⟩  → |(x+c) mod L⟩|0⟩
        *flag* is the MSB of *tgt*: we recycle it as the “overflow” bit.

        Steps   (all reversible):
        1.  x ← x+c                    (plain addition)
        2.  x-L?  set flag if x ≥ L    (comparator)
        3.  conditional −L             (controlled on flag)
        4.  uncompute comparator       (flag back to |0⟩)
    """
    k = len(tgt)-1                     # data bits 0…k-1, flag at k
    data = tgt[:k]                     # shorthand
    fbit = tgt[k]

    # 1. add c
    qft_add_const(qc, data, c)

    # 2. compute “x ≥ L”  (compare to L-1 in two’s complement):
    inv = ((1 << k) - L)               # actually add (2^k − L)
    qft_add_const(qc, data, inv)       # x ← x + 2^k - L   (mod 2^k)
    qc.x(data[-1])                     # MSB is 0  ↔  overflow  (‖ 2’s-complement sign)
    qc.cx(data[-1], fbit)              # copy sign into flag
    qc.x(data[-1])

    # 3. if overflow, add L back
    qft_add_const(qc, data, (L if c else 0))      # tidy up phase
    qc.cx(fbit, data[-1])
    qc.x(data[-1])
    qc.cx(data[-1], fbit)
    qft_add_const(qc, data, (1 << k) - L)         # undo comparator residue

    # 4. uncompute flag
    qft_add_const(qc, data, (1 << k) - c)         # restore original basis
    qc.cx(data[-1], fbit)                         # flag back to |0〉
    qft_add_const(qc, data, c)                    # data contains (x+c) mod L

def controlled_mul_const_mod(qc: QuantumCircuit,
                             ctl, tgt: list, const: int, L: int) -> None:
    """If ctl==1 multiply |y⟩ by fixed *const* modulo L   (textbook “double-and-add”).

       Uses the upper m ancilla qubits already available inside *tgt*.
    """
    k = (len(tgt) - 1) // 2            # k = m
    acc   = tgt[:k]                    # low  m  qubits – current value
    work  = tgt[k:2*k]                 # upper m qubits – scratch (starts/ends |0⟩)
    flag  = tgt[-1]                    # one shared ancilla

    # copy acc into work so that we can accumulate there
    for j in range(k):
        qc.cx(acc[j], work[j])

    for j in range(k):                 # multiply via “shift-add”
        bit_const = (const >> j) & 1
        if bit_const:
            # add (2^j mod L) * y   controlled on ctl & acc[j]
            cc = QuantumCircuit(1, name=f'×{const}·2^{j}')
            add_const_mod(qc, work + [flag], (const << j) % L, L, flag)
            for t in [*work, flag]:
                qc.cx(ctl, t).c_if(acc[j], 1)     # dual control: ctl & acc[j]

    # swap result into low register, clean ancilla
    for j in range(k):
        qc.cx(work[j], acc[j])
        qc.cx(acc[j], work[j])
        qc.cx(work[j], acc[j])

# ------------------------------------------------------------------
# complete solution
# ------------------------------------------------------------------
def solve(n: int, m: int, a: int, L: int) -> QuantumCircuit:
    """
    Prepares  (1/√2^n) Σ_k |k⟩ |a^k mod L⟩     with *real* modular arithmetic.
    Register layout (little-endian):
        x  : n          exponent qubits
        y  : m          value   qubits         –  current number   (< L ≤ 2^m)
             m          work    ancillae       –  scratch that returns to |0〉
             1          overflow flag
    """
    x = QuantumRegister(n,          'x')
    y = QuantumRegister(2 * m + 1,  'y')
    qc = QuantumCircuit(x, y)

    # |k〉 superposition
    qc.h(x)

    # initialise y to 1 (multiplicative identity)
    qc.x(y[0])

    # repeated-squaring table  a^{2^i} (mod L)
    powers = [pow(a, 1 << i, L) for i in range(n)]

    # for each exponent bit apply controlled modular-multiplication
    for i, const in enumerate(powers):
        if const != 1:                          # skip no-op factors
            controlled_mul_const_mod(qc, x[i], list(y), const, L)

    return qc
'''


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

# ──────────────────────────────────────────────────────────────
# 1.  In-place QFT / inverse QFT  (little-endian, no final swaps)
# ──────────────────────────────────────────────────────────────
def qft(qc: QuantumCircuit, qubits) -> None:
    """Quantum Fourier Transform on *qubits* (little-endian, in-place)."""
    n = len(qubits)
    for j in range(n):
        qc.h(qubits[j])
        for k in range(j + 1, n):
            qc.cp(pi / (1 << (k - j)), qubits[k], qubits[j])

def iqft(qc: QuantumCircuit, qubits) -> None:
    """Inverse QFT matching qft()."""
    n = len(qubits)
    for j in reversed(range(n)):
        for k in reversed(range(j + 1, n)):
            qc.cp(-pi / (1 << (k - j)), qubits[k], qubits[j])
        qc.h(qubits[j])

# ──────────────────────────────────────────────────────────────
# 2.  QFT-based in-register constant adder  (little-endian)
# ──────────────────────────────────────────────────────────────
def qft_add_const(qc: QuantumCircuit, q, c: int) -> None:
    """|x⟩ → |x+c mod 2^k⟩   on little-endian register *q* (k = len(q))."""
    k = len(q)
    qft(qc, q)
    for j in range(k):
        angle = 2 * pi * ((c % (1 << k)) >> j) / (1 << (j + 1))
        if abs(angle) > 1e-12:
            qc.p(angle, q[j])
    iqft(qc, q)

# ──────────────────────────────────────────────────────────────
# 3.  Add-constant-mod-L gadget   (Cuccaro comparator trick)
# ──────────────────────────────────────────────────────────────
def add_const_mod(qc: QuantumCircuit, tgt: list, c: int, L: int, flag):
    """
    Reversible: |x⟩|0⟩ ↦ |(x+c) mod L⟩|0⟩.
    *tgt*  : k+1 qubits  (k data, 1 overflow flag  = tgt[-1] = *flag*)
    *c*    : classical integer < L
    *L*    : modulus (≤ 2^k)
    """
    k = len(tgt) - 1
    data = tgt[:k]

    # 1. x ← x + c (mod 2^k)
    qft_add_const(qc, data, c)

    # 2. compute overflow : if x ≥ L copy sign bit into flag
    qft_add_const(qc, data, (1 << k) - L)      # add 2^k − L
    qc.x(data[-1])
    qc.cx(data[-1], flag)
    qc.x(data[-1])

    # 3. conditional −L  (only if overflow flag = 1)
    qc.ctrl(1)
    qft_add_const(qc, data, L)                 # add L back  (controlled)
    qc.ctrl(0)

    # 4. uncompute comparator & restore basis
    qft_add_const(qc, data, L - c)             # net effect: +(1<<k)−L−c
    qc.cx(data[-1], flag)
    qft_add_const(qc, data, c)                 # final value  (x+c) mod L

# ──────────────────────────────────────────────────────────────
# 4.  Controlled “× const  (mod L)” via double-and-add
# ──────────────────────────────────────────────────────────────
def controlled_mul_const_mod(qc: QuantumCircuit, ctl, tgt: list,
                             const: int, L: int) -> None:
    """If *ctl*==1 multiply |y⟩ by *const* modulo L in-place."""
    m = (len(tgt) - 1) // 2
    acc  = tgt[:m]          # value register
    work = tgt[m:2 * m]     # scratch (starts/ends |0⟩)
    flag = tgt[-1]

    # copy acc → work   (so we can accumulate result in *work*)
    for j in range(m):
        qc.cx(acc[j], work[j])

    # “shift–add” multiply
    for j in range(m):
        if (const >> j) & 1:
            # add (2^j · acc)  controlled on ctl & acc[j]
            qc.ccx(ctl, acc[j], flag)          # create combined control in *flag*
            add_const_mod(qc, work + [flag], (const << j) % L, L, flag)
            qc.ccx(ctl, acc[j], flag)          # uncompute flag

    # swap result into acc, clear work
    for j in range(m):
        qc.cx(work[j], acc[j])
        qc.cx(acc[j], work[j])
        qc.cx(work[j], acc[j])

# ──────────────────────────────────────────────────────────────
# 5.  Complete solver
# ──────────────────────────────────────────────────────────────
def solve(n: int, m: int, a: int, L: int) -> QuantumCircuit:
    """
    Prepare  |A⟩ = 2^{-n/2} Σ_k |k⟩ |a^k mod L⟩   with genuine modular arithmetic.
    Layout (little-endian):
        x : n               – exponent
        y : m               – value
            m               – work
            1               – overflow flag
    """
    x = QuantumRegister(n,          'x')
    y = QuantumRegister(2 * m + 1,  'y')
    qc = QuantumCircuit(x, y)

    # uniform superposition over |k⟩
    qc.h(x)

    # initial value register ← 1
    qc.x(y[0])

    # repeated-squaring table  a^{2^i} (mod L)
    powers = [pow(a, 1 << i, L) for i in range(n)]

    # modular exponentiation by controlled multiplications
    for i, const in enumerate(powers):
        if const != 1:
            controlled_mul_const_mod(qc, x[i], list(y), const, L)

    return qc
'''


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

def make_control(g, bit):
    return g if bit == 0 else g.control(bit)

class QArith:
    @staticmethod
    def qft(n):
        qc = QuantumCircuit(n)

        thetas = []
        for k in range(0, 1 + n):
            thetas.append(2 * math.pi / (2**k))

        for idx in range(0, n):
            qc.h(n - 1 - idx)
            for jdx in range(idx + 1, n):
                thetaidx = jdx - idx + 1
                qc.cp(thetas[thetaidx], n - 1 - jdx, n - 1 - idx)

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

        return qc

    @staticmethod
    def add_const(n: int, add: int, control_bits: int = 0) -> QuantumCircuit:
        from qiskit.circuit.library.standard_gates import PhaseGate

        k, control = QuantumRegister(n), QuantumRegister(control_bits)
        qc = QuantumCircuit(k, control)

        q = QArith.qft(n)
        qc.compose(q, inplace=True)

        for idx in range(n):
            theta = 2 * add * math.pi * (2**idx) / (2**n)
            p = make_control(PhaseGate(theta), control_bits)
            qc.append(p, [*control, k[idx]])

        qc.compose(q.inverse(), inplace=True)

        return qc

    @staticmethod
    def modadd_const(n: int, a: int, mod: int, control_bits: int = 0) -> QuantumCircuit:
        regin, control = QuantumRegister(n + 1), QuantumRegister(control_bits)
        qc = QuantumCircuit(regin, control)

        def _separate_rot(n: int, s: int, t: int, control_bits: int = 0) -> QuantumCircuit:
            from qiskit.circuit.library import XGate

            regin, control = QuantumRegister(n + 1), QuantumRegister(control_bits)
            qc = QuantumCircuit(regin, control)

            mcx = make_control(XGate(), control_bits)

            qc.append(mcx, [*control, regin[-1]])
            qc.compose(QArith.add_const(n, -s, 1 + control_bits), inplace=True)
            qc.append(mcx, [*control, regin[-1]])
            qc.compose(
                QArith.add_const(n, 2 ** (n + 1) - t, 1 + control_bits),
                inplace=True,
            )

            return qc

        qc.compose(
            QArith.add_const(n + 1, 2**n - mod + a, control_bits),
            inplace=True,
        )
        qc.compose(_separate_rot(n, 2**n - mod + a, 2**n + a, control_bits), inplace=True)
        qc.compose(QArith.add_const(n + 1, a, control_bits), inplace=True)

        return qc

    @staticmethod
    def modmult_const(n: int, a: int, mod: int, control_bits: int = 0) -> QuantumCircuit:
        """|x>|y> -> |x>|y+ax>"""
        x, y, control = QuantumRegister(n), QuantumRegister(n + 1), QuantumRegister(control_bits)
        qc = QuantumCircuit(x, y, control)

        for idx in range(n):
            add = a * (2**idx) % mod
            qc.compose(QArith.modadd_const(n, add, mod, 1 + control_bits), [*y, *control, x[idx]], inplace=True)

        return qc

    @staticmethod
    def modmult_const_inplace(n: int, a: int, mod: int, control_bits: int = 0) -> QuantumCircuit:
        """|x>|0> -> |ax>|0>"""
        from qiskit.circuit.library import SwapGate

        x, y, control = QuantumRegister(n), QuantumRegister(n + 1), QuantumRegister(control_bits)
        qc = QuantumCircuit(x, y, control)

        qc.compose(QArith.modmult_const(n, a, mod, control_bits), inplace=True)

        cswap = make_control(SwapGate(), control_bits)
        for idx in range(n):
            qc.append(cswap, [*control, x[idx], y[idx]])

        qc.compose(QArith.modmult_const(n, -pow(a, -1, mod), mod, control_bits), inplace=True)

        return qc

def solve(n: int, m: int, a: int, mod: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(2 * m + 1)
    qc = QuantumCircuit(x, y)

    qc.h(x)
    qc.x(y[0])
    for idx in range(n):
        multpow = QArith.modmult_const_inplace(m, pow(a, 2**idx, mod), mod, 1)
        qc.compose(multpow, [*y, x[idx]], inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, m: int, a: int, L: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(2 * m + 1)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(n):
        qc.h(x[i])
        qc.x(y[0])
        d=2**(2*m+1)
    def U(r):
        M=np.zeros((d,d),dtype=complex)
        for i in range(d):
            j=(i*r)%L if i<L else i
            M[j,i]=1
        return M
    for j in range(n):
        r=pow(a,2**j,L)
        gate=UnitaryGate(U(r),label="U"+str(r))
        cgate=gate.control(1)
        qc.append(cgate,[x[j]]+list(y))
    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


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

def solve():
    qc = QuantumCircuit(1)
    qc.x(range(1))
    qc.h(range(1))

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    qc.x(0)
    qc.h(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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

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


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


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

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


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
    qc.x(0)  # Xゲート
    qc.z(0)  # Zゲート
    qc.x(0)  # Xゲート
    return qc

print(solve().draw())
'''


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
    qc.initialize(initial_state, 0)
    qc.x(0)  # Xゲート
    qc.z(0)  # Zゲート
    qc.x(0)  # Xゲート
    return qc

print(solve().draw())
'''


'''python
from qiskit import QuantumCircuit

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

print(solve().draw())
'''


'''python
from qiskit import QuantumCircuit

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

    qc.x(0)
    qc.h(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit
def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
    qc.h(0)
    qc.z(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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

    qc.x(0)
    qc.h(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    # Write your code here:
    qc.h(0)   # Apply Hadamard to qubit 0
    qc.z(0)   # Apply Z gate to qubit 0
    return qc
'''


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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


'''python
import numpy as np
import scipy
import math
from qiskit import QuantumCircuit
from qiskit import QuantumRegister
from qiskit.circuit.library.standard_gates import *

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

    return qc

def main():
    print(solve())

if __name__ == "__main__":
    main()
'''


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

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

    return qc

def main():
    print(solve())

if __name__ == "__main__":
    main()
'''


'''python
from qiskit import QuantumCircuit

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

    return qc

def main():
    print(solve())

if __name__ == "__main__":
    main()
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


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

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    qc.append(
        RYGate(180),
        [0],
    )
    return qc
'''


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

def solve() -> QuantumCircuit:
    qc = QuantumCircuit(1)
    qc.append(
        RYGate(pi/2),
        [0],
    )
    return qc
'''


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

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


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    # Write your code here:
    qc.h(0)
    qc.z(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    qc.x(0)
    qc.h(0)

    return qc
'''


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

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

    qc.h(0)
    qc.z(0)

    return qc

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


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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


'''python
from qiskit import QuantumCircuit

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


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

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

    mc_h = HGate().control(n)
    qc.append(mc_h, range(n, -1, -1))
    for i in range(n):
        qc.x(i+1)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)

    qc.x(k)
    ch_gate = HGate().control(n)
    qc.append(HGate().control(n), k[:] + m[:])
    qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(k)
    qc.append(HGate().control(n), [*k, m])
    qc.x(k)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(k)
    qc.append(HGate().control(n), [*k, m])
    qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

from qiskit.circuit.library.standard_gates import HGate

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

    mch_gate = HGate().control(n)
    qc.append(mch_gate, range(n, -1, -1))

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

from qiskit.circuit.library.standard_gates import HGate

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

    qc.x(k)

    mch_gate = HGate().control(n)
    qc.append(mch_gate, range(n, -1, -1))

    qc.x(k)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(k)
    qc.append(HGate().control(n), k, m)
    qc.x(k)
    qc.z(m)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(k)
    qc.append(HGate().control(n), [k, m])
    qc.x(k)
    qc.z(m)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(k)
    qc.append(HGate().control(n), list(range(1,n+1))+[0])
    qc.x(k)
    qc.z(m)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(k)
    qc.append(HGate().control(n), [k,m])
    qc.x(k)
    qc.z(m)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(m)
    qc.x(k)
    qc.append(HGate().control(n), list(range(1,n+1))+[0])
    qc.x(k)
    qc.z(m)
    qc.x(m)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(k)
    qc.append(HGate().control(n), range(n+1))#list(range(1,n+1))+[0])
    qc.x(k)
    qc.z(m)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(k)
    qc.append(HGate().control(n), list(range(1,n+1))+[0])
    qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)

    # Write your code here:
    if m == 0:
        qc.h(0)
        qc.z(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)

    # Write your code here:
    if k == 0:
        qc.h(0)
        qc.z(0)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)

    # Write your code here:
    # k = 0 の場合を全て 1 が立つようにする
    for i in range(1, n+1):
        qc.x(i)

    qc.h(0)
    qc.cz(range(1, n+1), 0)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)

    # Write your code here:
    # k = 0 の場合を全て 1 が立つようにする
    for i in range(1, n+1):
        qc.x(i)

    qc.h(0)
    qc.cz(range(1, n+1), 0)
    qc.h(0)

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

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)

    # Write your code here:
    # k = 0 の場合を全て 1 が立つようにする
    for i in range(1, n+1):
        qc.x(i)

    qc.h(0)
    qc.append(ZGate().control(n), list(range(1, n+1)))
    qc.h(0)

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

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)

    # Write your code here:
    # k = 0 の場合を全て 1 が立つようにする
    for i in range(1, n+1):
        qc.x(i)

    qc.h(0)
    qc.mcz(list(range(1, n+1)) + [0])
    qc.h(0)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)

    # Write your code here:
    # k = 0 の場合を全て 1 が立つようにする
    for i in range(1, n+1):
        qc.x(i)

    qc.h(0)
    qc.mcz(list(range(1, n+1)) + [0])
    qc.h(0)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)

    # Write your code here:
    # k = 0 の場合を全て 1 が立つようにする
    for i in range(1, n+1):
        qc.x(i)

    qc.h(0)
    qc.mcz(list(range(1, n+1)), 0)
    qc.h(0)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)

    # Write your code here:
    # k = 0 の場合を全て 1 が立つようにする
    for i in range(1, n+1):
        qc.x(i)

    qc.h(0)
    qc.mcz(range(1, n+1), 0)
    qc.h(0)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)

    # Write your code here:
    # k = 0 の場合を全て 1 が立つようにする
    for i in range(n):
        qc.x(k[i])

    qc.h(m[0])
    qc.mcz(k, m[0])
    qc.h(m[0])

    for i in range(n):
        qc.x(k[i])

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)

    # Write your code here:
    # k = 0 の場合を全て 1 が立つようにする
    for i in range(1, n+1):
        qc.x(i)

    qc.h(0)
    qc.mcz(range(1, n+1), 0)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1, 'm'), QuantumRegister(n, 'k')
    qc = QuantumCircuit(m, k)

    for i in range(n):
        qc.x(k[i])

    qc.h(m[0])
    if n == 1:
        qc.cz(k[0], m[0])
    else:
        qc.mcx(list(k), m[0])
    qc.h(m[0])

    for i in range(n):
        qc.x(k[i])

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1, 'm'), QuantumRegister(n, 'k')
    qc = QuantumCircuit(m, k)

    for i in range(n):
        qc.x(k[i])

    qc.h(m[0])
    if n == 1:
        qc.cz(k[0], m[0])
    else:
        qc.mcx(list(k), m[0])

    for i in range(n):
        qc.x(k[i])

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)

    for i in range(n):
        qc.x(k[i])

    qc.h(m[0])
    if n == 1:
        qc.cz(k[0], m[0])
    else:
        qc.mcx(list(k), m[0])

    for i in range(n):
        qc.x(k[i])

    return qc
'''


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

def oracle_qc(n: int):
    qc = QuantumCircuit(n + 2)  # m(1) + k(n) + ancilla(1)
    m_idx = 0
    k_idxs = list(range(1, n+1))
    anc_idx = n + 1

    # Step 1: ancillaをセット（k=0を検出）
    for i in k_idxs:
        qc.x(i)  # k_i=0 なら1になるよう反転
    qc.mcx(k_idxs, anc_idx)  # 全ビットが0のとき ancilla = 1
    for i in k_idxs:
        qc.x(i)  # 元に戻す

    # Step 2: ancillaが1ならmに対して変換を行う
    qc.h(m_idx)
    qc.cz(anc_idx, m_idx)
    qc.h(m_idx)

    # Step 3: ancillaをuncompute
    for i in k_idxs:
        qc.x(i)
    qc.mcx(k_idxs, anc_idx)
    for i in k_idxs:
        qc.x(i)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)

    qc.x(k)

    qc.compose(HGate().control(len(k)), [*k, m[0]], inplace=True)

    qc.x(k)

    return qc
'''


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

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

    qc.x(k)
    qc.append(HGate().control(n), reversed(range(0, n + 1)))

    return qc
'''


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

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

    qc.append(HGate().control(n), reversed(range(0, n + 1)))

    return qc
'''


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

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

    qc.x(k)
    qc.append(HGate().control(n), reversed(range(0, n + 1)))
    qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(i for i in k)
    qc.append(qc.h().control(m[0]), k)
    qc.x(i for i in k)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(i for i in k)
    qc.qppend(HGate().control(0), range(n+1))
    qc.x(i for i in k)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(i for i in k)
    qc.append(HGate().control(n), list(k) + [m[0]])
    qc.x(i for i in k)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.cx(k[n-1], m[0])
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    qc.append(HGate().control(n - 1), range(n))
    qc.append(ZGate().control(0), [m[0]])

    qc.x(k)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    qc.append(HGate().control(n), list(k) + [m])
    qc.append(ZGate().control(n), list(k) + [m])

    qc.x(k)
    return qc
'''


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

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

    qc.append(HGate().control(n), list(k) + [m])
    qc.append(ZGate().control(n), list(k) + [m])

    qc.x(k)
    return qc
'''


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

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

    qc.append(HGate().control(n - 1), range(n))
    qc.append(ZGate().control(0), [m[0]])

    qc.x(k)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    qc.append(HGate().control(n, ctrl_state='0'*n), k[:] + [m[0]])
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.h(k)
    qc.x(k)
    qc.append(HGate().control(1), range(n+1))
    qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector
from qiskit import QuantumRegister
from qiskit.circuit.library import HGate

# import math
# from qiskit.circuit.library import ZGate

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.h(k)
    qc.x(k)
    qc.append(HGate().control(1), range(n+1))
    qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.quantum_info import Statevector
from qiskit import QuantumRegister
from qiskit.circuit.library import HGate

# import math
# from qiskit.circuit.library import ZGate

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.h(k)
    qc.x(k)
    qc.append(HGate().control(1), range(n+1))
    qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.quantum_info import Statevector
from qiskit import QuantumRegister
from qiskit.circuit.library import HGate

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.h(k)
    qc.x(k)
    qc.append(HGate().control(n), range(n+1))
    qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.quantum_info import Statevector
from qiskit import QuantumRegister
from qiskit.circuit.library import HGate

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.h(k)
    qc.x(k)
    qc.append(HGate().control(n), [range(n+1), 0])
    qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.quantum_info import Statevector
from qiskit import QuantumRegister
from qiskit.circuit.library import HGate

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.h(k)
    qc.x(k)
    qc.append(HGate().control(n), [k, m])
    qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.quantum_info import Statevector
from qiskit import QuantumRegister
from qiskit.circuit.library import HGate

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.h(k)
    qc.x(k)
    qc.append(HGate().control(n), [1, 0])
    qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.quantum_info import Statevector
from qiskit import QuantumRegister
from qiskit.circuit.library import HGate

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.h(k)
    qc.x(k)
    control = [i+1 for i in range(n)]
    qc.append(HGate().control(n), [*control, 0])
    qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.quantum_info import Statevector
from qiskit import QuantumRegister
from qiskit.circuit.library import HGate

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.h(k)
    qc.x(k)
    qc.x(m)
    control = [i+1 for i in range(n)]
    qc.append(HGate().control(n), [*control, 0])
    qc.x(m)
    qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.quantum_info import Statevector
from qiskit import QuantumRegister
from qiskit.circuit.library import HGate, XGate

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.h(k)
    qc.x(k)
    control = [i+1 for i in range(n)]
    qc.append(XGate().control(n), [*control, 0])
    qc.append(HGate().control(n), [*control, 0])
    qc.append(XGate().control(n), [*control, 0])
    qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.quantum_info import Statevector
from qiskit import QuantumRegister
from qiskit.circuit.library import HGate

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.h(k)
    qc.x(k)
    control = [i+1 for i in range(n)]
    qc.append(HGate().control(n), [*control, 0])
    qc.x(k)
    qc.h(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.quantum_info import Statevector
from qiskit import QuantumRegister
from qiskit.circuit.library import HGate

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.h(k)
    control = [i+1 for i in range(n)]
    qc.append(HGate().control(n), [*control, 0])

    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.quantum_info import Statevector
from qiskit import QuantumRegister
from qiskit.circuit.library import HGate

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    control = [i+1 for i in range(n)]
    qc.append(HGate().control(n), [*control, 0])

    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.quantum_info import Statevector
from qiskit import QuantumRegister
from qiskit.circuit.library import HGate

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.h(k)
    qc.x(m)
    control = [i+1 for i in range(n)]
    qc.append(HGate().control(n), [k, m])
    qc.x(m)

    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.quantum_info import Statevector
from qiskit import QuantumRegister
from qiskit.circuit.library import HGate

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.h(k)
    qc.x(m)
    control = [i+1 for i in range(n)]
    qc.append(HGate().control(n), k, m)
    qc.x(m)

    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.quantum_info import Statevector
from qiskit import QuantumRegister
from qiskit.circuit.library import HGate

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.h(k)
    qc.x(k)
    control = [i+1 for i in range(n)]
    qc.append(HGate().control(n), [k, m])
    qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.quantum_info import Statevector
from qiskit import QuantumRegister
from qiskit.circuit.library import HGate

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.h(m)
    qc.h(k)
    qc.x(k)
    control = [i+1 for i in range(n)]
    qc.append(HGate().control(n), [k, m])
    qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.quantum_info import Statevector
from qiskit import QuantumRegister
from qiskit.circuit.library import HGate

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.h(m)
    qc.h(k)

    qc.x(k)
    qc.append(HGate().control(n), [m, k])
    qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.quantum_info import Statevector
from qiskit import QuantumRegister
from qiskit.circuit.library import HGate

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.h(m)
    qc.h(k)

    qc.x(k)
    qc.append(HGate().control(n), [1, 0])
    qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.quantum_info import Statevector
from qiskit import QuantumRegister
from qiskit.circuit.library import HGate

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.h(m)
    qc.h(k)

    qc.x(k)
    control = [i+1 for i in range(n)]
    qc.append(HGate().control(n), [*control, 0])
    qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.quantum_info import Statevector
from qiskit import QuantumRegister
from qiskit.circuit.library import HGate

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.h(m)
    qc.h(k)

    #qc.x(k)
    control = [i+1 for i in range(n)]
    qc.append(HGate().control(n), [*control, 0])
    #qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.quantum_info import Statevector
from qiskit import QuantumRegister
from qiskit.circuit.library import HGate

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    # qc.h(m)
    # qc.h(k)

    qc.x(k)
    control = [i+1 for i in range(n)]
    qc.append(HGate().control(n), [*control, 0])
    qc.x(k)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)

    qc.h(m)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)

    qc.x(k)
    qc.ch(k, m)
    qc.x(k)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)

    qc.x(k)
    qc.append(HGate().control(n), [*k, *m])
    qc.x(k)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(k)
    qc.append(HGate().control(n), k[:] + [m[0]])
    qc.append(ZGate().control(n + 1), k[:] + [m[0]])
    qc.x(k)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(k)
    qc.append(HGate().control(n), k[:] + [m[0]])
    qc.append(ZGate().control(n), k[:] + [m[0]])
    qc.x(k)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(k)
    qc.append(HGate().control(n), k[:] + [m[0]])
    qc.x(k)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    if k==0:
      qc.x(k)
      qc.h(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    if k==0:
      qc.x(m)
      qc.h(m)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    if k==0:
      qc.h(m)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    if k==0:
      qc.h(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    if k==0:
      qc.h(0)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    if k==0:
      qc.h(m)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    if k==0:
      qc.h(m[0])
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(k)
    qc.append(x.control(n-1), range(n))
    qc.append(h.control(n-1), range(n))
    qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(k)
    qc.append(x.control(n), range(n+1))
    qc.append(h.control(n), range(n+1))
    qc.x(k)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(k)
    qc.append(x.control(n), range(n+1))
    qc.append(h.control(n), range(n+1))
    qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import X, H

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(k)
    qc.append(X.control(n), range(n+1))
    qc.append(H.control(n), range(n+1))
    qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(k)
    qc.append(x(0).control(n), range(n+1))
    qc.append(h(0).control(n), range(n+1))
    qc.x(k)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(k)
    qc.append(XGate().control(n), range(n+1))
    qc.append(HGate().control(n), range(n+1))
    qc.x(k)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(k)
    qc.append(XGate().control(n), range(n+1))
    qc.append(HGat().control(n), range(n+1))
    qc.x(k)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(k)
    qc.append(XGate().control(n), range(n+1))
    qc.append(HGate().control(n), range(n+1))
    qc.x(k)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(k)
    qc.append(XGate().control(0), range(n+1))
    qc.append(HGate().control(0), range(n+1))
    qc.x(k)

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(k)
    qc.append(XGate().control(0), list(k) + [m[0]])
    qc.append(HGate().control(0), list(k) + [m[0]])
    qc.x(k)

    return qc
'''