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:
import sys
lines = sys.stdin.readlines()
(n, k) = map(int, lines[0].strip().split(" "))
nums = list(map(int, lines[1].strip().split(" ")))
pt = 0
seen = set()
while pt not in seen:
  seen.add(pt)
  pt = nums[pt] - 1
  k -= 1

cyc = 1
entry = pt
pt = nums[pt]-1
while pt != entry:
  cyc += 1
  pt = nums[pt] -1
k %= cyc
for i in range(k):
  pt = nums[pt] -1 
print(pt+1)

Submission status: Wrong Answer

Solution code:
import sys

def IL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def main():
	N, K = IL()
	a_list = IL()

	tele_dict = {}
	port_list = [1]
	for i in range(K):
		if i == 0:
			port = a_list[i]
		else:
			port = a_list[port-1]
		if port in port_list:
			break
		port_list.append(port)
	if i == (K-1):
		print(port_list[-1])
	else:
		loop = len(port_list) - port_list.index(port)
		start = len(port_list) - loop
		loop_number = K + 1 - start
		loop_list = port_list[start:]
		amari = loop_number % len(loop_list)
		print(loop_list[amari-1])


if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
now = 1
 
for i in range(len(bin(k))-2):
    if (k>>i)&1:
        now = A[now-1]
    A = [A[a] for a in range(n)]
print(now)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
a = [[ai,0] for ai in a]
i = 1
x = 0
ans = 0
while(True):
    ai = a[i-1]
    if ai[1]>=1:
        y=k%(x+1)
        for i in range(y):
            ai = a[ai[0]-1]
        ans = ai[0]
        break
    else:
        ai[1]+=1
        x+=1
    i = ai[0]
print(ans)

Submission status: Wrong Answer

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

num=[-1]*N
rep=[]

now=0
for i in range(N):
    if i==K:
        print(now)
        exit()
    aft=A[now]-1
    rep.append(now)
    if num[now]==-1:
        num[now]=i
    else:
        break
    now=aft

