'''python
from qiskit import QuantumCircuit, QuantumRegister
import numpy as np
from qiskit.circuit.library.standard_gates import ZGate, XGate, XXPlusYYGate, RZZGate

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    a, b, c, anc = (
        QuantumRegister(1),
        QuantumRegister(1),
        QuantumRegister(n - 1),
        QuantumRegister(1),
    )

    qc = QuantumCircuit(a, b, c, anc)
    zangle = 2 * np.pi / (2 ** (n - 1))

    qc.x(c)
    qc.x(anc)
    qc.mcx(c, anc)
    qc.x(c)

    if m_left == 0:
        for i in range(n - 1):
            qc.crz(zangle * k_left * (2**i), c[i], a)

    else:
        for i in range(n - 1):
            qc.crz(zangle * k_left * (2**i), c[i], a)

        qc.cx(anc, a)

    qc.x(c)

    # 11
    if (m_left + k_left) % 2 == 1:
        qc.append(ZGate().control(1), [*a, *b])

    qc.x(a)
    # 01
    if (k_left) % 2 == 1:
        qc.append(ZGate().control(1), [*a, *b])

    qc.x(a)
    qc.x(b)
    # 10
    if (m_left) % 2 == 1:
        qc.append(ZGate().control(1), [*a, *b])

    qc.x(b)

    qc.mcx(c, anc)
    qc.x(anc)
    qc.x(c)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import numpy as np
from qiskit.circuit.library.standard_gates import ZGate, XGate, XXPlusYYGate, RZZGate

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    a, b, c, anc = (
        QuantumRegister(1),
        QuantumRegister(1),
        QuantumRegister(n - 1),
        QuantumRegister(1),
    )

    qc = QuantumCircuit(a, b, c, anc)
    zangle = 2 * np.pi / (2 ** (n - 1))

    qc.x(c)
    qc.x(anc)
    qc.mcx(c, anc)
    qc.x(c)

    if m_left == 0:
        for i in range(n - 1):
            qc.crz(zangle * k_left * (2**i), c[i], a)

    else:
        for i in range(n - 1):
            qc.crz(zangle * k_left * (2**i), c[i], a)

        qc.cx(anc, a)

    qc.x(c)

    # 11
    if (m_left + k_left) % 2 == 1:
        qc.append(ZGate().control(2), [*a, *b, *anc])

    qc.x(a)
    # 01
    if (k_left) % 2 == 1:
        qc.append(ZGate().control(2), [*a, *b, *anc])

    qc.x(a)
    qc.x(b)
    # 10
    if (m_left) % 2 == 1:
        qc.append(ZGate().control(2), [*a, *b, *anc])

    qc.x(b)

    qc.mcx(c, anc)
    qc.x(anc)
    qc.x(c)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import numpy as np
from qiskit.circuit.library.standard_gates import ZGate, XGate, XXPlusYYGate, RZZGate

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    a, b, c, anc = (
        QuantumRegister(1),
        QuantumRegister(1),
        QuantumRegister(n - 1),
        QuantumRegister(1),
    )

    qc = QuantumCircuit(a, b, c, anc)
    zangle = 2 * np.pi / (2 ** (n - 1))

    qc.x(c)
    qc.x(anc)
    qc.mcx(c, anc)
    qc.x(c)

    if m_left == 0:
        for i in range(n - 1):
            qc.crz(zangle * k_left * (2**i), c[i], a)

    else:
        for i in range(n - 1):
            qc.crz(zangle * k_left * (2**i), c[i], a)

        qc.cx(anc, a)

    qc.x(anc)

    # 11
    if (m_left + k_left) % 2 == 1:
        qc.append(ZGate().control(2), [*a, *b, *anc])

    qc.x(a)
    # 01
    if (k_left) % 2 == 1:
        qc.append(ZGate().control(2), [*a, *b, *anc])

    qc.x(a)
    qc.x(b)
    # 10
    if (m_left) % 2 == 1:
        qc.append(ZGate().control(2), [*a, *b, *anc])

    qc.x(b)

    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    return qc

## test here
import qutils

cv = qutils.CircuitViewer(solve(3, 1, 1))

cv.draw()
# cv.print_mapping()
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import numpy as np
from qiskit.circuit.library.standard_gates import ZGate, XGate, XXPlusYYGate, RZZGate

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    a, b, c, anc = (
        QuantumRegister(1),
        QuantumRegister(1),
        QuantumRegister(n - 1),
        QuantumRegister(1),
    )

    qc = QuantumCircuit(a, b, c, anc)
    zangle = 2 * np.pi / (2 ** (n - 1))

    qc.x(c)
    qc.x(anc)
    qc.mcx(c, anc)
    qc.x(c)

    if m_left == 0:
        for i in range(n - 1):
            qc.crz(zangle * k_left * (2**i), c[i], a)

    else:
        for i in range(n - 1):
            qc.crz(zangle * k_left * (2**i), c[i], a)

        qc.cx(anc, a)

    qc.x(anc)

    # 11
    if (m_left + k_left) % 2 == 1:
        qc.append(ZGate().control(2), [*a, *b, *anc])

    qc.x(a)
    # 01
    if (k_left) % 2 == 1:
        qc.append(ZGate().control(2), [*a, *b, *anc])

    qc.x(a)
    qc.x(b)
    # 10
    if (m_left) % 2 == 1:
        qc.append(ZGate().control(2), [*a, *b, *anc])

    qc.x(b)

    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    return qc
'''


'''python
import math

from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import XGate, ZGate, RZGate

def A3(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(m)
    qc.x(k)
    qc.compose(XGate().control(len(k)),[*k, m[0]], inplace=True)
    qc.x(k)
    return qc

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    a, b, c = QuantumRegister(1), QuantumRegister(1), QuantumRegister(n - 1)
    qc = QuantumCircuit(a, b, c)
    # Write your code here:
    qc.x(c)
    if m_left:
        qc.compose(ZGate().control(len(c)),[*c, *a])
    if k_left % 2:
        qc.compose(ZGate().control(len(c)),[*c, *b])
    qc.x(c)
    for i in range(n - 1):
        qc.compose(RZGate(2 ** (2 - n + i) * math.pi).control(1),[*c, *a])
    if m_left:
        qc.compose(A3(n - 1) ,[a[0], *c], inplace=True)

    return qc
'''


'''python
import math

from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import XGate, ZGate, RZGate

def A3(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(m)
    qc.x(k)
    qc.compose(XGate().control(len(k)),[*k, m[0]], inplace=True)
    qc.x(k)
    return qc

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    a, b, c = QuantumRegister(1), QuantumRegister(1), QuantumRegister(n - 1)
    qc = QuantumCircuit(a, b, c)
    # Write your code here:
    qc.x(c)
    if m_left:
        qc.compose(ZGate().control(len(c)),[*c, *a])
    if k_left % 2:
        qc.compose(ZGate().control(len(c)),[*c, *b])
    qc.x(c)
    for i in range(n - 1):
        qc.compose(RZGate(2 ** (2 - n + i) * k_left * math.pi).control(1),[*c, *a])
    if m_left:
        qc.compose(A3(n - 1), [a[0], *c], inplace=True)

    return qc
'''


