Problem:


Score: 400 points

Problem Statement

The Kingdom of Takahashi has N towns, numbered 1 through N.

There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.

Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Help the king by writing a program that answers this question.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

If we start at Town 1 and use the teleporter 5 times, our travel will be as follows: 1 \to 3 \to 4 \to 1 \to 3 \to 4.

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2



Sample input:
4 5
3 2 4 1


Sample output:
4

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
A = list(map(int,input().split()))

check = [0] * N #町を訪問するのが初めてか
check[0] = 1
li = [] #町の移動順
li.append(1)
i, j = 1, 1 #現在の町の番号と移動回数

while True:
  k = A[i-1] #移動先の町の番号
  if check[k-1] == 1:
    #前に訪問したことがある場合
    n = li.index(k) #ループに入った時点での移動回数
    break
  check[k-1] = 1
  li.append(k)
  i = k
  j += 1

if K <= n-1: print(li[K])
else:
  if j-n > 0:
    print(li[((K - (n-1)) % (j-n)) + n - 1])
  else:
    print(li[((K - (n-1)) % (j)) + n - 1])

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
A = list(map(int,input().split()))

check = [0] * N #町を訪問するのが初めてか
check[0] = 1
li = [] #町の移動順
li.append(1)
i, j = 1, 1 #現在の町の番号と移動回数

while True:
  k = A[i-1] #移動先の町の番号
  if check[k-1] == 1:
    #前に訪問したことがある場合
    n = li.index(k) #ループに入った時点での移動回数
    break
  check[k-1] = 1
  li.append(k)
  i = k
  j += 1

if K <= n-1: print(li[K])
else: print(li[((K - (n-1)) % (j-n)) + n - 1])

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
A = list(map(int,input().split()))

check = [0] * N
li = []
check[0], i, j = 1, 1, 1
li.append(1)
#K % N 

while True:
  k = A[i-1]
  if check[k-1] == 1:
    n = li.index(k)
    break
  check[k-1] = 1
  li.append(k)
  i = k
  j += 1
if K <= n-1: print(li[K])
else: print(li[(K - (n - 1)) % (j-n) - 1 + n])

Submission status: Wrong Answer

Solution code:
N, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
check = [0]*N
ima = 0

for i in range(K):
  if check[A[ima]-1] != 0:
    loopen = ima
    check[ima] = i+1
    looplen = check[ima] - check[A[ima]-1] + 1
    loopend = i
    break
  if i == K-1:
    print(ima+1)
    exit()
  check[ima] = i+1
  ima = A[ima]-1

offset = (K-(i-looplen)-1)%looplen
ima = 0
for i in range(loopend-looplen+offset+1):
  ima = A[ima]-1
print(ima+1)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
visit = [False for i in range(n)]
loop = 0
now = 1
visit[0] = True
while True:
  now = a[now - 1]
  loop += 1
  if visit[now-1]:
    break
  visit[now-1] = True
p = now
leng = 0
while True:
  p = a[p - 1]
  leng += 1
  if p == now:
    break 
d = loop - leng
cnt = (k-d)%(leng) + d
  
now = 1
for i in range(cnt):
  now = a[now-1]
print(now)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
visit = [False for i in range(n)]
loop = 0
now = 1
visit[0] = True
while True:
  now = a[now - 1]
  loop += 1
  if visit[now-1]:
    break
  visit[now-1] = True
p = now
leng = 0
while True:
  p = a[p - 1]
  leng += 1
  if p == now:
    break 

cnt = (loop-leng) + (k-(loop-leng))%leng
now = 1
for i in range(cnt):
  now = a[now-1]
print(now)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
visit = [False for i in range(n)]
loop = 0
now = 1
visit[0] = True
while True:
  now = a[now - 1]
  loop += 1
  if visit[now-1]:
    break
  visit[now-1] = True
p = 1


leng = 0
while True:
  p = a[p - 1]
  leng += 1
  if p == now:
    break 
if leng != loop:
  cnt = leng + (k-leng)%(loop - leng)
else:
  cnt = k%loop
  
now = 1
for i in range(cnt):
  now = a[now-1]
print(now)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
visit = [False for i in range(n)]
loop = 0
now = 1
visit[0] = True
while True:
  now = a[now - 1]
  loop += 1
  if visit[now-1]:
    break
  visit[now-1] = True
