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


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

# Adds about 3n circuit depth
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_bitflip(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):
		apply_bitflip(diff[i])
	for i in range(cut + 1, len(diff)):
		apply_bitflip(diff[i])
	apply_bitflip(diff[cut])
	for i in range(cut):
		apply_bitflip(diff[i])
	for i in range(cut + 1, len(diff)):
		apply_bitflip(diff[i])
	return qc

# Adds about 3 * n * 2**n circuit depth
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
	ops = 2
	for cycle in cycles:
		for i in reversed(range(len(cycle) - 1)):
			apply_stateswap(qc, cycle[i], cycle[i + 1])
	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")
'''


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

# Adds about 3n circuit depth
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_bitflip(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):
		apply_bitflip(diff[i])
	for i in range(cut + 1, len(diff)):
		apply_bitflip(diff[i])
	apply_bitflip(diff[cut])
	for i in range(cut):
		apply_bitflip(diff[i])
	for i in range(cut + 1, len(diff)):
		apply_bitflip(diff[i])
	return qc

# Adds about 3 * n * 2**n circuit depth
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
	ops = 2
	for cycle in cycles:
		for i in range(len(cycle) - 1):
			apply_stateswap(qc, cycle[i], cycle[i + 1])
	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")
'''


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

# Adds about 3n circuit depth
def apply_stateswap(qc, x, y):
	n = qc.num_qubits
	assert 0 <= min(x, y) and max(x, y) < 2**n
	if x == y:
		return qc
	def apply_bitflip(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):
		apply_bitflip(diff[i])
	for i in reversed(range(cut + 1, len(diff))):
		apply_bitflip(diff[i])
	apply_bitflip(diff[cut])
	for i in reversed(range(cut)):
		apply_bitflip(diff[i])
	for i in range(cut + 1, len(diff)):
		apply_bitflip(diff[i])
	return qc

# Adds about 3 * n * 2**n circuit depth
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
	ops = 2
	for cycle in cycles:
		for i in reversed(range(len(cycle) - 1)):
			apply_stateswap(qc, cycle[i], cycle[i + 1])
	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")
'''


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

# Adds up to 6n circuit depth
def apply_stateswap(qc, x, y):
	n = qc.num_qubits
	assert 0 <= min(x, y) and max(x, y) < 2**n
	if x == y:
		return qc
	def apply_bitflip(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]
	for i in diff:
		apply_bitflip(i)
	for i in reversed(range(diff[:-1])):
		apply_bitflip(diff[i])
	return qc

# Adds up to 4 * n * 2**n circuit depth
# Requires apply_stateswap
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
	ops = 2
	for cycle in cycles:
		for i in reversed(range(len(cycle) - 1)):
			apply_stateswap(qc, cycle[i], cycle[i + 1])
	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")
'''


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

# Adds up to 6n circuit depth
def apply_stateswap(qc, x, y):
	n = qc.num_qubits
	assert 0 <= min(x, y) and max(x, y) < 2**n
	if x == y:
		return qc
	diff = [i for i in range(n) if (x ^ y) >> i & 1]
	for i in diff + list(reversed(diff[:-1])):
		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)
	return qc

# Adds up to 4 * n * 2**n circuit depth
# Requires apply_stateswap
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
	ops = 2
	for cycle in cycles:
		for i in reversed(range(len(cycle) - 1)):
			apply_stateswap(qc, cycle[i], cycle[i + 1])
	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")
'''


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

# Adds up to 4 * n circuit depth
def apply_stateswap(qc, x, y):
	n = qc.num_qubits
	assert 0 <= min(x, y) and max(x, y) < 2**n
	if x == y:
		return qc
	diff = [i for i in range(n) if (x ^ y) >> i & 1]
	last_mask = 0
	for i in diff + list(reversed(diff[:-1])):
		cur_mask = ~x & (2**i - 1) | ~y & (2**n - 2**(i + 1))
		for j in range(n):
			if (last_mask ^ cur_mask) >> j & 1:
				qc.x(j)
		qc.mcx(list(j for j in range(n) if i != j), i)
		last_mask = cur_mask
	for j in range(n):
		if last_mask >> j & 1:
			qc.x(j)
	return qc

# Adds up to 4 * n * 2**n circuit depth
# Requires apply_stateswap
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
	ops = 2
	for cycle in cycles:
		for i in reversed(range(len(cycle) - 1)):
			apply_stateswap(qc, cycle[i], cycle[i + 1])
	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")
'''


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

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

"""

def addition(n: int, a: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.append(QFT(n), range(n))
    for i in range(n):
        qc.append(PhaseGate(2*pi*a*(1/2)**(n-i)), [i])
    qc.append(QFT(n).inverse(), range(n))
    return qc

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

    qc.append(QFT(n).control(1), [n, *range(n)])
    for i in range(n):
        qc.append(PhaseGate(2*pi*a*(1/2)**(n-i)).control(1), [n, i])
    qc.append(QFT(n,inverse=True).control(1), [n, *range(n)])

    return qc
'''


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

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

"""