'''python
import math

from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import XGate, ZGate, RZGate

def A3(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(m)
    qc.x(k)
    qc.compose(XGate().control(len(k)),[*k, m[0]], inplace=True)
    qc.x(k)
    return qc

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    a, b, c = QuantumRegister(1), QuantumRegister(1), QuantumRegister(n - 1)
    qc = QuantumCircuit(a, b, c)
    # Write your code here:
    qc.x(c)
    if m_left:
        qc.compose(ZGate().control(len(c)), [*c, a[0]], inplace=True)
    if k_left % 2:
        qc.compose(ZGate().control(len(c)), [*c, b[0]], inplace=True)
    qc.x(c)
    for i in range(n - 1):
        qc.compose(RZGate(2 ** (2 - n + i) * k_left * math.pi).control(1), [c[i], a[0]], inplace=True)
    if m_left:
        qc.compose(A3(n - 1), [a[0], *c], inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:

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

    angle = 2 * math.pi / 2 * m_left
    qc.p(angle, 0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:

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

    angle = 2 * math.pi / 2 * m_left
    qc.p(angle, 0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:

    angle = 2 * math.pi / 2 * m_left
    qc.p(angle, 0)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:

    if m_left == 1:
        qc.x(m[0])

    # 2. kレジスタにk_left加算 or -k_left加算（m制御）
    if k_left != 0:
        # m = 0 のとき加算, m = 1 のとき減算
        # a. m=0: kに+k_left
        qc.mcx([m[0]], k[0])  # m[0]=1のときのみ作用
        for i in range(n):
            if (k_left >> i) & 1:
                qc.cx(m[0], k[i])
        qc.mcx([m[0]], k[0])  # 戻す

        # b. m=1: kに-k_left（= 2^n - k_left加算）
        # (m[0]=1 の時にだけk_leftを差し引く加算を行う)
        for i in range(n):
            if (k_left >> i) & 1:
                qc.cx(m[0], k[i])

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:

    m = n  # mは最上位ビット、kは下位nビット

    # 1. mレジスタのXOR（CNOT）
    if m_left == 1:
        qc.x(m)

    # 2. kレジスタに制御加算・減算
    # まず m==0 のとき加算 (Xで反転して制御→加算→戻す)
    qc.x(m)
    for i in range(n):
        if (k_left >> i) & 1:
            qc.cx(m, i)
    qc.x(m)
    # 次に m==1 のとき減算 (そのまま加算: -k_left = 2^n - k_left)
    if k_left != 0:
        for i in range(n):
            if (k_left >> i) & 1:
                qc.cx(m, i)
    return qc
'''


'''python
from math import ceil,floor,acos,asin,atan,sqrt,pi,gcd,sin,cos,tan,log2
import numpy as np
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

import numpy as np
from qiskit import QuantumCircuit
from qiskit.circuit.library import QFT

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    k_qubits = list(range(n))
    m_qubit = n
    qc.compose(qft(n), k_qubits, inplace=True)

    for i in range(n):
        angle = (2 * np.pi * k_left) / (2**(i + 1))
        if angle != 0:
            qc.p(angle, k_qubits[i])

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

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

    for i in range(n):
        angle = -(2 * np.pi * (2 * k_left)) / (2**(i + 1))
        if angle != 0:
            qc.cp(angle, m_qubit, k_qubits[i])

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

    if m_left == 1:
        qc.x(m_qubit)

    return qc
'''


'''python
from math import ceil,floor,acos,asin,atan,sqrt,pi,gcd,sin,cos,tan,log2
import numpy as np
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, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    k_qubits = list(range(n))
    m_qubit = n
    qc.compose(qft(n), k_qubits, inplace=True)

    for i in range(n):
        angle = (2 * np.pi * k_left) / (2**(i + 1))
        if angle != 0:
            qc.p(angle, k_qubits[i])

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

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

    for i in range(n):
        angle = -(2 * np.pi * (2 * k_left)) / (2**(i + 1))
        if angle != 0:
            qc.cp(angle, m_qubit, k_qubits[i])

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

    if m_left == 1:
        qc.x(m_qubit)

    return qc
'''


'''python
from math import ceil,floor,acos,asin,atan,sqrt,pi,gcd,sin,cos,tan,log2
import numpy as np
from qiskit import QuantumCircuit,QuantumRegister
from qiskit.circuit.library import RzGate

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    if m_left==1:
        qc.x(0)
    t=2 *pi / 2**(n-1)
    qc.rz(t,1)
    # Write your code here:
    for i in range(2:n+1):
        qc.x(0)
    qc.mcx(list(range(2:n+1)),0)
    qrz = RzGate(2*pi - t).control(n-1)
    qc.append(qrz,qc[2:n+1],qc[0:2])
    for i in range(2:n+1):
        qc.x(0)
    return qc
'''


'''python
from math import ceil,floor,acos,asin,atan,sqrt,pi,gcd,sin,cos,tan,log2
import numpy as np
from qiskit import QuantumCircuit,QuantumRegister
from qiskit.circuit.library import RZGate

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    if m_left==1:
        qc.x(0)
    t=2 *pi / 2**(n-1)
    qc.rz(t,1)
    # Write your code here:
    for i in range(2,n+1):
        qc.x(0)
    qc.mcx(list(range(2,n+1)),0)
    qrz = RzGate(2*pi - t).control(n-1)
    qc.append(qrz,qc[2:n+1],qc[0:2])
    for i in range(2,n+1):
        qc.x(0)
    return qc
'''


'''python
from math import ceil,floor,acos,asin,atan,sqrt,pi,gcd,sin,cos,tan,log2
import numpy as np
from qiskit import QuantumCircuit,QuantumRegister
from qiskit.circuit.library import RZGate

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    if m_left==1:
        qc.x(0)
    t=2 *pi / 2**(n-1)
    qc.rz(t,1)
    # Write your code here:
    for i in range(2,n+1):
        qc.x(0)
    qc.mcx(list(range(2,n+1)),0)
    qrz = RZGate(2*pi - t).control(n-1)
    qc.append(qrz,qc[2:n+1],qc[0:2])
    for i in range(2,n+1):
        qc.x(0)
    return qc
'''


'''python
from math import ceil,floor,acos,asin,atan,sqrt,pi,gcd,sin,cos,tan,log2
import numpy as np
from qiskit import QuantumCircuit,QuantumRegister
from qiskit.circuit.library import RZGate

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    if m_left==1:
        qc.x(0)
    t=2 *pi / 2**(n-1)
    qc.rz(t,1)
    # Write your code here:
    for i in range(2,n+1):
        qc.x(0)
    qc.mcx(list(range(2,n+1)),0)
    qrz = RZGate(2*pi - t).control(n-1)
    qc.append(qrz,qc[2:n+1]+qc[0:2])
    for i in range(2,n+1):
        qc.x(0)
    return qc
'''