p = 1


leng = 0
while True:
  p = a[p - 1]
  leng += 1
  if p == now:
    break 
if leng != loop:
  cnt = k%(loop - leng)
else:
  cnt = k%loop
  
now = 1
for i in range(cnt + (loop - leng)):
  now = a[now-1]
print(now)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
visit = [False for i in range(n)]
loop = 0
now = 1
visit[0] = True
while True:
  now = a[now - 1]
  loop += 1
  if visit[now-1]:
    break
  visit[now-1] = True
p = 1
leng = 0
while True:
  p = a[p - 1]
  leng += 1
  if p == now:
    break 
if leng != loop:
  cnt = k%(loop - leng) + leng+1
else:
  cnt = k%loop
  
now = 1
for i in range(cnt):
  now = a[now-1]
print(now)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = [0] + list(map(int,input().split()))

i = 1
pas = [i]
k = K
while k > 0:
    if A[i] in pas:
#         print(i,pas)
        break
    pas.append(A[i])
    i = A[i]
    k -= 1
if k == 0:
    print(pas[-2])
else:
    roopindx = pas.index(A[i])
    roopn = (K-roopindx)%(len(pas)-roopindx)
    print(pas[roopindx+roopn])

Submission status: Wrong Answer

Solution code:
from collections import deque

N, K = map(int, input().split())
towns = [0] + list(map(int, input().split()))
checked = [0 for _ in range(N+1)]
next = towns[1]
count = 1
    
while(checked[next] == 0):
  checked[next] = count
  count += 1
  next = towns[next]
loop = count - checked[next]
pos = 0
if K > count:
  pos = checked[next] + (K-checked[next])%loop
else:
  pos = K
for i in range(1, N+1):
  if checked[i] == pos:
    print(i)
    exit()

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))

tele = [1]
went = set(tele)
now = 1
cnt = 0
flag = True

for i in range(k):
  now = a[now-1]
  if i != k-1 and now in went:
    tele = tele[tele.index(now):]
    cnt = len(tele)
    flag = False
    break
  tele.append(now)
  went.add(now)

if flag == True:
  print(tele[-1])

else:
  cnt = k % cnt
  print(tele[cnt])

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))

tele = [1]
went = set(tele)
now = 1
cnt = 0
flag = True

for i in range(k):
  now = a[now-1]
  if now in went:
    tele = tele[tele.index(now):]
    cnt = len(tele)
    flag = False
    break
  tele.append(now)
  went.add(now)

if flag == True:
  print(tele[-1])

else:
  cnt = k % cnt
  print(tele[cnt])

print(tele)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))

tele = [1]
went = set(tele)
now = 1
cnt = 0
flag = True

for i in range(k):
  now = a[now-1]
  if now in went:
    went = set(tele[tele.index(now):])
    cnt = len(went)
    flag = False
    break
  tele.append(now)
  went.add(now)

if flag == True:
  print(tele[-1])

else:
  cnt = k % cnt
  print(tele[cnt])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(lambda x:int(x)-1,input().split()))
i = 0
visited = set([0])

while K > 0:
    i = A[i]
    K -= 1
    if i in visited:
        break
    visited.add(i)
if K == 0:
    print(i)
    exit(0)
ls = [i]
j = i
while True:
    j = A[j]
    if j == i:
        break
    ls.append(j)
print(ls[K % len(ls)]+1)

Submission status: Wrong Answer

Solution code:
def delete_head_zeros(n):
	n = str(n)
	l = len(n)
	if "." in n:
		l = n.find(".")
	head_zeros = 0
	for i in range(l - 1):
		if n[i] == "0":
			head_zeros += 1
		else:
			break

	return n[head_zeros:]

# compare a, b
# a, b: int or string
def bigint_compare(a, b):
	a = delete_head_zeros(a)
	b = delete_head_zeros(b)
	if len(a) > len(b):
		return 1
	elif len(a) < len(b):
		return -1
	else:
		if a > b:
			return 1
		elif a < b:
			return -1
		else:
			return 0

