Given a sequence of $n$ integers, $p(1),p(2),...,p(n)$ where each element is distinct and satisfies $1\leq p(x)\leq n$. For each $\boldsymbol{x}$ where $1\leq x\leq n$, that is $\boldsymbol{x}$ increments from $\mbox{1}$ to $n$, find any integer $y$ such that $p(p(y))\equiv x$ and keep a history of the values of $y$ in a return array.

Example   

$p=[5,2,1,3,4]$    

Each value of $\boldsymbol{x}$ between $\mbox{1}$ and $5$, the length of the sequence, is analyzed as follows:

$x=1\equiv p[3],p[4]=3$, so $p[p[4]]=1$
$x=2\equiv p[2],p[2]=2$, so $p[p[2]]=2$
$x=3\equiv p[4],p[5]=4$, so $p[p[5]]=3$
$x=4\equiv p[5],p[1]=5$, so $p[p[1]]=4$
$x=5\equiv p[1],p[3]=1$, so $p[p[3]]=5$

The values for $y$ are $[4,2,5,1,3]$.  

Function Description  

Complete the permutationEquation function in the editor below.   

permutationEquation has the following parameter(s):  

int p[n]: an array of integers  

Returns   

int[n]: the values of $y$ for all $\boldsymbol{x}$ in the arithmetic sequence $\mbox{1}$ to $n$

Input Format

The first line contains an integer $n$, the number of elements in the sequence. 

The second line contains $n$ space-separated integers $p[i]$ where $1\leq i\leq n$.

Constraints

$1\leq n\leq50$
$1\leq p[i]\leq50$, where $1\leq i\leq n$.
Each element in the sequence is distinct.

Sample Input 0
3
2 3 1

Sample Output 0
2
3
1

Explanation 0

Given the values of $p(1)=2$, $p(2)=3$, and $p(3)=1$, we calculate and print the following values for each $\boldsymbol{x}$ from $\mbox{1}$ to $n$:

$x=1\equiv p(3)=p(p(2))=p(p(y))$, so we print the value of $y=2$ on a new line.
$x=2\equiv p(1)=p(p(3))=p(p(y))$, so we print the value of $y=3$ on a new line.
$x=3\equiv p(2)=p(p(1))=p(p(y))$, so we print the value of $y=1$ on a new line.

Sample Input 1
5
4 3 5 1 2

Sample Output 1
1
3
5
4
2

Source: hackerrank | Difficulty: EASY
Tags: Implementation


Solutions:
n = int(input())
ps = [int(x) for x in input().split()]
for x in range(1, n + 1):
	for y in range(n):
		if ps[ps[y] - 1] == x:
			print(y + 1)
			break


n = int(input())
p = list(map(lambda x: int(x) - 1, input().split()))
for i in range(n):
	for y in range(n):
		if p[p[y]] == i:
			print(y + 1)
			break


n = int(input())
p = [0] + list(map(int, input().split()))
for i in range(1, n + 1):
	for s in range(1, n + 1):
		if p[p[s]] == i:
			print(s)
			break


n = int(input())
arr = input().split()
for i in range(0, n):
	arr[i] = int(arr[i])
for i in range(1, n + 1):
	print(arr.index(arr.index(i) + 1) + 1)


n = int(input())
prec = {}
a = list(map(int, input().split()))
for i in range(n):
	b = a[i]
	prec[b] = i + 1
for i in range(n):
	print(prec[prec[i + 1]])


n = int(input().strip())
p = list(map(int, input().strip().split(' ')))
array = []
for i in range(n):
	array.append([])
for i in range(n):
	array[p[p[i] - 1] - 1].append(str(i + 1))
for i in range(n):
	string = ''
	for elt in array[i]:
		string = string + elt + ' '
	print(string)


n = int(input())
p = list(map(int, input().split()))
x = [0] * n
for (i, v) in enumerate(p):
	x[p[v - 1] - 1] = i + 1
for v in x:
	print(v)


n = int(input())
perm = [int(a) - 1 for a in input().split()]
inv = [0] * n
for (i, p) in enumerate(perm):
	inv[p] = i
for i in range(n):
	print(inv[inv[i]] + 1)