'''python
from math import ceil,floor,acos,asin,atan,sqrt,pi,gcd,sin,cos,tan,log2
import numpy as np
from qiskit import QuantumCircuit,QuantumRegister
from qiskit.circuit.library import RZGate

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    if m_left==1:
        qc.x(0)
    t=2 *pi *k_left / 2**(n-1)
    qc.rz(t,1)
    # Write your code here:
    for i in range(2,n+1):
        qc.x(0)
    qc.mcx(list(range(2,n+1)),0)
    qrz = RZGate(2*pi - t).control(n-1)
    control_qubits = list(range(2, n + 1))
    target_qubit = 1
    qc.append(qrz, control_qubits + [target_qubit])
    for i in range(2,n+1):
        qc.x(0)
    return qc
'''


'''python
from math import ceil,floor,acos,asin,atan,sqrt,pi,gcd,sin,cos,tan,log2
import numpy as np
from qiskit import QuantumCircuit,QuantumRegister
from qiskit.circuit.library import RZGate

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    if m_left==1:
        qc.x(0)
    for i in range(2,n+1):
        t=2 *pi *k_left * 2**(i-2) / 2**(n-1)
        qc.crz(t,i,1)
    qc.append(qrz, control_qubits + [target_qubit])
    # Write your code here:
    for i in range(2,n+1):
        qc.x(0)
    qc.mcx(list(range(2,n+1)),0)
    qc.append(qrz, control_qubits + [target_qubit])
    for i in range(2,n+1):
        qc.x(0)
    return qc
'''


'''python
from math import ceil,floor,acos,asin,atan,sqrt,pi,gcd,sin,cos,tan,log2
import numpy as np
from qiskit import QuantumCircuit,QuantumRegister
from qiskit.circuit.library import RZGate

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    if m_left==1:
        qc.x(0)
    for i in range(2,n+1):
        t=2 *pi *k_left * 2**(i-2) / 2**(n-1)
        qc.crz(t,i,1)
    qc.append(qrz, control_qubits + [target_qubit])
    # Write your code here:
    for i in range(2,n+1):
        qc.x(0)
    qc.mcx(list(range(2,n+1)),0)
    for i in range(2,n+1):
        qc.x(0)
    return qc
'''


'''python
from math import ceil,floor,acos,asin,atan,sqrt,pi,gcd,sin,cos,tan,log2
import numpy as np
from qiskit import QuantumCircuit,QuantumRegister
from qiskit.circuit.library import RZGate

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    if m_left==1:
        qc.x(0)
    for i in range(2,n+1):
        t=2 *pi *k_left * 2**(i-2) / 2**(n-1)
        qc.crz(t,i,1)
    # Write your code here:
    for i in range(2,n+1):
        qc.x(0)
    qc.mcx(list(range(2,n+1)),0)
    for i in range(2,n+1):
        qc.x(0)
    return qc
'''


'''python
from math import ceil,floor,acos,asin,atan,sqrt,pi,gcd,sin,cos,tan,log2
import numpy as np
from qiskit import QuantumCircuit,QuantumRegister
from qiskit.circuit.library import RZGate

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    if m_left==1:
        qc.x(0)
    for i in range(2,n+1):
        t=2 *pi *k_left * 2**(i-2) / 2**(n-1)
        qc.crz(t,i,1)
    # Write your code here:
    for i in range(2,n+1):
        qc.x(0)
    if m_left==1:
        qc.mcx(list(range(2,n+1)),0)
    for i in range(2,n+1):
        qc.x(0)
    return qc
'''


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

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:

    qc.cx(0, 1)
    # now qubit 1 is exactly m
    qc.cx(1, 0)
    ###

    # rotate qubit 0 by (-1)^m c k_left
    for i in range(n - 1):
        theta = 2 * math.pi * k_left * 2 ** i / 2 ** (n - 1)
        qc.append(RZGate(-theta).control(1), [i + 2, 0])
        qc.append(RZGate(theta * 2).control(2), [i + 2, 1, 0])
    if m_left == 1:
        qc.x(1)

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

    return qc
'''


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

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:

    qc.cx(0, 1)
    # now qubit 1 is exactly m
    qc.cx(1, 0)
    ###

    # rotate qubit 0 by (-1)^m c k_left
    for i in range(n - 1):
        theta = 2 * math.pi * k_left * 2 ** i / 2 ** (n - 1)
        qc.append(RZGate(-theta).control(1), [i + 2, 0])
        qc.append(RZGate(theta * 2).control(2), [i + 2, 1, 0])

    if m_left == 1:
        qc.x(range(2, n + 1))
        qc.append(XGate().control(n - 1), list(range(2, n + 1)) + [1])
        qc.x(range(2, n + 1))

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

    qc.x(range(2, n + 1))
    if m_left == 1:
        qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [0])
    if k_left % 2 == 1:
        qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [1])
    qc.x(range(2, n + 1))

    return qc
'''


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

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:

    qc.cx(0, 1)
    # now qubit 1 is exactly m
    qc.cx(1, 0)
    ###

    # rotate qubit 0 by (-1)^m c k_left
    for i in range(n - 1):
        theta = 2 * math.pi * k_left * 2 ** i / 2 ** (n - 1)
        qc.append(RZGate(theta).control(1), [i + 2, 0])
        qc.append(RZGate(-theta * 2).control(2), [i + 2, 1, 0])

    if m_left == 1:
        qc.x(range(2, n + 1))
        qc.append(XGate().control(n - 1), list(range(2, n + 1)) + [1])
        qc.x(range(2, n + 1))

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

    qc.x(range(2, n + 1))
    if m_left == 1:
        qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [0])
    if k_left % 2 == 1:
        qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [1])
    qc.x(range(2, n + 1))

    return qc
'''


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

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:

    qc.cx(0, 1)
    # now qubit 1 is exactly m
    qc.cx(1, 0)
    ###

    # rotate qubit 0 by (-1)^m c k_left
    for i in range(n - 1):
        theta = 2 * math.pi * k_left * 2 ** i / 2 ** (n - 1)
        qc.append(RZGate(-theta).control(2), [i + 2, 1, 0])
        qc.x(1)
        qc.append(RZGate(theta).control(2), [i + 2, 1, 0])
        qc.x(1)

    if m_left == 1:
        qc.x(range(2, n + 1))
        qc.append(XGate().control(n - 1), list(range(2, n + 1)) + [1])
        qc.x(range(2, n + 1))

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

    qc.x(range(2, n + 1))
    if m_left == 1:
        qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [0])
    if k_left % 2 == 1:
        qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [1])
    qc.x(range(2, n + 1))

    return qc
'''


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

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:

    qc.cx(0, 1)
    # now qubit 1 is exactly m
    qc.cx(1, 0)
    ###

    # rotate qubit 0 by (-1)^m c k_left
    for i in range(n - 1):
        theta = 2 * math.pi * k_left * 2 ** i / 2 ** n
        qc.append(RZGate(-theta).control(2), [i + 2, 1, 0])
        # qc.x(1)
        qc.append(RZGate(theta * 2).control(2), [i + 2, 0])
        # qc.x(1)

    # apply X^m_left
    if m_left == 1:
        qc.x(range(2, n + 1))
        qc.append(XGate().control(n - 1), list(range(2, n + 1)) + [1])
        qc.x(range(2, n + 1))

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

    qc.x(range(2, n + 1))
    if m_left == 1:
        qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [0])
    if k_left % 2 == 1:
        qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [1])
    qc.x(range(2, n + 1))

    return qc