# calculate a + b
# a, b: int or string
def bigint_plus(a, b):
	a = str(a)
	b = str(b)

	d = max([len(a), len(b)])
	a = '0' * (d - len(a)) + a
	b = '0' * (d - len(b)) + b

	ans = ""
	carry = 0
	for i in range(d):
		s = int(a[-i-1]) + int(b[-i-1]) + carry
		carry = s // 10
		ans = str(s % 10) + ans
	else:
		if carry:
			ans = str(carry) + ans

	return ans

# calculate a - b
# a, b: int or string
def bigint_minus(a, b): 
	a = str(a)
	b = str(b)
	M = []
	m = []
	sign = ""

	if len(a) > len(b) or (len(a) == len(b) and a >= b):
		[M, m] = [a, b]
	else:
		[M, m] = [b, a]
		sign = "-"
	m = '0' * (len(M) - len(m)) + m

	ans = ""
	borrow = 0
	for i in range(len(M)):
		s = int(M[-i-1]) - int(m[-i-1]) - borrow
		if s < 0:
			borrow = 1
			s += 10
		else:
			borrow = 0
		ans = str(s) + ans

	return sign + delete_head_zeros(ans)

# calculate a * b
# a, b: int or string
def bigint_multiply(a, b):
	a = str(a)
	b = str(b)

	md = []
	for j in range(len(b)):
		carry = 0
		mj = ""
		for i in range(len(a)):
			m = int(a[-i-1]) * int(b[-j-1]) + carry
			carry = m // 10
			mj = str(m % 10) + mj
		else:
			if carry:
				mj = str(carry) + mj
		md.append(mj)

	ans = 0
	for k in range(len(md)):
		ans = bigint_plus(md[k] + "0" * k, ans)

	return ans


# calculate a / b to d digits after decimal point
# a, b, d: int or string
def bigint_divide(a, b, d = 0):
	a = str(a)
	b = str(b)
	d = int(d)
	if d < 0:
		d = 0

	ans = ""
	r = ""
	for i in range(len(a) + d):
		q = 0
		if i < len(a):
			r += a[i]
		elif i == len(a):
			ans += "."
			r += "0"
		else:
			r += "0"

		if bigint_compare(r, b) == -1:
			ans += str(q)
		else:
			while bigint_compare(r, b) >= 0:
				r = bigint_minus(r, b)
				q += 1
			ans += str(q)

	return delete_head_zeros(ans)

def main():
	N, K = input().split(" ")
	A = [int(a) - 1 for a in input().split(" ")]
	visited = [0] * int(N)
	towns = []
	now = 0
	while visited[now] == 0:
		towns.append(now)
		visited[now] = 1
		now = A[now]
	else:
		loop_begin_idx = towns.index(now)
		loop_end_idx = len(towns) - 1
		loop_n = loop_end_idx - loop_begin_idx + 1
	if int(K) <= len(towns) + 1:
		print(towns[int(K) - 1] + 1)
		return 0
	K_from_loop = bigint_minus(K, loop_begin_idx)
	q = bigint_divide(K_from_loop, loop_n)
	r = bigint_minus(K_from_loop, bigint_multiply(loop_n, q))
	print(towns[loop_begin_idx + int(r)] + 1)

main()

Submission status: Wrong Answer

Solution code:
def delete_head_zeros(n):
	n = str(n)
	l = len(n)
	if "." in n:
		l = n.find(".")
	head_zeros = 0
	for i in range(l - 1):
		if n[i] == "0":
			head_zeros += 1
		else:
			break

	return n[head_zeros:]

# compare a, b
# a, b: int or string
def bigint_compare(a, b):
	a = delete_head_zeros(a)
	b = delete_head_zeros(b)
	if len(a) > len(b):
		return 1
	elif len(a) < len(b):
		return -1
	else:
		if a > b:
			return 1
		elif a < b:
			return -1
		else:
			return 0

# calculate a + b
# a, b: int or string
def bigint_plus(a, b):
	a = str(a)
	b = str(b)

	d = max([len(a), len(b)])
	a = '0' * (d - len(a)) + a
	b = '0' * (d - len(b)) + b

	ans = ""
	carry = 0
	for i in range(d):
		s = int(a[-i-1]) + int(b[-i-1]) + carry
		carry = s // 10
		ans = str(s % 10) + ans
	else:
		if carry:
			ans = str(carry) + ans

	return ans