n = int(input())
numbers = list(map(int, input().split()))
for i in range(1, n + 1):
	y = numbers.index(i)
	print(numbers.index(y + 1) + 1)


n = int(input())
nums = list(map(int, input().split()))
for x in range(1, n + 1):
	f = nums.index(x) + 1
	a = nums.index(f) + 1
	print(a)


import sys
n = int(input().strip())
p = list(map(int, input().strip().split(' ')))
for i in range(n):
	i1 = p.index(i + 1)
	print(p.index(i1 + 1) + 1)


n = int(input())
l = list(map(int, input().split()))
for x in range(1, n + 1):
	for i in range(n):
		if l[i] == x:
			break
	i += 1
	for j in range(n):
		if l[j] == i:
			k = j
			break
	print(j + 1)


import sys
n = int(input())
P = [None] + [int(p) for p in input().split()]
for x in range(1, n + 1):
	tmp = P.index(x)
	y = P.index(tmp)
	print(y)


n = int(input())
x = list(map(int, input().split()))
for i in range(1, n + 1):
	found = False
	for ind1 in range(1, n + 1):
		py = x[ind1 - 1]
		ppy = x[py - 1]
		if ppy == i:
			print(ind1)
			found = True
			break


input()
p = [int(m) for m in input().strip().split()]
n = len(p)
A = []
for (I, i) in enumerate(p):
	A.append((p[i - 1], I + 1))
A.sort()
for i in range(1, n + 1):
	for j in A:
		if j[0] == i:
			print(j[1])


n = int(input())
fDict = dict()
fInvDict = dict()
L = input().split()
for i in range(n):
	fDict[i + 1] = int(L[i])
	fInvDict[int(L[i])] = i + 1
for x in range(1, n + 1):
	print(fInvDict[fInvDict[x]])


n = int(input())
ar = [int(t) for t in input().split()]
for i in range(1, n + 1):
	ind = ar.index(i)
	ind1 = ar.index(ind + 1)
	print(ind1 + 1)


n = int(input().strip())
p = [int(x) - 1 for x in input().strip().split()]
x = list(enumerate([p[p[y]] for y in range(n)]))
x.sort(key=lambda x: x[1])
for (y, i) in x:
	print(y + 1)


n = int(input().strip())
nlist = [int(x) for x in input().strip().split(' ')]
for x in range(n):
	print(nlist.index(nlist.index(x + 1) + 1) + 1)


x = int(input())
a = [int(a) for a in input().strip().split(' ')]
for i in range(1, x + 1):
	for j in range(x):
		if a[j] == i:
			for k in range(x):
				if a[k] == j + 1:
					print(k + 1)


(a, l) = (int(input()), list(map(int, input().split())))
for i in range(1, a + 1):
	print(l.index(l.index(i) + 1) + 1)


n = int(input())
a = [int(temp) for temp in input().split()]
for i in range(1, n + 1):
	print(a.index(a.index(i) + 1) + 1)


n = int(input())
sequence = [int(x) for x in input().split()]
for i in range(n):
	print(sequence.index(sequence.index(i + 1) + 1) + 1)


n = int(input())
l = list(map(int, input().split()))
for x in range(1, n + 1):
	i = l.index(x) + 1
	ans = l.index(i) + 1
	print(ans)


n = int(input())
num = list(map(int, input().split()))
for i in range(n):
	print(num.index(num.index(i + 1) + 1) + 1)


n = int(input().strip())
p = [int(x) for x in input().strip().split()]
pp = sorted([(p[p[i] - 1], i + 1) for i in range(n)])
for (x, ppy) in pp:
	print(ppy)


n = int(input())
l = [0] * (n + 1)
for (j, x) in enumerate([int(i) for i in input().split()]):
	l[x] = j + 1
for i in range(1, n + 1):
	print(l[l[i]])


n = int(input())
p = [int(x) for x in input().split()]
for x in range(1, n + 1):
	j = 0
	for (i, v) in enumerate(p):
		if v == x:
			j = i + 1
			break
	for (i, v) in enumerate(p):
		if v == j:
			print(i + 1)
			break


import sys