'''


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

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:

    qc.cx(0, 1)
    # now qubit 1 is exactly m
    qc.cx(1, 0)
    ###

    # rotate qubit 0 by (-1)^m c k_left
    for i in range(n - 1):
        theta = 2 * math.pi * k_left * 2 ** i / 2 ** n
        qc.append(RZGate(-theta).control(2), [i + 2, 1, 0])
        # qc.x(1)
        qc.append(RZGate(theta * 2).control(1), [i + 2, 0])
        # qc.x(1)

    # apply X^m_left
    if m_left == 1:
        qc.x(range(2, n + 1))
        qc.append(XGate().control(n - 1), list(range(2, n + 1)) + [1])
        qc.x(range(2, n + 1))

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

    qc.x(range(2, n + 1))
    if m_left == 1:
        qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [0])
    if k_left % 2 == 1:
        qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [1])
    qc.x(range(2, n + 1))

    return qc
'''


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

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:

    qc.cx(0, 1)
    # now qubit 1 is exactly m
    qc.cx(1, 0)
    ###

    # rotate qubit 0 by (-1)^m c k_left
    for i in range(n - 1):
        theta = 2 * math.pi * k_left * 2 ** i / 2 ** n
        qc.append(RZGate(theta).control(2), [i + 2, 1, 0])
        # qc.x(1)
        qc.append(RZGate(-theta * 2).control(1), [i + 2, 0])
        # qc.x(1)

    # apply X^m_left
    if m_left == 1:
        qc.x(range(2, n + 1))
        qc.append(XGate().control(n - 1), list(range(2, n + 1)) + [1])
        qc.x(range(2, n + 1))

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

    qc.x(range(2, n + 1))
    if m_left == 1:
        qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [0])
    if k_left % 2 == 1:
        qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [1])
    qc.x(range(2, n + 1))

    return qc
'''


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

# def B1(n: int, k_const: int) -> QuantumCircuit:
#     qc = QuantumCircuit(n)
#     for i in range(n):
#         qc.append(PhaseGate(2 * pi / (1 << n) * k_const), [i])
#         k_const *= 2
#         k_const %= (1 << n)

#     return qc

# X を通すと回転量が -1 倍
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    N = n - 1
    for i in range(N):
        qc.append(RZGate(2 * pi / (1 << n) * k_left).control(1), [i + 2, 0])
        k_left *= 2
        k_left %= (1 << n)
    if m_left:
        qc.x(0)
        qc.append(XGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
        qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
    if k_left & 1:
        qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])

    return qc
'''


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

# def B1(n: int, k_const: int) -> QuantumCircuit:
#     qc = QuantumCircuit(n)
#     for i in range(n):
#         qc.append(PhaseGate(2 * pi / (1 << n) * k_const), [i])
#         k_const *= 2
#         k_const %= (1 << n)

#     return qc