def QFT(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n//2):
        qc.swap(i, n-i-1)
    for i in range(n):
        qc.h(i)
        for j in range(i+1, n):
            qc.append(CU1Gate(pi/2**(j-i)), [j, i])
    return qc

def addition(n: int, a: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.compose(QFT(n), range(n), inplace=True)
    for i in range(n):
        qc.append(PhaseGate(2*pi*a*(1/2)**(n-i)), [i])
    qc.compose(QFT(n).inverse(), range(n), inplace=True)
    return qc

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

    qc.append(QFT(n).control(1), [n, *range(n)])
    for i in range(n):
        qc.append(PhaseGate(2*pi*a*(1/2)**(n-i)).control(1), [n, i])
    qc.append(QFT(n).inverse().control(1), [n, *range(n)])

    return qc.decompose(reps=2)
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

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

def iqft(qc: QuantumCircuit, qubits):
    n = len(qubits)
    for j in reversed(range(n)):
        for k in reversed(range(j+1, n)):
            qc.cp(-math.pi/2**(k - j), qubits[k], 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:
    qft(qc, k)

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

    iqft(qc, k)

    return qc
'''


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

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

def iqft(qc: QuantumCircuit, qubits):
    n = len(qubits)
    for j in reversed(range(n)):
        for k in reversed(range(j+1, n)):
            qc.cp(-math.pi/2**(k - j), qubits[k], 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:
    qft(qc, k)

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

    iqft(qc, k)

    return qc
'''


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

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

def iqft(qc: QuantumCircuit, qubits):
    n = len(qubits)
    for j in reversed(range(n)):
        for k in range(n-1, j, -1):
            qc.cp(-math.pi/2**(k - j), qubits[k], 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:
    qft(qc, k)

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

    iqft(qc, k)

    return qc
'''


'''python
import math

from qiskit import QuantumCircuit

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

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

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

    return qc

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)

    # when k_{n-1} = 1
    qc.compose(cadd(n, 2 ** (n + 1) - t), qubits=range(n + 1), inplace=True)

    # when k_{n-1} = 0
    qc.x(n)
    qc.compose(cadd(n, -s), qubits=range(n + 1), inplace=True)
    qc.x(n)

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

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    qc.x(n)
    qc.compose(add_constant_controlled(n, (2**n - s)), qubits=range(n + 1), inplace=True)
    qc.x(n)
    qc.compose(add_constant_controlled(n, (2**(n+1)-t)), qubits=range(n + 1), inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    for i in range(n): qc.cx(i, n)
    for i in range(n):
        qc.ccx(i, n, n + 1)
        qc.cx(i, n + 1)
        qc.ccx(i, n, n + 1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    qc.cx(range(n), n)
    qc.ccx(range(n), n, n + 1)
    qc.cx(range(n), n + 1)
    qc.ccx(range(n), n, n + 1)

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

def oracle_less(bitcount: int, n: int) -> QuantumCircuit:
    regin, regout = QuantumRegister(bitcount), QuantumRegister(1)
    qc = QuantumCircuit(regin, regout)

    if n >= 2**bitcount:
        qc.x(regout)
        return qc

    bitrep = bin(n)[2:].zfill(bitcount)[::-1]
    masks = []

    for idx in range(bitcount):
        if bitrep[idx] == "1":
            mask = ("0" + bitrep[idx + 1 :]).rjust(bitcount, "*")
            masks.append(mask)

    flipped = [False] * bitcount
    for mask in masks:
        controls = []
        for idx in range(bitcount):
            if mask[idx] == "*":
                continue

            controls.append(regin[idx])
            if (mask[idx] == "0") != (flipped[idx]):
                flipped[idx] = not flipped[idx]
                qc.x(regin[idx])

        if len(controls) != 0:
            qc.mcx(controls, regout)

    for idx in range(bitcount):
        if flipped[idx]:
            qc.x(regin[idx])

    return qc

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    regin = QuantumRegister(n + 1)
    qc = QuantumCircuit(regin)
    # Write your code here:

    anc_range = QuantumRegister(1)
    anc_pred = QuantumRegister(1)
    qc.add_register(anc_range)
    qc.add_register(anc_pred)

    # case 1
    qc.compose(oracle_less(n+1, s), [*regin, *anc_range], inplace=True)
    qc.x(regin[-1])
    qc.x(anc_range)
    qc.ccx(regin[-1], anc_range, anc_pred)
    qc.compose(controlled_add(n, -s), [*regin[:-1], *anc_pred], inplace=True)
    qc.ccx(regin[-1], anc_range, anc_pred)
    qc.x(anc_range)
    qc.x(regin[-1])

    # case 2
    qc.compose(oracle_less(n+1, t), [*regin, *anc_range], inplace=True)
    qc.ccx(regin[-1], anc_range, anc_pred)
    qc.compose(controlled_add(n+1, 2**(n+1)-t), [*regin, *anc_pred], inplace=True)
    qc.ccx(regin[-1], anc_range, anc_pred)
    qc.x(anc_range)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # 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 * s * 2 ** (i - n), n, i, ctrl_state=1)
        qc.cp(-2 * math.pi * t * 2 ** (i - n), n, i, ctrl_state=0)

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

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # 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 * s * 2 ** (i - n), n, i, ctrl_state=1)
        qc.cp(-2 * math.pi * t * 2 ** (i - n), n, i, ctrl_state=0)

    # 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
import mat

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # 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 * s * 2 ** (i - n), n, i, ctrl_state=0)
        qc.cp(-2 * math.pi * t * 2 ** (i - n), n, i, ctrl_state=1)

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

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # 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 * s * 2 ** (i - n), n, i, ctrl_state=0)
        qc.cp(-2 * math.pi * t * 2 ** (i - n), n, i, ctrl_state=1)

    # 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

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, s: int, t: int) -> QuantumCircuit:

    qc = QuantumCircuit(n + 1)
    # Write your code here:
    QFT(qc, list(range(n))):

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

    qc.x(n)

    for i in range(n):
        theta = 2 * math.pi * (-s) * (2 ** i) / (2 ** n)

    qc.x(n)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

def QFT(qc : QuantumCircuit, idx : list[int], 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, s: int, t: int) -> QuantumCircuit:

    qc = QuantumCircuit(n + 1)
    # Write your code here:
    QFT(qc, list(range(n))):

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

    qc.x(n)

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

    qc.x(n)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

def QFT(qc : QuantumCircuit, idx : list[int], 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, s: int, t: int) -> QuantumCircuit:

    qc = QuantumCircuit(n + 1)
    # Write your code here:
    QFT(qc, list(range(n)))

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

    qc.x(n)

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

    qc.x(n)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)

    def add(qc, n, a):
        for i in range(n):
            if (a & (1 << i)):
                n_not = n - i
                for j in range(i+n_not-1, i-1, -1):
                        qc.mcx(list(range(i, j)) + [n], j)

    def subtract(qc, n, a):
        for i in range(n-1, -1, -1):
            if (a & (1 << i)):
                n_not = n - i
                for j in range(i, i+n_not):
                    qc.mcx(list(range(i, j)) + [n], j)

    qc.cx(n-1, n)
    subtract(qc, n, s)
    qc.x(n)
    subtract(qc, n, t)
    qc.x(n)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)

    def add(qc, n, a):
        for i in range(n):
            if (a & (1 << i)):
                n_not = n - i
                for j in range(i+n_not-1, i-1, -1):
                        qc.mcx(list(range(i, j)) + [n], j)

    def subtract(qc, n, a):
        for i in range(n-1, -1, -1):
            if (a & (1 << i)):
                n_not = n - i
                for j in range(i, i+n_not):
                    qc.mcx(list(range(i, j)) + [n], j)

    qc.cx(n-1, n)
    subtract(qc, n, s)
    qc.x(n)
    subtract(qc, n, t)
    #qc.x(n)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)

    def add(qc, n, a):
        for i in range(n):
            if (a & (1 << i)):
                n_not = n - i
                for j in range(i+n_not-1, i-1, -1):
                        qc.mcx(list(range(i, j)) + [n], j)

    def subtract(qc, n, a):
        for i in range(n-1, -1, -1):
            if (a & (1 << i)):
                n_not = n - i
                for j in range(i, i+n_not):
                    qc.mcx(list(range(i, j)) + [n], j)

    qc.cx(n-1, n)
    qc.x(n)
    subtract(qc, n, s)
    qc.x(n)
    subtract(qc, n, t)
    #qc.x(n)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)

    def add(qc, n, a):
        for i in range(n):
            if (a & (1 << i)):
                n_not = n - i
                for j in range(i+n_not-1, i-1, -1):
                        qc.mcx(list(range(i, j)) + [n], j)

    def subtract(qc, n, a):
        for i in range(n-1, -1, -1):
            if (a & (1 << i)):
                n_not = n - i
                for j in range(i, i+n_not):
                    qc.mcx(list(range(i, j)) + [n], j)
    # initialize state to 5

    qc.cx(n-1, n)
    subtract(qc, n, s)
    qc.x(n)
    add(qc, n, 2**(n+1) - t)
    qc.x(n)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)

    def add(qc, n, a):
        for i in range(n):
            if (a & (1 << i)):
                n_not = n - i
                for j in range(i+n_not-1, i-1, -1):
                        qc.mcx(list(range(i, j)) + [n], j)

    def subtract(qc, n, a):
        for i in range(n-1, -1, -1):
            if (a & (1 << i)):
                n_not = n - i
                for j in range(i, i+n_not):
                    qc.mcx(list(range(i, j)) + [n], j)
    # initialize state to 7
    for i in range(n):
        if (k & (1 << i)):
            qc.x(i)

    add(qc, n, 2**(n+1)-t)
    qc.x(n)
    subtract(qc, n, s)
    qc.x(n)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)

    def add(qc, n, a):
        for i in range(n):
            if (a & (1 << i)):
                n_not = n - i
                for j in range(i+n_not-1, i-1, -1):
                        qc.mcx(list(range(i, j)) + [n], j)

    def subtract(qc, n, a):
        for i in range(n-1, -1, -1):
            if (a & (1 << i)):
                n_not = n - i
                for j in range(i, i+n_not):
                    qc.mcx(list(range(i, j)) + [n], j)

    add(qc, n, 2**(n+1)-t)
    qc.x(n)
    subtract(qc, n, s)
    qc.x(n)

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

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(n + 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):
        tt = 2 * pi * (t-2**(n+1)) * 2**i / 2**n
        qc.cp(tt,n, i)
        qc.x(n)
        ts=s
        qc.cp(ts,n, i)
        qc.x(0)

    qc.compose(qft_n.inverse(),k,inplace=True)
    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 b3(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

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(n + 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):
        tt = 2 * pi * (t-2**(n+1)) * 2**i / 2**n
        qc.cp(tt,n, i)
        qc.x(n)
        ts=2 * pi * s * 2**i / 2**n
        qc.cp(ts,n, i)
        qc.x(0)

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


'''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 b3(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

def solve(n: int, s: int, t: 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):
        tt = 2 * pi * (t-2**(n+1)) * 2**i / 2**n
        qc.cp(tt,n, i)
        qc.x(n)
        ts=2 * pi * s * 2**i / 2**n
        qc.cp(ts,n, i)
        qc.x(n)

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


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

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

"""

def QFT(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n//2):
        qc.swap(i, n-i-1)
    for i in range(n):
        qc.h(i)
        for j in range(i+1, n):
            qc.append(CU1Gate(pi/2**(j-i)), [j, i])
    return qc

def addition(n: int, a: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.compose(QFT(n), range(n), inplace=True)
    for i in range(n):
        qc.append(PhaseGate(2*pi*a*(1/2)**(n-i)), [i])
    qc.compose(QFT(n).inverse(), range(n), inplace=True)
    return qc

def B3(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # qc.x(n)

    qc.append(QFT(n).control(1), [n, *range(n)])
    for i in range(n):
        qc.append(PhaseGate(2*pi*a*(1/2)**(n-i)).control(1), [n, i])
    qc.append(QFT(n).inverse().control(1), [n, *range(n)])

    return qc.decompose(reps=2)

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    qc.compose(B3(n, -t), range(n + 1), inplace=True)
    qc.x(n)
    qc.compose(B3(n, -s), range(n + 1), inplace=True)
    qc.x(n)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)

    # Define the range for k
    lower_bound = s
    upper_bound = 2**n
    second_lower_bound = 2**n
    second_upper_bound = t

    # Create a classical condition to check the ranges
    # We will use the last qubit as an auxiliary qubit to help with the logic
    # We will use the last qubit to indicate which operation to perform

    # For k in [s, 2^n)
    for k in range(lower_bound, upper_bound):
        # Convert k to binary and set the qubits accordingly
        binary_k = format(k, f'0{n}b')[::-1]  # little-endian
        for i in range(n):
            if binary_k[i] == '1':
                qc.x(i)  # Set the qubit to |1>

        # Apply the operation O: |k> -> |k - s>
        # This is equivalent to subtracting s from k
        # We need to apply a series of controlled NOTs to achieve this
        binary_s = format(s, f'0{n}b')[::-1]  # little-endian
        for i in range(n):
            if binary_s[i] == '1':
                qc.cx(i, n)  # Controlled NOT to subtract s

        # Reset the qubits to prepare for the next iteration
        for i in range(n):
            if binary_k[i] == '1':
                qc.x(i)  # Reset the qubit to |0>

    # For k in [2^n, t)
    for k in range(second_lower_bound, second_upper_bound):
        # Convert k to binary and set the qubits accordingly
        binary_k = format(k, f'0{n}b')[::-1]  # little-endian
        for i in range(n):
            if binary_k[i] == '1':
                qc.x(i)  # Set the qubit to |1>

        # Apply the operation O: |k> -> |k + (2^(n+1) - t)>
        # This is equivalent to adding (2^(n+1) - t) to k
        addition = (2**(n + 1) - t)
        binary_addition = format(addition, f'0{n}b')[::-1]  # little-endian
        for i in range(n):
            if binary_addition[i] == '1':
                qc.cx(i, n)  # Controlled NOT to add (2^(n+1) - t)

        # Reset the qubits to prepare for the next iteration
        for i in range(n):
            if binary_k[i] == '1':
                qc.x(i)  # Reset the qubit to |0>

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    qc.append(constant_addition(n,-t), qargs=range(n+1))
    qc.x(n)
    qc.append(constant_addition(n,-s), qargs=range(n+1))
    qc.x(n)
    return qc

def constant_addition(n,a):
    qc = QuantumCircuit(n+1)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    for i in range(n):
        qc.cp(2*math.pi*a/2**(n-i),n,i)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(-2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    qc.append(constant_addition(n,-t), qargs=range(n+1))
    qc.x(n)
    qc.append(constant_addition(n,-s), qargs=range(n+1))
    qc.x(n)
    return qc

def constant_addition(n,a):
    qc = QuantumCircuit(n+1)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    for i in range(n):
        qc.cp(2*math.pi*a/2**(n-i),n,i)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(-2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    qc.append(constant_addition(n,-t), qargs=range(n+1))
    return qc

def constant_addition(n,a):
    qc = QuantumCircuit(n+1)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    for i in range(n):
        qc.cp(2*math.pi*a/2**(n-i),n,i)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(-2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, a: int) -> QuantumCircuit:
    qc = QuantumCircuit(n+1)
    # Write your code here:
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    for i in range(n):
        qc.cp(2*math.pi*a/2**(n-i),n,i)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(-2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n+1)
    # Write your code here:
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    for i in range(n):
        qc.cp(2*math.pi*a/2**(n-i),n,i)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(-2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n+1)
    # Write your code here:
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    for i in range(n):
        qc.cp(2*math.pi*(-t)/2**(n-i),n,i)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(-2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n+1)
    # Write your code here:
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    for i in range(n):
        qc.cp(2*math.pi*(-t)/2**(n-i),n,i)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(-2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    qc.x(n)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    for i in range(n):
        qc.cp(2*math.pi*(-s)/2**(n-i),n,i)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(-2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    qc.x(n)
    return qc
'''


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

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

    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(((1 << n + 1) - t - s) * theta, ctl, 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

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    for k in range(s, 2**n):
        qc.cx(k, n)

    for k in range(2**n, t):
        qc.cx(k, n)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)

    # Apply the oracle O based on the conditions
    for k in range(s + 1, 2**n):
        # Apply the first condition for s < k < 2^n
        # Example: Apply X gate to the last qubit if condition is met
        qc.x(n)

    for k in range(2**n + 1, t):
        # Apply the second condition for 2^n < k < t
        # Example: Apply Y gate to the last qubit if condition is met
        qc.y(n)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    t = 2 ** (n + 1) - t
    for j in range(t):
        for i in range(n):
            qc.cx(n, 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.cx(n, i)
    qc.x(n)
    for j in range(s):
        qc.cx(n, 0)
        for i in range(n - 1):
            qc.mcx(list(range(0, i + 1)) + [n], i + 1)
    qc.x(n)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    t = 2 ** (n + 1) - t
    for i in range(n):
        qc.cx(n, i)
    for j in range(t):
        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.cx(n, i)
    qc.x(n)
    for j in range(s):
        qc.cx(n, 0)
        for i in range(n - 1):
            qc.mcx(list(range(0, i + 1)) + [n], i + 1)
    qc.x(n)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    t = 2 ** (n + 1) - t
    for i in range(n):
        qc.cx(n, i)
    for j in range(10):
        if (t / (2 ** j)) % 2 == 1:
            qc.cx(n, j)
            for i in range(j, n - 1):
                qc.mcx(list(range(j, i + 1)) + [n], i + 1)
    for i in range(n):
        qc.cx(n, i)
    qc.x(n)
    for j in range(10):
        if (s / (2 ** j)) % 2 == 1:
            qc.cx(n, j)
            for i in range(j, n - 1):
                qc.mcx(list(range(j, i + 1)) + [n], i + 1)
    qc.x(n)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    t = 2 ** (n + 1) - t
    for i in range(n):
        qc.cx(n, i)
    for j in range(10):
        if (t // (2 ** j)) % 2 == 1:
            qc.cx(n, j)
            for i in range(j, n - 1):
                qc.mcx(list(range(j, i + 1)) + [n], i + 1)
    for i in range(n):
        qc.cx(n, i)
    qc.x(n)
    for j in range(10):
        if (s // (2 ** j)) % 2 == 1:
            qc.cx(n, j)
            for i in range(j, n - 1):
                qc.mcx(list(range(j, i + 1)) + [n], i + 1)
    qc.x(n)
    return qc
'''


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

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

"""

def QFT(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n//2):
        qc.swap(i, n-i-1)
    for i in range(n):
        qc.h(i)
        for j in range(i+1, n):
            qc.append(CU1Gate(pi/2**(j-i)), [j, i])
    return qc

def B2(n: int, a: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.compose(QFT(n), range(n), inplace=True)
    for i in range(n):
        qc.append(PhaseGate(2*pi*a*(1/2)**(n-i)), [i])
    qc.compose(QFT(n).inverse(), range(n), inplace=True)
    return qc

def B3(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # qc.x(n)

    qc.append(QFT(n).control(1), [n, *range(n)])
    for i in range(n):
        qc.append(PhaseGate(2*pi*a*(1/2)**(n-i)).control(1), [n, i])
    qc.append(QFT(n).inverse().control(1), [n, *range(n)])

    return qc.decompose(reps=2)

def B4(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    qc.compose(B3(n, -t), range(n + 1), inplace=True)
    qc.x(n)
    qc.compose(B3(n, -s), range(n + 1), inplace=True)
    qc.x(n)

    return qc

def solve(n: int, a: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    qc.compose(B2(n+1, 2**n-a), range(n + 1), inplace=True)
    qc.compose(B4(n, 2**n-a, 2**n-a+L), range(n + 1), inplace=True)
    qc.compose(B2(n+1, L-a), range(n + 1), inplace=True)

    return qc
'''


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

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

"""

def QFT(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n//2):
        qc.swap(i, n-i-1)
    for i in range(n):
        qc.h(i)
        for j in range(i+1, n):
            qc.append(CU1Gate(pi/2**(j-i)), [j, i])
    return qc

def B2(n: int, a: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.compose(QFT(n), range(n), inplace=True)
    for i in range(n):
        qc.append(PhaseGate(2*pi*a*(1/2)**(n-i)), [i])
    qc.compose(QFT(n).inverse(), range(n), inplace=True)
    return qc

def B3(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # qc.x(n)

    qc.append(QFT(n).control(1), [n, *range(n)])
    for i in range(n):
        qc.append(PhaseGate(2*pi*a*(1/2)**(n-i)).control(1), [n, i])
    qc.append(QFT(n).inverse().control(1), [n, *range(n)])

    return qc.decompose(reps=2)

def B4(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    qc.compose(B3(n, -t), range(n + 1), inplace=True)
    qc.x(n)
    qc.compose(B3(n, -s), range(n + 1), inplace=True)
    qc.x(n)

    return qc

def solve(n: int, a: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    qc.compose(B2(n+1, 2**n-a), range(n + 1), inplace=True)
    qc.compose(B4(n, 2**n-a, 2**n-a+L), range(n + 1), inplace=True)
    qc.x(n)
    qc.compose(B2(n+1, -(2**n-a)), range(n + 1), inplace=True)

    return qc
'''


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

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

"""

def QFT(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n//2):
        qc.swap(i, n-i-1)
    for i in range(n):
        qc.h(i)
        for j in range(i+1, n):
            qc.append(CU1Gate(pi/2**(j-i)), [j, i])
    return qc

def B2(n: int, a: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.compose(QFT(n), range(n), inplace=True)
    for i in range(n):
        qc.append(PhaseGate(2*pi*a*(1/2)**(n-i)), [i])
    qc.compose(QFT(n).inverse(), range(n), inplace=True)
    return qc

def B3(n: int, a: int) -> QuantumCircuit:
    k, c = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(k, c)
    # qc.x(n)

    qc.append(QFT(n).control(1), [n, *range(n)])
    for i in range(n):
        qc.append(PhaseGate(2*pi*a*(1/2)**(n-i)).control(1), [n, i])
    qc.append(QFT(n).inverse().control(1), [n, *range(n)])

    return qc.decompose(reps=2)

def B4(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    qc.compose(B3(n, -t), range(n + 1), inplace=True)
    qc.x(n)
    qc.compose(B3(n, -s), range(n + 1), inplace=True)
    qc.x(n)

    return qc

def solve(n: int, a: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    a=L-a
    # Write your code here:
    qc.compose(B2(n+1, 2**n-a), range(n + 1), inplace=True)
    qc.compose(B4(n, 2**n-a, 2**n-a+L), range(n + 1), inplace=True)
    qc.x(n)
    qc.compose(B2(n+1, -2**n+L-a), range(n + 1), inplace=True)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, a: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    x = QuantumRegister(1)
    qc.add_bits(x)
    qc.x(n+1)
    qc.compose(constant_addition(n+1,q-L), inplace=True)
    qc.compose(constant_addition(n,L), inplace=True)
    qc.x(n+1)
    qc.swap(n,n+1)
    return qc

def constant_addition(n,a):
    qc = QuantumCircuit(n+1)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    for i in range(n):
        qc.cp(2*math.pi*a/2**(n-i),n,i)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(-2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, a: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    x = QuantumRegister(1)
    qc.add_bits(x)
    qc.x(n+1)
    qc.compose(constant_addition(n+1,a-L), inplace=True)
    qc.compose(constant_addition(n,L), inplace=True)
    qc.x(n+1)
    qc.swap(n,n+1)
    return qc

def constant_addition(n,a):
    qc = QuantumCircuit(n+1)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    for i in range(n):
        qc.cp(2*math.pi*a/2**(n-i),n,i)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(-2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, a: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    x = QuantumRegister(1)
    qc.add_bits(x)
    qc.x(n+1)
    qc.compose(constant_addition(n+1,a-L), inplace=True)
    qc.compose(constant_addition(n,L), qubits=list(range(n+1)), inplace=True)
    qc.x(n+1)
    qc.swap(n,n+1)
    return qc

def constant_addition(n,a):
    qc = QuantumCircuit(n+1)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    for i in range(n):
        qc.cp(2*math.pi*a/2**(n-i),n,i)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(-2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    return qc
'''


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

def solve(n: int, a: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    x = QuantumRegister(1)
    qc.add_bits(x)
    qc.x(n+1)
    qc.compose(constant_addition(n+1,a-L), inplace=True)
    qc.compose(constant_addition(n,L), qubits=list(range(n+1)), inplace=True)
    qc.x(n+1)
    qc.swap(n,n+1)
    return qc

def constant_addition(n,a):
    qc = QuantumCircuit(n+1)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    for i in range(n):
        qc.cp(2*math.pi*a/2**(n-i),n,i)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(-2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    return qc
'''


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

def solve(n: int, a: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    x = QuantumRegister(1)
    qc.add_bits(x)
    qc.x(n+1)
    qc.compose(constant_addition(n+1,a-L), inplace=True)
    qc.compose(constant_addition(n,L), inplace=True)
    qc.x(n+1)
    qc.x(n)
    return qc

def constant_addition(n,a):
    qc = QuantumCircuit(n+1)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    for i in range(n):
        qc.cp(2*math.pi*a/2**(n-i),n,i)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(-2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    return qc
'''


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

def solve(n: int, a: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    qc.compose(add(n,2^n-L+a), inplace=True)
    qc.compose(pack(n,2^n-L+a,2^n+a), inplace=True)
    qc.compose(add(n,a), inplace=True)

    return qc

def pack(n: int, s: int, t: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    qc.compose(add(n,-t), inplace=True)
    qc.x(n)
    qc.compose(add(n,-s), inplace=True)
    qc.x(n)
    return qc

def add(n,a):
    qc = QuantumCircuit(n+1)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    for i in range(n):
        qc.cp(2*math.pi*a/2**(n-i),n,i)
    for i in range(n-1,-1,-1):
        qc.ch(n,i)
        for j in range(i-1,-1,-1):
            qc.mcp(-2*math.pi/2**(i-j+1),[n,j],i)
    for k in range(n//2):
        qc.cswap(n,k,n-1-k)
    return qc
'''


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

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

    return qc

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

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


'''python
from qiskit import QuantumCircuit

def add(qc, a):
    for i in range(n):
        if (a & (1 << i)):
            n_not = n - i
            for j in range(i+n_not-1, i-1, -1):
                if j == i:
                    qc.x(j)
                else:
                    qc.mcx(list(range(i, j)), j)

def subtract(qc, a):
    for i in range(n-1, -1, -1):
        if (a & (1 << i)):
            n_not = n - i
            for j in range(i, i+n_not):
                if j == i:
                    qc.x(j)
                else:
                    qc.mcx(list(range(i, j)), j)

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

    subtract(qc, L-a)
    add(qc, L)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    def add(qc, a):
        for i in range(n):
            if (a & (1 << i)):
                n_not = n - i
                for j in range(i+n_not-1, i-1, -1):
                    if j == i:
                        qc.x(j)
                    else:
                        qc.mcx(list(range(i, j)), j)

    def subtract(qc, a):
        for i in range(n-1, -1, -1):
            if (a & (1 << i)):
                n_not = n - i
                for j in range(i, i+n_not):
                    if j == i:
                        qc.x(j)
                    else:
                        qc.mcx(list(range(i, j)), j)

    subtract(qc, L-a)
    add(qc, L)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    def add(qc, a):
        for i in range(n):
            if (a & (1 << i)):
                n_not = n - i
                for j in range(i+n_not-1, i-1, -1):
                    if j == i:
                        qc.x(j)
                    else:
                        qc.mcx(list(range(i, j)), j)

    def subtract(qc, n, a):
        for i in range(n-1, -1, -1):
            if (a & (1 << i)):
                n_not = n - i
                for j in range(i, i+n_not):
                    if j == i:
                        qc.x(j)
                    else:
                        qc.mcx(list(range(i, j)), j)

    subtract(qc, n+1, L-a)
    add(qc, L)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    def add(qc, n, a):
        for i in range(n):
            if (a & (1 << i)):
                n_not = n - i
                for j in range(i+n_not-1, i-1, -1):
                    if j == i:
                        qc.x(j)
                    else:
                        qc.mcx(list(range(i, j)), j)

    def subtract(qc, n, a):
        for i in range(n-1, -1, -1):
            if (a & (1 << i)):
                n_not = n - i
                for j in range(i, i+n_not):
                    if j == i:
                        qc.x(j)
                    else:
                        qc.mcx(list(range(i, j)), j)

    subtract(qc, n+1,L-a)
    add(qc, n+1, L)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    # Convert a to binary representation
    a_bin = format(a, f'0{n}b')  # Get binary representation of a with n bits

    # Add a to k using controlled operations
    for i in range(n):
        if a_bin[i] == '1':
            # Apply a series of CNOT gates to add a to k
            # The control is on the i-th qubit of the input k
            # The target is the (n+1)-th qubit (which is the auxiliary qubit)
            qc.cx(i, n)  # CNOT from k_i to auxiliary qubit

    # Now we need to handle the modulo L operation
    # If L is a power of 2, we can simply ignore the overflow
    # If L is not a power of 2, we need to implement a more complex logic
    # However, since L <= 2^n, we can assume that the addition will not exceed the range of n bits

    # We will use a simple approach to ensure we do not exceed L
    # This is a naive approach and may not be optimal for all cases
    # We will check if the result exceeds L and apply a correction if necessary

    # This is a placeholder for the correction logic
    # In practice, we would need to implement a more complex logic to handle the modulo correctly

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, a: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    b = 2 ** n - L + a
    # add b
    for i in range(n):
        qc.x(i)
    for j in range(10):
        if (t // (2 ** j)) % 2 == 1:
            qc.x(j)
            for i in range(j, n - 1):
                qc.mcx(list(range(j, i + 1)), i + 1)
    for i in range(n):
        qc.x(i)
    qc.x(n)
    for j in range(10):
        if (L // (2 ** j)) % 2 == 1:
            qc.cx(n, j)
            for i in range(j, n - 1):
                qc.mcx(list(range(j, i + 1)) + [n], i + 1)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, a: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)
    # Write your code here:
    b = 2 ** n - L + a
    # add b
    for i in range(n):
        qc.x(i)
    for j in range(10):
        if (b // (2 ** j)) % 2 == 1:
            qc.x(j)
            for i in range(j, n - 1):
                qc.mcx(list(range(j, i + 1)), i + 1)
    for i in range(n):
        qc.x(i)
    qc.x(n)
    for j in range(10):
        if (L // (2 ** j)) % 2 == 1:
            qc.cx(n, j)
            for i in range(j, n - 1):
                qc.mcx(list(range(j, i + 1)) + [n], i + 1)
    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 add(n: int, a: int) -> QuantumCircuit:
    k = QuantumRegister(n)
    qc = QuantumCircuit(k)
    # 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.p(theta, k[idx])

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

    return qc

def controlled_add(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

def oracle_less(bitcount: int, n: int) -> QuantumCircuit:
    regin, regout = QuantumRegister(bitcount), QuantumRegister(1)
    qc = QuantumCircuit(regin, regout)

    if n >= 2**bitcount:
        qc.x(regout)
        return qc

    bitrep = bin(n)[2:].zfill(bitcount)[::-1]
    masks = []

    for idx in range(bitcount):
        if bitrep[idx] == "1":
            mask = ("0" + bitrep[idx + 1 :]).rjust(bitcount, "*")
            masks.append(mask)

    flipped = [False] * bitcount
    for mask in masks:
        controls = []
        for idx in range(bitcount):
            if mask[idx] == "*":
                continue

            controls.append(regin[idx])
            if (mask[idx] == "0") != (flipped[idx]):
                flipped[idx] = not flipped[idx]
                qc.x(regin[idx])

        if len(controls) != 0:
            qc.mcx(controls, regout)

    for idx in range(bitcount):
        if flipped[idx]:
            qc.x(regin[idx])

    return qc

def lr_something(n: int, s: int, t: int) -> QuantumCircuit:
    regin = QuantumRegister(n + 1)
    qc = QuantumCircuit(regin)
    # Write your code here:

    anc_range = QuantumRegister(1)
    anc_pred = QuantumRegister(1)
    qc.add_register(anc_range)
    qc.add_register(anc_pred)

    # case 1
    qc.compose(oracle_less(n + 1, s), [*regin, *anc_range], inplace=True)
    qc.x(regin[-1])
    qc.x(anc_range)
    qc.ccx(regin[-1], anc_range, anc_pred)
    qc.compose(controlled_add(n, -s), [*regin[:-1], *anc_pred], inplace=True)
    qc.ccx(regin[-1], anc_range, anc_pred)
    qc.x(anc_range)
    qc.x(regin[-1])

    # case 2
    qc.compose(oracle_less(n + 1, t), [*regin, *anc_range], inplace=True)
    qc.ccx(regin[-1], anc_range, anc_pred)
    qc.compose(
        controlled_add(n + 1, 2 ** (n + 1) - t), [*regin, *anc_pred], inplace=True
    )
    qc.ccx(regin[-1], anc_range, anc_pred)
    qc.x(anc_range)

    return qc

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

    anc_range = QuantumRegister(1)
    qc.add_register(anc_range)

    qc.compose(add(n+1, a), inplace=True)
    qc.x(anc_range)
    qc.compose(oracle_less(n+1, L), inplace=True)
    qc.compose(controlled_add(n+1, -L), inplace=True)
    qc.compose(oracle_less(n+1, L-a-1), inplace=True)

    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 add(n: int, a: int) -> QuantumCircuit:
    k = QuantumRegister(n)
    qc = QuantumCircuit(k)
    # 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.p(theta, k[idx])

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

    return qc

def controlled_add(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

def oracle_less(bitcount: int, n: int) -> QuantumCircuit:
    regin, regout = QuantumRegister(bitcount), QuantumRegister(1)
    qc = QuantumCircuit(regin, regout)

    if n >= 2**bitcount:
        qc.x(regout)
        return qc

    bitrep = bin(n)[2:].zfill(bitcount)[::-1]
    masks = []

    for idx in range(bitcount):
        if bitrep[idx] == "1":
            mask = ("0" + bitrep[idx + 1 :]).rjust(bitcount, "*")
            masks.append(mask)

    flipped = [False] * bitcount
    for mask in masks:
        controls = []
        for idx in range(bitcount):
            if mask[idx] == "*":
                continue

            controls.append(regin[idx])
            if (mask[idx] == "0") != (flipped[idx]):
                flipped[idx] = not flipped[idx]
                qc.x(regin[idx])

        if len(controls) != 0:
            qc.mcx(controls, regout)

    for idx in range(bitcount):
        if flipped[idx]:
            qc.x(regin[idx])

    return qc

def lr_something(n: int, s: int, t: int) -> QuantumCircuit:
    regin = QuantumRegister(n + 1)
    qc = QuantumCircuit(regin)
    # Write your code here:

    anc_range = QuantumRegister(1)
    anc_pred = QuantumRegister(1)
    qc.add_register(anc_range)
    qc.add_register(anc_pred)

    # case 1
    qc.compose(oracle_less(n + 1, s), [*regin, *anc_range], inplace=True)
    qc.x(regin[-1])
    qc.x(anc_range)
    qc.ccx(regin[-1], anc_range, anc_pred)
    qc.compose(controlled_add(n, -s), [*regin[:-1], *anc_pred], inplace=True)
    qc.ccx(regin[-1], anc_range, anc_pred)
    qc.x(anc_range)
    qc.x(regin[-1])

    # case 2
    qc.compose(oracle_less(n + 1, t), [*regin, *anc_range], inplace=True)
    qc.ccx(regin[-1], anc_range, anc_pred)
    qc.compose(
        controlled_add(n + 1, 2 ** (n + 1) - t), [*regin, *anc_pred], inplace=True
    )
    qc.ccx(regin[-1], anc_range, anc_pred)
    qc.x(anc_range)

    return qc

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

    anc_range1 = QuantumRegister(1)
    anc_range2 = QuantumRegister(1)
    anc_range3 = QuantumRegister(1)
    qc.add_register(anc_range1)
    qc.add_register(anc_range2)
    qc.add_register(anc_range3)

    qc.x(anc_range1)
    qc.compose(oracle_less(n+1, L), [*regin, *anc_range2], inplace=True)
    qc.compose(oracle_less(n+1, L-a), [*regin, *anc_range1], inplace=True)
    qc.ccx(anc_range1, anc_range2, anc_range3)

    qc.compose(add(n+1, a), inplace=True)
    qc.compose(controlled_add(n+1, -L), [*regin, *anc_range3], inplace=True)
    qc.ccx(anc_range1, anc_range2, anc_range3)

    qc.x(anc_range2)
    qc.x(regin[-1])
    qc.ccx(regin[-1], anc_range2, anc_range3)
    qc.x(regin[-1])
    qc.compose(controlled_add(n+1, L), [*regin, *anc_range3], inplace=True)

    qc.compose(oracle_less(n+1, 2**(n+1) + 1 - L), [*regin, *anc_range3], inplace=True)
    qc.compose(oracle_less(n+1, L), [*regin, *anc_range3], inplace=True)

    qc.x(anc_range2)
    qc.compose(oracle_less(n+1, L), [*regin, *anc_range2], inplace=True)

    qc.compose(oracle_less(n+1, L), [*regin, *anc_range1], inplace=True)
    qc.compose(oracle_less(n+1, a), [*regin, *anc_range1], inplace=True)
    qc.x(anc_range1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    for _ in range(n):
        result.append(a)
        a *= a
        a %= L

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    for _ in range(100001):
        result.append(a)
        a *= a
        a %= L

    return result
'''


'''python
def solve(n: int, a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    for _ in range(n):
        result.append(a)
        a *= a
        a %= L

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    for i in range(n):
        result.append(n)

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    prev = a % L
    result.append(a % L)

    for i in range(1,n):
        prev = prev * prev % L
        result.append(prev)

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    prev = a % L
    result.append(a % L)

    for i in range(1,10**5):
        prev = prev * prev % L
        result.append(prev)

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    x = a % L

    for i in range(n):
        result.append(x)
        x = x * x % L

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    x = a % L

    for i in range(100):
        result.append(x)
        x = x * x % L

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    global n

    x = a % L

    for i in range(n):
        result.append(x)
        x = x * x % L

    return result
'''


'''python
def solve(n: int, a: int, L: int) -> list[int]:
    result: list[int] = [a]
    # Write your code here:

    for i in range(1, n):
        result.append((result[i-1]*result[i-1]) % L)
    return result
'''


'''python
def solve(n: int, a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    results = []
    current = a % L  # a^1 mod L
    for _ in range(n):
        results.append(current)
        current = (current * current) % L  # (a^(2^k) mod L) = (previous_result)^2 mod L
    return results
    return result
'''


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


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


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    ans=a%L
    for k in range(n):
        result.append(ans)
        ans=(ans*ans)%L
    return result
'''


'''python
def solve(n: int, a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    ans=a%L
    for k in range(n):
        result.append(ans)
        ans=(ans*ans)%L
    return result
'''


'''python
def solve(n: int, a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    # [a, a^2, a^4, ...] (mod L)
    x = a
    for i in range(n):
        result.add(x)
        x = x * x % L

    return result
'''


'''python
def solve(n: int, a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    # [a, a^2, a^4, ...] (mod L)
    x = a
    for i in range(n):
        result.append(x)
        x = x * x % L

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    ans = a % L
    result.append(ans)
    for _ in range(n-1):
        ans = (ans ** 2) % L
        result.append(ans)

    return result
'''


'''python
def solve(n:int a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    ans = a % L
    result.append(ans)
    for _ in range(n-1):
        ans = (ans ** 2) % L
        result.append(ans)

    return result
'''


'''python
def solve(n:int, a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    ans = a % L
    result.append(ans)
    for _ in range(n-1):
        ans = (ans ** 2) % L
        result.append(ans)

    return result
'''


'''python
def solve(n: int, a: int, L: int) -> list[int]:
    result = []
    power = 1  # Start with a^(2^0) which is just 'a'

    for _ in range(n):
        result.append(power % L)
        power = (power * power) % L  # Squaring the power each time

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []

    # Calculate initial value a^1 mod L
    current = a % L

    # For each power of 2, square the previous result
    # This efficiently calculates a^(2^k) mod L for k = 0,1,2,...,n-1
    while len(result) < n:
        result.append(current)
        current = (current * current) % L

    return result
'''


'''python
def solve(a: int, L: int, n: int) -> list[int]:
    result = []
    power = 1  # a^0 = 1
    for _ in range(n):
        result.append(power % L)
        power = (power * a) % L  # Using modular multiplication to avoid overflow
    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result = []
    current = a  # Start with a^1

    for _ in range(n):  # Compute up to a^(2^(n-1))
        result.append(current % L)
        current = pow(current, 2, L)  # Efficient modular exponentiation

    return result
'''


'''python
def solve(a: int, L: int, n: int) -> list[int]:
    result: list[int] = []
    current = 1
    for k in range(1, n):
        current = (current * a) % L
        result.append(current)
    return result

# Example usage:
# a = 3
# L = 7
# n = 5
# print(solve(a, L, n))  # Output: [3, 2, 6, 4]
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    x = a % L
    for _ in range(n):
        result.append(x)
        x = (x * x) % L

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    n = int(input().strip())

    x = a % L
    for _ in range(n):
        result.append(x)
        x = (x * x) % L

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    i = 1
    n = 0
    while i < L:
        i <<= 1
        n += 1
    x = a % L
    for _ in range(n):
        result.append(x)
        x = (x * x) % L

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    i = 1
    n = 0
    while i < L:
        i <<= 1
        n += 1
    n -= 1
    x = a % L
    for _ in range(n):
        result.append(x)
        x = (x * x) % L

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    i = 1
    n = 0
    while i < L:
        i << 1
        n += 1
    n -= 1
    x = a % L
    for _ in range(n):
        result.append(x)
        x = (x * x) % L

    return result
'''


'''python
def gcd(x: int, y: int) -> int:
    while y != 0:
        x, y = y, x % y
    return x

def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    i = 1
    n = 0
    while i < L:
        i << 1
        n += 1
    n -= 1

    while n > 0:
        if gcd(n, a) == 1 and gcd(n, L) == 1:
            break
        n -= 1

    x = a % L
    for _ in range(n):
        result.append(x)
        x = (x * x) % L

    return result
'''


'''python
def gcd(x: int, y: int) -> int:
    while y != 0:
        x, y = y, x % y
    return x

def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    n=10*10*10*10*10

    x = a % L
    for _ in range(1,n+1):
        result.append(x)
        x = (x * x) % L

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here

    x = a % L
    for _ in range(1,n+1):
        result.append(x)
        x = (x * x) % L

    return result
'''


'''python
from qiskit.quantum_info import Statevector
from qiskit.visualization import *
from qiskit import QuantumCircuit
def ccx_same(qc, c1, c2, tgt):

    qc.cx(c2, tgt)
    qc.cx(c1, c2)
    qc.cx(c2, tgt)
    qc.cx(c1, c2)

def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here

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

    for i in range(n - 1):
        qc.cx(i, i+1)
        ccx_same(qc, i, i+1, i)

    qc.x(n - 1)

    for i in reversed(range(n - 1)):
        ccx_same(qc, i, i+1, i)
        qc.cx(i, i+1)

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

    return result
'''


'''python
from qiskit import QuantumCircuit
def ccx_same(qc, c1, c2, tgt):

    qc.cx(c2, tgt)
    qc.cx(c1, c2)
    qc.cx(c2, tgt)
    qc.cx(c1, c2)

def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here

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

    for i in range(n - 1):
        qc.cx(i, i+1)
        ccx_same(qc, i, i+1, i)

    qc.x(n - 1)

    for i in reversed(range(n - 1)):
        ccx_same(qc, i, i+1, i)
        qc.cx(i, i+1)

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

    return result
'''


'''python
def solve(n: int, a: int, L: int) -> list:
    result = [a]
    for i in range(n - 1):
        result.append(result[i-1] ** 2 % L)

    return result
'''


'''python
def solve(n: int, a: int, L: int) -> list:
    result = [a]
    for i in range(1,n):
        result.append(result[i-1] ** 2 % L)

    return result
'''


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

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    result.append(a)
    k = 1
    while True:
        x = pow(a, 2**k, mod=L)
        if x == a:
            break
        if(x == 1):
            break
        result.append(x)
        k += 1

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    result.append(a)
    k = 1
    while True:
        x = pow(a, pow(2, k), mod=L)
        if x == a:
            break
        if(x == 1):
            break
        result.append(x)
        k += 1

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    result.append(a)
    k = 1
    while True:
        x = pow(a, k, mod=L)
        if x == a:
            break
        if (x == 1):
            break
        if (result[-1] == x):
            break
        result.append(x)
        k *= 2

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    result.append(a) # 2^0
    k = 2 # 2^1
    while True:
        x = pow(a, k, mod=L)
        if x == a:
            break
        if (x == 1):
            break
        if (result[-1] == x):
            break
        result.append(x)
        k *= 2

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    result.append(a) # 2^0
    k = 2 # 2^1
    while True:
        x = pow(a, k, mod=L)
        if (x == a || x == 1 || x == 0):
            break
        if (result[-1] == x):
            break
        result.append(x)
        k *= 2

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    k = 1
    for i in range(10^5):
        x = pow(a, k, mod=L)
        result.append(x)
        k *= 2

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    for k in range(10**5):
        x = pow(a, pow(2, k), mod=L)
        result.append(x)

    return result
'''


'''python
def solve(n: int, a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    for k in range(10**5):
        x = pow(a, pow(2, k), mod=L)
        result.append(x)

    return result
'''


'''python
def solve(n: int, a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    x = a % L
    result.append(x)
    for k in range(1, 10**5):
        x = (x * 2) % L
        result.append(x)

    return result
'''


'''python
def solve(n: int, a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    for k in range(10**5):
        x = pow(a, 2*k, mod=L)
        result.append(x)

    return result
'''


'''python
def solve(n: int, a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    for k in range(n):
        x = pow(a, 2*k, mod=L)
        result.append(x)

    return result
'''


'''python
def solve(n: int, a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    for k in range(n):
        x = pow(a, pow(2, k), mod=L)
        result.append(x)

    return result
'''


'''python
def solve(n: int, a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    for k in range(n):
        x = pow(x, 2, mod=L)
        result.append(x)

    return result
'''


'''python
def solve(n: int, a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    x = a
    result.append(x)
    for k in range(1, n):
        x = pow(x, 2, mod=L)
        result.append(x)

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    result = [pow(a, 2**k, L) for k in range(n)]
    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    for k in range(n):
        result.append(pow(a, 2**k, L))
    return result
'''


'''python
import sympy.ntheory.factor_

def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    L_totient = int(sympy.ntheory.factor_.totient(L))
    for k in range(n):
        tmp = pow(2, k, L_totient)
        result.append(pow(a, tmp, L))
    return result
'''


'''python
def phi(num):
    cnt=0
    for i in range (1,num):
        if gcd(num,i)==1:
            cnt+=1
    return cnt

def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    L_totient = phi(L)
    for k in range(n):
      tmp = pow(2, k, L_totient)
      result.append(pow(a, tmp, L))
    return result
'''


'''python
from math import gcd

def phi(num):
    cnt=0
    for i in range (1,num):
        if gcd(num,i)==1:
            cnt+=1
    return cnt

def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    L_totient = phi(L)
    for k in range(n):
      tmp = pow(2, k, L_totient)
      result.append(pow(a, tmp, L))
    return result
'''


'''python
from math import gcd

def phi(num):
    cnt=0
    for i in range (1,num):
        if gcd(num,i)==1:
            cnt+=1
    return cnt

def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    L_totient = phi(L)
    for k in range(n):
        tmp = pow(2, k, L_totient)
        result.append(pow(a, tmp, L))
    return result
'''


'''python
from math import gcd

def phi(num):
    cnt=0
    for i in range (1,num):
        if gcd(num,i)==1:
            cnt+=1
    return cnt

def solve(n int, a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    L_totient = phi(L)
    for k in range(n):
        tmp = pow(2, k, L_totient)
        result.append(pow(a, tmp, L))
    return result
'''


'''python
from math import gcd

def phi(num):
    cnt=0
    for i in range (1,num):
        if gcd(num,i)==1:
            cnt+=1
    return cnt

def solve(n: int, a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    L_totient = phi(L)
    for k in range(n):
        tmp = pow(2, k, L_totient)
        result.append(pow(a, tmp, L))
    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    for i in range(n):
        tmp = a**(2**i) % L
        result.append(tmp)

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    for i in range(n):
        tmp = (a**(2**i)) % L
        result.append(tmp)

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    current = a% L
    for _ in range(n):
        result.append(current)
        current = (current * current) % L

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    current = a
    for _ in range(n):
        result.append(current % L)
        current = (current * current) % L

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    current = a
    for _ in range(n):
        result.append(current % L)
        current = (current * current) % L

    return result[:n]
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    current = a % L
    for _ in range(n):
        result.append(current)
        current = (current * current) % L

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    for _ in range(n):
        result.append(current)
        current = (current * current) % L

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    current = a
    for _ in range(n):
        result.append(current)
        current = (current * current) % L

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    current = a
    for _ in range(n):
        result.append(current)
        current = pow(current, 2, L)

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    x = a % L
    for _ in range(n):
        result.append(x)
        # Square x modulo L for the next term
        x = (x * x) % L  # or x = pow(x, 2, L)

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    x = a % L
    for _ in range(n-1):
        result.append(x)
        # Square x modulo L for the next term
        x = (x * x) % L  # or x = pow(x, 2, L)

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    x = a
    for _ in range(n-1):
        result.append(x)
        # Square x modulo L for the next term
        x = (x * x) % L  # or x = pow(x, 2, L)

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    x = a
    for _ in range(n):
        result.append(x)
        # Square x modulo L for the next term
        x = (x * x) % L  # or x = pow(x, 2, L)

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:

    result = [a]
    for _ in range(n - 1):
        result.append(result[-1] ** 2 % L)

    return result
'''


'''python
def solve(n: int, a: int, L: int) -> list:
    result = [a]
    for _ in range(n - 1):
        result.append(result[-1] ** 2 % L)

    return result
'''


'''python
def solve(n: int, a: int, L: int) -> list[int]:
    res = [1]
    cur = 1
    for i in range(1, n):
        cur = cur * cur
        if(cur >= L) cur = cur % L
        res.push_back(cur)
    return res
'''


'''python
def solve(n: int, a: int, L: int) -> list[int]:
    res = [1]
    expmod = pow(a, -1, L)
    curexp = 1
    for i in range(1, n):
        curexp = (2*curexp) % expmod
        res.push_back(pow(a, curexp, L))
    return res
'''


'''python
def solve(n: int, a: int, L: int) -> list[int]:
    expmod = pow(a, -1, L)
    curexp = 1
    for i in range(n):
        curexp = (2*curexp) % expmod
        res.push_back(pow(a, curexp, L))
    return res
'''


'''python
def solve(n: int, a: int, L: int) -> list[int]:
    expmod = pow(a, -1, L)
    curexp = 1
    res = []
    for i in range(n):
        curexp = (2*curexp) % expmod
        res.push_back(pow(a, curexp, L))
    return res
'''


'''python
def solve(n: int, a: int, L: int) -> list[int]:
    expmod = pow(a, -1, L)
    curexp = 1
    res =
    for i in range(n):
        curexp = (2*curexp) % expmod
        res.append(pow(a, curexp, L))
    return res
'''


'''python
def solve(n: int, a: int, L: int) -> list[int]:
    res = [1]*n
    cur = a
    for i in range(n):
        res[i] = cur
        cur = (cur*cur)%L
    return res
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    result: list[int] = []
    current_value = a

    for k in range(L):
        result.append(current_value % L)
        current_value = (current_value * a * a) % L

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    current_value = a % L
    result.append(current_value)

    for k in range(1, n):
        current_value = (current_value * current_value) % L
        result.append(current_value)

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    current_value = a % L
    result.append(current_value)

    max_k = L.bit_length()

    for k in range(1, max_k):
        current_value = (current_value * current_value) % L
        result.append(current_value)

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    current_value = a % L
    result.append(current_value)

    max_k = 100000  # 最大回数として適当な大きな値（問題の制約に基づく）

    for k in range(1, max_k):
        current_value = (current_value * current_value) % L
        result.append(current_value)

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    current_value = a % L
    result.append(current_value)

    for _ in range(n):
        result.append(current % L)
        current = (current * current) % L

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    for k in range(n):
        result.append(current)
        current = (current ** 2) % L

    return result
'''


'''python
def solve(n: int, a: int, L: int) -> list[int]:

    result: list[int] = []
    # Write your code here:
    for k in range(n):
        result.append(current)
        current = (current ** 2) % L

    return result
'''


'''python
def solve(n: int, a: int, L: int) -> list[int]:
    result: list[int] = []
    current = a % L  # 初期値 a^2^0 mod L (a^1 mod L)

    for k in range(n):
        result.append(current)
        # 次の a^(2^(k+1)) mod L を計算
        current = (current ** 2) % L

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    for k in range(n):
        result = pow(a, 2**k, )
        results.append(result)

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    current = a % L
    for _ in range(n):
        result.append(current)
        current = (current * current) % L

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    current = a % L
    n = 2 ** 1024
    for _ in range(n):
        result.append(current)
        current = (current * current) % L

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    current = a % L
    n = 10 ** 5
    for _ in range(n):
        result.append(current)
        current = (current * current) % L

    return result
'''


'''python
def solve(a: int, L: int) -> list[int]:
    result: list[int] = []
    # Write your code here:
    if not (1 <= a < L <= 2**1024):
        raise ValueError("条件 1 ≤ a < L ≤ 2^1024 を満たしていません。")
    current = a % L
    n = 10 ** 5
    for _ in range(n):
        result.append(current)
        current = (current * current) % L

    return result
'''