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

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

    # Apply the QFT
    for i in range(n):
        qc.h(i)  # Apply Hadamard gate to qubit i
        for j in range(i+1, n):
            # Apply controlled phase rotation (CP) with decreasing angles
            qc.cp(np.pi / 2**(j-i), j, i)

    # Reverse the order of qubits by swapping them
    for i in range(n // 2):
        qc.swap(i, n-i-1)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

  for i in range(n):
    qc.h(i)
    for j in range(2, n - i + 1):
      qc.cp(2 * math.pi / pow(2, j), i + j - 1, i)
  return qc
'''


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

def QFT(qc : QuantumCircuit, idx : list[int]) -> QuantumCircuit:
  n = len(idx)
  for j in range(n):
    qc.h(idx[j])
    for k in range(j+1, n):
      theta = math.pi / (2 ** (k - j))
      qc.cp(theta, idx[k], idx[j])

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

  return qc

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    QFT(qc, list(n))
    return qc
'''


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

def QFT(qc : QuantumCircuit, idx : list[int]) -> QuantumCircuit:
    n = len(idx)
    for j in range(n):
        qc.h(idx[j])
        for k in range(j+1, n):
            theta = math.pi / (2 ** (k - j))
            qc.cp(theta, idx[k], idx[j])

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

    return qc

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    QFT(qc, list(n))
    return qc
'''


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

def QFT(qc : QuantumCircuit, idx : list[int]) -> QuantumCircuit:
    n = len(idx)
    for j in range(n):
        qc.h(idx[j])
        for k in range(j+1, n):
            theta = math.pi / (2 ** (k - j))
            qc.cp(theta, idx[k], idx[j])

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

    return qc

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


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

def QFT(qc : QuantumCircuit, idx : list[int]) -> QuantumCircuit:
    n = len(idx)
    for j in range(n):
        qc.h(idx[j])
        for k in range(j+1, n):
            theta = 2 * math.pi / (2 ** (k - j))
            qc.cp(theta, idx[k], idx[j])

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

    return qc

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


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

def QFT(qc : QuantumCircuit, idx : list[int]) -> QuantumCircuit:
    n = len(idx)
    for j in range(n):
        qc.h(idx[j])
        for k in range(j+1, n):
            theta = 2 * math.pi / (2 ** (k - j + 1))
            qc.cp(theta, idx[k], idx[j])

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

    return qc

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


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

def QFT(qc : QuantumCircuit, idx : list[int]) -> QuantumCircuit:
    for i in range(n // 2):
        qc.swap(idx[i], idx[n - i - 1])

    n = len(idx)
    for j in range(n):
        qc.h(idx[j])
        for k in range(j+1, n):
            theta = math.pi / (2 ** (k - j))
            qc.cp(theta, idx[k], idx[j])

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

    return qc

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


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

def QFT(qc : QuantumCircuit, idx : list[int]) -> QuantumCircuit:
    n = len(idx)

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

    for j in range(n):
        qc.h(idx[j])
        for k in range(j+1, n):
            theta = math.pi / (2 ** (k - j))
            qc.cp(theta, idx[k], idx[j])

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

    return qc

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


'''python
from qiskit import QuantumCircuit
import math

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

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

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


'''python
from qiskit import QuantumCircuit
import math

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

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

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


'''python
from qiskit import QuantumCircuit
import math

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

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

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


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


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

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

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    def qft_com(i):
        qc.h(i)
        for j in range(n - 1 - i):
            qc.cp((np.pi / 2 **(j + 1)), i + j + 1, i)
    for i in range(n):
        qft_com(i)
    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)
        for j in range(n - 1 - i):
            qc.cp((np.pi / (2 ** (j + 1))), i + j + 1, i)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)
        for j in range(n - 1 - i):
            qc.cp((np.pi / (2 ** (j + 1))), i + j + 1, i)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)
        for j in range(n - 1 - i):
            qc.cp((np.pi / (2 ** (j + 1))), i, i + j + 1)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    def qft_com(i):
        qc.h(i)
        for j in range(n - 1 - i):
            qc.cp((np.pi / 2 **(j + 1)), i + j + 1, i)
    for i in range(n):
        qft_com(i)
    for i in range(n // 2):
        qc.cx(i, n - 1 - i)
        qc.cx(n - 1 - i, i)
        qc.cx(i, n - 1 - i)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    def qft_com(i):
        qc.h(i)
        for j in range(n - 1 - i):
            qc.cp(np.pi / (2 **(j + 1)), i + j + 1, i)
    for i in range(n):
        qft_com(i)
    for i in range(n // 2):
        qc.cx(i, n - 1 - i)
        qc.cx(n - 1 - i, i)
        qc.cx(i, n - 1 - i)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    def qft_rotations(circuit, m):
        if m == 0:
            return circuit
        m -= 1
        circuit.h(n)
        for qubit in range(n):
            circuit.cp(pi/2**(n-qubit), qubit, m)
        qft_rotations(circuit, m)
    qft_rotations(qc, n)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    def qft_rotations(circuit, m):
        if m == 0:
            return circuit
        m -= 1
        circuit.h(m)
        for qubit in range(m):
            circuit.cp(pi/2**(m-qubit), qubit, m)
        qft_rotations(circuit, m)
    qft_rotations(qc, n)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    def qft_rotations(circuit, m):
        if m == 0:
            return circuit
        m -= 1
        circuit.h(m)
        for qubit in range(m):
            circuit.cp(np.pi/2**(-qubit), qubit, m)
        qft_rotations(circuit, m)
    qft_rotations(qc, n)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    def qft_rotations(circuit, m):
        if m == 0:
            return circuit
        m -= 1
        circuit.h(m)
        for qubit in range(m):
            circuit.cp(np.pi/2**(-qubit), qubit, m)
        qft_rotations(circuit, m)
    def swap_registers(circuit, m):
        for qubit in range(m//2):
            circuit.swap(qubit, m-qubit-1)
        return circuit
    qft_rotations(qc, n)
    swap_registers(qc, n)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for itarg in range(n - 1, -1, -1):
        qc.h(itarg)
        for ictrl in range(itarg - 1, -1, -1):
            power = ictrl - itarg - 1 + n
            qc.cp((2 ** power) * 2. * np.pi / (2 ** n), ictrl, itarg)

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


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.h(i)
        for k in range(i + 1, n):
            qc.append(PhaseGate(np.pi / (2 ** (k - i))).control(1), [k, i])
    qc.swap(0, n - 1)
    for i in range(n // 2):
        qc.swap(i, n - i - 1)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n == 1:
        qc.h(0)
        return qc
    for i in range(n):
        qc.h(i)
        for k in range(i + 1, n):
            qc.append(PhaseGate(np.pi / (2 ** (k - i))).control(1), [k, i])
    qc.swap(0, n - 1)
    for i in range(n // 2):
        qc.swap(i, n - i - 1)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qft = QFT(num_qubits=n).to_gate()
    qc.append(qft, qargs=list(range(n)))

    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)
        for j in range(i + 1, n):
            phase = 2 * pi / (2 ** (j - 1))
            qc.crz(phase / 2, i, j)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)
        for j in range(i + 1, n):
            phase = 2 * pi / (2 ** (j + 1))
            qc.crz(phase / 2, i, j)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)
        for j in range(i + 1, n):
            phase = 2 * pi / (2 ** (j + 1))
            qc.crz(phase / 2, j, i)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)
        for j in range(i + 1, n):
            phase = 2 * pi / (2 ** (j + 1))
            qc.crz(- phase / 2, j, i)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)
        for j in range(i + 1, n):
            phase = 2 * pi / (2 ** (j - i + 1))
            qc.crz(phase / 2, j, i)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)
        for j in range(i + 1, n):
            phase = 2 * pi / (2 ** (j - i + 1))
            qc.crz(phase, j, i)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)
        for j in range(i + 1, n):
            phase = 2 * pi / (2 ** (j - i + 1))
            qc.cp(phase , j, i)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)
        for j in range(i + 1, n):
            phase = 2 * pi / (2 ** (j - i + 1))
            qc.crz(phase , j, i)
    for i in range(n // 2):
        qc.swap(i, n - i - 1)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)
        for j in range(i + 1, n):
            phase = 2 * pi / (2 ** (j - i + 1))
            qc.cp(phase , j, i)
    for i in range(n // 2):
        qc.swap(i, n - i - 1)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)
        for j in range(i + 1, n):
            phase = pi / (2 ** (j - i))
            qc.cp(phase, j, i)  # Controlled phase gate
    qc.barrier()
    for i in range(n // 2):
        qc.swap(i, n - i - 1)  # Swap to reverse the order
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)
        for j in range(i + 1, n):
            phase = pi / (2 ** (j - i))
            qc.cp(phase, j, i)
    return qc