# X を通すと回転量が -1 倍
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # qc.x(0)
    # qc.x(1)
    # qc.x(2)
    # Write your code here:
    N = n - 1
    k = k_left
    for i in range(N):
        qc.append(RZGate(2 * pi / (1 << N) * k).control(1), [i + 2, 0])
        k *= 2
        k %= (1 << n)
    if m_left:
        qc.x(0)
        qc.append(XGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
        qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
    if k_left & 1:
        qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [1])

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)

    if m_left == 1:
        qc.x(n)

    if k_left != 0:
        angle = 2 * 3.14159265359 / (2**(n-1)) * k_left
        qc.rz(angle, 0)

    for i in range(n):
        if k_left != 0:
            angle = 2 * 3.14159265359 / (2**(n-1)) * k_left
            qc.crz(-2*angle, n, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)

    if m_left == 1:
        qc.x(n)

    if k_left != 0:
        angle = 2 * 3.14159265359 * k_left / (2**n)

        for i in range(n):
            qc.rz(angle / (2**i), i)

        for i in range(n):
            qc.cx(n, i)
            qc.rz(-angle / (2**i), i)
            qc.cx(n, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)

    # Apply X gate to control qubit if m_left = 1
    if m_left == 1:
        qc.x(n)

    # Apply controlled rotation gates if k_left != 0
    if k_left != 0:
        angle = 2 * 3.14159265359 * k_left / (2**n)

        # Apply rotation gates to target qubits
        for i in range(n):
            qc.rz(angle / (2**i), i)

        # Apply controlled rotation gates
        for i in range(n):
            qc.cx(n, i)
            qc.rz(-angle / (2**i), i)
            qc.cx(n, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, m_right: int, k_right: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def _negctrl_x(qc: QuantumCircuit, ctrls, tgt):

    if not ctrls:
        qc.x(tgt)
        return

    for q in ctrls:
        qc.x(q)
    qc.mcx(ctrls, tgt)
    for q in ctrls:
        qc.x(q)

def _negctrl_z(qc: QuantumCircuit, ctrls, tgt):
    if not ctrls:
        qc.z(tgt)
        return

    for q in ctrls:
        qc.x(q)
    qc.h(tgt)
    qc.mcx(ctrls, tgt)
    qc.h(tgt)
    for q in ctrls:
        qc.x(q)

def solve(n: int, m_right: int, k_right: int):

    qc = QuantumCircuit(n + 1)

    a, b = 0, 1
    c_qubits = list(range(2, n + 1))
    base_angle = 2 * math.pi / (1 << (n - 1))

    if m_right & 1:
        qc.x(b)
        _negctrl_x(qc, c_qubits, b)

    k_mod = k_right % (1 << n)
    if k_mod:
        for j, cq in enumerate(c_qubits):
            exponent = (k_mod * (1 << j)) % (1 << n)
            if exponent == 0:
                continue
            angle = exponent * base_angle
            qc.crz(angle, cq, b)

    if m_right & 1:
        _negctrl_z(qc, c_qubits, a)
    if k_right & 1:
        _negctrl_z(qc, c_qubits, b)

    return qc
'''


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

# X を通すと回転量が -1 倍
# X がかかっているかどうかを取得しないといけないのか
# X = 0 なら， R^{k} -> R^{k + kr} or R^{k - kr}
# X = 1 なら， R^{-k} -> R^{-k - kr} or R^{-k + kr}
# |01> と |11> を +kr 倍， |00> と |10> を -kr 倍したい
# 1 bit 目にあるじゃん
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # qc.x(0)
    # qc.x(1)
    # qc.x(2)
    # Write your code here:
    N = n - 1
    k = k_left
    qc.cx(0, 1)
    for i in range(N):
        qc.append(RZGate(2 * pi / (1 << N) * k).control(1), [i + 2, 1])
        k *= 2
        k %= (1 << n)
    if m_left:
        qc.x(0)
        qc.append(XGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
        qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
    if k_left & 1:
        qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [1])

    return qc
'''


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

# X を通すと回転量が -1 倍
# X がかかっているかどうかを取得しないといけないのか
# X = 0 なら， R^{k} -> R^{k + kr} or R^{k - kr}
# X = 1 なら， R^{-k} -> R^{-k - kr} or R^{-k + kr}
# |01> と |11> を +kr 倍， |00> と |10> を -kr 倍したい
# 1 bit 目にあるじゃん
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # qc.x(0)
    # qc.x(1)
    # qc.x(2)
    # Write your code here:
    N = n - 1
    k = k_left
    if m_left:
        k *= -1
    for i in range(N):
        qc.append(RZGate(2 * pi / (1 << N) * k).control(1), [i + 2, 1])
        k *= 2
        k %= (1 << n)
    if m_left:
        qc.x(0)
        qc.append(XGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
        qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
    if k_left & 1:
        qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [1])

    return qc
'''


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

# X を通すと回転量が -1 倍
# X がかかっているかどうかを取得しないといけないのか
# X = 0 なら， R^{k} -> R^{k + kr} or R^{k - kr}
# X = 1 なら， R^{-k} -> R^{-k - kr} or R^{-k + kr}
# Xr = 0 のとき，|01> と |11> を +kr 倍， |00> と |10> を -kr 倍したい
# 1 bit 目にあるじゃん
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # qc.x(0)
    # qc.x(1)
    # qc.x(2)
    # qc.x(3)
    # Write your code here:
    N = n - 1
    k = k_left
    for i in range(N):
        qc.append(RZGate(2 * pi / (1 << N) * k).control(1), [i + 2, 1])
        k *= 2
        k %= (1 << n)
    if m_left:
        qc.x(0)
        qc.append(XGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
        qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
    if k_left & 1:
        qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [1])

    return qc
'''


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

# X を通すと回転量が -1 倍
# X がかかっているかどうかを取得しないといけないのか
# m = 0 なら， R^{k} -> R^{k + kr} or R^{k - kr}
# m = 1 なら， R^{-k} -> R^{-k - kr} or R^{-k + kr}
# mr = 0, m = 0 のとき， -k |00> + k |11>  -->  (-k - kr) |00> + (k + kr) |11>
# mr = 0, m = 1 のとき， -k |10> + k |01>  -->  (k - kr) |10> + (-k + kr) |01>
# 0 bit 目に R を適用，swap(0, 1)

# mr = 1, m = 0 のとき， -k |00> + k |11>  -->  (-k - kr) |10> + (k + kr) |01>
# mr = 1, m = 1 のとき，  k |01> - k |10>  -->  (k - kr) |00> + (-k + kr) |11>
# 0 bit 目に R を適用，swap(0, 1) をして，x(0)
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # qc.x(0)
    # qc.x(1)
    qc.x(2)
    # qc.x(3)
    # Write your code here:
    N = n - 1
    k = k_left
    for i in range(N):
        qc.append(RZGate(2 * pi / (1 << N) * k).control(1), [i + 2, 0])
        k *= 2
        k %= (1 << n)
    qc.swap(0, 1)
    qc.append(SwapGate().control(N, ctrl_state='0' * N), list(range(2, N + 2)) + [0, 1])
    if m_left:
        qc.x(0)
        qc.append(XGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
        qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
    if k_left & 1:
        qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [1])

    return qc
'''


'''python
import math

from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import XGate, ZGate, RZGate

def A3(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(m[0])
    qc.x(k)
    qc.compose(XGate().control(len(k)),[*k, m[0]], inplace=True)
    qc.x(k)
    return qc

def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    a, b, c = QuantumRegister(1), QuantumRegister(1), QuantumRegister(n - 1)
    qc = QuantumCircuit(a, b, c)
    # Write your code here:
    qc.x(c)
    if m_left:
        qc.compose(ZGate().control(len(c)), [*c, a[0]], inplace=True)
    if k_left % 2:
        qc.compose(ZGate().control(len(c)), [*c, b[0]], inplace=True)
    qc.x(c)
    if m_right:
        qc.compose(A3(n - 1), [b[0], *c], inplace=True)
    for i in range(n - 1):
        qc.compose(RZGate(2 ** (2 - n + i) * k_left * math.pi).control(1), [c[i], b[0]], inplace=True)

    return qc
'''


'''python
import math

from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import XGate, ZGate, RZGate

def A3(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(m[0])
    qc.x(k)
    qc.compose(XGate().control(len(k)),[*k, m[0]], inplace=True)
    qc.x(k)
    return qc

def solve(n: int, m_right: int, k_right: int) -> QuantumCircuit:
    a, b, c = QuantumRegister(1), QuantumRegister(1), QuantumRegister(n - 1)
    qc = QuantumCircuit(a, b, c)
    # Write your code here:
    qc.x(c)
    if m_left:
        qc.compose(ZGate().control(len(c)), [*c, a[0]], inplace=True)
    if k_left % 2:
        qc.compose(ZGate().control(len(c)), [*c, b[0]], inplace=True)
    qc.x(c)
    if m_right:
        qc.compose(A3(n - 1), [b[0], *c], inplace=True)
    for i in range(n - 1):
        qc.compose(RZGate(2 ** (2 - n + i) * k_right * math.pi).control(1), [c[i], b[0]], inplace=True)

    return qc
'''


'''python
import math

from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import XGate, ZGate, RZGate

def A3(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
    qc.x(m[0])
    qc.x(k)
    qc.compose(XGate().control(len(k)),[*k, m[0]], inplace=True)
    qc.x(k)
    return qc

def solve(n: int, m_right: int, k_right: int) -> QuantumCircuit:
    a, b, c = QuantumRegister(1), QuantumRegister(1), QuantumRegister(n - 1)
    qc = QuantumCircuit(a, b, c)
    # Write your code here:
    qc.x(c)
    if m_right:
        qc.compose(ZGate().control(len(c)), [*c, a[0]], inplace=True)
    if k_right % 2:
        qc.compose(ZGate().control(len(c)), [*c, b[0]], inplace=True)
    qc.x(c)
    if m_right:
        qc.compose(A3(n - 1), [b[0], *c], inplace=True)
    for i in range(n - 1):
        qc.compose(RZGate(2 ** (2 - n + i) * k_right * math.pi).control(1), [c[i], b[0]], inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import numpy as np
from qiskit.circuit.library.standard_gates import ZGate, XGate, XXPlusYYGate, RZZGate

def solve(n: int, m_right: int, k_right: int) -> QuantumCircuit:
    a, b, c, anc = (
        QuantumRegister(1),
        QuantumRegister(1),
        QuantumRegister(n - 1),
        QuantumRegister(1),
    )

    qc = QuantumCircuit(a, b, c, anc)
    zangle = 2 * np.pi / (2 ** (n - 1))

    qc.x(c)
    qc.x(anc)
    qc.mcx(c, anc)
    qc.x(c)

    if m_right == 0:
        for i in range(n - 1):
            qc.crz(zangle * k_right * (2**i), c[i], a)

    else:
        qc.cx(anc, a)

        for i in range(n - 1):
            qc.crz(zangle * k_right * (2**i), c[i], a)

    qc.x(anc)

    qc.h([*a, *b])
    if m_right:
        qc.cx(anc, a)

    if k_right % 2 == 1:
        qc.cx(anc, b)

    qc.h([*a, *b])

    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    return qc
'''


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

def solve(n: int, m_right: int, k_right: int) -> QuantumCircuit:
    a, b, c, anc = (
        QuantumRegister(1),
        QuantumRegister(1),
        QuantumRegister(n - 1),
        QuantumRegister(1),
    )

    qc = QuantumCircuit(a, b, c, anc)
    zangle = 2 * np.pi / (2 ** (n - 1))

    # Ancilla (|anc> = |c=0>)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    # c = 0
    if m_right:
        qc.cz(anc, a)

    if k_right % 2 == 1:
        qc.cz(anc, b)

    # c != 0
    qc.x(anc)
    for i in range(n - 1):
        qc.crz(zangle * k_right * (2**i), c[i], b)

    if m_right == 1:
        qc.cx(anc, b)

    # Uncompute
    qc.x(anc)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    return qc
'''


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

def solve(n: int, m_right: int, k_right: int) -> QuantumCircuit:
    a, b, c, anc = (
        QuantumRegister(1),
        QuantumRegister(1),
        QuantumRegister(n - 1),
        QuantumRegister(1),
    )

    qc = QuantumCircuit(a, b, c, anc)
    zangle = 2 * np.pi / (2 ** (n - 1))

    # Ancilla (|anc> = |c=0>)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    # c = 0
    if m_right:
        qc.cz(anc, a)

    if k_right % 2 == 1:
        qc.cz(anc, b)

    # c != 0
    qc.x(anc)
    if m_right == 1:
        qc.cx(anc, b)

    for i in range(n - 1):
        qc.crz(-zangle * k_right * (2**i), c[i], b)

    # Uncompute
    qc.x(anc)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    return qc
'''


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

def solve(n: int, m_right: int, k_right: int) -> QuantumCircuit:
    a, b, c, anc = (
        QuantumRegister(1),
        QuantumRegister(1),
        QuantumRegister(n - 1),
        QuantumRegister(1),
    )

    qc = QuantumCircuit(a, b, c, anc)
    zangle = 2 * np.pi / (2 ** (n - 1))

    # Ancilla (|anc> = |c=0>)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    # c = 0
    if m_right:
        qc.cz(anc, a)

    if k_right % 2 == 1:
        qc.cz(anc, b)

    # c != 0
    qc.x(anc)
    if m_right == 1:
        qc.cx(anc, b)

    sign = -1 if m_right == 0 else 1
    for i in range(n - 1):
        qc.crz(sign * zangle * k_right * (2**i), c[i], b)

    # Uncompute
    qc.x(anc)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    return qc
'''


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

def solve(n: int, m_right: int, k_right: int) -> QuantumCircuit:
    a, b, c, anc = (
        QuantumRegister(1),
        QuantumRegister(1),
        QuantumRegister(n - 1),
        QuantumRegister(1),
    )

    qc = QuantumCircuit(a, b, c, anc)
    zangle = 2 * np.pi / (2 ** (n - 1))

    # Ancilla (|anc> = |c=0>)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    # c = 0
    if m_right:
        qc.cz(anc, a)

    if k_right % 2 == 1:
        qc.cz(anc, b)

    # c != 0
    qc.x(anc)
    if m_right == 1:
        qc.cx(anc, b)

    sign = 1 if m_right == 0 else -1
    for i in range(n - 1):
        qc.crz(sign * zangle * k_right * (2**i), c[i], b)

    # Uncompute
    qc.x(anc)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    return qc
'''


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

def solve(n: int, m_right: int, k_right: int) -> QuantumCircuit:
    a, b, c, anc = (
        QuantumRegister(1),
        QuantumRegister(1),
        QuantumRegister(n - 1),
        QuantumRegister(1),
    )

    qc = QuantumCircuit(a, b, c, anc)
    zangle = 2 * np.pi / (2 ** (n - 1))

    # Ancilla (|anc> = |c=0>)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    # c = 0
    if m_right:
        qc.cz(anc, a)

    if k_right % 2 == 1:
        qc.cz(anc, b)

    # c != 0
    qc.x(anc)

    sign = -1 if m_right == 0 else 1
    for i in range(n - 1):
        qc.crz(sign * zangle * k_right * (2**i), c[i], b)

    if m_right == 1:
        qc.cx(anc, b)

    # Uncompute
    qc.x(anc)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    return qc
'''


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

def solve(n: int, m_right: int, k_right: int) -> QuantumCircuit:
    a, b, c, anc = (
        QuantumRegister(1),
        QuantumRegister(1),
        QuantumRegister(n - 1),
        QuantumRegister(1),
    )

    qc = QuantumCircuit(a, b, c, anc)
    zangle = 2 * np.pi / (2 ** (n - 1))

    # Ancilla (|anc> = |c=0>)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    # c = 0
    if m_right:
        qc.cz(anc, a)

    if k_right % 2 == 1:
        qc.cz(anc, b)

    # c != 0
    qc.x(anc)

    if m_right == 1:
        qc.cx(anc, b)

    for i in range(n - 1):
        qc.crz(-zangle * k_right * (2**i), c[i], b)

    # Uncompute
    qc.x(anc)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    return qc
'''


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

def solve(n: int, m_right: int, k_right: int) -> QuantumCircuit:
    a, b, c, anc = (
        QuantumRegister(1),
        QuantumRegister(1),
        QuantumRegister(n - 1),
        QuantumRegister(1),
    )

    qc = QuantumCircuit(a, b, c, anc)
    zangle = 2 * np.pi / (2 ** (n - 1))

    # Ancilla (|anc> = |c=0>)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    # c = 0
    if m_right:
        qc.cz(anc, a)

    if k_right % 2 == 1:
        qc.cz(anc, b)

    # c != 0
    qc.x(anc)

    if m_right == 1:
        qc.cx(anc, b)

    for i in range(n - 1):
        qc.crz(zangle * k_right * (2**i), c[i], b)

    # Uncompute
    qc.x(anc)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    return qc
'''


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

def b2(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    a, b, c, anc = (
        QuantumRegister(1),
        QuantumRegister(1),
        QuantumRegister(n - 1),
        QuantumRegister(1),
    )

    qc = QuantumCircuit(a, b, c, anc)
    zangle = 2 * np.pi / (2 ** (n - 1))

    # Ancilla (|anc> = |c=0>)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    # c = 0
    if m_left:
        qc.cz(anc, a)

    if k_left % 2 == 1:
        qc.cz(anc, b)

    # c != 0
    qc.x(anc)
    for i in range(n - 1):
        qc.crz(zangle * k_left * (2**i), c[i], a)

    if m_left == 1:
        qc.cx(anc, a)

    # Uncompute
    qc.x(anc)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    return qc

def b3(n: int, m_right: int, k_right: int) -> QuantumCircuit:
    a, b, c, anc = (
        QuantumRegister(1),
        QuantumRegister(1),
        QuantumRegister(n - 1),
        QuantumRegister(1),
    )

    qc = QuantumCircuit(a, b, c, anc)
    zangle = 2 * np.pi / (2 ** (n - 1))

    # Ancilla (|anc> = |c=0>)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    # c = 0
    if m_right:
        qc.cz(anc, a)

    if k_right % 2 == 1:
        qc.cz(anc, b)

    # c != 0
    qc.x(anc)

    if m_right == 1:
        qc.cx(anc, b)

    for i in range(n - 1):
        qc.crz(zangle * k_right * (2**i), c[i], b)

    # Uncompute
    qc.x(anc)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    return qc

def shift(n):
    qc = QuantumCircuit(n)

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

    return qc

def cneg(n):
    control, x = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(control, x)
    qc.cx(control, x)

    for idx in reversed(range(n)):
        qc.mcx([control] + x[:idx], x[idx])

    return qc

def qft(n):
    qc = QuantumCircuit(n)

    thetas = []
    for k in range(0, 1 + n):
        thetas.append(2 * np.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 mk_to_emk(n):
    m, k, anc = QuantumRegister(1), QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(m, k, anc)

    qc.compose(qft(n), k, inplace=True)
    qc.compose(shift(n), k, inplace=True)
    qc.compose(cneg(n), k, inplace=True)

    qc.x(k[1:])
    qc.mcx(k[1:], anc)
    qc.x(k[1:])

    # c=0
    qc.ch(anc, m)

    qc.x(k[1:])
    # c>0
    qc.cx([*anc, k[0]], m)

    qc.mcx(k[1:], anc)
    qc.x(k[1:])

    return qc

def solve(
    n: int, m_before: int, k_before: int, m_after: int, k_after: int
) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)

    qc.compose(mk_to_emk(n), inplace=True)
    qc.compose(b3(n, m_before, k_before), inplace=True)
    qc.compose(b2(n, m_after, k_after), inplace=True)
    qc.compose(mk_to_emk(n).inverse(), inplace=True)

    return qc
'''


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

def b2(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    a, b, c, anc = (
        QuantumRegister(1),
        QuantumRegister(1),
        QuantumRegister(n - 1),
        QuantumRegister(1),
    )

    qc = QuantumCircuit(a, b, c, anc)
    zangle = 2 * np.pi / (2 ** (n - 1))

    # Ancilla (|anc> = |c=0>)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    # c = 0
    if m_left:
        qc.cz(anc, a)

    if k_left % 2 == 1:
        qc.cz(anc, b)

    # c != 0
    qc.x(anc)
    for i in range(n - 1):
        qc.crz(zangle * k_left * (2**i), c[i], a)

    if m_left:
        qc.cx(anc, a)

    # Uncompute
    qc.x(anc)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    return qc

def b3(n: int, m_right: int, k_right: int) -> QuantumCircuit:
    a, b, c, anc = (
        QuantumRegister(1),
        QuantumRegister(1),
        QuantumRegister(n - 1),
        QuantumRegister(1),
    )

    qc = QuantumCircuit(a, b, c, anc)
    zangle = 2 * np.pi / (2 ** (n - 1))

    # Ancilla (|anc> = |c=0>)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    # c = 0
    if m_right:
        qc.cz(anc, a)

    if k_right % 2 == 1:
        qc.cz(anc, b)

    # c != 0
    qc.x(anc)

    if m_right:
        qc.cx(anc, b)

    for i in range(n - 1):
        qc.crz(zangle * k_right * (2**i), c[i], b)

    # Uncompute
    qc.x(anc)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    return qc

def shift(n):
    qc = QuantumCircuit(n)

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

    return qc

def cneg(n):
    control, x = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(control, x)
    qc.cx(control, x)

    for idx in reversed(range(n)):
        qc.mcx([control] + x[:idx], x[idx])

    return qc

def qft(n):
    qc = QuantumCircuit(n)

    thetas = []
    for k in range(0, 1 + n):
        thetas.append(2 * np.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 mk_to_emk(n):
    m, k, anc = QuantumRegister(1), QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(m, k, anc)

    qc.compose(qft(n), k, inplace=True)
    qc.compose(shift(n), k, inplace=True)
    qc.compose(cneg(n - 1), k, inplace=True)

    qc.x(k[1:])
    qc.mcx(k[1:], anc)
    qc.x(k[1:])

    # c=0
    qc.ch(anc, m)

    qc.x(anc)
    # c>0
    qc.mcx([*anc, k[0]], m)

    qc.x(k[1:])
    qc.x(anc)
    qc.mcx(k[1:], anc)
    qc.x(k[1:])

    return qc

def solve(
    n: int, m_before: int, k_before: int, m_after: int, k_after: int
) -> QuantumCircuit:
    a, b, c, anc = (
        QuantumRegister(1),
        QuantumRegister(1),
        QuantumRegister(n - 1),
        QuantumRegister(1),
    )
    qc = QuantumCircuit(a, b, c, anc)

    qc.compose(mk_to_emk(n), inplace=True)
    qc.compose(b3(n, m_before, k_before), inplace=True)
    qc.compose(b2(n, m_after, k_after), inplace=True)
    qc.compose(mk_to_emk(n).inverse(), inplace=True)

    return qc
'''


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

def b2(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    a, b, c, anc = (
        QuantumRegister(1),
        QuantumRegister(1),
        QuantumRegister(n - 1),
        QuantumRegister(1),
    )

    qc = QuantumCircuit(a, b, c, anc)
    zangle = 2 * np.pi / (2 ** (n - 1))

    # Ancilla (|anc> = |c=0>)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    # c = 0
    if m_left:
        qc.cz(anc, a)

    if k_left % 2 == 1:
        qc.cz(anc, b)

    # c != 0
    qc.x(anc)
    for i in range(n - 1):
        qc.crz(zangle * k_left * (2**i), c[i], a)

    if m_left:
        qc.cx(anc, a)

    # Uncompute
    qc.x(anc)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    return qc

def b3(n: int, m_right: int, k_right: int) -> QuantumCircuit:
    a, b, c, anc = (
        QuantumRegister(1),
        QuantumRegister(1),
        QuantumRegister(n - 1),
        QuantumRegister(1),
    )

    qc = QuantumCircuit(a, b, c, anc)
    zangle = 2 * np.pi / (2 ** (n - 1))

    # Ancilla (|anc> = |c=0>)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    # c = 0
    if m_right:
        qc.cz(anc, a)

    if k_right % 2 == 1:
        qc.cz(anc, b)

    # c != 0
    qc.x(anc)

    if m_right:
        qc.cx(anc, b)

    for i in range(n - 1):
        qc.crz(zangle * k_right * (2**i), c[i], b)

    # Uncompute
    qc.x(anc)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    return qc

def shift(n):
    qc = QuantumCircuit(n)

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

    return qc

def cneg(n):
    control, x = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(control, x)
    qc.cx(control, x)

    for idx in reversed(range(n)):
        qc.mcx([control] + x[:idx], x[idx])

    return qc

def qft(n):
    qc = QuantumCircuit(n)

    thetas = []
    for k in range(0, 1 + n):
        thetas.append(2 * np.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 mk_to_emk(n):
    m, k, anc = QuantumRegister(1), QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(m, k, anc)

    qc.compose(qft(n), k, inplace=True)
    qc.compose(shift(n), k, inplace=True)
    qc.compose(cneg(n - 1), k, inplace=True)
    qc.x(k[0])

    qc.x(k[1:])
    qc.mcx(k[1:], anc)
    qc.x(k[1:])

    # c=0
    qc.ch(anc, m)

    qc.x(anc)
    # c>0
    qc.mcx([*anc, k[0]], m)

    qc.x(k[1:])
    qc.x(anc)
    qc.mcx(k[1:], anc)
    qc.x(k[1:])

    return qc
'''


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

def b2(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    a, b, c, anc = (
        QuantumRegister(1),
        QuantumRegister(1),
        QuantumRegister(n - 1),
        QuantumRegister(1),
    )

    qc = QuantumCircuit(a, b, c, anc)
    zangle = 2 * np.pi / (2 ** (n - 1))

    # Ancilla (|anc> = |c=0>)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    # c = 0
    if m_left:
        qc.cz(anc, a)

    if k_left % 2 == 1:
        qc.cz(anc, b)

    # c != 0
    qc.x(anc)
    for i in range(n - 1):
        qc.crz(zangle * k_left * (2**i), c[i], a)

    if m_left:
        qc.cx(anc, a)

    # Uncompute
    qc.x(anc)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    return qc

def b3(n: int, m_right: int, k_right: int) -> QuantumCircuit:
    a, b, c, anc = (
        QuantumRegister(1),
        QuantumRegister(1),
        QuantumRegister(n - 1),
        QuantumRegister(1),
    )

    qc = QuantumCircuit(a, b, c, anc)
    zangle = 2 * np.pi / (2 ** (n - 1))

    # Ancilla (|anc> = |c=0>)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    # c = 0
    if m_right:
        qc.cz(anc, a)

    if k_right % 2 == 1:
        qc.cz(anc, b)

    # c != 0
    qc.x(anc)

    if m_right:
        qc.cx(anc, b)

    for i in range(n - 1):
        qc.crz(zangle * k_right * (2**i), c[i], b)

    # Uncompute
    qc.x(anc)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    return qc

def shift(n):
    qc = QuantumCircuit(n)

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

    return qc

def cneg(n):
    control, x = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(control, x)
    qc.cx(control, x)

    for idx in reversed(range(n)):
        qc.mcx([control] + x[:idx], x[idx])

    return qc

def qft(n):
    qc = QuantumCircuit(n)

    thetas = []
    for k in range(0, 1 + n):
        thetas.append(2 * np.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 mk_to_emk(n):
    m, k, anc = QuantumRegister(1), QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(m, k, anc)

    qc.compose(qft(n), k, inplace=True)
    qc.compose(shift(n), k, inplace=True)
    qc.compose(cneg(n - 1), k, inplace=True)
    qc.x(k[0])

    qc.x(k[1:])
    qc.mcx(k[1:], anc)
    qc.x(k[1:])

    # c=0
    qc.ch(anc, m)

    qc.x(anc)
    # c>0
    qc.mcx([*anc, k[0]], m)

    qc.x(k[1:])
    qc.x(anc)
    qc.mcx(k[1:], anc)
    qc.x(k[1:])

    return qc

def solve(
    n: int, m_before: int, k_before: int, m_after: int, k_after: int
) -> QuantumCircuit:
    m, k, anc = (
        QuantumRegister(1),
        QuantumRegister(n),
        QuantumRegister(1),
    )
    qc = QuantumCircuit(m, k, anc)

    qc.compose(mk_to_emk(n), inplace=True)
    qc.compose(b3(n, m_before, k_before), inplace=True)
    qc.compose(b2(n, m_after, k_after), inplace=True)
    qc.compose(mk_to_emk(n).inverse(), inplace=True)

    return qc
'''


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

def b2(n: int, m_left: int, k_left: int) -> QuantumCircuit:
    a, b, c, anc = (
        QuantumRegister(1),
        QuantumRegister(1),
        QuantumRegister(n - 1),
        QuantumRegister(1),
    )

    qc = QuantumCircuit(a, b, c, anc)
    zangle = 2 * np.pi / (2 ** (n - 1))

    # Ancilla (|anc> = |c=0>)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    # c = 0
    if m_left:
        qc.cz(anc, a)

    if k_left % 2 == 1:
        qc.cz(anc, b)

    # c != 0
    qc.x(anc)
    for i in range(n - 1):
        qc.crz(zangle * k_left * (2**i), c[i], a)

    if m_left:
        qc.cx(anc, a)

    # Uncompute
    qc.x(anc)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    return qc

def b3(n: int, m_right: int, k_right: int) -> QuantumCircuit:
    a, b, c, anc = (
        QuantumRegister(1),
        QuantumRegister(1),
        QuantumRegister(n - 1),
        QuantumRegister(1),
    )

    qc = QuantumCircuit(a, b, c, anc)
    zangle = 2 * np.pi / (2 ** (n - 1))

    # Ancilla (|anc> = |c=0>)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    # c = 0
    if m_right:
        qc.cz(anc, a)

    if k_right % 2 == 1:
        qc.cz(anc, b)

    # c != 0
    qc.x(anc)

    if m_right:
        qc.cx(anc, b)

    for i in range(n - 1):
        qc.crz(zangle * k_right * (2**i), c[i], b)

    # Uncompute
    qc.x(anc)
    qc.x(c)
    qc.mcx(c, anc)
    qc.x(c)

    return qc

def shift(n):
    qc = QuantumCircuit(n)

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

    return qc

def cneg(n):
    control, x = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(control, x)
    qc.cx(control, x)

    for idx in reversed(range(n)):
        qc.mcx([control] + x[:idx], x[idx])

    return qc

def qft(n):
    qc = QuantumCircuit(n)

    thetas = []
    for k in range(0, 1 + n):
        thetas.append(2 * np.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 mk_to_emk(n):
    m, k, anc = QuantumRegister(1), QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(m, k, anc)

    qc.compose(qft(n), k, inplace=True)
    qc.compose(shift(n), k, inplace=True)
    qc.compose(cneg(n - 1), k, inplace=True)

    qc.x(k[1:])
    qc.mcx(k[1:], anc)
    qc.x(k[1:])

    # c=0
    qc.ch(anc, m)

    qc.x(anc)
    # c>0
    qc.cx(anc, k[0])
    qc.mcx([*anc, k[0]], m)

    qc.x(k[1:])
    qc.x(anc)
    qc.mcx(k[1:], anc)
    qc.x(k[1:])

    return qc

def solve(
    n: int, m_before: int, k_before: int, m_after: int, k_after: int
) -> QuantumCircuit:
    m, k, anc = (
        QuantumRegister(1),
        QuantumRegister(n),
        QuantumRegister(1),
    )
    qc = QuantumCircuit(m, k, anc)

    qc.compose(mk_to_emk(n), inplace=True)
    qc.compose(b3(n, m_before, k_before), inplace=True)
    qc.compose(b2(n, m_after, k_after), inplace=True)
    qc.compose(mk_to_emk(n).inverse(), inplace=True)

    return qc
'''