# calculate a - b
# a, b: int or string
def bigint_minus(a, b): 
	a = str(a)
	b = str(b)
	M = []
	m = []
	sign = ""

	if len(a) > len(b) or (len(a) == len(b) and a >= b):
		[M, m] = [a, b]
	else:
		[M, m] = [b, a]
		sign = "-"
	m = '0' * (len(M) - len(m)) + m

	ans = ""
	borrow = 0
	for i in range(len(M)):
		s = int(M[-i-1]) - int(m[-i-1]) - borrow
		if s < 0:
			borrow = 1
			s += 10
		else:
			borrow = 0
		ans = str(s) + ans

	return sign + delete_head_zeros(ans)

# calculate a * b
# a, b: int or string
def bigint_multiply(a, b):
	a = str(a)
	b = str(b)

	md = []
	for j in range(len(b)):
		carry = 0
		mj = ""
		for i in range(len(a)):
			m = int(a[-i-1]) * int(b[-j-1]) + carry
			carry = m // 10
			mj = str(m % 10) + mj
		else:
			if carry:
				mj = str(carry) + mj
		md.append(mj)

	ans = 0
	for k in range(len(md)):
		ans = bigint_plus(md[k] + "0" * k, ans)

	return ans


# calculate a / b to d digits after decimal point
# a, b, d: int or string
def bigint_divide(a, b, d = 0):
	a = str(a)
	b = str(b)
	d = int(d)
	if d < 0:
		d = 0

	ans = ""
	r = ""
	for i in range(len(a) + d):
		q = 0
		if i < len(a):
			r += a[i]
		elif i == len(a):
			ans += "."
			r += "0"
		else:
			r += "0"

		if bigint_compare(r, b) == -1:
			ans += str(q)
		else:
			while bigint_compare(r, b) >= 0:
				r = bigint_minus(r, b)
				q += 1
			ans += str(q)

	return delete_head_zeros(ans)

def main():
	N, K = input().split(" ")
	A = [int(a) - 1 for a in input().split(" ")]
	visited = [0] * int(N)
	towns = []
	now = 0
	while visited[now] == 0:
		towns.append(now)
		visited[now] = 1
		now = A[now]
	else:
		loop_begin_idx = towns.index(now)
		loop_end_idx = len(towns) - 1
		loop_n = loop_end_idx - loop_begin_idx + 1
	if int(K) <= len(towns) + 1:
		print(towns[int(K) - 1])
		return 0
	K_from_loop = bigint_minus(K, loop_begin_idx)
	q = bigint_divide(K_from_loop, loop_n)
	r = bigint_minus(K_from_loop, bigint_multiply(loop_n, q))
	print(towns[loop_begin_idx + int(r)] + 1)

main()

Submission status: Wrong Answer

Solution code:
def delete_head_zeros(n):
	n = str(n)
	l = len(n)
	if "." in n:
		l = n.find(".")
	head_zeros = 0
	for i in range(l - 1):
		if n[i] == "0":
			head_zeros += 1
		else:
			break

	return n[head_zeros:]

# compare a, b
# a, b: int or string
def bigint_compare(a, b):
	a = delete_head_zeros(a)
	b = delete_head_zeros(b)
	if len(a) > len(b):
		return 1
	elif len(a) < len(b):
		return -1
	else:
		if a > b:
			return 1
		elif a < b:
			return -1
		else:
			return 0

# calculate a + b
# a, b: int or string
def bigint_plus(a, b):
	a = str(a)
	b = str(b)

	d = max([len(a), len(b)])
	a = '0' * (d - len(a)) + a
	b = '0' * (d - len(b)) + b

	ans = ""
	carry = 0
	for i in range(d):
		s = int(a[-i-1]) + int(b[-i-1]) + carry
		carry = s // 10
		ans = str(s % 10) + ans
	else:
		if carry:
			ans = str(carry) + ans

	return ans

# calculate a - b
# a, b: int or string
def bigint_minus(a, b): 
	a = str(a)
	b = str(b)
	M = []
	m = []
	sign = ""

	if len(a) > len(b) or (len(a) == len(b) and a >= b):
		[M, m] = [a, b]
	else:
		[M, m] = [b, a]
		sign = "-"
	m = '0' * (len(M) - len(m)) + m

	ans = ""
	borrow = 0
	for i in range(len(M)):
		s = int(M[-i-1]) - int(m[-i-1]) - borrow
		if s < 0:
			borrow = 1
			s += 10
		else:
			borrow = 0
		ans = str(s) + ans

	return sign + delete_head_zeros(ans)