def solve(L):
	n = len(L)

	def p(n):
		return L[n - 1]
	values = {p(p(value)): value for value in range(1, n + 1)}
	for value in sorted(values.keys()):
		print(values[value])

def main(inp=sys.stdin):
	n = int(inp.readline().strip())
	L = [int(x) for x in inp.readline().strip().split()]
	solve(L)
main()


n = int(input())
L = [int(x) - 1 for x in input().split()]
D = {}
for i in range(len(L)):
	x = L[L[i]]
	if x not in D:
		D[x] = i
for i in range(len(L)):
	print(D[i] + 1)


T = int(input())
seq = input().split(' ')
seq_dict = {}
for i in range(1, T + 1):
	seq_dict[seq[i - 1]] = i
for i in range(1, T + 1):
	print(seq_dict[str(seq_dict[str(i)])])


n = int(input())
L = list(map(int, input().split()))
d1 = [(i + 1, L[i]) for i in range(0, len(L))]
d2 = [(L[i], i + 1) for i in range(0, len(L))]
d1 = dict(d1)
d2 = dict(d2)
for x in range(1, n + 1):
	print(d2[d2[x]])


N = int(input())
arr = [int(x) for x in input().split()]
arr.insert(0, 0)
for i in range(1, N + 1):
	for j in range(1, N + 1):
		if arr[arr[j]] == i:
			print(j)
			break


_ = input()
nums = [int(p) for p in input().split()]
aa = set(nums)
for (idx, num) in enumerate(nums):
	p = idx + 1
	if p in aa:
		sec_p = nums.index(p) + 1
		if sec_p in aa:
			print(nums.index(sec_p) + 1)


n = int(input())
arr = list(map(int, input().split(' ')))
dic = dict()
for i in range(1, n + 1):
	dic[arr[i - 1]] = i
for i in range(1, n + 1):
	print(dic[dic[i]])


n = int(input().strip())
arr = [int(arr_temp) for arr_temp in input().strip().split(' ')]
for i in range(1, n + 1):
	j = arr.index(i)
	j = j + 1
	j = arr.index(j)
	print(j + 1)


N = int(input())
arr = [int(val) for val in input().strip().split(' ')]
dic_index = dict()
dic_value = dict()
for i in range(N):
	if i + 1 not in dic_index:
		dic_index[i + 1] = arr[i]
	if arr[i] not in dic_value:
		dic_value[arr[i]] = i + 1
for i in range(N):
	index_x = dic_value[i + 1]
	print(dic_value[index_x])


N = int(input())
p = [int(x) - 1 for x in input().split()]
ans = [0] * N
for i in range(N):
	ans[p[p[i]]] = i
for i in range(N):
	print(ans[i] + 1)


n = int(input())
x = [int(c_temp) for c_temp in input().strip().split(' ')]
for i in range(1, n + 1):
	temp = x.index(i) + 1
	temp = x.index(temp) + 1
	print(temp)


def find_inv(p, n, ele):
	for x in range(n):
		if ele == p[x]:
			return x
n = int(input())
p = input().split()
p = [int(x) for x in p]
for x in range(n):
	res = find_inv(p, n, x + 1)
	res += 1
	print(find_inv(p, n, res) + 1)


N = int(input())
seq = [int(x) - 1 for x in input().split()]
p = {}
for i in range(N):
	p[seq[i]] = i
for i in range(N):
	print(p[p[i]] + 1)


n = int(input())
l = list(map(int, input().split()))
for x in range(1, n + 1):
	i = l.index(x) + 1
	ans = l.index(i) + 1
	print(ans)


n = int(input())
p = list(map(int, input().split()))
for i in range(1, n + 1):
	for y in range(n):
		if p[p[y] - 1] == i:
			print(y + 1)
			break


(_, vals) = (input(), [int(x) for x in input().split()])
for x in range(1, len(vals) + 1):
	p_y = vals.index(x) + 1
	p_p_y = vals.index(p_y) + 1
	print(p_p_y)


n = int(input())
A = [int(x) for x in input().strip().split(' ')]
for i in range(1, n + 1):
	ind_1 = A.index(i)
	ind_2 = A.index(ind_1 + 1)
	print(ind_2 + 1)