end=i
start=num[aft]
loop=end-start+1
print(rep[start+(K-start)%loop]+1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
doubleA = {}
doubleA[1] = A
for i in range(2, 73):
    doubleA[i] = [doubleA[i-1][doubleA[i-1][j]-1] for j in range(n)]
now = 1
for i in range(len(bin(k))-3):
    if (k>>i)&1:
        now = doubleA[i+1][now-1]
print(now)

Submission status: Wrong Answer

Solution code:
def main():
    N,K=map(int,input().split())
    A=list(map(int,input().split()))
    L=[]
    I=0
    while True:
        a=A[I]
        if a in L:
            I=L.index(a)
            break
        L.append(a)
        I=a-1
    K=K-1
    if K <= I: print(L[I])
    else: print(L[I+(K-I)%(len(L)-I)])
    return

if __name__=='__main__':
    main()

Submission status: Wrong Answer

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

counter = 0
place_list = []
next = 1
while True:
    
    if not next in place_list:
        place_list.append(next)
        next = A[next-1]
    else:
        duration_cycle = counter - place_list.index(next)
        break
    counter += 1


print(place_list[K%duration_cycle])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
doubleA = {}
doubleA[1] = [A[i-1] for i in range(n)]
def f(x):
    return A[x-1]
for i in range(2, 73):
    doubleA[i] = [doubleA[i-1][doubleA[i-1][j]-1] for j in range(n)]
c = k - 2**(len(bin(k))-3)
start = doubleA[len(bin(k))-3][0]
for i in range(c):
    start = f(start)
print(start)

Submission status: Wrong Answer

Solution code:
n,k = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
 
l = [1]
now = 1
notloop = 0
dupl = -1
 
for i in range(n-1):
  now = a[now-1]
  
  if now in l:
    dupl = now
    break
  else:
    l.append(now)
    
if(dupl == -1):
  print(now)
  
else:
  notloop = l.index(dupl)
  length = len(l)-notloop
  print(l[(k-notloop)%length+notloop])

Submission status: Wrong Answer

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

li = []
pnt = 0
flg = False

while True:
    if a[pnt] == 1:
        li.append(a[pnt])
        break
    if a[pnt] in li:
        flg = True
        zzz = li.index(a[pnt])
        break
    li.append(a[pnt])
    pnt = a[pnt] - 1

if flg:
    if k <= len(li):
        print(li[k])
    else:
        print(li[(k - zzz - 1) % (len(li) - zzz) + zzz])
else:
    print(li[(k - 1) % len(li)])

Submission status: Wrong Answer

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

townOrder = [-1 for _ in range(N+1)]
townOrder[1] = 1
nextTown = 1
cicleStart = 0
cicleEnd = 0

for order in range(1, N+1):
  nextTown = trans[nextTown-1]
  if townOrder[nextTown] == -1:
    townOrder[nextTown] = order +1
  else:
    cicleStart = townOrder[nextTown]
    cicleEnd = order + 1
    break

exc = cicleStart -1
cicle = cicleEnd - cicleStart

if K >= exc:
  K -= exc
  rest = K % cicle
  for _ in range(rest):
    nextTown = trans[nextTown-1]
  print(nextTown)
else:
  nextTown = 1
  for _ in range(K):
    nextTown = townOrder[nextTown-1]
  print(nextTown)

Submission status: Wrong Answer

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

townOrder = [-1 for _ in range(N+1)]
townOrder[1] = 1
nextTown = 1
cicleStart = 0
cicleEnd = 0

for order in range(1, N+1):
  nextTown = trans[nextTown-1]
  if townOrder[nextTown] == -1:
    townOrder[nextTown] = order +1
  else:
    cicleStart = townOrder[nextTown]
    cicleEnd = order + 1
    break

exc = cicleStart -1
cicle = cicleEnd - cicleStart

if K >= exc:
  K -= exc
  rest = K % cicle
  for _ in range(rest):
    nextTown = trans[nextTown]
  print(nextTown)
else:
  nextTown = 1
  for _ in range(K):
    nextTown = townOrder[nextTown]
  print(nextTown)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
teleporter = list(map(lambda x: int(x) - 1, input().split()))

destinations = {0: 0}
curr_city = 0
while True:
    curr_city = teleporter[curr_city]
    if curr_city in destinations:
        break
    destinations[curr_city] = len(destinations)

cycle = len(destinations) - destinations[curr_city]
rem = (k - destinations[curr_city]) % cycle

for i in range(rem):
    curr_city = teleporter[curr_city]

print(curr_city + 1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
teleporter = list(map(lambda x: int(x) - 1, input().split()))

destinations = {0: 0}
curr_city = 0
while True:
    curr_city = teleporter[curr_city]
    if curr_city in destinations:
        break
    destinations[curr_city] = len(destinations)

if len(destinations) == k + 1:
    print(curr_city + 1)
else:
    cycle = len(destinations) - destinations[curr_city]
    rem = (k - destinations[curr_city]) % cycle

    for i in range(rem):
        curr_city = teleporter[curr_city]

    print(curr_city + 1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
teleporter = list(map(lambda x: int(x) - 1, input().split()))

destinations = {0: 0}
curr_city = 0
cycle_start = -1
while True:
    next_city = teleporter[curr_city]

    if next_city in destinations:
        cycle_start = destinations[next_city]
        break

    destinations[next_city] = len(destinations)
    curr_city = next_city

if cycle_start == -1:
    print(curr_city + 1)
else:
    for i in range((k - cycle_start) % (len(destinations) - cycle_start) + 1):
        curr_city = teleporter[curr_city]
    print(curr_city + 1)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
A=[a-1 for a in A]
Appeared=[]
k=0
location=0
T=0
T_List=[]
while k<=K:
    k+=1
    Appeared.append(location)
    location=A[location]
    if location in Appeared:
        T=k-Appeared.index(location)
        T_List=Appeared[k-T:k]
        break
if len(T_List)==0:
    print(location)
else:
    Destination=(K-(k-T-1))%T
    print(T_List[Destination-1]+1)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
A=[a-1 for a in A]
Appeared=[]
k=0
location=0
T=0
T_List=[]
while True:
    k+=1
    Appeared.append(location)
    location=A[location]
    if location in Appeared:
        T=k-Appeared.index(location)
        T_List=Appeared[k-T:k]
        break
Destination=(K-(k-T-1))%T
print(T_List[Destination-1]+1)

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.readline
n, k = map(int,readline().split())
a = list(map(int,readline().split()))
cnt = [1]
jun = 0
i = 0
while len(cnt) == len(set(cnt)):
    cnt.append(a[i])
    i = a[i]-1
x = cnt.pop()
id = cnt.index(x)
k -= id
cnt = cnt[id:]
print(cnt[k%len(cnt)])

Submission status: Wrong Answer

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

Appeared=[]
flag=0
k=0
location=0
T=0
T_List=[]
while flag==0:
    k+=1
    Appeared.append(location)
    location=A[location]-1
    if location in Appeared:
        flag=1
        T=k-Appeared.index(location)
        T_List=Appeared[k-T:k]
Destination=(K-(k-T-1))%T
print(T_List[Destination-1]+1)

Submission status: Wrong Answer

Solution code:
import numpy as np
N,K=map(int,input().split())
A=np.array(list(map(int,input().split())))

Appeared=[]
flag=0
k=0
location=0
T=0
T_List=[]
while flag==0:
    k+=1
    Appeared.append(location)
    location=A[location]-1
    if location in Appeared:
        flag=1
        T=k-Appeared.index(location)
        T_List=Appeared[k-T:k]
Destination=(K-(k-T-1))%T
print(T_List[Destination-1]+1)

Submission status: Wrong Answer

Solution code:
N,K,*A=map(int,open(0).read().split())
a=1
while K>0:
    if K&1:
        a=A[a-1]
        A=[A[A[i]-1] for i in range(N)]
    K>>=1
print(a)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
cnt = [1]
jun = 0
i = 0
while len(cnt) == len(set(cnt)):
    cnt.append(a[i])
    i = a[i]-1
x = cnt.pop()
id = cnt.index(x)
k -= id
cnt = cnt[id:]
print(cnt[k%len(cnt)])

Submission status: Wrong Answer

Solution code:
numOfCity, numOfTeleport = map(int, input().split())
A = list(map(int, input().split()))

cityIndex = 0

while numOfTeleport > 0:
    cityIndex = A[A[cityIndex] - 1] - 1
    numOfTeleport -= 1

print(A[cityIndex])

Submission status: Wrong Answer

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

x = 1
X = [1]
num = 0
while num < K:
    x = A[x-1]
    if x in X:
        kurikaeshi = len(X) - X.index(x)
        amari = (K + 1 - X.index(x)) % kurikaeshi
        junban = X.index(x) + amari -1
        num = X[junban]
        break
    X.append(x)
    num += 1
print(num)

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    nk = input()
    nk = nk.split()
    n = int(nk[0])
    k = int(nk[1])

    A_N = input()
    A_N = A_N.split()
    a_n = [0]*n

    for i in range(n):
        a_n[i] = int(A_N[i])

    now = 1
    nows = [0]*n
    nows[0] = 1
    for i in range(n-1):
        now = a_n[now-1]
        nows[i+1] = now

    if k > n:
        ans = nows[k%n]
    else:
        ans = nows[k-1]

    print(ans)

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    nk = input()
    nk = nk.split()
    n = int(nk[0])
    k = int(nk[1])

    A_N = input()
    A_N = A_N.split()
    a_n = [0]*n

    for i in range(n):
        a_n[i] = int(A_N[i])

    now = 1
    nows = [0]*n
    nows[0] = 1
    for i in range(n-1):
        now = a_n[now-1]
        nows[i+1] = now

    ans = nows[k%n]

    print(ans)

Submission status: Wrong Answer

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

for i in range(n):
    a[i] -= 1

reg = [0] * n
i = 0
loop = False
while loop == False:
    if reg[a[i]] != 0 or a[i] == 0:
        path_loop, path_noloop = i, a[i]
        loop = True
    else:
        reg[a[i]] = reg[i] + 1
        i = a[i]

k -= reg[path_noloop]
#print(reg)
if reg[path_loop] - reg[path_noloop] != 0:
    k %= reg[path_loop] - reg[path_noloop] + 1
else:
    print(path_noloop+1)

for i in range(n):
    if reg[i] == reg[path_noloop] + k:
        print(i+1)
        exit()

Submission status: Wrong Answer

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

for i in range(n):
    a[i] -= 1

go = [0]
i = 0

while i < n:
    if go.count(a[go[i]]):
        last = i
        break
    go.append(a[go[i]])
    i += 1
        
for num in range(len(go)):
    if go[num] == a[go[last]]:
        break
#print(last, num)
k -= num
if last - num != 0:
    k %= (last-num + 1)
    print(go[k+num]+1)
else:
    print(go[num]+1)

Submission status: Wrong Answer

Solution code:
N, K = [int(v) for v in input().split(" ")]
Ai = [int(v) for v in input().split(" ")]
assert len(Ai) == N


path = [0]
while True:
  next_city = Ai[path[len(path) - 1]] - 1
  if next_city in path:
    break
  path.append(next_city)
  if len(path) == K:
    print(next_city + 1)
    exit()

for i, city in enumerate(path):
  if city == next_city:
    start = path[:i]
    circle = path[i:]
    break

if K <= len(start):
  print(start[K] + 1)
else:
  K -= len(start)
  print(circle[K % len(circle)] + 1)

Submission status: Wrong Answer

Solution code:
N, K = [int(v) for v in input().split(" ")]
Ai = [int(v) for v in input().split(" ")]
assert len(Ai) == N


circle = [0]
while True:
  next_city = Ai[circle[len(circle) - 1]] - 1
  if next_city in circle:
    break
  circle.append(next_city)
  
  
print(circle[K % len(circle)] + 1)

Submission status: Wrong Answer

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

ans=-1
now=0
i=1
result=[0]
while i<=k:
    now=a[now]
    if now in result:
        x=result.index(now)
        length=i-x
        ans=result[(k-i)%length+x]
        break
    result.append(now)
    i+=1
    
if ans!=-1:
    print(ans+1)
else:
    print(result[-1])

Submission status: Wrong Answer

Solution code:
#200510D
N, K = map(int, input().split())
A = list(map(int,input().split()))
 
b = A[0]
count = 1
 
X = []
flag = 0
loop = 0
ans = 0
 
if A[0] == 1:
    ans = 1

else:   
    #print("a")
    for i in range(N+1):
        c = A[b-1]
        b = c
        l = len(X)
        
        if l == len(set(X)):
            for j in range(l):
                if b == X[j-1]:
                    loop = i - j + 1
                    flag = 1
                    Z = i -loop
                    #print(loop)
                    break
 
 
        if flag == 1:
            break
        else:
            X.append(b)
            #print(X)
    if loop ==1:
        ans =X[Z]
    elif loop == 0:
        #print("A")
        ans = b
        
    else:        
        #print(loop)        
        Y = (K-Z)%loop
        #print(Y)
        #print(Z)
        #print(Z+Y-1-loop)
        ans =A[X[Z+Y]-1]
#print(loop) 
print(ans)

Submission status: Wrong Answer

Solution code:
#200510D
N, K = map(int, input().split())
A = list(map(int,input().split()))
 
b = A[0]
count = 1
 
X = []
flag = 0
loop = 0
ans = 0
 
if A[0] == 1:
    ans = 1

else:   
    #print("a")
    for i in range(N+1):
        c = A[b-1]
        b = c
        l = len(X)
        
        if l == len(set(X)):
            for j in range(l):
                if b == X[j-1]:
                    loop = i - j + 1
                    flag = 1
                    Z = i -loop
                    #print(loop)
                    break
 
 
        if flag == 1:
            break
        else:
            X.append(b)
            #print(X)
    if loop ==1:
        ans =X[Z]
    elif loop == 0:
        #print("A")
        ans = b
        
    else:        
        #print(loop)        
        Y = (K-Z)%loop
        #print(Y)
        #print(Z)
        #print(Z+Y-1-loop)
        ans =A[X[Z+Y]-1]
print(loop) 
print(ans)

Submission status: Wrong Answer

Solution code:
#200510D
N, K = map(int, input().split())
A = list(map(int,input().split()))
 
b = A[0]
count = 1
 
X = []
flag = 0
loop = 0
ans = 0
 
if A[0] == 1:
    ans = 1

else:   
    #print("a")
    for i in range(N+1):
        c = A[b-1]
        b = c
        l = len(X)
        
        if l == len(set(X)):
            for j in range(l):
                if b == X[j-1]:
                    loop = i - j + 1
                    flag = 1
                    Z = i -loop
                    #print(loop)
                    break
 
 
        if flag == 1:
            break
        else:
            X.append(b)
            #print(X)
    if loop ==1:
        ans =A[Z]
        print("aaaa")
    else:        
        #print(loop)        
        Y = (K-Z)%loop
        #print(Y)
        #print(Z)
        #print(Z+Y-1-loop)
        ans = A[Z+Y]
 
print(ans)

Submission status: Wrong Answer

Solution code:
#200510D
N, K = map(int, input().split())
A = list(map(int,input().split()))
 
b = A[0]
count = 1
 
X = []
flag = 0
loop = 0
ans = 0
 
if A[0] == 1:
    ans = 1

else:   
    #print("a")
    for i in range(N+1):
        c = A[b-1]
        b = c
        l = len(X)
        
        if l == len(set(X)):
            for j in range(l):
                if b == X[j-1]:
                    loop = i - j + 1
                    flag = 1
                    Z = i -loop
                    #print(loop)
                    break
 
 
        if flag == 1:
            break
        else:
            X.append(b)
            #print(X)
    if loop ==1:
        ans =A[Z]
    else:        
        #print(loop)        
        Y = (K-Z)%loop
        #print(Y)
        #print(Z)
        #print(Z+Y-1-loop)
        ans = A[Z+Y]
 
print(ans)

Submission status: Wrong Answer

Solution code:
#200510D
N, K = map(int, input().split())
A = list(map(int,input().split()))
 
b = A[0]
count = 1
 
X = []
flag = 0
loop = 0
ans = 0
 
if A[0] == 1:
    ans = 1

else:   
    #print("a")
    for i in range(N+1):
        c = A[b-1]
        b = c
        l = len(X)
        for j in range(l):
            if b == X[j-1]:
                loop = i - j + 1
                flag = 1
                Z = i -loop
                #print(loop)
                break
 
 
        if flag == 1:
            break
        else:
            X.append(b)
            #print(X)
    if loop ==1:
        ans =A[Z]
    else:        
        #print(loop)        
        Y = (K-Z)%loop
        #print(Y)
        #print(Z)
        #print(Z+Y-1-loop)
        ans = A[Z+Y]
 
print(ans)

Submission status: Wrong Answer

Solution code:
#200510D
N, K = map(int, input().split())
A = list(map(int,input().split()))
 
b = A[0]
count = 1
 
X = []
flag = 0
loop = 0
ans = 0
 
if A[0] == 1:
    ans = 1

else:   
    #print("a")
    for i in range(N+1):
        c = A[b-1]
        b = c
        l = len(X)
        for j in range(l):
            if b == X[j-1]:
                loop = i - j + 1
                flag = 1
                Z = i -loop
                #print(loop)
                break
 
 
        if flag == 1:
            break
        else:
            X.append(b)
            #print(X)
    if loop ==1:
        ans =A[Z]
    else:        
        #print(loop)        
        Y = (K-Z)%loop
        #print(Y)
        #print(Z)
        #print(Z+Y-1-loop)
        ans = A[Z+Y-1-loop]
 
print(ans)

Submission status: Wrong Answer

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

s = 1
g = 0
i = 1
history = [s]
flag = [False] * n
flag[0] = True

while True:
    g = a[s-1]
    if flag[g-1] == True:
        break
    else:
        flag[g-1] = True
        history.append(g)
        s = g
        i += 1


x = history.index(g)

d = (k-x) % (i - x)

ans = history[x + d]

print(ans)

Submission status: Wrong Answer

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

s = 1
g = 0
i = 1
history = [s]

while True:
    g = a[s-1]
    if g in history:
        break
    else:
        history.append(g)
        s = g
        i += 1


x = history.index(g)

d = (k-x) % (i - x)

ans = history[x + d]

print(ans)

Submission status: Wrong Answer

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

s = 1
i = 0
history = [s]

while i < 2*n:
    g = a[s-1]
    history.append(g)
    s = g
    i += 1

index_list = [n for n, v in enumerate(history) if v == history[-1]]
x,  y= index_list[-2], index_list[-1]

sec = y - x

l = (k - x) % sec

ans = history[x + l]

print(ans)

Submission status: Wrong Answer

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

s = 1
i = 0
history = [s]

while i < n:
    g = a[s-1]
    history.append(g)
    s = g
    i += 1

index_list = [n for n, v in enumerate(history) if v == history[-1]]
x,  y= index_list[-2], index_list[-1]

sec = y - x

l = (k - x) % sec

ans = history[x + l]

print(ans)

Submission status: Wrong Answer

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

s = 1
i = 0
history = [s]

while i < n:
    g = a[s-1]
    history.append(g)
    s = g
    i += 1

j = history.index(history[-1])

sec = n - j

l = (k - j) % sec

ans = history[j + l]

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N, K = map(int, input().split())
As = [0] + list(map(int, input().split()))
# print(As)
d = [-1]*(N+1)
i = 1
step = 1
while step <= K:
	# print("step: {}, idx: {}".format(step,i))
	next_i = As[i]
	if d[next_i] != -1:
		freq = step - d[next_i] +1
		step = step + int((K - step) / freq)*freq
	d[next_i] = step
	step += 1
	i = next_i

print(i)

Submission status: Wrong Answer

Solution code:
import sys
N, K = map(int, input().split())
As = [0] + list(map(int, input().split()))
# print(As)
d = [-1]*(N+1)
i = 1
step = 1
while step <= K:
	# print("step: {}, idx: {}".format(step,i))
	next_i = As[i]
	if d[next_i] != -1:
		freq = step - d[next_i]
		step = step + int((K - step) / freq)*freq
	d[next_i] = step
	step += 1
	i = next_i

print(i)

Submission status: Wrong Answer

Solution code:
import sys
N, K = map(int, input().split())
As = [0] + list(map(int, input().split()))
# print(As)
d = [-1]*(N+1)
i = 1
step = 1
while step < K:
	# print(d)
	# print(i)
	next_i = As[i]
	if d[next_i] != -1:
		freq = step - d[next_i]
		step = step + int((K - step) / freq)*freq
	d[next_i] = step
	step += 1
	i = next_i

print(i)

Submission status: Wrong Answer

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

def forward(L ,n=1, count=K):
    n = L[L[n]]
    if (count == K):
        return n
    forward(L, n)
    
forward(A)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
As = list(map(int, input().split()))
history = []

location = 1
for i in range(N+1):
    history.append(location)
    location = As[location-1]
    if location in history:
        break

lead = history.index(location) - 1
loop = len(history) - (lead+1)

if K > lead:
    idx = (K - lead) % loop + lead
else:
    idx = K

print(history[idx])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
town = a[0]
passed = [1]
while town not in passed :
    passed.append(town)
    town = a[town-1]
i = passed.index(town)
rep = len(passed)-i
t = (k-i)%rep
town = passed[i]
while t>0:
    town = a[town-1]
    t-=1 
print(town)

Submission status: Wrong Answer

Solution code:
import numpy as np

N, K = map(int, input().split())
A = np.zeros(N+1, dtype = np.int)
A[1:] = list(map(int, input().split()))

machis = {1}

now_machi = 1
count = 0

while True:
    next_machi = A[now_machi]
    count += 1
    if next_machi in machis:
        point = next_machi
        break
    machis.add(next_machi)
    now_machi = next_machi

now_machi = point
count2 = 0
while True:
    count2 += 1
    next_machi = A[now_machi]
    if next_machi == point:
        break
    now_machi = next_machi

times = (K - count) % count2
now_machi = point
for t in range(times):
    next_machi = A[now_machi]
    now_machi = next_machi

print(now_machi)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
town = a[1-1]
passed = [1]
while town not in passed :
    passed.append(town)
    town = a[town-1]
i = passed.index(town)
rep = len(passed)-i
if i>0:
    t = (k+i+1)%rep
else:
    t = (k)%rep
town = 1 
while t>0:
    town = a[town-1]
    t-=1 
print(town)

Submission status: Wrong Answer

Solution code:
def test(N,K,a):
	count = 0
	index = 1
	for i in range(K):
		count = a[index]
		index = count
	return count

N,K = list(map(int, input().split(" ")))
if K>20:
	c = K%N
	a = list(map(int, input().split(" ")))
	b = [0] + a 
	print(test(N,c,b))
else:
  a = list(map(int, input().split(" ")))
  b = [0] + a 
  print(test(N,K,b))

Submission status: Wrong Answer

Solution code:
def test(N,K,a):
	count = 0
	index = 1
	for i in range(K):
		count = a[index]
		index = count
	return count

N,K = list(map(int, input().split(" ")))
if K>1000:
	c = K%N
	a = list(map(int, input().split(" ")))
	b = [0] + a 
	print(test(N,c,b))
else:
  a = list(map(int, input().split(" ")))
  b = [0] + a 
  print(test(N,K,b))

Submission status: Wrong Answer

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

t = 1
tl = [1]
ts = {1}
for i in range(len(a)):

    t = a[t-1]
    if t in ts:
        break
    else:
        tl.append(t)
        ts.add(t)

tl2 = tl[tl.index(t):len(tl)]

print(tl)
print(tl2)

if k < len(tl):
    ans = tl[k]
else:
    k = k - (len(tl) - len(tl2))
    j = k % len(tl2)
    ans = tl2[j]

print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=[]
cnt=1
for i in range(n):
    b.append(a[cnt-1])
    cnt=a[cnt-1]
#print(b)
c=[]
for i in range(len(b)):
    if b[0]==b[i] and i!=0:
        break
    else:
        c.append(b[i])
#print(c)
print(c[k%len(c)-1])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
c = list(map(int, input().split()))
x = 1
for i in range(5):
  x = c[x-1]
print(x)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
tel = list(map(int, input().split()))
pos = tel[0]
ls = [pos]
for i in range(len(tel)*3):
    pos = tel[pos - 1]
    ls.append(pos)
patarn = ls[-len(set(ls[-len(tel):])):]
destination = patarn[((k-len(ls)) % len(patarn)) - 1]
print(destination)

Submission status: Wrong Answer

Solution code:
NK = list(map(int, input().split()))
N = NK[0]; K = NK[1]
A = list(map(int, input().split()))
 
rlist = [0]*N
flag = 1
location = 1
div = 0
 
while flag>0:
    location = A[location-1]
    if rlist[A[location-1]-1] == 0:
        rlist[A[location-1]-1] = rlist[location-1]+1
    else:
        div = rlist[location-1]+1-rlist[A[location-1]-1]
        flag = -1
 
cnt = (K-rlist[A[location-1]-1])%div + rlist[A[location-1]-1]
 
result = 0
for i in range(cnt):
    result = A[result]-1
 
print(result+1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
tel = list(map(int, input().split()))
pos = tel[0]
ls = [pos]
for i in range(len(tel)*2):
    pos = tel[pos - 1]
    ls.append(pos)
patarn = ls[-len(set(ls[-len(tel):])):]
destination = patarn[((k-len(ls)) % len(patarn)) - 1]
print(destination)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
N, K = input().split(' ')
A = list(map(int, input().split(' ')))
count = 0
i = 0
a = []
while True:
    a.append(i + 1)
    i = A[i] - 1
   
    if len(a) != len(set(a)):
        break
    count += 1

b = count - a.index(a[count])

K = int(K) - a.index(a[count])

K = K % b

while K >= 0:
    i = A[i] - 1
    K -= 1
print(A[i])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
rd = [-1 for _ in range(N)]
rd[0] = 1

def nomal_loop():
    now_town = 1
    for _ in range(K):
        now_town = A[now_town]
    print(now_town)

    return 0

def else_loop():
    next_town = A[0]
    count = 1
    K_count = K
    while(1):
        count +=1
        K_count -= 1
        if rd[next_town-1] == -1:
            rd[next_town-1] = count
            next_town = A[next_town-1]
        else:
            warukazu = count - rd[next_town-1]
            result = (K_count+warukazu) % warukazu
            for _ in range(result):
                next_town = A[next_town-1]
            print(next_town)
            break
    return 0

if N > K:
    nomal_loop()
else:
    else_loop()

Submission status: Wrong Answer

Solution code:
# D - Teleporter
def main():
    N, K, *A = map(int, open(0).read().split())
    A = [0] + A
    last_visit = [-1] * (N + 1)
    last_visit[1] = 0
    cur_pos, i = 1, 1

    while True:
        nxt_pos = A[cur_pos]
        
        if i == K:
            print(cur_pos)
            break
        elif (j := last_visit[nxt_pos]) != -1:  # detect a loop
            print(last_visit.index(j + (K - i) % (i - j)))
            break
        
        cur_pos = nxt_pos
        last_visit[cur_pos] = i
        i += 1


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# D - Teleporter
def main():
    N, K, *A = map(int, open(0).read().split())
    A = [0] + A
    last_visit = [-1] * (N + 1)
    last_visit[1] = 0
    cur_pos, i = 1, 1

    while True:
        nxt_pos = A[cur_pos]
        if (j := last_visit[nxt_pos]) != -1:  # detect a loop
            print(last_visit.index(j + (K - i) % (i - j)))
            return
        cur_pos = nxt_pos
        last_visit[cur_pos] = i
        i += 1


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# D - Teleporter
def main():
    N, K, *A = map(int, open(0).read().split())
    A = [0] + A
    cur_pos = 1
    last_visit = [-1] * (N + 1)
    last_visit[1] = 0
    
    for i in range(1, N + 1):
        nxt_pos = A[cur_pos]
        if (j := last_visit[nxt_pos]) != -1:
            print(last_visit.index(j + (K - i) % (i - j)))
            return
        cur_pos = nxt_pos
        last_visit[cur_pos] = i
    
    print(cur_pos)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, K, *A = map(int, open(0).read().split())
    A = [0] + A
    cur_pos = 1
    last_visit = [-1] * (N + 1)
    last_visit[cur_pos] = 0
    for i in range(1, K + 1):
        nxt_pos = A[cur_pos]
        if cur_pos == nxt_pos:
            print(nxt_pos)
            break
        elif (j := last_visit[nxt_pos]) != -1:
            print(last_visit.index((K - j) % (i - j) + j))
            break
        else:
            last_visit[nxt_pos] = last_visit[cur_pos] + 1
            cur_pos = nxt_pos


if __name__ == "__main__":
    main()