# calculate a * b
# a, b: int or string
def bigint_multiply(a, b):
	a = str(a)
	b = str(b)

	md = []
	for j in range(len(b)):
		carry = 0
		mj = ""
		for i in range(len(a)):
			m = int(a[-i-1]) * int(b[-j-1]) + carry
			carry = m // 10
			mj = str(m % 10) + mj
		else:
			if carry:
				mj = str(carry) + mj
		md.append(mj)

	ans = 0
	for k in range(len(md)):
		ans = bigint_plus(md[k] + "0" * k, ans)

	return ans


# calculate a / b to d digits after decimal point
# a, b, d: int or string
def bigint_divide(a, b, d = 0):
	a = str(a)
	b = str(b)
	d = int(d)
	if d < 0:
		d = 0

	ans = ""
	r = ""
	for i in range(len(a) + d):
		q = 0
		if i < len(a):
			r += a[i]
		elif i == len(a):
			ans += "."
			r += "0"
		else:
			r += "0"

		if bigint_compare(r, b) == -1:
			ans += str(q)
		else:
			while bigint_compare(r, b) >= 0:
				r = bigint_minus(r, b)
				q += 1
			ans += str(q)

	return delete_head_zeros(ans)

def main():
	N, K = [int(k) for k in input().split(" ")]
	A = [int(a) - 1 for a in input().split(" ")]
	visited = [0] * N
	towns = []
	now = 0
	while visited[now] == 0:
		towns.append(now)
		visited[now] = 1
		now = A[now]
	else:
		loop_begin_idx = towns.index(now)
		loop_end_idx = len(towns) - 1
		loop_n = loop_end_idx - loop_begin_idx + 1
	if K <= len(towns) + 1:
		print(towns[K - 1])
		return 0
	K_from_loop = bigint_minus(K, loop_begin_idx)
	q = bigint_divide(K_from_loop, loop_n)
	r = bigint_minus(K_from_loop, bigint_multiply(loop_n, q))
	print(towns[loop_begin_idx + int(r)] + 1)

main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(lambda x: int(x)-1, input().split()))
path = [-1] * N
nex = A[0]
path[0] = 0
cnt = 1
if K > N:
    while path[nex] == -1:
        path[nex] = cnt
        cnt += 1
        nex = A[nex]

    mod = cnt - path[nex]
    n = path[nex] + (K-path[nex])%mod
else:
    n = N

ans = 0
for _ in range(n):
    ans = A[ans]
ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(lambda x: int(x)-1, input().split()))
path = [-1] * N
nex = A[0]
path[0] = 0
cnt = 1
while path[nex] == -1:
    path[nex] = cnt
    cnt += 1
    nex = A[nex]

mod = cnt - path[nex]
n = path[nex] + max(0, K-path[nex])%mod
ans = 0
for _ in range(n):
    ans = A[ans]
ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(lambda x: int(x)-1, input().split()))
contains = [-1] * N
nex = A[0]
contains[0] = 0
cnt = 1
while contains[nex] == -1:
    contains[nex] = cnt
    cnt += 1
    nex = A[nex]

mod = cnt - contains[nex]
n = contains[nex] + (K-contains[nex])%mod
ans = 0
for _ in range(n):
    ans = A[ans]
ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
f=[0]*N
l,r=[],[]
p=0
while f[p]<2:
    if f[p]:
        r.append(A[p])
        K+=1
    else:
        l.append(A[p])
        K-=1
    f[p]+=1
    p=A[p]-1
print(r[K%len(r)-1])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
f=[0]*N
l,r=[],[]
p=0
while f[p]<2:
    if f[p]:
        r.append(A[p])
        K-=1
    else:
        l.append(A[p])
        K+=1
    f[p]+=1
    p=A[p]-1
print(r[K%len(r)-1])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
f=[0]*N
l,r=[],[]
p=0
while f[p]<2:
    p=A[p]-1
    if f[p]:r.append(A[p])
    else:
        l.append(A[p])
    f[p]+=1