n = int(input())
values = input().strip().split(' ')
for i in range(n):
	index1 = values.index(str(i + 1)) + 1
	finindex = values.index(str(index1)) + 1
	print(str(finindex))


n = int(input())
lst = list(map(int, input().split()))
for i in range(1, n + 1):
	y = lst.index(i) + 1
	index = lst.index(y)
	print(index + 1)


t = int(input())
arr = [int(i) for i in input().split()]
ans = [0] * t
for i in range(t):
	ans[arr[arr[i] - 1] - 1] = i + 1
for i in range(t):
	print(ans[i])


n = input()
p_s = input().strip().split(' ')
p = [int(s) for s in p_s]
d = {y: i + 1 for (i, y) in enumerate(p)}
n = len(p)
for x in range(1, n + 1):
	if x in d:
		i = d[x]
		if i in d:
			i = d[i]
			print(i)


n = int(input())
p = [int(x) for x in input().strip().split(' ')]
p.insert(0, '')
for i in range(1, n + 1):
	if i in p:
		if p.index(i) in p:
			print(p.index(p.index(i)))


n = int(input().strip())
p = [int(i) for i in input().strip().split()]
d = dict(zip(p, range(1, n + 1)))
for i in range(1, n + 1):
	print(d[d[i]])


n = int(input())
nums = list(map(int, input().split()))
d = {}
for (i, num) in enumerate(nums):
	d[num] = i + 1
for i in range(1, n + 1):
	print(d[d[i]])


n = int(input())
dict = {}
a = [int(e) for e in input().split()]
for i in range(n):
	dict[a[a[i] - 1]] = i + 1
for j in range(1, n + 1):
	print(dict[j])


n = int(input())
a = list(map(int, input().split()))
b = [0] * (n + 1)
c = [0] * (n + 1)
for i in range(1, n + 1):
	b[i] = a[a[i - 1] - 1]
	c[b[i]] = i
for i in range(1, n + 1):
	print(c[i])


n = int(input(''))
in1 = input('')
in1 = in1.split(' ')
dict1 = {}
for (i, j) in enumerate(in1):
	dict1[int(j)] = int(i + 1)
for (i, j) in sorted(dict1.items()):
	x1 = dict1[i]
	x2 = dict1[x1]
	print(x2)


import sys
n = int(input())
p = [int(x) for x in input().strip().split(' ')]
for i in range(1, n + 1):
	for j in p:
		if p[j - 1] == i:
			y = p.index(j)
			print(y + 1)


n = int(input().strip())
array = [int(x) for x in input().strip().split(' ')]
dic = {}
for i in range(n):
	dic[i + 1] = array[i]
new_dic = {}
for key in dic:
	try:
		new_dic[key] = dic[dic[key]]
	except:
		continue
result = {}
for key in new_dic:
	result[new_dic[key]] = key
for key in sorted(result):
	print(result[key])


n = int(input())
arr = [int(x) for x in input().split()]
for i in range(1, n + 1):
	loking_who_returns = arr.index(i) + 1
	print(arr.index(loking_who_returns) + 1)


def apply(p):
	n = len(p)
	return [p[p[i] - 1] for i in range(n)]
n = int(input().strip())
arr = [int(i) for i in input().strip().split()]
arr = apply(arr)
for i in range(1, n + 1):
	print(arr.index(i) + 1)


n = int(input())
values_s = input().split()
values = []
for i in values_s:
	values.append(int(i))
for i in range(n):
	for j in values:
		if i + 1 == j:
			k = values.index(j) + 1
			for l in values:
				if l == k:
					index = values.index(l) + 1
					print(index)


n = int(input())
a = [int(a) for a in input().strip().split()]
for x in range(1, n + 1):
	y1 = a.index(x) + 1
	y = a.index(y1) + 1
	print(y)


n = int(input())
arr = [int(i) for i in input().split()]
arr = [0] + arr
for x in range(1, n + 1):
	i = arr.index(x)
	j = arr.index(i)
	print(j)


n = int(input())
array = [int(i) for i in input().split()]
for i in range(1, n + 1):
	a = array.index(i)
	b = array.index(a + 1)
	print(b + 1)


