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

def qft_butterfly(qc: QuantumCircuit, qubits, inverse=False):
    """
    バタフライ構造で QFT (inverse=False) または iQFT (inverse=True) を実装する。
    リトルエンディアン想定で、qubits[0] が最下位ビット。

    通常の O(n^2) QFT 実装より浅い深さで済むが、
    ゲート数は変わらず O(n^2)。n<=10 程度なら十分。

    ※ 位相ゲート cp() をバタフライの各ステージで並列に配置する。
    """
    n = len(qubits)
    if n <= 1:
        return  # 1量子ビット以下なら何もしない

    # 1) bitの並びを反転 (最下位<->最上位, ..., 交換)
    #    順QFTなら最後にこれをするが、バタフライ実装では最初にやったほうが簡便
    for i in range(n//2):
        qc.swap(qubits[i], qubits[n-1-i])

    # もし inverse=True (逆QFT) なら
    #   QFT(|x>) = 1/sqrt(2^n) Σy e^{2πi xy/2^n}|y>
    # を逆にする際、cpゲートの符号が逆になるだけ。
    # 以下、"angle = ±π/2^(...) or ±2π/2^(...)" で符号を切り替える。
    sign = -1.0 if not inverse else +1.0

    # 2) バタフライ演算: log(n) 段のループ
    #    ステージ s=1..log(n)
    #    ブロックサイズ = 2^s
    #    各ブロック内で "半分の長さ" にわたって制御位相 (cp) をかけていく
    stage = 1
    while (1 << stage) <= n:
        block_size = 1 << stage  # 2^stage
        half_size  = block_size >> 1

        # ブロックの先頭位置を step
        for start in range(0, n, block_size):
            # start + i と start + i + half_size がペアになる
            for i in range(half_size):
                # A = qubits[start + i]
                # B = qubits[start + i + half_size]
                # ここで A, B をカップルにして制御位相をかける
                angle = sign * math.pi / (block_size >> 1) * i
                if abs(angle) > 1e-15:
                    qc.cp(angle, qubits[start + i + half_size], qubits[start + i])

        stage += 1

    # 3) 各ビットにHをかける (parallel)
    for i in range(n):
        qc.h(qubits[i])

    # inverse=True (iQFT) の場合、ここまでで完了。
    # inverse=False (順QFT) の場合、さらにビット反転が必要だが
    #   この実装では最初に swap を済ませているので不要。
    #   ただしダミー的にもう一度 swap して元に戻したいなら最後にやってもOK。
    if not inverse:
        for i in range(n//2):
            qc.swap(qubits[i], qubits[n-1-i])

def solve(n: int, a: int) -> QuantumCircuit:
    """
    リトルエンディアン:
      k[0] が最下位ビット
    補助ビット c[0] を加え: c=0 のとき位相なし, c=1 のとき位相 2π a k / 2^n

    回路深さを抑えるため:
      1) "バタフライ版QFT" で k を変換 (深さ ≈ n log n)
      2) 単制御位相を各ビットに一括してかける (深さ 1～2)
      3) バタフライ版 iQFT (深さ ≈ n log n)
    """
    k = QuantumRegister(n, 'k')  # n量子ビット
    c = QuantumRegister(1, 'c')  # 1量子ビット
    qc = QuantumCircuit(k, c)

    # 1) QFT(k)
    qft_butterfly(qc, k, inverse=False)

    # 2) c=1 のときにだけ k[j] へ位相 cp(...) を掛ける
    #    ただし QFT空間では k[j] が "周波数ビット" になっているので，
    #    角度は 2π a * 2^j / 2^n
    #    ここはすべて並列実行可能 => 深さ1～2程度
    for j in range(n):
        angle_j = 2 * math.pi * a * (2**j) / (2**n)
        qc.cp(angle_j, c[0], k[j])

    # 3) iQFT(k)
    qft_butterfly(qc, k, inverse=True)

    return qc
'''


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

def qft_butterfly(qc, qubits, inverse=False):
    """
    バタフライ構造のQFT/iQFT (リトルエンディアン) を簡潔に実装。
      - qubits[0] が最下位ビット
      - inverse=False で 順QFT
      - inverse=True  で 逆QFT (iQFT)

    通常のQFTより同時並列にcpをかける部分が多く、回路深さを削減できる。
    """
    n = len(qubits)
    if n <= 1:
        return

    # 1) ビット列を左右反転 (最下位<->最上位, ... )
    for i in range(n // 2):
        qc.swap(qubits[i], qubits[n - 1 - i])

    # 2) バタフライ段階：段数 ~ log2(n)
    sign = -1 if not inverse else +1
    stage = 1
    while (1 << stage) <= n:
        size = 1 << stage
        half = size >> 1
        # sizeごとのブロックに分割して並列にcp
        for start in range(0, n, size):
            for i in range(half):
                angle = sign * math.pi * i / half
                if abs(angle) > 1e-15:
                    qc.cp(angle, qubits[start + half + i], qubits[start + i])
        stage += 1

    # 3) 各ビットにH
    for i in range(n):
        qc.h(qubits[i])

    # 順QFTの場合は最後に再びビット反転が必要だが
    # ここでは最初にswap済みなので再度swapして元に戻す
    if not inverse:
        for i in range(n // 2):
            qc.swap(qubits[i], qubits[n - 1 - i])

def solve(n: int, a: int) -> QuantumCircuit:
    """
    リトルエンディアン:
      k[0] が最下位ビット (2^0),
      k[n-1] が最上位ビット (2^(n-1)).

    c = 0 のとき位相なし,
    c = 1 のとき exp(2πi a k / 2^n) を付与する。
    """
    k = QuantumRegister(n, 'k')
    c = QuantumRegister(1, 'c')
    qc = QuantumCircuit(k, c)

    # 1) QFT(k) --- バタフライ実装
    qft_butterfly(qc, k, inverse=False)

    # 2) c=1 のときにのみ位相 exp(i * 2π a 2^j / 2^n) を掛ける
    #    ( j=0..n-1 を全並列で処理 => 深さわずかに +1 程度 )
    for j in range(n):
        angle = 2 * math.pi * a * (2**j) / (2**n)
        qc.cp(angle, c[0], k[j])

    # 3) iQFT(k) --- バタフライ実装
    qft_butterfly(qc, k, inverse=True)

    return qc
'''


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

def qft_butterfly(qc: QuantumCircuit, qubits, inverse=False):
    """
    バタフライ構造(QFT/iQFT) - リトルエンディアン対応。
      - qubits[0] が最下位ビット
      - inverse=False:  順QFT
      - inverse=True :  逆QFT (iQFT)
    通常のQFTより多くの cp を並列化できるため、回路深さを小さく抑えられる。
    """
    n = len(qubits)
    if n < 2:
        return  # 1ビット以下なら何もしない

    # (1) ビット反転: qubits[i] <-> qubits[n-1-i]
    for i in range(n // 2):
        qc.swap(qubits[i], qubits[n - 1 - i])

    # (2) バタフライ段階：log(n) ステージ
    #     各ステージで「ブロックサイズ = 2^stage」に分割し，
    #     半分の長さ half について並列に cp を掛ける
    sign = -1 if not inverse else +1
    stage = 1
    while (1 << stage) <= n:
        size = 1 << stage       # 2^stage
        half = size >> 1       # 2^(stage-1)
        for start in range(0, n, size):
            for i in range(half):
                angle = sign * math.pi * i / half
                if abs(angle) < 1e-15:
                    continue
                # cp(angle, qubits[start+half+i], qubits[start+i])
                qc.cp(angle, qubits[start + half + i], qubits[start + i])
        stage += 1

    # (3) 各ビットに H
    for i in range(n):
        qc.h(qubits[i])

    # 順QFTの場合、最後にビットを再び反転しておく
    if not inverse:
        for i in range(n // 2):
            qc.swap(qubits[i], qubits[n - 1 - i])

def solve(n: int, a: int) -> QuantumCircuit:
    """
    0 <= k < 2^n, c in {0,1}:
      |k>_n |c>  ---> exp(2πi * a * c * k / 2^n) |k>_n |c>

    リトルエンディアン: k[0] が最下位ビット
    回路深さを小さくするために「バタフライ版QFT -> 単制御位相 -> iQFT」の流れ。
    """
    k = QuantumRegister(n)  # n量子ビット (k[0]がLSB)
    c = QuantumRegister(1)  # 1量子ビット (制御)
    qc = QuantumCircuit(k, c)

    # 1) QFT(k)  [バタフライ構造で深さを抑える]
    qft_butterfly(qc, k, inverse=False)

    # 2) c=1 のときにだけ位相 exp(i * 2π a 2^j / 2^n) をかける
    #    ( j=0..n-1 を並列に適用 => 深さは +1～2程度 )
    for j in range(n):
        theta_j = 2 * math.pi * a * (2**j) / (2**n)
        qc.cp(theta_j, c[0], k[j])

    # 3) iQFT(k)
    qft_butterfly(qc, k, inverse=True)

    return qc
'''


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

def qft_butterfly(qc: QuantumCircuit, qubits, inverse=False):
    """
    バタフライ構造のQFT/iQFT (リトルエンディアン):
      - qubits[0]が最下位ビット
      - inverse=False -> 順QFT
      - inverse=True  -> 逆QFT (iQFT)
    回路深さを小さくしつつ、n=2等の小規模でも安全に動作するよう調整。
    """
    n = len(qubits)
    if n <= 1:
        # 1量子ビット以下なら何もしない
        if n == 1:
            qc.h(qubits[0])
        return

    #
    # (1) ビット反転: qubits[i] <-> qubits[n-1-i]
    #
    for i in range(n // 2):
        qc.swap(qubits[i], qubits[n - 1 - i])

    #
    # (2) バタフライ段階
    #
    sign = -1.0 if not inverse else +1.0
    stage = 1
    while (1 << stage) <= n:
        size = 1 << stage  # 2^stage
        if size > n:
            break  # 範囲外を避けるためスキップ
        half = size >> 1   # 2^(stage-1)

        # ブロックを size ごとに切って並列実行
        for start in range(0, n, size):
            # block: [start .. start+size-1]
            # 前半: [start .. start+half-1]
            # 後半: [start+half .. start+size-1]
            # バタフライカーネル
            for i in range(half):
                # 後半 = (start+half+i), 前半 = (start+i)
                idxA = start + i
                idxB = start + half + i
                if idxA >= n or idxB >= n:
                    continue

                angle = sign * math.pi * i / half
                # angle=0 のときcpしても位相が0なのでスキップ
                if abs(angle) < 1e-15:
                    continue

                qc.cp(angle, qubits[idxB], qubits[idxA])

        stage += 1

    #
    # (3) 各ビットに H
    #
    for i in range(n):
        qc.h(qubits[i])

    #
    # (4) 順QFTなら最後にもう一度ビット反転
    #
    if not inverse:
        for i in range(n // 2):
            qc.swap(qubits[i], qubits[n - 1 - i])

def solve(n: int, a: int) -> QuantumCircuit:
    """
    問題:
      0 <= k < 2^n, c in {0,1} に対し
        |k>_n |c> -> exp(2πi * a * c * k / 2^n) |k>_n |c>
    リトルエンディアン( k[0] が最下位ビット )想定。
    バタフライ版QFTで回路深さを削減しつつ、位相を付与。
    """
    k = QuantumRegister(n)  # n量子ビット (k[0]がLSB)
    c = QuantumRegister(1)  # 1量子ビット (制御)
    qc = QuantumCircuit(k, c)

    # 1) QFT(k)
    qft_butterfly(qc, k, inverse=False)

    # 2) c=1 のとき k[j] に位相 2π a 2^j / 2^n を付与 (並列にcp)
    for j in range(n):
        theta_j = 2 * math.pi * a * (2**j) / (2**n)
        qc.cp(theta_j, c[0], k[j])

    # 3) iQFT(k)
    qft_butterfly(qc, k, inverse=True)

    return qc
'''


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

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

    for j in range(n):
        theta_j = 2 * math.pi * a * (2**j) / (2**n)
        qc.cp(theta_j, c[0], k[j])

    return qc
'''


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

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

    # Iterate over all possible values of k (0 to 2^n - 1)
    for k_value in range(2**n):
        # Calculate the phase shift angle
        phase_shift = (2 * math.pi * a * k_value) / (2**n)

        # Apply the controlled phase shift if c is 1
        # We use the `cp` gate for controlled phase shift
        qc.cp(phase_shift, c[0], k)  # c[0] controls the phase shift on all k qubits

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    for k_val in range(2**n):
        qc.u1(2 * math.pi * a * k_val / 2**n, k)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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


'''python
from qiskit import QuantumCircuit, QuantumRegister

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


'''python
from qiskit import QuantumCircuit, QuantumRegister

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


'''python
from qiskit import QuantumCircuit, QuantumRegister

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


'''python
from qiskit import QuantumCircuit, QuantumRegister

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


'''python
from qiskit import QuantumCircuit, QuantumRegister

def oracle(n, a):
    qc = QuantumCircuit(n)
    for i in range(n):
        phase = 2 * math.pi * a * (2**(i+1)) / (2**n)
        qc.p(phase, i)
    return qc.to_gate().control(1)

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    oracle_gate = oracle(n, a)
    qbits = []
    qbits.append(n)
    for i in range(0, n):
        qbits.append(i)
    qc.append(oracle_gate, qbits)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def oracle(n, a):
    for i in range(n-1, 0, -1):
        qc.swap(qc, i, i-1)
    qc = QuantumCircuit(n)
    for i in range(n-1):
        phase = 2 * math.pi * a * (2**(i+1)) / (2**n)
        qc.cp(phase, i+1, i)
    phase = 2 * math.pi * a * (2**n) / (2**n)
    for i in range(0, n-1):
        qc.swap(qc, i, i+1)
    qc.cp(phase, 0, n-1)

    return qc.to_gate().control(1)

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    oracle_gate = oracle(n, a)
    qbits = []
    qbits.append(n)
    for i in range(0, n):
        qbits.append(i)
    qc.append(oracle_gate, qbits)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def oracle(n, a):
    qc = QuantumCircuit(n)
    for i in range(n-1, 0, -1):
        qc.swap(i, i-1)
    for i in range(n-1):
        phase = 2 * math.pi * a * (2**(i+1)) / (2**n)
        qc.cp(phase, i+1, i)
    phase = 2 * math.pi * a * (2**n) / (2**n)
    for i in range(0, n-1):
        qc.swap(i, i+1)
    qc.cp(phase, 0, n-1)

    return qc.to_gate().control(1)

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    oracle_gate = oracle(n, a)
    qbits = []
    qbits.append(n)
    for i in range(0, n):
        qbits.append(i)
    qc.append(oracle_gate, qbits)
    return qc
solve(3, 2).draw()
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def oracle(n, a):
    qc = QuantumCircuit(n)
    for i in range(n-1, 0, -1):
        qc.swap(i, i-1)
    for i in range(n-1):
        phase = 2 * math.pi * a * (2**(i+1)) / (2**n)
        qc.cp(phase, i+1, i)
    phase = 2 * math.pi * a * (2**n) / (2**n)
    for i in range(0, n-1):
        qc.swap(i, i+1)
    qc.cp(phase, 0, n-1)

    return qc.to_gate().control(1)

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    oracle_gate = oracle(n, a)
    qbits = []
    qbits.append(n)
    for i in range(0, n):
        qbits.append(i)
    qc.append(oracle_gate, qbits)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def oracle(n, a):
    qc = QuantumCircuit(n)
    for i in range(n-1, 0, -1):
        qc.swap(i, i-1)

    for i in range(n-1):
        phase = 2 * math.pi * a * (2**(i+1)) / (2**n)
        qc.cp(phase, i+1, i)
    phase = 2 * math.pi * a * (2**n) / (2**n)
    qc.cp(phase, 0, n-1)

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

    return qc.to_gate().control(1)

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    oracle_gate = oracle(n, a)
    qbits = []
    qbits.append(n)
    for i in range(0, n):
        qbits.append(i)
    qc.append(oracle_gate, qbits)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def oracle(n, a):
    qc = QuantumCircuit(n)
    for i in range(n-1, 0, -1):
        qc.swap(i, i-1)

    for i in range(n-1):
        phase = 2 * math.pi * a * (2**(i)) / (2**n)
        qc.cp(phase, i+1, i)
    phase = 2 * math.pi * a * (2**(n-1)) / (2**n)
    qc.cp(phase, 0, n-1)

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

    return qc.to_gate().control(1)

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    oracle_gate = oracle(n, a)
    qbits = []
    qbits.append(n)
    for i in range(0, n):
        qbits.append(i)
    qc.append(oracle_gate, qbits)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def oracle(n, a):
    qc = QuantumCircuit(n)
    for i in range(n-1, 0, -1):
        qc.swap(i, i-1)
    for i in range(n-1):
        phase = 2 * math.pi * a * (2**(n-i)) / (2**n)
        qc.cp(phase, i+1, i)
    phase = 2 * math.pi * a * (2**(n-n+1)) / (2**n)
    qc.cp(phase, 0, n-1)
    for i in range(0, n-1):
        qc.swap(i, i+1)
    qc.draw()
    return qc.to_gate().control(1)

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    oracle_gate = oracle(n, a)
    qbits = []
    qbits.append(n)
    for i in range(0, n):
        qbits.append(i)
    qc.append(oracle_gate, qbits)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def oracle(n, a):
    qc = QuantumCircuit(n)
    for i in range(n-1, 0, -1):
        qc.swap(i, i-1)
    for i in range(n-1):
        phase = 2 * math.pi * a * (2**(n-i+1)) / (2**n)
        qc.cp(phase, i+1, i)
    phase = 2 * math.pi * a * (2**(n-n+1)) / (2**n)
    qc.cp(phase, 0, n-1)
    for i in range(0, n-1):
        qc.swap(i, i+1)
    qc.draw()
    return qc.to_gate().control(1)

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    oracle_gate = oracle(n, a)
    qbits = []
    qbits.append(n)
    for i in range(0, n):
        qbits.append(i)
    qc.append(oracle_gate, qbits)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def oracle(n, a):
    qc = QuantumCircuit(n)
    for i in range(n-1, 0, -1):
        qc.swap(i, i-1)

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

    for i in range(0, n-1):
        qc.swap(i, i+1)
    qc.draw()
    return qc.to_gate().control(1)

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    oracle_gate = oracle(n, a)
    qbits = []
    qbits.append(n)
    for i in range(0, n):
        qbits.append(i)
    qc.append(oracle_gate, qbits)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def oracle(n, a):
    qc = QuantumCircuit(n)
    for i in range(n-1, 0, -1):
        qc.swap(i, i-1)

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

    for i in range(0, n-1):
        qc.swap(i, i+1)
    qc.draw()
    return qc.to_gate().control(1)

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    oracle_gate = oracle(n, a)
    qbits = []
    qbits.append(n)
    for i in range(0, n):
        qbits.append(i)
    qc.append(oracle_gate, qbits)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def oracle(n, a):
    qc = QuantumCircuit(n)
    for i in range(n-1, 0, -1):
        qc.swap(i, i-1)

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

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

    return qc.to_gate().control(1)

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    oracle_gate = oracle(n, a)
    qbits = []
    qbits.append(n)
    for i in range(0, n):
        qbits.append(i)
    qc.append(oracle_gate, qbits)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def oracle(n, a):
    qc = QuantumCircuit(n)
    for i in range(n-1, 0, -1):
        qc.swap(i, i-1)

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

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

    return qc.to_gate().control(1)

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    oracle_gate = oracle(n, a)
    qbits = []
    qbits.append(n)
    for i in range(0, n):
        qbits.append(i)
    qc.append(oracle_gate, qbits)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def oracle(n, a):
    qc = QuantumCircuit(n)
    for i in range(n-1, 0, -1):
        qc.swap(i, i-1)

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

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

    return qc.to_gate().control(1)

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    oracle_gate = oracle(n, a)
    qbits = []
    qbits.append(n)
    for i in range(0, n):
        qbits.append(i)
    qc.append(oracle_gate, qbits)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def oracle(n, a):
    qc = QuantumCircuit(n)
    for i in range(n-1, 0, -1):
        qc.swap(i, i-1)

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

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

    return qc.to_gate().control(1)

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    oracle_gate = oracle(n, a)
    qbits = []
    qbits.append(n)
    for i in range(0, n):
        qbits.append(i)
    qc.append(oracle_gate, qbits)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    for i in range(0, n):
        phase = 2 * math.pi * a * (2**i) / (2**n)
        qc.cp(phase, n, i)
    return qc
'''


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

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    for i in range(0, n):
        phase = 2 * math.pi * a * (2**i) / (2**n)
        qc.cp(phase, n, i)
    return qc
'''


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

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

    for i in range(n):
        theta = 2 * math.pi * a * (2 ** i)  / (2 ** n)
        qc.cry(theta, k[i], c)
    return qc
'''


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

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

    for i in range(n):
        theta = 2 * math.pi * a * (2 ** i)  / (2 ** n)
        qc.cp(theta, k[i], c)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    for i in range(k):
        theta = 2 * math.pi * a / 2**k
        qc.cp(theta,c[0],i)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    theta = 2 * math.pi * a / 2**n
    for i in range(n):
        qc.cp(theta,n,i)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    theta = 2 * math.pi * a / 2**n
    for i in range(n):
        qc.cp(theta,c[0],i)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    theta = 2 * math.pi * a / 2**n

    for i in range(n):
        qc.cp(theta,c[0],i)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    theta = 2 * math.pi * a / 2**n

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    theta = 2 * math.pi * a / 2**n

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    theta = 2 * math.pi * a / 2**n

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    theta = 2 * math.pi * a / 2**n

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    theta = 2 * math.pi * a / 2**n

    for i in range(n):
        theta = (2 * math.pi * a * 2**(n-i-1)) / 2**n
        qc.cp(theta,c[0],i)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    theta = 2 * math.pi * a / 2**n

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    theta = 2 * math.pi * a / 2**n

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

    return qc
'''


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

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

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

    return qc
'''


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

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    for i in range(n):
        qc.cp(2*math.pi*a*2**i/2**n, c, i)

    return qc
'''


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

def QFT(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in reversed(range(n)):
        qc.h(i)
        for j in reversed(range(i)):
            qc.cp(pi/(2**(i-j)), j, i)
    for i in range(n//2):
        qc.swap(i, n - i - 1)
    return qc

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    qc.compose(QFT(n), k, inplace = True)
    a_list = [(a//(2**i))%2 for i in range(n)]
    for i in range(n):
      qc.cp(2*pi*a_list[i]/(2**(n - i)), n, i)
    qc.compose(QFT(n).inverse(), k, inplace = True)
    return qc
'''


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

def QFT(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in reversed(range(n)):
        qc.h(i)
        for j in reversed(range(i)):
            qc.cp(pi/(2**(i-j)), j, i)
    for i in range(n//2):
        qc.swap(i, n - i - 1)
    return qc

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    qc.compose(QFT(n), k, inplace = True)
    a_list = [(a//(2**i))%2 for i in range(n)]
    for i in range(n):
        qc.cp(2*pi*a_list[i]/(2**(n - i)), n, i)
    qc.compose(QFT(n).inverse(), k, inplace = True)
    return qc
'''


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

def tmp(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in reversed(range(n)):
        qc.h(i)
        for j in reversed(range(i)):
            qc.cp(pi/(2**(i-j)), j, i)
    for i in range(n//2):
        qc.swap(i, n - i - 1)
    return qc

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    qc.compose(tmp(n), k, inplace = True)
    a_list = [(a//(2**i))%2 for i in range(n)]
    for i in range(n):
        qc.cp(2*pi*a_list[i]/(2**(n - i)), n, i)
    qc.compose(tmp(n).inverse(), k, inplace = True)
    return qc
'''


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

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    for i in range(n - 1, -1, -1):
        qc.h(i)
        for j in range(i - 1, -1, -1):
            qc.cp(pi / (1 << (i - j)), j, i)

    for i in range(n):
        theta = tau / (1 << n - i)
        # qc.cp(a * theta[i], k[n - 1 - i])
        qc.cp(a * theta, c[0], k[n - 1 - i])

    for i in range(n):
        for j in range(i):
            qc.cp(-pi / (1 << (i - j)), j, i)
        qc.h(i)

    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 b2(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # init = [0]*(2**(n+1))
    # init[2**n+1]=1
    # qc.initialize(init)

    # Write your code here:
    for i in range(n):
        qc.mcp(2*math.pi*a/(2**(n-i)), [n], i)

    return qc

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    qc.compose(quantum_fourier_transform(n), k, inplace=True)
    for i in range(n):
        qc.mcp(2*math.pi*a/(2**(n-i)), [n], i)
    qc.compose(quantum_fourier_transform(n).inverse(), k, inplace=True)

    return qc

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


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    for i in range(n):
        if (a >> i) & 1:
            qc.cx(c[0], k[i])
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    # 補助レジスタ
    carry = QuantumRegister(1, 'carry')
    result = QuantumRegister(n, 'result')
    qc.add_register(carry, result)

    # cの値に応じてaを加算
    for i in range(n):
        qc.cx(c, k[i])

    # k + a mod 2^n を計算
    for i in range(n):
        qc.ccx(k[i], carry, result[i])
        qc.cx(k[i], carry)
        qc.cx(carry, result[i])

    # 結果をkレジスタにコピー
    for i in range(n):
        qc.cx(result[i], k[i])

    # 補助レジスタをリセット
    for i in range(n):
        qc.cx(result[i], result[i])
    qc.cx(carry, carry)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    for i in range(n):
        # a の i 番目のビットが 1 の場合に制御加算
        if (a >> i) & 1:
            qc.cx(c, k[i])

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    for i in range(n):
        if (a >> i) & 1:
            qc.cx(c, k[i])
    qc.barrier()
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    for i in range(n):
        if (a >> i) & 1:
            qc.cx(k[i], k[(i + 1) % n])

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    for i in range(n):
        if (a >> i) & 1:
            qc.cx(c, k[i])

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import RYGate
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, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:

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

    for idx in range(n):
        theta = 2 * a * math.pi * (2**idx) / (2**n)
        qc.cp(theta, c, k[idx])

    qc.compose(qft(n).inverse(), inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    for i in range(n):
        if (a & (1 << i)):
            qc.cx(c[0], k[n - i - 1])

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    for i in range(n):
        if (a & (1 << i)):
            print(i)
            n_not = n - i
            for j in range(i+n_not-1, i-1, -1):
                qc.mcx(k[i:j] + [c[0]], k[j])

    return qc
'''


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

def qft_rotations(qc, n):
    """Apply QFT rotations to specified qubits"""
    for i in range(n):
        qc.h(i)
        for j in range(i+1, n):
            qc.cp(math.pi/float(2**(j-i)), j, i)

def inverse_qft_rotations(qc, n):
    """Apply inverse QFT rotations to specified qubits"""
    for i in range(n-1, -1, -1):
        for j in range(n-1, i, -1):
            qc.cp(-math.pi/float(2**(j-i)), j, i)
        qc.h(i)

def add_constant(qc, n, a, control):
    """Add constant a (controlled by control qubit) using phase rotations"""
    for i in range(n):
        angle = (2 * math.pi * (a / (2 ** (n-i)))) % (2 * math.pi)
        if angle != 0:  # Only apply non-zero rotations
            qc.cp(angle, control, i)

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

    # Apply QFT to register k
    qft_rotations(qc, n)

    # Add constant a controlled by c
    add_constant(qc, n, a, n)

    # Apply inverse QFT
    inverse_qft_rotations(qc, n)

    return qc
'''


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

def manual_qft(n):
    """Manually implements the Quantum Fourier Transform (QFT)."""
    qc = QuantumCircuit(n)
    for j in range(n):
        qc.h(j)
        for k in range(j + 1, n):
            qc.cp(np.pi / 2**(k - j), k, j)  # Controlled phase rotation
    return qc

def manual_iqft(n):
    """Manually implements the Inverse Quantum Fourier Transform (IQFT)."""
    return manual_qft(n).inverse()

def controlled_modular_addition(n, a):
    """Implements controlled modular addition using QFT & controlled phase shifts."""
    qc = QuantumCircuit(n + 1)  # Extra qubit for control

    # Apply QFT to the lower n qubits (excluding control qubit)
    qc.append(manual_qft(n), range(n))

    # Apply controlled phase shifts based on the bits of 'a'
    for i in range(n):
        if (a >> i) & 1:  # Check if the i-th bit of 'a' is set
            qc.cp(np.pi / 2**(n - i - 1), n, i)  # Apply controlled phase shift

    # Apply inverse QFT
    qc.append(manual_iqft(n), range(n))

    return qc

def solve(n: int, a: int) -> QuantumCircuit:
    """Constructs the controlled modular addition circuit."""
    k_reg = QuantumRegister(n, name='k')  # Register for k
    c_reg = QuantumRegister(1, name='c')  # Control qubit
    qc = QuantumCircuit(k_reg, c_reg)

    # Apply controlled modular addition only when c = 1
    controlled_add = controlled_modular_addition(n, a).to_gate(label="C-ADD(a)").control(1)

    # Apply controlled modular addition with explicit control
    qc.append(controlled_add, [c_reg[0]] + k_reg[:])

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def controlled_modular_addition(n, a):
    """Implements controlled modular addition using bitwise operations."""
    qc = QuantumCircuit(n + 1)  # Extra qubit for control

    for i in range(n):
        if (a >> i) & 1:  # If the i-th bit of 'a' is 1
            qc.cx(n, i)  # Controlled-X: If control is 1, flip bit i of 'k'

    return qc

def solve(n: int, a: int) -> QuantumCircuit:
    """Constructs an optimized controlled modular addition circuit."""
    k_reg = QuantumRegister(n, name='k')  # Register for k
    c_reg = QuantumRegister(1, name='c')  # Control qubit
    qc = QuantumCircuit(k_reg, c_reg)

    # Apply controlled modular addition (bitwise controlled-X operations)
    controlled_add = controlled_modular_addition(n, a).to_gate(label="C-ADD(a)").control(1)

    qc.append(controlled_add, [c_reg[0]] + k_reg[:])

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    # Iterate through each bit of 'a' to perform the addition
    for i in range(n):
        if (a >> i) & 1:
            # Apply controlled-X gate if the bit is 1
            qc.mcx([c[0]] + [k[j] for j in range(i)], k[i])

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, a: int) -> QuantumCircuit:
    # Create quantum registers and circuit
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)

    # Implement controlled addition of constant a
    # We'll add the constant only when c=1
    for i in range(n):
        if (a >> i) & 1:  # Check if ith bit of a is 1
            # Add controlled-NOT gates where needed
            qc.cx(c[0], k[i])

            # Handle carry propagation
            for j in range(i+1, n):
                if (a >> j) & 1:
                    qc.ccx(c[0], k[i], k[j])

    return qc
'''


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

def qft(qc: QuantumCircuit, qubits, inverse=False):
    """
    リトルエンディアン版の QFT/iQFT (n^2ステップの素朴実装)。
      - qubits[0] が最下位ビット
      - inverse=False: 順QFT
      - inverse=True : 逆QFT (iQFT)
    """
    n = len(qubits)
    if not inverse:
        # --- 順QFT ---
        for j in range(n):
            # 1量子ビットH
            qc.h(qubits[j])
            # 制御位相 cp(π/2^(m-j)) for m = j+1..n-1
            for m in range(j+1, n):
                angle = math.pi / (2**(m-j))
                qc.cp(angle, qubits[m], qubits[j])
        # ビット順反転
        for i in range(n // 2):
            qc.swap(qubits[i], qubits[n - 1 - i])
    else:
        # --- 逆QFT ---
        # ビット順反転
        for i in range(n // 2):
            qc.swap(qubits[i], qubits[n - 1 - i])
        # 上から下へ
        for j in reversed(range(n)):
            for m in reversed(range(j+1, n)):
                angle = - math.pi / (2**(m-j))
                qc.cp(angle, qubits[m], qubits[j])
            qc.h(qubits[j])

def solve(n: int, a: int) -> QuantumCircuit:
    """
    問題:
      整数 n, a (1 <= n <= 10, 0 <= a < 2^n) が与えられる。
      0 <= k < 2^n, c in {0,1} の任意の組に対し、
        |k>_n|c>_1 --> |(k + c*a) mod 2^n>_n |c>_1
      を実装する量子回路 qc を構成せよ。

    リトルエンディアン:
      k[0] が最下位ビット (2^0)。
    """
    # レジスタ k: n量子ビット, c: 1量子ビット
    k = QuantumRegister(n, 'k')
    c = QuantumRegister(1, 'c')
    qc = QuantumCircuit(k, c)

    # --- 1) QFT(k) ---
    qft(qc, k, inverse=False)

    # --- 2) c=1 のときだけ k に「 + a 」を加算する
    #       具体的にはビット j=0..n-1 に単制御位相 cp( 2π a 2^j / 2^n )
    #       リトルエンディアンなので k[j] は 2^j の重み
    for j in range(n):
        theta_j = 2 * math.pi * a * (2**j) / (2**n)
        qc.cp(theta_j, c[0], k[j])

    # --- 3) iQFT(k) ---
    qft(qc, k, inverse=True)

    return qc
'''


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

def qft(qc: QuantumCircuit, qubits, inverse=False):
    """
    リトルエンディアン版の QFT/iQFT (n^2ステップの素朴実装)。
      - qubits[0] が最下位ビット
      - inverse=False: 順QFT
      - inverse=True : 逆QFT (iQFT)
    """
    n = len(qubits)
    if not inverse:
        # --- 順QFT ---
        for j in range(n):
            # 1量子ビットH
            qc.h(qubits[j])
            # 制御位相 cp(π/2^(m-j)) for m = j+1..n-1
            for m in range(j+1, n):
                angle = math.pi / (2**(m-j))
                qc.cp(angle, qubits[m], qubits[j])
        # ビット順反転
        for i in range(n // 2):
            qc.swap(qubits[i], qubits[n - 1 - i])
    else:
        # --- 逆QFT ---
        # ビット順反転
        for i in range(n // 2):
            qc.swap(qubits[i], qubits[n - 1 - i])
        # 上から下へ
        for j in reversed(range(n)):
            for m in reversed(range(j+1, n)):
                angle = - math.pi / (2**(m-j))
                qc.cp(angle, qubits[m], qubits[j])
            qc.h(qubits[j])

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

    # --- 1) QFT(k) ---
    qft(qc, k, inverse=False)

    # --- 2) c=1 のときだけ k に「 + a 」を加算する
    #       具体的にはビット j=0..n-1 に単制御位相 cp( 2π a 2^j / 2^n )
    #       リトルエンディアンなので k[j] は 2^j の重み
    for j in range(n):
        theta_j = 2 * math.pi * a * (2**j) / (2**n)
        qc.cp(theta_j, c[0], k[j])

    # --- 3) iQFT(k) ---
    qft(qc, k, inverse=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    for j in range(n):
        theta_j = 2 * math.pi * a * (2**j) / (2**n)
        qc.cp(theta_j, c[0], k[j])

    return qc
'''


'''python
from qiskit.quantum_info import Statevector
from qiskit.visualization import *
from qiskit import QuantumCircuit, QuantumRegister

import math

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

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

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

    return qc

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

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

    return qc

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

    qc.compose(qft(n), qubits=k, inplace=True)
    qc.compose(crot(n, a), qubits=[*k, *c], inplace=True)
    qc.compose(qft(n).inverse(), qubits=k, inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

import math

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

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

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

    return qc

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

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

    return qc

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

    qc.compose(qft(n), qubits=k, inplace=True)
    qc.compose(crot(n, a), qubits=[*k, *c], inplace=True)
    qc.compose(qft(n).inverse(), qubits=k, inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    def f(num):
        for i in reversed(range(num,n)):
            qc.mcx(list(range(num,i))+[n],i)
    for i in range(n):
        if 2**i ^ a < a:
            f(i)

    return qc

solve(4,3).draw()
'''


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

def solve(n: int, a: int) -> QuantumCircuit:
    """
    Construct a quantum circuit that realises the oracle O:
        |k⟩_n |c⟩ → |(k + c·a) mod 2^n⟩_n |c⟩   (little-endian)
    No ancilla qubits are used, only the given n+1 qubits.
    """
    # Registers (little-endian: qubit 0 = LSB)
    k = QuantumRegister(n, "k")      # data register
    c = QuantumRegister(1, "c")      # control qubit
    qc = QuantumCircuit(k, c)

    # --- 1.  Quantum Fourier Transform on k (little-endian, no swaps) ---
    for i in range(n):                         # iterate LSB → MSB
        qc.h(k[i])
        for j in range(i + 1, n):
            angle = pi / (2 ** (j - i))        # controlled phase R_{j-i+1}
            qc.cp(angle, k[j], k[i])

    # --- 2.  Controlled phase rotations encoding +a when c == 1 ---
    for i in range(n):
        # fractional part of a / 2^{i+1}
        frac = (a % (1 << (i + 1))) / (1 << (i + 1))
        angle = 2 * pi * frac                  # 2π * frac
        if angle:                              # skip zero rotations
            qc.cp(angle, c[0], k[i])           # controlled by c

    # --- 3.  Inverse QFT on k (undo step 1) ---
    for i in reversed(range(n)):
        for j in reversed(range(i + 1, n)):
            angle = -pi / (2 ** (j - i))
            qc.cp(angle, k[j], k[i])
        qc.h(k[i])

    return qc
'''


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

def qft(qc: QuantumCircuit, q: QuantumRegister) -> None:
    """ビッグエンディアン実装 (SWAP あり)"""
    n = len(q)
    for i in reversed(range(n)):
        qc.h(q[i])
        for j in reversed(range(i)):
            qc.cp(pi / 2 ** (i - j), q[j], q[i])
    for i in range(n // 2):                      # 反転してリトルエンディアンに戻す
        qc.swap(q[i], q[n - i - 1])

def iqft(qc: QuantumCircuit, q: QuantumRegister) -> None:
    """qft の逆演算"""
    n = len(q)
    for i in reversed(range(n // 2)):            # SWAP を先に戻す
        qc.swap(q[i], q[n - i - 1])
    for i in range(n):
        for j in range(i):
            qc.cp(-pi / 2 ** (i - j), q[j], q[i])
        qc.h(q[i])

def solve(n: int, a: int) -> QuantumCircuit:
    """
    Oracle O:
        |k⟩_n |c⟩ → |(k + c·a) mod 2^n⟩_n |c⟩
    * 整数はリトルエンディアン (qubit 0 = LSB)
    * 追加の量子ビットは使用しない
    """
    k = QuantumRegister(n, "k")      # データレジスタ
    c = QuantumRegister(1, "c")      # 制御ビット
    qc = QuantumCircuit(k, c)

    # 1. QFT
    qft(qc, k)                       # ここでレジスタは再びリトルエンディアン順

    # 2. +a を位相に刻む（c==1 のときのみ）
    #    各フーリエ量子ビット i (LSB から) に角度 2π·a / 2^{i+1}
    for i in range(n):
        angle = (2 * pi * a) / (1 << (i + 1))
        # 角度が 0 (a が 2^{i+1} の倍数) ならスキップ
        if angle % (2 * pi):
            qc.cp(angle, c[0], k[i])

    # 3. 逆 QFT
    iqft(qc, k)

    return qc
'''


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

def qft_le(qc: QuantumCircuit, q: QuantumRegister) -> None:
    """little-endian QFT with final SWAP (式(2) を実現)"""
    n = len(q)
    for i in reversed(range(n)):
        qc.h(q[i])
        for j in reversed(range(i)):
            qc.cp(pi / 2**(i - j), q[j], q[i])
    for i in range(n // 2):
        qc.swap(q[i], q[n - i - 1])

def iqft_le(qc: QuantumCircuit, q: QuantumRegister) -> None:
    n = len(q)
    for i in reversed(range(n // 2)):
        qc.swap(q[i], q[n - i - 1])
    for i in range(n):
        for j in range(i):
            qc.cp(-pi / 2**(i - j), q[j], q[i])
        qc.h(q[i])

def solve(n: int, a: int) -> QuantumCircuit:
    """
    |k⟩_n|c⟩ → |(k + c·a) mod 2^n⟩_n|c⟩   (little-endian, ancilla-free)
    """
    k = QuantumRegister(n, "k")       # データ
    c = QuantumRegister(1, "c")       # 制御
    qc = QuantumCircuit(k, c)

    # 1) QFT
    qft_le(qc, k)

    # 2) controlled R_i(a)  — 角度は 2π a / 2^{n-i} !
    for i in range(n):
        angle = 2 * pi * a / (1 << (n - i))
        if angle % (2 * pi):
            qc.cp(angle, c[0], k[i])  # 控えめに skip(0∘)

    # 3) inverse QFT
    iqft_le(qc, k)
    return qc
'''


'''python
from math import ceil,floor,acos,asin,atan,sqrt,pi,gcd,sin,cos,tan
from qiskit import QuantumCircuit,QuantumRegister

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

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

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

    return qc

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

    qc.compose(qft_n,k,inplace=True)

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

    qc.compose(qft_n.inverse(),k,inplace=True)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    # QFT
    for i in reversed(range(n)):
        qc.h(i)
        for j in reversed(range(i)):
            qc.cp(math.pi / 2 ** (i - j), j, i)

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

    # B2
    for i in range(n):
        qc.cp(2 * math.pi * a * 2 ** (i - n), c[0], k[i])

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

    return qc
'''


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

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    # QFT
    for i in reversed(range(n)):
        qc.h(i)
        for j in reversed(range(i)):
            qc.cp(math.pi / 2 ** (i - j), j, i)

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

    # B2
    for i in range(n):
        qc.cp(2 * math.pi * a * 2 ** (i - n), c[0], k[i])

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

    return qc
'''


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

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    # QFT
    for i in reversed(range(n)):
        qc.h(i)
        for j in reversed(range(i)):
            qc.cp(math.pi / 2 ** (i - j), j, i)

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

    # B2
    for i in range(n):
        qc.cp(2 * math.pi * a * 2 ** (i - n), c[0], k[i])

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

    return qc
'''


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

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    # QFT
    for i in reversed(range(n)):
        qc.h(i)
        for j in reversed(range(i)):
            qc.cp(math.pi / 2 ** (i - j), j, i)

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

    # B2
    for i in range(n):
        qc.cp(2 * math.pi * a * 2 ** (i - n), c[0], k[i])

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

    return qc
'''


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

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

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    qc.compose(qft(n), inplace=True)

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

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


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    for _ in range(a):
        for i in range(n-1,-1,-1):
            qc.mcx([n]+list(range(i)),i)

    return qc
'''


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

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

    for i in range(n):
        qc.ch(n, i)
        for j in range(i+1, n):
            qc.mcp(2 * np.pi / 2 ** (j), [j, n], i)
    for i in range(n // 2):
        qc.cswap(n, i, (n - i - 1))

    for i in range(n):
        angle = (2 * np.pi * ((a >> i) & 1)) / 2 ** (i + 1)
        if (angle != 0):
            qc.cp(n, angle, i)

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

    for i in reversed(range(n)):
        for j in reversed(range(i + 1, n)):
            qc.mcp(-2 * np.pi / 2 ** (j), [j, n], i)
        qc.ch(n, i)

    return qc
'''


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

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

    for i in range(n):
        qc.ch(n, i)
        for j in range(i+1, n):
            qc.mcp(2 * np.pi / 2 ** (j), [j, n], i)
    for i in range(n // 2):
        qc.cswap(n, i, (n - i - 1))

    for i in range(n):
        angle = (2 * np.pi * ((a >> i) & 1)) / 2 ** (i + 1)
        if (angle != 0):
            qc.cp(angle, n, i)

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

    for i in reversed(range(n)):
        for j in reversed(range(i + 1, n)):
            qc.mcp(-2 * np.pi / 2 ** (j), [j, n], i)
        qc.ch(n, i)

    return qc
'''


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

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

    for i in range(n):
        qc.ch(n, i)
        for j in range(i+1, n):
            qc.mcp(2 * np.pi / 2 ** (j), [j, n], i)
    for i in range(n // 2):
        qc.cswap(n, i, (n - i - 1))

    for i in range(n):
        angle = (2 * np.pi * ((a >> i) & 1)) / 2 ** (i + 1)
        qc.cp(angle, n, i)

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

    for i in reversed(range(n)):
        for j in reversed(range(i + 1, n)):
            qc.mcp(-2 * np.pi / 2 ** (j), [j, n], i)
        qc.ch(n, i)

    return qc
'''


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

def QFT(qc : QuantumCircuit, idx : list[int], inversed : bool = False) -> 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))
            if (inversed):
                theta *= -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, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:

    QFT(qc, list(range(n)))

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

    QFT(qc, list(range(n)), inversed = True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, a: int) -> QuantumCircuit:
    k = QuantumRegister(n)  # Register for k
    c = QuantumRegister(1)   # Register for c
    qc = QuantumCircuit(k, c)

    # Convert a to its binary representation
    a_bin = [int(x) for x in format(a, f'0{n}b')[::-1]]  # Little-endian binary representation of a

    # Apply the controlled addition of a if c is 1
    for i in range(n):
        if a_bin[i] == 1:
            # Use a Toffoli gate to control the addition
            # We need to add a to k if c is 1
            # This means we need to add a to k[i] controlled by c[0]
            # We will use a series of CNOT and Toffoli gates

            # First, we need to create a carry bit
            if i == 0:
                # For the least significant bit, we can directly use CNOT
                qc.cx(c[0], k[i])
            else:
                # For higher bits, we need to use Toffoli gates to handle carry
                # Create a carry bit from the previous bit
                qc.ccx(c[0], k[i-1], k[i])  # Toffoli gate
                qc.cx(c[0], k[i])  # CNOT gate for the current bit

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    for j in range(a):
        for i in range(n):
            qc.x(i)
        qc.x(0)
        for i in range(n - 1):
            qc.mcx(list(range(0, i + 1)) + [n], i + 1)
        for i in range(n):
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    for j in range(a):
        for i in range(n):
            qc.x(i)
        qc.cx(n, 0)
        for i in range(n - 1):
            qc.mcx(list(range(0, i + 1)) + [n], i + 1)
        for i in range(n):
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    import math
    if False:
    	isv = Statevector([1 if i == (1+8) else 0 for i in range(16)])
    	print(isv)
    	qc.prepare_state(isv)

    # QFT
    for i in range(n)[::-1]:
    	qc.h(i)
    	for j in range(i)[::-1]:
    	    t = 2 * math.pi / (2 ** (i-j+1))
    	    qc.cp(t,j,i)

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

    # op

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

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

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

    return qc
'''


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

    import math
    if False:
    	isv = Statevector([1 if i == (3+8) else 0 for i in range(2**(n+1))])
    	print(isv)
    	qc.prepare_state(isv)

    # QFT
    for i in range(n)[::-1]:
    	qc.h(i)
    	for j in range(i)[::-1]:
    	    t = 2 * math.pi / (2 ** (i-j+1))
    	    qc.cp(t,j,i)

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

    # op

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

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    import math
    if False:
    	isv = Statevector([1 if i == (3+8) else 0 for i in range(2**(n+1))])
    	print(isv)
    	qc.prepare_state(isv)

    # QFT
    for i in range(n)[::-1]:
    	qc.h(i)
    	for j in range(i)[::-1]:
    	    t = 2 * math.pi / (2 ** (i-j+1))
    	    qc.cp(t,j,i)

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

    # op

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

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

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

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    for k in range(n):
        if(a%2 == 1):
            for i in range(n-1,k-1,-1):
                if(i == k):
                    qc.cx(n,i)
                else:
                    qc.mcx(list(range(k,i,1)) + [n],i)
        a = int(a/2)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    qc.x(0)
    for i in range(1, n):
        qc.mcx(list(range(i)), i)
    qc.inverse()
    qc.repeat(a)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    qc.cx(n, 0)
    for i in range(1, n):
        qc.mcx(list(range(i))+[n], i)
    qc.inverse()
    qc.repeat(a)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    qc.cx(n, 0)
    for i in range(1, n):
        qc.mcx(list(range(i))+[n], i)
    qc.repeat(2**n-a)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    for i in range(n):
        if((a >> i)&1):
            qc.cx(n, i)
            for j in range(i+1, n):
                qc.mcx([c] + list(range(i, j)), j)
    qc.inverse()
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    for i in range(n):
        if((a >> i)&1):
            qc.cx(n, i)
            for j in range(i+1, n):
                qc.mcx([n] + list(range(i, j)), j)
    qc.inverse()
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    for i in range(n):
        if((a >> i)&1):
            for j in range(n):
                l =[c]
                for k in range(j):
                    l.append((k + i) % n)
                qc.mcx(l, (j+i)%n)
    qc.inverse()
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    for i in range(n):
        if((a >> i)&1):
            for j in range(n):
                l =[n]
                for k in range(j):
                    l.append((k + i) % n)
                qc.mcx(l, (j+i)%n)
    qc.inverse()
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

def solve(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    for i in range(n):
        if((a >> i)&1):
            for j in range(i, n):
                qc.mcx([n] + list(range(i, j)), j)
    qc.inverse()
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

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

    return qc

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

    qft_n = qft(n)

    qc.compose(qft_n, k, inplace=True)

    for j in range(n):
        for i in range(n):
            theta = (2 * math.pi * a / 2**m) * 2**j
            qc.mcp(theta, [x[i],c], y[j])

    qc.compose(qft_n.inverse(), k, inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

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

    return qc

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

    qft_n = qft(n)

    qc.compose(qft_n, k, inplace=True)

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

    qc.compose(qft_n.inverse(), k, inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

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

    return qc

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

    qft_n = qft(n)

    qc.compose(qft_n, k, inplace=True)

    for j in range(n):
        theta = (math.pi * (a & 2**j) / 2**n) * 2**j
        qc.cp(theta, c, k[j])

    qc.compose(qft_n.inverse(), k, inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

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

    return qc

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

    qft_n = qft(n)

    qc.compose(qft_n, k, inplace=True)

    for j in range(n):
        theta = (2 * math.pi * (a & 2**j) / 2**n) * 2**j
        qc.cp(theta, c[0], k[j])

    qc.compose(qft_n.inverse(), k, inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

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

    return qc

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

    qft_n = qft(n)

    qc.compose(qft_n, k, inplace=True)

    # for j in range(n):
    #     theta = (2 * math.pi * (a & 2**j) / 2**n) * 2**j
    #     qc.cp(theta, c[0], k[j])

    qc.compose(qft_n.inverse(), k, inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

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

    return qc

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

    qft_n = qft(n)

    # qc.compose(qft_n, k, inplace=True)

    # for j in range(n):
    #     theta = (2 * math.pi * (a & 2**j) / 2**n) * 2**j
    #     qc.cp(theta, c[0], k[j])

    # qc.compose(qft_n.inverse(), k, inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

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

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

    return qc

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

    # qft_n = qft(n)

    # qc.compose(qft_n, k, inplace=True)

    # for j in range(n):
    #     theta = (2 * math.pi * (a & 2**j) / 2**n) * 2**j
    #     qc.cp(theta, c[0], k[j])

    # qc.compose(qft_n.inverse(), k, inplace=True)

    return qc
'''


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

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

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

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

    return qc

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

    qft_n = qft(n)

    qc.compose(qft_n, k, inplace=True)

    # for j in range(n):
    #     theta = (2 * math.pi * (a & 2**j) / 2**n) * 2**j
    #     qc.cp(theta, c[0], k[j])

    qc.compose(qft_n.inverse(), k, inplace=True)

    return qc
'''


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

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

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

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

    return qc

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

    qft_n = qft(n)

    qc.compose(qft_n, k, inplace=True)

    for j in range(n):
        theta = (2 * math.pi * (a & 2**j) / 2**n) * 2**j
        qc.cp(theta, c[0], k[j])

    qc.compose(qft_n.inverse(), k, inplace=True)

    return qc
'''


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

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

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

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

    return qc

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

    qft_n = qft(n)

    qc.compose(qft_n, k, inplace=True)

    for j in range(n):
        if (a & (1 << j)):  # Check if j-th bit of a is 1
            theta = 2 * math.pi * 2**j / 2**n
            qc.cp(theta, c[0], k[n-1-j])

    qc.compose(qft_n.inverse(), k, inplace=True)

    return qc
'''


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

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

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

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

    return qc

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

    qft_n = qft(n)

    qc.compose(qft_n, k, inplace=True)

    for j in range(n):
        if (a & (1 << j)):  # Check if j-th bit of a is 1
            theta = 2 * math.pi * 2**j / 2**n
            qc.cp(theta, c[0], k[j])

    qc.compose(qft_n.inverse(), k, inplace=True)

    return qc
'''


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

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

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

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

    return qc

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

    qft_n = qft(n)

    qc.compose(qft_n, k, inplace=True)

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

    qc.compose(qft_n.inverse(), k, inplace=True)

    return qc
'''


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

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

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

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

    return qc

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

    qft_n = qft(n)

    qc.compose(qft_n, k, inplace=True)

    for j in range(n):
        theta = 2 * math.pi * a * 2**j / 2**n
        qc.cp(theta, c[0], k[j])

    qc.compose(qft_n.inverse(), k, inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

import math

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

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

    return qc

def crot(n, a):
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    for i in range(n):
        qc.cp(2*math.pi*a*2**i/2**n, c, i)

    return qc

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

    qc.compose(qft(n), qubits=k, inplace=True)
    qc.compose(crot(n, a), qubits=[*k, *c], inplace=True)
    qc.compose(qft(n).inverse(), qubits=k, inplace=True)

    return qc
'''


'''python
import math

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

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

    return qc

def crot(n, a):
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    for i in range(n):
        qc.cp(2*math.pi*a*2**i/2**n, c, i)

    return qc

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

    qc.compose(qft(n), qubits=k, inplace=True)
    qc.compose(crot(n, a), qubits=[*k, *c], inplace=True)
    qc.compose(qft(n).inverse(), qubits=k, inplace=True)

    return qc
'''


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

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

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

    return qc

def crot(n, a):
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    for i in range(n):
        qc.cp(2*math.pi*a*2**i/2**n, c, i)

    return qc

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

    qc.compose(qft(n), qubits=k, inplace=True)
    #qc.compose(crot(n, a), qubits=[*k, *c], inplace=True)
    #qc.compose(qft(n).inverse(), qubits=k, inplace=True)

    return qc

solve(3,1).draw("mpl")
'''


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

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

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

    return qc

def crot(n, a):
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    for i in range(n):
        qc.cp(2*math.pi*a*2**i/2**n, c, i)

    return qc

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

    qc.compose(qft(n), qubits=k, inplace=True)
    qc.compose(crot(n, a), qubits=[*k, *c], inplace=True)
    qc.compose(qft(n).inverse(), qubits=k, inplace=True)

    return qc
'''


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

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

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

    return qc

def crot(n, a):
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    for i in range(n):
        qc.cp(2*math.pi*a*2**i/2**n, c, i)

    return qc

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

    qc.compose(qft(n), qubits=k, inplace=True)
    qc.compose(crot(n, a), qubits=[*k, *c], inplace=True)
    qc.compose(qft(n).inverse(), qubits=k, inplace=True)

    return qc
'''


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

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

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

    return qc

def crot(n, a):
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    for i in range(n):
        qc.cp(2*math.pi*a*2**i/2**n, c, i)

    return qc

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

    qc.compose(qft(n), qubits=k, inplace=True)
    qc.compose(crot(n, a), qubits=[*k, *c], inplace=True)
    qc.compose(qft(n).inverse(), qubits=k, inplace=True)

    return qc
'''


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

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

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

    return qc

def crot(n, a):
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    for i in range(n):
        qc.cp(2*math.pi*a*2**i/2**n, c, i)

    return qc

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

    qc.compose(qft(n), qubits=k, inplace=True)
    qc.compose(crot(n, a), qubits=[*k, *c], inplace=True)
    qc.compose(qft(n).inverse(), qubits=k, inplace=True)

    return qc
'''


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

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

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

    return qc

def crot(n, a):
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    for i in range(n):
        qc.cp(2*math.pi*a*2**i/2**n, c, i)

    return qc

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

    qc.compose(qft(n), qubits=k, inplace=True)
    qc.compose(crot(n, a), qubits=[*k, *c], inplace=True)
    qc.compose(qft(n).inverse(), qubits=k, inplace=True)

    return qc
'''


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

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

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

    return qc

def crot(n, a):
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    for i in range(n):
        qc.cp(2*math.pi*a*2**i/2**n, c, i)

    return qc

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

    qc.compose(qft(n), qubits=k, inplace=True)
    qc.compose(crot(n, a), qubits=[*k, *c], inplace=True)
    qc.compose(qft(n).inverse(), qubits=k, inplace=True)

    return qc
'''


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

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

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

    return qc

def crot(n, a):
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # Write your code here:
    for i in range(n):
        qc.cp(2*math.pi*a*2**i/2**n, c, i)

    return qc

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

    qc.compose(qft(n), qubits=k, inplace=True)
    qc.compose(crot(n, a), qubits=[*k, *c], inplace=True)
    qc.compose(qft(n).inverse(), qubits=k, inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    for q in range(n):
        current_a_bit = a % 2
        a //=2
        if current_a_bit == 1:
            for i in range(q+1, n):
                qc.mcx([c[0], q] + list(range(i+1, n)), i)
            qc.x(q)
        else:
            assert current_a_bit == 0

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

    for q in range(n):
        current_a_bit = a % 2
        a //=2
        if current_a_bit == 1:
            for i in range(q+1, n):
                qc.mcx([c[0]] + list(range(q, i)), i)
            qc.x(q)
        else:
            assert current_a_bit == 0

    return qc
'''


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

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

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

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

    return qc

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

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

    return qc

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

    qc.compose(qft(n), qubits=k, inplace=True)
    qc.compose(crot(n, a), qubits=[*k, *c], inplace=True)
    qc.compose(qft(n).inverse(), qubits=k, inplace=True)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
from numpy import pi, sqrt, acos

def solve(n, a) -> QuantumCircuit:
	k, c = QuantumRegister(n), QuantumRegister(1)
	qc = QuantumCircuit(k, c)
	vis = [False] * 2**n
	for start in range(2**n):
		if vis[start]:
			continue
		vis[start] = True
		x = start
		while not vis[(x - a) % 2**n]:
			y = (x - a) % 2**n
			vis[y] = True
			to_flip = []
			to_control = []
			to_zero_control = []
			for i in range(n):
				if (x ^ y) >> i & 1:
					to_flip.append(i)
				else:
					to_control.append(i)
					if ~x >> i & 1:
						to_zero_control.append(i)
			for i in to_zero_control:
				qc.x(i)
			for i in to_flip:
				if to_control:
					qc.mcx(to_control, i)
				else:
					qc.x(i)
			for i in to_zero_control:
				qc.x(i)
			x = y
	return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
from numpy import pi, sqrt, acos

def apply_permutation(qc, p, option = "cycle"):
	assert option in ["cycle", "list"]
	n = qc.num_qubits
	cycles = []
	if option == "cycle":
		cycles = p[:]
	elif option == "list":
		assert sorted(p) == list(range(len(p)))
		vis = [False] * len(p)
		for x in range(len(p)):
			if vis[x]:
				continue
			cycle = []
			while not vis[x]:
				vis[x] = True
				cycle.append(x)
				x = p[x]
			cycles.append(cycle)
	else:
		assert False
	for cycle in sorted(cycles, key = lambda c: len(c)):
		def recurse(l, r):
			if r - l <= 1:
				return
			m = l + r >> 1
			recurse(l, m), recurse(m, r)
			x, y = cycle[l], cycle[m]
			def apply_swap(i):
				for j in range(n):
					if j < i and ~x >> j & 1 or j > i and ~y >> j & 1:
						qc.x(j)
				qc.mcx(list(j for j in range(n) if i != j), i)
				for j in range(n):
					if j < i and ~x >> j & 1 or j > i and ~y >> j & 1:
						qc.x(j)
			diff = [i for i in range(n) if (x ^ y) >> i & 1]
			cut = len(diff) >> 1
			for i in range(cut - 1):
				apply_swap(diff[i])
			for i in range(cut + 1, len(diff)):
				apply_swap(diff[i])
			apply_swap(diff[cut])
			for i in range(cut - 1):
				apply_swap(diff[i])
			for i in range(cut + 1, len(diff)):
				apply_swap(diff[i])
		recurse(0, len(cycle))
	return qc

def solve(n, a) -> QuantumCircuit:
	k, c = QuantumRegister(n), QuantumRegister(1)
	qc = QuantumCircuit(k, c)
	perm = [(j << n) + (i + a * j) % 2**n for j in range(2) for i in range(2**n)]
	print(f"{perm = }")
	return apply_permutation(qc, perm, option = "list")
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
from numpy import pi, sqrt, acos

def apply_stateswap(qc, x, y):
	n = qc.num_qubits
	assert x != y
	assert 0 <= min(x, y) and max(x, y) < 2**n
	def apply_bitswap(i):
		for j in range(n):
			if j < i and ~x >> j & 1 or j > i and ~y >> j & 1:
				qc.x(j)
		qc.mcx(list(j for j in range(n) if i != j), i)
		for j in range(n):
			if j < i and ~x >> j & 1 or j > i and ~y >> j & 1:
				qc.x(j)
	diff = [i for i in range(n) if (x ^ y) >> i & 1]
	cut = len(diff) / 2
	for i in range(cut - 1):
		apply_bitswap(diff[i])
	for i in range(cut + 1, len(diff)):
		apply_bitswap(diff[i])
	apply_bitswap(diff[cut])
	for i in range(cut - 1):
		apply_bitswap(diff[i])
	for i in range(cut + 1, len(diff)):
		apply_bitswap(diff[i])
	return qc

def apply_permutation(qc, p, option = "cycle"):
	assert option in ["cycle", "list"]
	n = qc.num_qubits
	cycles = []
	if option == "cycle":
		cycles = p[:]
	elif option == "list":
		assert sorted(p) == list(range(len(p)))
		vis = [False] * len(p)
		for x in range(len(p)):
			if vis[x]:
				continue
			cycle = []
			while not vis[x]:
				vis[x] = True
				cycle.append(x)
				x = p[x]
			cycles.append(cycle)
	else:
		assert False
	for cycle in sorted(cycles, key = lambda c: len(c)):
		def recurse(l, r):
			if r - l <= 1:
				return
			m = l + r >> 1
			recurse(l, m), recurse(m, r)
			apply_stateswap(qc, cycle[l], cycle[m])
		recurse(0, len(cycle))
	return qc

def solve(n, a) -> QuantumCircuit:
	k, c = QuantumRegister(n), QuantumRegister(1)
	qc = QuantumCircuit(k, c)
	perm = [2**n * j + (i + a * j) % 2**n for j in range(2) for i in range(2**n)]
	return apply_permutation(qc, perm, option = "list")
'''