K-=(len(l)-len(r))
print(r[K%len(r)-1])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
f=[0]*N
l,r=[],[]
p=0
while f[p]<2:
    if f[p]:r.append(A[p])
    else:
        l.append(A[p])
    f[p]+=1
    p=A[p]-1
K-=(len(l)-len(r))
print(r[K%len(r)-1])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
f=[0]*N
l,r=[],[]
p=0
while f[p]<2:
    if f[p]:r.append(A[p])
    else:l.append(A[p])
    f[p]+=1
    p=A[p]-1
print(l[((K-len(r))%len(r))%len(l)-1])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
button = list(map(int,input().split()))
visit = [-1]*n
hisitory = [-1]
push = 0
f = 0

for i in range(k):
    visit[push] = i+1
    hisitory.append(push)
    push = button[push] -1
    if(visit[push] != -1):  
        f = 1
        start = visit[push]
        last=push
        end = i+1
        break
if(f == 0):
    print(hisitory[-1]+1)
else:
    geta = start -1
    loopk = k - geta
    loop = end -start + 1
    modk = loopk%loop
    if(modk == 0):
        modk = loop
    print(button[hisitory[geta + modk]])
    # print(hisitory)
    # print(geta,modk,loop)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
button = list(map(int,input().split()))
visit = [-1]*n
hisitory = []
push = 0
f = 0
for i in range(k):
    visit[push] = i
    hisitory.append(push)
    push = button[push] -1
    if(visit[push] != -1):  
        f = 1
        start = visit[push]
        last=push
        end = i
        break
if(f == 0):
    print(hisitory[-1]+1)
else:
    loop = end - start +1
    geta = start-1
    kmod = (k-start+1)%loop
    if(kmod == 0):
        kmod = loop
    print(hisitory[geta + kmod]+1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

visited = [0 for _ in range(N)]
first_visit = [0 for _ in range(N)]

now = 0
flag = True
for i in range(10 ** 5 * 3):
    if first_visit[now] == 0:
        first_visit[now] = i

    visited[A[now] - 1] += 1
    now = A[now] - 1

    if i == K - 1:
        print(now + 1)
        flag = False
        break

if flag:
    num = 0
    for i in range(N):
        if visited[i] > 2:
            num += 1

    for i in range(N):
        if visited[i] > 2:
            if K % num == first_visit[i] % num:
                print(i + 1)
                break

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

visited = [0] * N
visited[0] = 1
loop = -1
loopTown = -1

for i in range(N):
    if visited[A[i] - 1]:
        loopTown = A[i] - 1
        loop = i - visited[A[i] - 1] + 2
        break
    else: visited[i] = i + 2

currentTown = 0
# print(loopTown)

while currentTown != loopTown and K > 0:
    currentTown = A[currentTown] - 1
    # print(currentTown)
    K -= 1

if loopTown != -1 and loop <= K: K %= loop

while K > 0:
    currentTown = A[currentTown] - 1
    K -= 1

print(currentTown + 1)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
town = list(map(int,input().split()))
visit = [[0,-1]]*n
teleport = [0]
go = 0
visit[0] = [1,-1]
loop = 0
for i in range(k):
    go = town[go] - 1
    if(visit[go][0] != 0):
        teleport.append(go)
        loop = 1
        break
    teleport.append(go)
    visit[go] = [1,i]
# print(visit)
# print(teleport)
if(loop == 0):
    print(teleport[-1]+1)
else:
    loop = visit[teleport[-2]][1] -visit[teleport[-1]][1] + 1
    print(teleport[k%(loop)]+1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

visited = [0] * N
visited[0] = 1
loop = -1
loopTown = -1

for i in range(N):
    if visited[A[i] - 1]:
        loopTown = A[i] - 1
        loop = i - visited[A[i] - 1] + 2
        break
    else: visited[i] = i + 2

currentTown = 0
# print(loopTown)

while currentTown != loopTown and K > 0:
    currentTown = A[currentTown] - 1
    # print(currentTown)
    K -= 1

if loopTown != -1: K %= loop

while K > 0:
    currentTown = A[currentTown] - 1
    K -= 1

print(currentTown + 1)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))

ls = [1]
i = 0
while True:
  if A[i] in ls:
    cnt = ls.index(A[i])
    n_ls = ls[cnt:]
    break
  ls.append(A[i])
  i = A[i]-1
  