n = int(input())
l = list(map(int, input().split()))
for i in range(1, n + 1):
	print(l.index(l.index(i) + 1) + 1)


n = int(input())
numbers = [int(x) for x in input().split()]
for i in range(0, n):
	print(numbers.index(numbers.index(i + 1) + 1) + 1)


n = int(input().strip())
a = [int(t) for t in input().strip().split()]
for i in range(1, n + 1):
	for r in range(n):
		if i == a[r]:
			for h in range(n):
				if r + 1 == a[h]:
					print(h + 1)


def solve(n, p):
	sp = set(p)
	for x in range(n):
		if x + 1 not in sp:
			continue
		px = p.index(x + 1)
		if px + 1 not in sp:
			continue
		print(p.index(px + 1) + 1)
n = int(input())
p = list(map(int, input().split(' ')))
solve(n, p)


n = int(input())
values = [int(el) for el in input().split()]
i = 1
dic = {}
for value in values:
	dic[i] = value
	i += 1
for i in range(1, n + 1):
	for key in dic:
		if dic[key] == i:
			for key1 in dic:
				if dic[key1] == key:
					print(key1)
					break
			break


num = int(input())
arr = [int(temp) for temp in input().strip().split(' ')]
dict1 = {}
i = 1
for val in arr:
	dict1[val] = i
	i = i + 1
for j in range(1, i):
	if dict1[j] is None:
		continue
	k = dict1[j]
	if dict1[k] is not None:
		print(dict1[k])


x = int(input())
line = list(map(int, input().split(' ')))
for i in range(x):
	print(line.index(line.index(i + 1) + 1) + 1)


t = int(input().strip())
p = [int(x) for x in input().split()]
for i in range(1, t + 1):
	print(p.index(p.index(i) + 1) + 1)


n = int(input())
nums = [0]
nums.extend([int(i) for i in input().strip().split()])
for x in range(1, n + 1):
	index1 = nums.index(x)
	index2 = nums.index(index1)
	print(index2)


n = int(input())
values = [int(_) for _ in input().split(' ')]
for i in range(1, n + 1):
	indx = values.index(i) + 1
	print(values.index(indx) + 1)


n = int(input().strip())
a = [int(b) for b in input().strip().split(' ')]
for i in range(1, n + 1):
	for j in range(n):
		if i == a[j]:
			for k in range(n):
				if j + 1 == a[k]:
					print(k + 1)


input()
p = [0] + [int(x) for x in input().split()]
for x in range(1, len(p)):
	for y in range(1, len(p)):
		if p[p[y]] == x:
			print(y)
			break


n = int(input())
seq = list(map(int, input().split()))
dct = dict()
for (idx, item) in enumerate(seq):
	dct[item] = idx + 1
for i in range(1, n + 1):
	ans = dct[dct[i]]
	print(ans)


n = int(input())
p = [int(temp) for temp in input().strip().split(' ')]
for i in range(1, n + 1):
	for j in range(n):
		if i == p[j]:
			x = j + 1
			break
	for k in range(n):
		if x == p[k]:
			z = k + 1
			break
	print(z)


x = int(input())
y = input().split(' ')
for j in range(1, x + 1):
	for i in range(1, x + 1):
		if int(y[int(y[i - 1]) - 1]) == j:
			print(i)


n = int(input())
p = [int(x) for x in input().split(' ')]
p_x = {}
inv_x = {}
for (index, elem) in enumerate(p):
	p_x[index + 1] = elem
	inv_x[elem] = index + 1
for x in range(1, n + 1):
	print(inv_x[inv_x[x]])


n = input().strip()
s = input().strip().split(' ')
for i in range(0, len(s)):
	x = i + 1
	firs_index = s.index(str(x)) + 1
	sec_index = s.index(str(firs_index)) + 1
	print(sec_index)


a = int(input())
lis1 = [int(i) for i in input().split(' ')]
lis2 = sorted(lis1)
for i in lis2:
	for j in range(len(lis1)):
		if i == lis1[j]:
			s = j + 1
			for k in range(len(lis1)):
				if s == lis1[k]:
					print(k + 1)