'''


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

def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)
        for j in range(i + 1, n):
            phase = 2 * pi / (2 ** (j - i + 1))
            qc.crz(phase, j, i)
    for i in range(n // 2):
        qc.swap(i, n - 1 - i)
    return qc
'''


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

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

def qft(n: int, is_little_endian: bool = False):
    qc = QuantumCircuit(n)
    targets = range(n) if not is_little_endian else reversed(range(n))
    for i in targets:
        qc.h(i)
        controls = range(i + 1, n) if not is_little_endian else reversed(range(i + 1, n, 1))
        for j in controls:
            phase = 2 * pi / (2 ** (j - i + 1))
            qc.crz(phase, j, i)
    for i in range(n // 2):
        qc.swap(i, n - 1 - i)
    return qc
'''


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

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

def qft(n: int, is_little_endian: bool = False):
    qc = QuantumCircuit(n)
    targets = range(n) if not is_little_endian else reversed(range(n))
    for i in targets:
        qc.h(i)
        controls = range(i + 1, n) if not is_little_endian else reversed(range(i))
        for j in controls:
            phase = 2 * pi / (2 ** (abs(j - i) + 1))
            qc.crz(phase, j, i)
    for i in range(n // 2):
        qc.swap(i, n - 1 - i)
    return qc
'''


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

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

def qft(n: int, is_little_endian: bool = False):
    qc = QuantumCircuit(n)
    targets = range(n) if not is_little_endian else reversed(range(n))
    for i in targets:
        qc.h(i)
        controls = range(i + 1, n) if not is_little_endian else reversed(range(i))
        for j in controls:
            phase = 2 * pi / (2 ** (abs(j - i) + 1))
            qc.cp(phase, j, i)
    for i in range(n // 2):
        qc.swap(i, n - 1 - i)
    return qc
'''


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

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

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

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

    return qc
'''


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

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

    for j in range(n-1, -1, -1):
        qc.h(j)
        for k in range(j-1, -1, -1):
            qc.cp(pi/2**(j-k), k, j)

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.p(2 * math.pi * S[i] / 2 ** m, i)
    return qc

# if __name__ == "__main__":
#     from qiskit.quantum_info import Statevector
#     import numpy as np
#     qc = solve(2, 2, [1, 3])
#     # sv = Statevector(qc)
#     # print(sv)
#     print(qc)
#     print(f"{qc.depth() = }")
#     sv = Statevector.from_label('11')
#     print(sv.evolve(qc))
'''


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

def f(x, S):
    pass

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for (i, s) in zip(range(n), S):
        qc.p(2*pi *s/ (2**m), i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from math import tau

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i, s in enumerate(S):
        qc.p(tau * s / (1 << m), i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        theta = 2 * math.pi * S[i]
        qc.p(theta, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        theta = 2 * math.pi * S[i] / (2 ** n)
        qc.p(theta, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        theta = 2 * math.pi * S[i] / (2 ** m)
        qc.p(theta, i)

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i in range(n):
        qc.cp(2.0*np.pi/(2.0**m)*S[i])

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i in range(n):
        qc.p(2.0*np.pi/(2.0**m)*S[i], i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if S[i] == 1:
            qc.p(2*math.pi/(1<<(m-i)) , i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(len(S)):
        if S[i] == 1:
            qc.p(2*math.pi/(1<<(m-i)) , i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(len(S)):
        qc.p(2*math.pi*S[i]/(1<<m) , i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    # Implement the phase oracle
    for x in range(2**n):
        # Convert x to its binary representation
        bin_x = [int(bit) for bit in bin(x)[2:].zfill(n)]

        # Compute f(x)
        f_x = sum(S[i] * bin_x[i] for i in range(n))

        # Calculate the phase angle
        angle = (2 * pi * f_x) / (2 ** m)

        # Apply the phase shift to the state |x>
        for i in range(n):
            if bin_x[i] == 1:
                qc.rz(angle, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    # Implement the phase oracle
    for x in range(2**n):
        # Convert x to its binary representation
        bin_x = [int(bit) for bit in bin(x)[2:].zfill(n)]

        # Compute f(x)
        f_x = sum(S[i] * bin_x[i] for i in range(n))

        # Calculate the phase angle
        angle = (2 * pi * f_x) / (2 ** m)

        # Apply the phase shift to the correct state
        for i in range(n):
            if bin_x[i] == 1:
                qc.x(i)  # Apply X gate to flip qubit if necessary

        qc.mcx(list(range(n)), 0)  # Apply multi-controlled Z gate
        qc.rz(angle, 0)  # Apply the phase shift
        qc.mcx(list(range(n)), 0)  # Reverse the multi-controlled Z gate

        for i in range(n):
            if bin_x[i] == 1:
                qc.x(i)  # Revert the X gate if it was applied earlier

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.p(2.0*np.pi/(2.0**m)*S[i], i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i in range(n):
        qc.p(2 * math.pi * s[i] / pow(2, m), i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i in range(n):
        qc.phase(2 * math.pi * s[i] / pow(2, m), i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i in range(n):
        qc.phas(2 * pi * s[i] / pow(2, m), i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

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

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i in range(n):
        qc.p(2 * math.pi / pow(2, m), i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i in range(n):
        qc.p(2 * math.pi * float(s[i]) / pow(2, m), i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i in range(n):
        qc.p(2 * math.pi * S[i] / pow(2, m), i)

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for idx in range(0, n):
        theta = 2 * math.pi * S[idx] / (2**m)
        qc.p(theta, idx)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    for i in range(n):
        qc.p(math.pi * S[i] / 2**m, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    for i in range(n):
        qc.p(2 * math.pi * S[i] / 2**m, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.p(2 * math.pi * S[i] / 2 ** m, i)
    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.p(2*math.pi*S[i]/(2**m))
    return qc

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


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.p(2*math.pi*S[i]/(2**m), i)
    return qc

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


'''python
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
      qc.p(2*np.pi*S[i]/2**m, i)

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
      qc.p(2*np.pi*S[i]/2**m, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library.standard_gates import U1Gate
from math import pi

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    for i in range(n):
        qc.append(U1Gate(2 * pi * S[i] / 2 ** m), [i])

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    # For each qubit i, apply a controlled phase rotation
    for i in range(n):
        angle = 2 * pi * S[i] / (2 ** m)
        qc.cp(angle, i, 0)  # Controlled phase rotation with qubit i controlling

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.p(2*math.pi*S[i]/2**m,i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i in range(n):
        qc.p(S[i]/(2**m), i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i in range(n):
        qc.p(2*math.pi*S[i]/(2**m), i)

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n, name='oracle')
    # Write your code here:
    control_qubits = list(range(n))

    # Iterate through each qubit and apply controlled phase rotations
    for i in range(n):
        phase = 2 * np.pi * S[i] / (2 ** m)
        qc.cp(phase, control_qubits[i], n)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    import math

    for i in range(n):
        qc.p(S[i] * 2 * math.pi / 2**m, i)

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.x(0)
    qc.x(1)
    # Write your code here:
    if n==1:
        qc.p(2*np.pi*S[0]/(2**m),0)
        return qc
    for i in range(n):
        qc.cp(2*np.pi*S[i]/(2**m),i,(i+1)%n)

    return qc

qc = solve(2,2,[1,3])
print(qc)
#print(Statevector(qc)) #こことimportを消す
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    # Write your code here:
    if n==1:
        qc.p(2*np.pi*S[0]/(2**m),0)
        return qc
    for i in range(n):
        qc.cp(2*np.pi*S[i]/(2**m),i,(i+1)%n)

    return qc

qc = solve(2,2,[1,3])
print(qc)
#print(Statevector(qc)) #こことimportを消す
'''


'''python
from math import tau  # tau = 2π
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    """
    Return an n-qubit oracle O such that
        |x> ──►  exp(2πi f(x)/2^m) |x>,
    with  f(x) = Σ S_k x_k  (little-endian).
    Circuit depth = 1 (all gates parallel).
    """
    qc = QuantumCircuit(n)

    denom = 1 << m          # 2^m
    for k in range(n):
        # Phase angle ϕ_k = 2π S_k / 2^m  (wrapped automatically by Qiskit)
        phi = tau * (S[k] % denom) / denom
        qc.p(phi, k)        # Phase (diag[1, e^{iϕ_k}]) on qubit k

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math
def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
  qc = QuantumCircuit(n)
  for i in range(n):
    qc.p(math.pi * S[i] / pow(2, m), i)
  return qc
'''


'''python
from qiskit import QuantumCircuit
import math
def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
  qc = QuantumCircuit(n)
  for i in range(n):
    qc.p(2 * math.pi * S[i] / pow(2, m), i)
  return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def create_oracle(n, m, S):
    # Create a quantum circuit with n qubits
    qc = QuantumCircuit(n)

    # Iterate over all possible values of x from 0 to 2^n - 1
    for x in range(2**n):
        # Calculate f(x)
        f_x = sum(S[i] * ((x >> i) & 1) for i in range(n))

        # Calculate the phase
        phase = (2 * math.pi * f_x) / (2**m)

        # Apply the phase shift to the corresponding state |x>
        if phase != 0:
            qc.p(phase, x)  # Apply phase shift using the p gate

    return qc
'''


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

def create_oracle(n, m, S):
    # Create a quantum register and a quantum circuit
    qr = QuantumRegister(n)
    qc = QuantumCircuit(qr)

    # Iterate over all possible values of x (0 to 2^n - 1)
    for x in range(2**n):
        # Calculate f(x)
        f_x = sum(S[i] * ((x >> i) & 1) for i in range(n))

        # Calculate the phase
        phase = (2 * math.pi * f_x) / (2**m)

        # Apply the phase shift to the state |x>
        qc.p(phase, qr[0])  # Apply phase to the first qubit (little-endian)
        for i in range(1, n):
            qc.p(phase, qr[i])  # Apply phase to the remaining qubits

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.p(2. * np.pi / (2 ** m) * S[i], i)
    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.p(2. * np.pi / (2 ** m) * S[i], i)
    return qc
'''


'''python
from math import pi

from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i, s in enumerate(S):
        angle = 2 * pi / 2 ** m
        qc.rz(angle / 2, i)
    return qc
'''


'''python
from math import pi

from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i, s in enumerate(S):
        if s == 1:
            qc.x(i)
        angle = 2 * pi / 2 ** m
        qc.rz(angle / 2, i)
    return qc
'''


'''python
from math import pi

from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i, s in enumerate(S):
        if s == 1:
            qc.x(i)
        angle = 2 * pi / 2 ** m
        qc.rz(angle, i)
    return qc
'''


'''python
from math import pi

from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i, s in enumerate(S):
        angle = 2 * pi * s / 2 ** m
        if s == 1:
            qc.x(i)
        qc.p(angle, i)
    return qc
'''


'''python
from math import pi

from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i, s in enumerate(S):
        angle = 2 * pi * s / 2 ** m
        qc.p(angle, i)
    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for l in range(2**n):
        theta = 0
        for i in range(n):
            # check if i-th bit of l is 0 or 1
            if not ((l >> i) & 1):
                qc.x(i)
            else:
                theta += (1<<i)*S[i]
        if n == 1:
            qc.p(math.pi * theta / 2**m,0)
        else:
            # apply multiple controlled Z gate
            qc.append(PhaseGate(theta=math.pi * theta/(2**m)).control(n - 1), range(n))
        for i in range(n):
            if not ((l >> i) & 1):
                qc.x(i)
    return qc
'''


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

def solve(n: int, m:int, S:list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.p(S[i] * 2 * math.pi / 2 ** m,i)

    return qc

# for n in range(5):
#     for l in range(2**(n+1)):
#         print(f"{n+1} {l+1}")
#         solve(n+1,l+1)
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.p(2*np.pi*S[i]/2**m, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        for j in range(m):
            if (S[i] >> j) & 1:
                qc.cp(2 * np.pi / (2 ** m), i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if S[i] != 0:
            phase_shift = 2 * np.pi * S[i] / (2 ** m)
            qc.p(phase_shift, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if(i==0):
            continue
        if S[i] != 0:
            phase_shift = 2 * np.pi * S[i] / (2 ** m)
            qc.p(phase_shift, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if(i==0):
            continue
        if S[i] != 0:
            phase_shift = 2 * math.pi * S[i] / (2 ** m)
            qc.p(phase_shift, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if(i==0):
            continue
        if S[i] != 0:
            phase_shift = 2 * 3.141592 * S[i] / (2 ** m)
            qc.p(phase_shift, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if(i==0):
            continue
        if S[i] != 0:
            phase_shift = 2 * math.pi * S[i] / (2 ** m)
            qc.p(phase_shift, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if S[i] != 0:
            phase_shift = 2 * 3.141592 * S[i] / (2 ** m)
            qc.p(phase_shift, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n, m, s) -> QuantumCircuit:
	from numpy import pi
	qc = QuantumCircuit(n)
	for i in range(n):
		qc.p(pi * s[i] / 2**(m - 1), i)
	return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    for i in range(n):
        angle = 2 * pi * S[i] / (2**m)
        qc.p(angle, i)

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.x([0, 1])
    # Write your code here:

    if n == 1:
        theta = 2 * pi * S[0] / (2 ** m)
        qc.p(theta, 0)
    else:
        for i in range(n):
            theta = 2 * pi * S[i] / (2 ** m)
            qc.p(theta, i)
            # mp = PhaseGate(theta).control(2)
            # qc.append(mp, range(n))

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # qc.x([0, 1])
    # Write your code here:

    if n == 1:
        theta = 2 * pi * S[0] / (2 ** m)
        qc.p(theta, 0)
    else:
        for i in range(n):
            theta = 2 * pi * S[i] / (2 ** m)
            qc.p(theta, i)
            # mp = PhaseGate(theta).control(2)
            # qc.append(mp, range(n))

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i in range(n):
        qc.p(2 * pi * S[i] / 2 ** m, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.p(2.0*np.pi/(2.0**m)*S[i], i)

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.p(2.0*np.pi/(2.0**m)*S[i], i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for k in range(n):
        theta = 2*math.pi()*S[k]//2**m
        qc.p(theta,k)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for k in range(n):
        theta = 2*math.pi*S[k]//2**m
        qc.p(theta,k)
    return qc
'''


'''python
from qiskit import QuantumCircuit

import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for k in range(n):
        theta = 2*math.pi*S[k]/2**m
        qc.p(theta,k)
    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        angle = 2 * np.pi * S[i]
        qc.p(angle, i)
    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        angle = 2 * np.pi * S[i] / 2**m
        qc.p(angle, i)
    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        angle = 2 * np.pi * S[i] / 2**m
        qc.p(angle, i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.x(1)
    # Write your code here:
    for i in range(n):
        qc.p(2*math.pi*S[i]/(1<<m), i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.p(2*math.pi*S[i]/(1<<m), i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    phase_factor = 2.0*np.pi/(2.0**m)
    for i,s in enumerate(s):
        qc.p(phase_factor*s, i)

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    phase_factor = 2.0*np.pi/(2.0**m)
    for i,s in enumerate(s):
        qc.p(phase_factor*s, i)

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    phase_factor = 2.0*np.pi/(2.0**m)
    for i in range(n):
        qc.p(phase_factor*S[i], i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        # Calculate the phase angle for the current qubit
        phase_angle = 2 * np.pi * S[i] / (2 ** m)

        # Apply the phase gate to the i-th qubit
        qc.p(phase_angle, i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
!pip install numpy
import numpy as np

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        # Calculate the phase angle for the current qubit
        phase_angle = 2 * np.pi * S[i] / (2 ** m)

        # Apply the phase gate to the i-th qubit
        qc.p(phase_angle, i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

import numpy as np

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        # Calculate the phase angle for the current qubit
        phase_angle = 2 * np.pi * S[i] / (2 ** m)

        # Apply the phase gate to the i-th qubit
        qc.p(phase_angle, i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for x in range(2**n):
        fx = sum(S[i] * ((x >> i) & 1) for i in range(n))
        phase = 2 * np.pi * fx / (2**m)
        qc.append(PhaseGate(phase), range(n))
    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    for i in range(n):
        theta=math.pi
        #theta*=2**i
        theta/=2**m
        theta*=S[i]

        qc.p(theta,i)
    # Write your code here:

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.p(2*math.pi*S[i]/2**m, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here
    for i in range (n):
        theta=2*math.pi*S[i]/2**m
        qc.p(theta,i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here
    for i in range (n):
        theta=2*math.pi*S[i]/2**m
        qc.p(theta,i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i in range(n):
        angle = 2 * pi * S[i] / (2 ** m)
        qc.rz(angle, i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i in range(n):
        angle = 2 * math.pi * S[i] / (2 ** m)
        qc.rz(angle, i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        angle = (2 * math.pi * S[i]) / (2 ** m)
        qc.p(angle,i)
    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        lam = 2 * np.pi * S[i] / 2 ** m
        qc.u(0, 0, lam, i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.p(4*math.pi*S[i]/2**m,i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.p(4*math.pi*S[i]/2**(m/n),i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.p(2*math.pi*S[i]/2**m,i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        theta = 2*math.pi*S[i]/2**m
        qc.p(theta,i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        theta = 2*math.pi*S[i]/2**m
        qc.p(theta,i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.q(2*math.pi*S[i] / 2**m)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.p(2*math.pi*S[i] / 2**m)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.p(2*math.pi*S[i] / 2**m, i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

import numpy as np

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    # Apply Hadamard gates to the first n qubits (to create superposition)
    for qubit in range(n):
        qc.h(qubit)

    # Apply controlled-phase gates based on the values of S
    for i in range(n):
        for j in range(i, n):
            if S[i] != 0:
                angle = 2 * np.pi * S[i] / (2 ** m)
                qc.cp(angle, j, n)  # Apply controlled-phase gate
    return qc
'''


'''python
from qiskit import QuantumCircuit

import numpy as np

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n + m, m)
    # Write your code here:
    # Apply Hadamard gates to the first n qubits (to create superposition)
    for qubit in range(n):
        qc.h(qubit)

    # Apply controlled-phase gates based on the values of S
    for i in range(n):
        for j in range(i, n):
            if S[i] != 0:
                angle = 2 * np.pi * S[i] / (2 ** m)
                qc.cp(angle, j, n)  # Apply controlled-phase gate
    return qc
'''


'''python
from qiskit import QuantumCircuit

import numpy as np

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n + m, m)
    # Write your code here:
    for qubit in range(n):
        qc.h(qubit)

    # Apply the phase shift using controlled rotations
    for i in range(n):
        if S[i] != 0:
            angle = 2 * np.pi * S[i] / (2 ** m)
            # Apply multi-controlled phase shift
            for control in range(n):
                if control != i:
                    qc.cp(angle, control, n)

    # Apply inverse Hadamard gates to the first n qubits to complete the QFT
    for qubit in range(n):
        qc.h(qubit)
    return qc
'''


'''python
from qiskit import QuantumCircuit

import numpy as np

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n + m, m)
    # Write your code here:
    for qubit in range(n):
        qc.h(qubit)

    # Apply the phase shift for the given S values
    for i in range(n):
        if S[i] != 0:
            # Calculate the angle for the phase shift
            angle = 2 * np.pi * S[i] / (2 ** m)

            # Apply the controlled-phase gate
            for qubit in range(n):
                if (i & (1 << qubit)) != 0:
                    qc.cp(angle, qubit, n)  # Apply controlled-phase gate

    # Apply inverse QFT to undo the initial Hadamard gates
    for qubit in range(n):
        qc.h(qubit)
        for k in range(qubit):
            qc.cp(-np.pi / 2**(qubit - k), k, qubit)

    return qc
'''


'''python
from qiskit import QuantumCircuit

import numpy as np

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n + m, m)
    # Write your code here:
    # Apply Hadamard gates to the first n qubits (to create superposition)
    for qubit in range(n):
        qc.h(qubit)

    # Apply controlled rotations to encode f(x) into the phase of the target qubits
    for i in range(n):
        if S[i] != 0:
            # Calculate the phase angle for the S[i] value
            angle = 2 * np.pi * S[i] / (2 ** m)
            # Apply the controlled-phase gate to the target qubits
            for j in range(m):
                qc.crz(angle / (2 ** j), i, n + j)

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    bits = []
    for item in S:
        qc.p(2*pi/2**(m)*item)

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    bits = []
    for i, item in enumerate(S):
        qc.p(2*pi/2**(m)*item, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.p(2 * math.pi / (2 ** m) * S[i])
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.p(2 * math.pi / (2 ** m) * S[i], i)
    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)  # n qubits for the function and 1 auxiliary qubit

    # Apply a Hadamard gate to the last qubit
    qc.h(n)

    # Apply controlled phase gates based on the function f(x)
    for x in range(2 ** n):
        f_x = sum(S[k] * ((x >> k) & 1) for k in range(n))
        phase = np.exp(2j * np.pi * f_x / (2 ** m))

        # Apply the phase gate to the auxiliary qubit
        if phase != 1:
            qc.u1(np.angle(phase), n)  # U1 gate is used to apply the phase

    # Apply a Hadamard gate to the last qubit to complete the oracle
    qc.h(n)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit.circuit.library import PhaseGate
import numpy as np

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    # Quantum and classical registers
    q = QuantumRegister(n + 1, name='q')  # n qubits for function and 1 qubit for phase
    c = ClassicalRegister(n, name='c')
    qc = QuantumCircuit(q, c)

    # Apply Hadamard gates to all qubits (except the last one for phase)
    qc.h(q[:n])

    # Apply controlled phase gates based on the function f(x)
    for x in range(2 ** n):
        f_x = sum(S[k] * ((x >> k) & 1) for k in range(n))
        phase = np.exp(2j * np.pi * f_x / (2 ** m))

        # Apply controlled phase gate to the last qubit
        if phase != 1:
            qc.p(np.angle(phase), q[n])  # p gate applies phase to the last qubit

    # Optional: Apply additional operations if needed, such as using ancilla qubits

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.p(np.pi*S[i] / (2 ** m), i)

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    for i in range(n):
        qc.x(i)
        qc.p(np.pi * S[i] / (2 ** m), i)
        qc.x(i)
    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.x(i)
        qc.p(np.pi * S[i] / (2 ** m), i)
        qc.x(i)
    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.x(i)
        qc.p(2*np.pi * S[i] / (2 ** m), i)
        qc.x(i)
    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.x(i)
        qc.p(2*np.pi * S[-i-1] / (2 ** m), i)
        qc.x(i)
    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.x(i)
        qc.p(4*np.pi * S[i] / (2 ** m), i)
        qc.x(i)
    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.p(2*np.pi * S[i] / (2 ** m), i)
    return qc
'''


'''python
import math

from qiskit import QuantumCircuit, QuantumRegister

def qfts(qc, i, n) -> QuantumCircuit:
    for q in range(i):
            qc.cp(-math.pi / 2 ** (i - q), q, i)
    qc.h(i)

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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n+m)

    for j in range(m):
        qc.h(j)

    for j in range(m):
        for i in range(n):
            theta = (2 * math.pi * S[i] / 2**m) * 2**j
            qc.cp(theta, i+m,j)

    iqft(qc,m)

    return qc
solve(5,2,(1,0,0,0,1))
'''


'''python
import math

from qiskit import QuantumCircuit, QuantumRegister

def qfts(qc, i, n) -> QuantumCircuit:
    for q in range(i):
            qc.cp(-math.pi / 2 ** (i - q), q, i)
    qc.h(i)

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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n+m)

    for j in range(m):
        qc.h(j)

    for j in range(m):
        for i in range(n):
            theta = (2 * math.pi * S[i] / 2**m) * 2**j
            qc.cp(theta, i+m,j)

    iqft(qc,m)

    return qc
'''


'''python
import math

from qiskit import QuantumCircuit, QuantumRegister

def qfts(qc, i, n) -> QuantumCircuit:
    for q in range(i):
            qc.cp(-math.pi / 2 ** (i - q), q, i)
    qc.h(i)

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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n+m)

    for j in range(m):
        qc.h(j)

    for j in range(m):
        for i in range(n):
            theta = (2 * math.pi * S[i] / 2**m) * 2**j
            qc.cp(theta, j,i+m)

    iqft(qc,m)

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(m):
        qc.h(i)

        for j in range(n):
            qc.cp(2*np.pi*S[j]/2**m, (i,j))

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

    for i in range(m - 1, -1, -1):
        for j in range(i - 1, -1, -1):
            qc.cp(-np.pi / 2**(i - j), n+i, n+j)
        qc.h(n+i)

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(m):
        qc.h(i+n)

        for j in range(n):
            qc.cp(2*np.pi*S[j]/2**m, i+n,j)

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

    for i in range(m - 1, -1, -1):
        for j in range(i - 1, -1, -1):
            qc.cp(-np.pi / 2**(i - j), n+i, n+j)
        qc.h(n+i)

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(m):
        qc.h(i+n)

        for j in range(n):
            qc.cp(2*np.pi*S[j]/2**m, i+n,j)

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

    for i in range(m - 1, -1, -1):
        for j in range(i - 1, -1, -1):
            qc.cp(-np.pi / 2**(i - j), n+i, n+j)
        qc.h(n+i)

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(m):
        qc.h(i+n)

        for j in range(n):
            qc.cp(2*np.pi*S[j]/2**m, i+n,j)

    def qft(n: int) -> QuantumCircuit:
        qc = QuantumCircuit(n)
        # Write your code here:
        for i in range(n // 2):
            qc.swap(i, n - i - 1)

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

    qc.append(qft(m), range(n, n+m))

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(m):
        qc.h(i+n)

        for j in range(n):
            qc.cp(2*np.pi*S[j]/2**m, i+n,j)

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

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

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(m):
        qc.h(i+n)

        for j in range(n):
            qc.cp(2*np.pi*S[j]/2**m, i+n,j)

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

    for i in range(m):
        for j in range(i):
            qc.cp(-np.pi / 2**(i - j), j+n, i+n)
        qc.h(i+n)

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(m):
        qc.h(i+n)

        for j in range(n):
            qc.cp(2*np.pi*S[j]/2**(n-i), i+n,j)

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

    for i in range(m):
        for j in range(i):
            qc.cp(-np.pi / 2**(i - j), j+n, i+n)
        qc.h(i+n)

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(m):
        qc.h(i+n)

        for j in range(n):
            qc.cp(2*np.pi*S[j]/2**(m-i), i+n,j)

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

    for i in range(m):
        for j in range(i):
            qc.cp(-np.pi / 2**(i - j), j+n, i+n)
        qc.h(i+n)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)

    # 各項 S[i] * x[i] を計算してyに加算
    for i in range(n):
        # S[i]の各ビットについて処理
        for j in range(m):
            bit_value = (S[i] >> j) & 1  # S[i]のj番目のビット
            if bit_value == 1:
                # x[i]が1のときのみy[j]をフリップ
                qc.cx(x[i], y[j])

    return qc
'''


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

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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    qc.h(range(n, n+m))
    for idx in range(0, n):
        theta = 2 * math.pi * S[idx] / (2**m)
        for jdx in range(n, n+m):
            qc.cp(theta, idx, jdx)

    qc = qc.compose(qft(m).inverse(), range(n, n+m))

    return qc
'''


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

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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    qc.h(range(n, n+m))
    for idx in range(0, n):
        for jdx in range(n, n+m):
            theta = 2 * math.pi * S[idx] * (jdx-n+1) / (2**m)
            qc.cp(theta, idx, jdx)

    qc = qc.compose(qft(m).inverse(), range(n, n+m))
    return qc
'''


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

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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    qc.h(y)
    for idx in range(0, n):
        for jdx in range(0, m):
            theta = 2 * math.pi * S[idx] * (jdx+1) / (2**m)
            qc.cp(theta, x[idx], y[jdx])

    qc = qc.compose(qft(m).inverse(), y)
    return qc
'''


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

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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    qc.h(y)
    for idx in range(0, n):
        for jdx in range(0, m):
            theta = 2 * math.pi * S[idx] * (2**jdx) / (2**m)
            qc.cp(theta, x[idx], y[jdx])

    qc = qc.compose(qft(m).inverse(), y)
    return qc
'''


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

# from qiskit.quantum_info import Statevector

def quantum_fourier_transform(n: int) -> QuantumCircuit:
    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)

    i = 0
    while i<n-i-1:
        qc.swap(i, n-i-1)
        i += 1

    return qc

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)

    # init = [0]*(2**(n+m))
    # init[3] = 1
    # qc.initialize(init)

    # Write your code here:

    qc.compose(quantum_fourier_transform(m), y, inplace=True)
    for j in range(m):
        for i in range(n):
            qc.cp(2*math.pi*S[i]/(2**m)*(2**j), x[i], y[j])
    qc.compose(quantum_fourier_transform(m).inverse(), y, inplace=True)

    return qc

# if __name__ == "__main__":
#     qc = solve(2, 2, [1, 2])
#     print(Statevector(qc))
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    def qft(n):
      qc = QuantumCircuit(n)
      for i in range(n):
        qc.h(-1-i)
        for j in range(i+1,n):
            qc.cp(2 * np.pi / 2**(j+1-i), -1-j, -1-i)
      for i in range(n//2):
        qc.swap(i, n-i-1)

      return qc

    for i in range(n):
      qc.p(2*np.pi*S[i]/2**m, x[i])
    for j in range(m):
      qc.h(y[j])

    inv_qft = qft(m).inverse()
    qc.append(inv_qft, y)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    # Step through each qubit in x and apply the corresponding controlled addition
    for i in range(n):
        for j in range(m):
            # Calculate the bit of S[i] that needs to be added to y[j]
            if (S[i] >> j) & 1:
                qc.cx(x[i], y[j])  # Apply a controlled-X operation based on x[i]

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    for i in range(n):
        for j in range(m):
            if ((S[i] >> j) & 1) == 1:
                qc.cx(i, n + j)
                if j < m - 1:
                    qc.x(n + j)
                    qc.cx(n + j, n + j + 1)
                    qc.x(n + j)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    for i in range(n):
        for j in range(m - 1, -1, -1):
            if ((S[i] >> j) & 1) == 1:
                qc.cx(i, n + j)
                if j < m - 1:
                    qc.x(n + j)
                    qc.cx(n + j, n + j + 1)
                    qc.x(n + j)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    for i in range(n):
        for j in range(m):
            if ((S[i] >> j) & 1) == 1:
                qc.cx(i, n + j)
                for k in range(j, m - 1):
                    qc.x(n + k)
                    qc.cx(n + k, n + k + 1)
                    qc.x(n + k)

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    for i in range(n):
        for j in range(m):
            if ((S[i] >> j) & 1) == 1:
                qc.cx(i, n + j)
                for k in range(j + 1, m):
                    for s in range(j, k):
                        qc.x(n + s)
                    mcx = MCXGate(k - j)
                    qc.append(mcx, range(n + j, n + k + 1))
                    for s in range(j, k):
                        qc.x(n + s)

    return qc
'''


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

def qft(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        j = n - 1 - i
        if i >= j:
            break
        qc.swap(i, j)

    for i in range(n):
        qc.h(i)
        for j in range(i + 1, n):
            theta = 2 * pi / (2 ** (j + 1 - i))
            qc.cp(theta, j, i)

    return qc

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for j in range(m):
        qc.h(n + j)
        for i in range(n):
            theta = 2 * pi * S[i] * 2 ** (j - m)
            qc.cp(theta, j + n, i)

    qc.compose(qft(m).inverse(), y, inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(m):
        qc.h(i)
    for i in range(n):
        for j in range(m):
            qc.cp(2. * np.pi * S[i] * (2 ** i) * (2 **j) / (2 ** m), x[i], y[j])
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(m):
        qc.h(i)
    for i in range(n):
        for j in range(m):
            qc.cp(2. * np.pi * S[i] * (2 ** i) * (2 **j)/ (2 ** m), x[i], y[j])
    def qft(n):
        for itarg in range(n - 1, -1, -1):
            qc.h(itarg)
            for ictrl in range(itarg - 1, -1, -1):
                power = ictrl - itarg - 1 + n
                qc.cp((2 ** power) * 2. * np.pi / (2 ** n), ictrl, itarg)

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

    qc.compose(qft(m).inverse(), y, inplace=True)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(n):
        for j in range(m):
            if (S[i] >> j) & 1:
                qc.cx(x[i], y[j])
    return qc
'''


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

def r(qc: QuantumCircuit, control:int, target:int, l:int):
    #Apply Cphase operation
    theta = 2 * math.pi / (1 << l)
    qc.cp(theta, control, target)

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

    #Now swap qubits to reverse their order
    for i in range(n//2):
        qc.swap(i,n-i-1)

    #Apply the qft
    for i in range(n):
        qc.h(i)
        for j in range(i+1,n):
            r(qc, j, i, j-i+1)

    return qc

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    #Apply H-gate to all qubits in y
    qc.h(y)

    #Apply cphase gates
    for k in range(m):
        for i in range(n):
            theta = 2 * math.pi * S[i]/ (1<<m)*(1<<k)
            qc.cp(theta, y[k], x[i])

    #Apply IQFT to y-reg
    qc.append(qft(m).inverse(), y)

    return qc
'''


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

def r(qc: QuantumCircuit, control:int, target:int, l:int):
    #Apply Cphase operation
    theta = 2 * math.pi / (1 << l)
    qc.cp(theta, control, target)

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

    #Now swap qubits to reverse their order
    for i in range(n//2):
        qc.cx(i,n-1)
        qc.cx(n-i-1,i)
        qc.cx(i,n-i-1)

    #Apply the qft
    for i in range(0,n):
        qc.h(i)
        for j in range(1,n-1):
            r(qc, i+j, i, j+1)

    return qc

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    #Apply H-gate to all qubits in y
    qc.h(y)

    #Apply cphase gates
    for k in range(m):
        for i in range(n):
            theta = 2 * math.pi * S[i]/ (1<<m)*(1<<k)
            qc.cp(theta, y[k], x[i])

    #Apply IQFT to y-reg
    qc.compose(qft(m).inverse(), y)

    return qc
'''


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

def r(qc: QuantumCircuit, control:int, target:int, l:int):
    #Apply Cphase operation
    theta = 2 * math.pi / (1 << l)
    qc.cp(theta, control, target)

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

    #Now swap qubits to reverse their order
    for i in range(n//2):
        qc.cx(i,n-1)
        qc.cx(n-i-1,i)
        qc.cx(i,n-i-1)

    #Apply the qft
    for i in range(0,n):
        qc.h(i)
        for j in range(1,n-1):
            r(qc, i+j, i, j+1)

    return qc

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    #Apply H-gate to all qubits in y
    qc.h(y)

    #Apply cphase gates
    for k in range(m):
        for i in range(n):
            theta = 2 * math.pi * S[i]/ (1<<m)*(1<<k)
            qc.cp(theta, y[k], x[i])

    #Apply IQFT to y-reg
    qc = qc.compose(qft(m).inverse(), y)

    return qc
'''


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

def r(qc: QuantumCircuit, control:int, target:int, l:int):
    #Apply Cphase operation
    theta = 2 * math.pi / (1 << l)
    qc.cp(theta, control, target)

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

    #Now swap qubits to reverse their order
    for i in range(n//2):
        qc.cx(i,n-i-1)
        qc.cx(n-i-1,i)
        qc.cx(i,n-i-1)

    #Apply the qft
    for i in range(0,n):
        qc.h(i)
        for j in range(1,n-1):
            r(qc, i+j, i, j+1)

    return qc

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    #Apply H-gate to all qubits in y
    qc.h(y)

    #Apply cphase gates
    for k in range(m):
        for i in range(n):
            theta = 2 * math.pi * S[i]/ (1<<m)*(1<<k)
            qc.cp(theta, y[k], x[i])

    #Apply IQFT to y-reg
    qc = qc.compose(qft(m).inverse(), y)

    return qc
'''


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

def r(qc: QuantumCircuit, control:int, target:int, l:int):
    #Apply Cphase operation
    theta = 2 * math.pi / (1 << l)
    qc.cp(theta, control, target)

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

    #Now swap qubits to reverse their order
    for i in range(n//2):
        qc.cx(i,n-i-1)
        qc.cx(n-i-1,i)
        qc.cx(i,n-i-1)

    #Apply the qft
    for i in range(0,n):
        qc.h(i)
        for j in range(1,n-i):
            r(qc, i+j, i, j+1)

    return qc

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    #Apply H-gate to all qubits in y
    qc.h(y)

    #Apply cphase gates
    for k in range(m):
        for i in range(n):
            theta = 2 * math.pi * S[i]/ (1<<m)*(1<<k)
            qc.cp(theta, y[k], x[i])

    #Apply IQFT to y-reg
    qc = qc.compose(qft(m).inverse(), y)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(min(n, m)):
        angle = 2 * pi * S[i] / 2 ** m
        qc.cp(angle, i, i + n)
    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for i in range(min(n, m)):
        angle = 2 * pi * S[i] / 2 ** m
        qc.cp(angle, i, i + n)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    for i in range(n):
        bin_data = format(S[i], f'0{n}b')
        data_1_bits = [idx for idx, digit in enumerate(reversed(bin_data)) if digit == '1' && idx < m]

        if len(data_0_bits) > 0:
            qc.cx(x[i], y[data_0_bits])

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    for i in range(n):
        bin_data = format(S[i], f'0{n}b')
        data_1_bits = [idx for idx, digit in enumerate(reversed(bin_data)) if digit == '0' and idx < m]

        if len(data_0_bits) > 0:
            qc.cx(x[i], y[data_0_bits])

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    for i in range(n):
        bin_data = format(S[i], f'0{n}b')
        data_1_bits = [idx for idx, digit in enumerate(reversed(bin_data)) if digit == '0' and idx < m]

        if len(data_1_bits) > 0:
            qc.cx(x[i], y[data_1_bits])

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    def qft(n: int) -> QuantumCircuit:
        qc = QuantumCircuit(n)
        # Write your code here:
        for i in range(n-1,-1,-1):
            qc.h(i)
            for j in range(i):
                qc.cp(math.pi/(2**(j+1)), i-j-1, i)

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

        return qc

    qft_circ = qft(m)
    qc.append(qft_circ, y)

    for i in range(n):
        for j in range(m):
            phase = S[i] * 2**j / 2**m * math.pi
            qc.cp(phase, x[i], y[j])

    qc.append(qft_circ.inverse(), y)

    return qc
'''


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

def qft(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1,-1,-1):
        qc.h(i)
        for j in range(i):
            qc.cp(math.pi/(2**(j+1)), i-j-1, i)

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

    return qc

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    qft_circ = qft(m)
    qc.append(qft_circ, y)

    for i in range(n):
        for j in range(m):
            phase = S[i] * 2**j / 2**m * math.pi
            qc.cp(phase, x[i], y[j])

    qc.append(qft_circ.inverse(), y)

    return qc
'''


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

def qft(n: int) -> QuantumCircuit:
        qc = QuantumCircuit(n)
        # Write your code here:
        for i in range(n-1,-1,-1):
            qc.h(i)
            for j in range(i):
                qc.cp(math.pi/(2**(j+1)), i-j-1, i)

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

        return qc

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    qft_circ = qft(m)
    qc.append(qft_circ, y)

    for i in range(n):
        for j in range(m):
            phase = S[i] * 2**j / 2**m * math.pi
            qc.cp(phase, x[i], y[j])

    qc.append(qft_circ.inverse(), y)

    return qc
'''


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

def qft(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1,-1,-1):
        qc.h(i)
        for j in range(i):
            qc.cp(math.pi/(2**(j+1)), i-j-1, i)

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

    return qc

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    qc.append(qft_circ(m), y)

    for i in range(n):
        for j in range(m):
            phase = S[i] * 2**j / 2**m * math.pi
            qc.cp(phase, x[i], y[j])

    qc.append(qft_circ(m).inverse(), y)

    return qc
'''


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

def qft(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1,-1,-1):
        qc.h(i)
        for j in range(i):
            qc.cp(math.pi/(2**(j+1)), i-j-1, i)

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

    return qc

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    qc.append(qft(m), y)

    for i in range(n):
        for j in range(m):
            phase = S[i] * 2**j / 2**m * math.pi
            qc.cp(phase, x[i], y[j])

    qc.append(qft(m).inverse(), y)

    return qc
'''


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

def qft(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1,-1,-1):
        qc.h(i)
        for j in range(i):
            qc.cp(math.pi/(2**(j+1)), i-j-1, i)

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

    return qc

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    qc.compose(qft(m), y)

    for i in range(n):
        for j in range(m):
            phase = S[i] * 2**j / 2**m * math.pi
            qc.cp(phase, x[i], y[j])

    qc.compose(qft(m).inverse(), y)

    return qc
'''


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

def qft(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n-1,-1,-1):
        qc.h(i)
        for j in range(i):
            qc.cp(math.pi/(2**(j+1)), i-j-1, i)

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

    return qc

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    qc.h(y)

    for i in range(n):
        for j in range(m):
            phase = S[i] * 2**j / 2**m * math.pi
            qc.cp(phase, x[i], y[j])

    qc.compos(qft(m).inverse(), y)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x = QuantumRegister(n)  # Register for x
    y = QuantumRegister(m)  # Register for f(x) mod 2^m
    qc = QuantumCircuit(x, y)

    # We will use a series of controlled additions to compute f(x)
    for i in range(n):
        if S[i] > 0:  # Only consider non-zero S[i]
            # Convert S[i] to binary and apply controlled additions
            binary_S = format(S[i], '0' + str(m) + 'b')  # Get binary representation of S[i]
            for j in range(m):
                if binary_S[m - 1 - j] == '1':  # Check if the j-th bit is set
                    qc.cx(x[i], y[j])  # Apply CNOT from x[i] to y[j]

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i in range(n):
        for j in range(m):
            x = (1 << j)
            if (S[i] & x):
                for k in range(1 << m):
                    if(k % (2 * x) < x):
                        qc.swap(k, k + x)

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    for i in range(n):
        for j in range(m):
            x = (1 << j)
            if (S[i] & x):
                for k in range(j, m - 1):
                    if k == 0:
                        qc.swap(0, 1)
                    else:
                        qc.append(SwapGate(0, k + 1).control(k - 1), range(1, k))

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:

    for i in range(n):
        for j in range(m):
            x = (1 << j)
            if (S[i] & x):
                for k in range(j, m - 1):
                    if k == 0:
                        qc.swap(0, 1)
                    else:
                        qc.append(SwapGate(0, k + 1).control(k - 1), range(1, k))

    return qc
'''


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

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

    for i in range(n):
        for j in range(m):
            x = (1 << j)
            if (S[i] & x):
                for k in range(j, m - 1):
                    if k == 0:
                        qc.swap(0, 1)
                    else:
                        qc.append(SwapGate(0, k + 1).control(k - 1), range(1, k + 1))

    return qc
'''


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

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

    for i in range(n):
        for j in range(m):
            x = (1 << j)
            if (S[i] & x):
                for k in range(j, m - 1):
                    if k == 0:
                        qc.swap(0, 1)
                    else:
                        qc.append(SwapGate(0, k + 1).control(k), range(1, k + 1))

    return qc
'''


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

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

    for i in range(n):
        for j in range(m):
            x = (1 << j)
            if (S[i] & x):
                for k in range(min(j, m - 1):
                    if k == 0:
                        qc.swap(0, 1)
                    else:
                        qc.append(SwapGate(0, k + 1).control(k), range(1, k + 1))

    return qc
'''


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

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

    for i in range(n):
        for j in range(m):
            x = (1 << j)
            if (S[i] & x):
                for k in range(min(j, m - 1)):
                    if k == 0:
                        qc.swap(0, 1)
                    else:
                        qc.append(SwapGate(0, k + 1).control(k), range(1, k + 1))

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for j in range(m):
        qc.h(n+j)
    for i in range(n):
        for j in range(m):
            qc.cp(2*math.pi*S[i]/2**(m-j),i,n+j)
    for j in range(m-1,-1,-1):
        qc.h(n+j)
        for l in range(j-1,-1,-1):
            qc.cp(-2*math.pi/2**(j-l+1),n+l,n+j)
    for k in range(m//2):
        qc.swap(n+k,n+m-1-k)
    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    for j in range(m):
        qc.h(n+j)
    for i in range(n):
        for j in range(m):
            qc.cp(2*math.pi*S[i]/2**(m-j),i,n+j)
    for j in range(m-1,-1,-1):
        qc.h(n+j)
        for l in range(j-1,-1,-1):
            qc.cp(-2*math.pi/2**(j-l+1),n+l,n+j)
    for k in range(m//2):
        qc.swap(n+k,n+m-1-k)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    # Apply the oracle function f(x) = S0*x0 + S1*x1 + ... + Sn-1*xn-1
    for i in range(n):
        if S[i] != 0:
            qc.cx(x[i], y[0])
            for j in range(1, m):
                if S[i] & (1 << (j - 1)):
                    qc.cx(x[i], y[j])

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
     x = QuantumRegister(n, 'x')  # For the input bits
    y = QuantumRegister(m, 'y')  # For the output bits
    qc = QuantumCircuit(x, y)

    # We will use controlled addition to add S_i * x_i into the y register

    # Auxiliary qubit for carrying the addition results
    carry = QuantumRegister(1, 'carry')
    qc.add_register(carry)

    # Initialize carry qubit to |0⟩
    qc.reset(carry[0])

    # Controlled addition circuits for each S_i
    for i in range(n):
        # We will be adding S_i * x_i to the y register
        # Using controlled addition gates
        s = S[i]
        if s > 0:
            # Use the controlled adder technique to add s*x_i to the y register
            # Decompose s into its binary representation and add it conditionally
            for bit in range(m):
                if s & (1 << bit):
                    qc.cx(x[i], y[bit])

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y) for i in range(n):
    for j in range(m):
        if (S[i] >> j) & 1: qc.cx(x[i], y[j])
    return qc
'''


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

def r(qc: QuantumCircuit, control: int, target: int, l: int):
    qc.cp(2*math.pi/(1<<l), control, target)

def qft(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n//2):
        qc.cx(i, n-i-1)
        qc.cx(n-i-1, i)
        qc.cx(i, n-i-1)
    for i in range(0, n):
        qc.h(i)
        for j in range(1, n-i):
            r(qc, i+j, i, j+1)
    return qc

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # Write your code here:
    qc.h(y)
    for k in range(m):
        for i in range(n):
            qc.cp(2*math.pi*S[i]/(1<<m)*(1<<k), y[k], x[i])
    qc = qc.compose(qft(m).inverse(), y)

    return qc
'''


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

def solve(n: int, m: int, S: list[int]) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(m)
    qc = QuantumCircuit(x, y)
    # i を control として y に S[i] を加算しないといけない
    for j in reversed(range(m)):
        for i in range(n):
            if S[i] & (1 << j):
                # y に (1 << j) を加算
                for k in range(n - 1, j, -1):
                    qc.append(XGate().control(k - j + 1), [i] + list(range(n + j, n + k + 1)))
                qc.cx(i, n + j)

    return qc
'''