if cnt > K:
  print(ls[K-1])
else:
  K = (K - cnt) % len(n_ls)
  print(n_ls[K])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
ex = 0
B = [1]
for i in range(N):
    b = A[ex]
    if b == 1:
        break
    else:
        B.append(b)
        ex = A[ex] - 1
n = len(B)
ans = B[K % n]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

input = sys.stdin.readline
N, K = map(int, input().split())
A = list(map(int, input().split()))

visited = set()
q = deque()
q.append(1)

root = []
while q:
    pos = q.pop()
    if pos in visited:
        start = root.index(pos)
        s_len = len(root[:start])
        root = root[start:]
        break
    visited.add(pos)
    root.append(pos)

    q.append(A[pos-1])

c = len(root)
print(root[(K - s_len) % c])

Submission status: Wrong Answer

Solution code:
n,k=[int(i) for i in input().split()]

a=[int(i)-1 for i in input().split()]

circle=[0]
for i in range(n):
    circle.append(a[circle[-1]])

# print(circle)


def get_head_double(circle):
    check=[0]*(n)
    for i in circle:
        check[i]+=1
    for i in circle:
        if check[i]>=2:
            ans=i
            break
    ans_list=[]
    while len(ans_list)<2:
        for i,j in enumerate(circle):
            if j==ans:
                ans_list.append(i)
    return ans_list

ans_list=get_head_double(circle)
circle_front=circle[:ans_list[0]]
circle_back=circle[ans_list[0]:ans_list[1]]

if len(circle_front)>k:
    print(circle_front[k])
else:
    k=k-len(circle_front)
    print(circle_back[k%len(circle_back)]+1)

Submission status: Wrong Answer

Solution code:
def resolve():
    N, K = list(map(int, input().split()))
    A = list(map(lambda x: int(x)-1, input().split()))
    routes = [0]
    current = 0
    visited = [False for _ in range(N)]
    visited[0] = True
    while visited[A[current]] is False:
        current = A[current]
        routes.append(current)
        visited[current] = True
    leftlen = routes.index(A[current])
    repeat = routes[leftlen:]
    # print(leftlen)
    # print(routes)
    # print(repeat)
    print(repeat[(K-leftlen)%len(repeat)]+1 if K >= len(routes) else routes[K])


if '__main__' == __name__:
    resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    N, K = list(map(int, input().split()))
    A = list(map(lambda x: int(x)-1, input().split()))
    routes = [0]
    current = 0
    visited = [False for _ in range(N)]
    visited[0] = True
    while visited[A[current]] is False:
        current = A[current]
        routes.append(current)
        visited[current] = True
    leftlen = routes.index(A[current])
    repeat = routes[leftlen:]
    # print(leftlen)
    # print(routes)
    # print(repeat)
    print(repeat[(K-leftlen)%len(repeat)]+1 if K > leftlen else routes[K])


if '__main__' == __name__:
    resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    N, K = list(map(int, input().split()))
    A = list(map(lambda x: int(x)-1, input().split()))
    routes = [0]
    current = 0
    visited = [False for _ in range(N)]
    visited[0] = True
    while visited[A[current]] is False:
        current = A[current]
        routes.append(current)
        visited[current] = True
    leftlen = routes.index(A[current])
    repeat = routes[leftlen:]
    # print(leftlen)
    # print(routes)
    # print(repeat)
    print(repeat[(K-leftlen)%len(repeat)]+1)


if '__main__' == __name__:
    resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    N, K = list(map(int, input().split()))
    A = list(map(lambda x: int(x)-1, input().split()))
    routes = [0]
    current = 0
    while A[current] not in routes:
        current = A[current]
        routes.append(current)
    leftlen = routes.index(A[current])
    repeat = routes[leftlen:]
    # print(leftlen)
    # print(routes)
    # print(repeat)
    print(repeat[(K-leftlen)%len(repeat)]+1)


if '__main__' == __name__:
    resolve()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
c=[];u=0
for i in range(n):
    c.append(a[u])
    u=a[u]-1
b=c.index(c[-1])+1
ans=(k-b)%(n-b)
print(c[b+ans-1])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
c=[1];u=0
for i in range(n):
    c.append(a[u])
    u=a[u]-1