n = int(input())
p = list(map(int, input().split()))
for i in range(1, n + 1):
	print(p.index(p.index(i) + 1) + 1)


n = int(input().strip())
l = input().strip().split()
output = []
d = dict()
for i in range(n):
	d[int(l[i])] = i + 1
for j in range(1, n + 1):
	x = d[j]
	y = d[x]
	output.append(str(y))
print('\n'.join(output))


scn = int(input())
num_list = []
numbers = input()
num_list = numbers.split(' ')
for x in range(1, scn + 1):
	index = num_list.index(str(x))
	try:
		num_index = index + 1
		px_index = num_list.index(str(num_index))
		print(px_index + 1)
	except Exception as e:
		pass


n = int(input())
l = [int(i) for i in input().split()]
d = {}
for i in range(n):
	d[l[i]] = i + 1
for i in range(n):
	print(d[d[i + 1]])


n = int(input())
S = list(map(int, input().split(' ')))
Q = list(S)
S.sort()
for i in range(1, n + 1):
	for j in range(0, n):
		if i == Q[j]:
			a = j + 1
			for w in range(0, n):
				if a == Q[w]:
					print(w + 1)
					break


n = int(input())
arr = list(map(int, input().split()))
for i in range(1, len(arr) + 1):
	for j in range(len(arr)):
		if arr[arr[j] - 1] == i:
			print(j + 1)
			break


N = int(input())
intList = [int(x) for x in input().strip().split(' ')]
for x in range(1, len(intList) + 1):
	py = intList.index(x) + 1
	ppy = intList.index(py) + 1
	print(ppy)


n = int(input())
li = {}
a = input().split(' ')
a = list(map(int, a))
for i in range(1, n + 1):
	li.update({i: a[i - 1]})
for i in range(1, n + 1):
	for (k, v) in li.items():
		if i == v:
			b = k
			for (k, v) in li.items():
				if b == v:
					print(k)


n = int(input().strip())
p = [0] + list(map(int, input().strip().split(' ')))
q = [0] + [p.index(i) for i in range(1, n + 1)]
r = [0] + [p.index(q[i]) for i in range(1, n + 1)]
for i in range(1, n + 1):
	print(r[i])


n = int(input().strip())
p = [int(s) for s in input().strip().split(' ')]
p1 = [p[i - 1] for i in p]
p2 = list(zip(p1, range(1, n + 1)))
p2.sort()
p3 = [j for (i, j) in p2]
for i in p3:
	print(i)


n = int(input())
c = input()
p = {}
k = 1
p[0] = 0
for i in c.split(' '):
	p[k] = int(i)
	k += 1
for x in range(1, n + 1):
	for j in range(1, n + 1):
		if p[p[j]] == x:
			print(j)
			break


n = int(input())
l = list(input().split())
for i in range(1, n + 1):
	for j in range(n):
		if i == int(l[j]):
			for k in range(n):
				if j + 1 == int(l[k]):
					print(k + 1)


import sys
n = int(input())
arr = [-1]
for x in input().strip().split(' '):
	arr.append(int(x))
results = {}
for x in range(1, n + 1):
	results[arr[arr[x]]] = x
for a in range(1, n + 1):
	print(results[a])


count = int(input())
arr = input().split()

def find(iterable, number):
	iter = 0
	while int(iterable[iter]) != number:
		iter += 1
	return iter + 1
for i in range(1, count + 1):
	print(find(arr, find(arr, i)))


n = int(input().strip())
p = list(map(int, input().strip().split(' ')))
q = []
for i in range(n):
	for j in range(n):
		if p[j] == i + 1:
			for k in range(n):
				if p[k] == j + 1:
					q.append(k + 1)
					break
			break
for i in q:
	print(i)


n = int(input())
nums = [int(x) for x in input().strip().split()]
ifoo = {}
for (i, x) in enumerate(nums):
	ifoo[x] = i + 1
for i in range(1, n + 1):
	t1 = ifoo[i]
	t2 = ifoo[t1]
	print(t2)


n = int(input())
values = list(map(int, input().split()))
for i in range(1, n + 1):
	print(values.index(values.index(i) + 1) + 1)