b=c.index(c[-1])
ans=(k-b)%(n-b)
print(c[b+ans])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

A = [-1] + A

loop_pos = None
loop_start_time = None
loop_end_time = None
visited = [-1]*(N+1)
pos = 1
i = 0
while visited[pos] == -1:
    visited[pos] = i
    pos = A[pos]
    i += 1  
    if visited[pos] != -1:
        loop_pos = pos
        loop_start_time = visited[pos]
        loop_end_time = i

# print(loop_start_time, loop_end_time)
w = loop_end_time - loop_start_time
K = loop_start_time + (K-loop_start_time)%w
# print(f"K: {K}")

pos = 1
for i in range(K):
    # print(pos)
    pos = A[pos]
print(pos)

Submission status: Wrong Answer

Solution code:
# import sys
# input = sys.stdin.readline

def main():
    n, k = input_list()
    a = input_list()
    goto = [a[0]]
    index = 0
    looped = []
    g = 0
    for i in range(k):
        g = goto[index]
        # print(goto)
        if a[g-1] in goto:
            # print("goto!", a[g-1], goto)
            break
        goto.append(a[g-1])
        index += 1

    s = goto.index(a[g-1])
    looped = goto[s:]

    if k % len(looped) == 0:
        print(goto[-1])
    else:
        k -= len(goto) - len(looped)
        amari = k % len(looped)
        print(looped[amari-1])

#6 5 2 5 3 2
#1 2 3 4 5 6

def input_list():
    return list(map(int, input().split()))


def input_list_str():
    return list(map(str, input().split()))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A= list(map(int, input().split()))
machi=[0]*N
machi[0]=1
tugi=A[0] #next town number 3
count=0
amari=2
while(1):
    if machi[tugi-1]==1:
        count+=1
        amari+=1
        machi[tugi-1]=2
        loopstart = A[tugi - 1]

    elif machi[tugi-1]==2:

        break
    else:
        machi[tugi-1]=1

    tugi = A[tugi - 1]  # A[2]-1=3

amari=machi.count(1)
loop=K-amari
susumu=loop%count
if susumu==0:
    print(loopstart)
else:
    for a in range(susumu):
        kot=A[loopstart-1]
        loopstart=kot
    print(kot)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A= list(map(int, input().split()))
machi=[0]*N
machi[0]=1
tugi=A[0] #next town number 3
count=0
amari=2
while(1):
    if machi[tugi-1]==1:
        count+=1
        amari+=1
        machi[tugi-1]=2
        loopstart = A[tugi - 1]

    elif machi[tugi-1]==2:

        break
    else:
        machi[tugi-1]=1

    tugi = A[tugi - 1]  # A[2]-1=3

amari=machi.count(1)
loop=K-amari
susumu=loop%count
if susumu==0:
    print(A[loopstart])
else:
    for a in range(susumu):
        kot=A[loopstart-1]
        loopstart=kot
    print(kot)

Submission status: Wrong Answer

Solution code:
#! /usr/bin/env python3

from fractions import gcd
# from math import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

N, K = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]

flag = 0
done = [0] * (N + 1)

root = []
ind = 0

ind = A[0]
root = [1]
done[1] = 1

while True:
    
    if done[ind]:
        flag = 1
        break
    done[ind] = 1
    
    root += [ind]
    ind = A[ind-1]

if flag:
    left = root.index(ind) - 1
    loopcnt = len(root) - (root.index(ind))

    root2 = root[left+1:]
    if  K > left:
        print(root2[(K-(left+1)) % loopcnt])
    else:
        print(root[K-1])
else:
    print(root[K-1])

Submission status: Wrong Answer

Solution code:
#! /usr/bin/env python3

from fractions import gcd
# from math import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

N, K = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]

flag = 0
done = [0] * (N + 1)

root = []
ind = 0

ind = A[0]
root = [1]
done[1] = 1

while True:
    
    if done[ind]:
        flag = 1
        break
    done[ind] = 1
    
    root += [ind]
    ind = A[ind-1]

if flag:
    left = root.index(ind) - 1
    loopcnt = len(root) - (root.index(ind))

    root = root[left+1:]
    print(root[(K-(left+1)) % loopcnt])
else:
    print(root[K-1])