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()))
l = [0 for _ in range(N)]

now = 0
l[now] = 1
count = 0
loop = [0]

while count < K:
    count += 1
    now = A[now]-1
    loop.append(now)
    if l[now] == 1:
        loop = loop[loop.index(now):-1]
        break
    else:
        l[now] = 1

if count == K:
    print(now + 1)
else:
    ans = loop[K % len(loop)]
    print(ans + 1)

Submission status: Wrong Answer

Solution code:
# D

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

cnt = 0
temp = []

d = [0]*(2*(10**5) + 1)

now = 1
while True:
  d[now] += 1
  if d[now] > 1:
    break

  temp.append(now)
  now = A[now-1]


for t in temp[::-1]:
  cnt += 1
  if t == now:
    break

temp2 = temp[-cnt::]

if len(temp) <= K:
  kk = (K-(len(temp)-cnt))%cnt
  print(temp2[kk])
else:
  temp[K]

Submission status: Wrong Answer

Solution code:
# D

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

cnt = 0
temp = []

d = [0]*(2*(10**5) + 1)

now = 1
while True:
  d[now] += 1
  if d[now] > 1:
    break

  temp.append(now)
  now = A[now-1]

for t in temp[::-1]:
  cnt += 1
  if t == now:
    break



# print(temp)
# print(cnt)
print(temp[K%cnt])

Submission status: Wrong Answer

Solution code:
# D

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

cnt = 0
temp = []

d = [0]*(2*(10**5) + 1)

now = 1
while True:
  d[now] += 1
  if d[now] > 1:
    break

  cnt +=1
  temp.append(now)
  now = A[now-1]

# print(temp)
# print(cnt)
print(temp[K%cnt])

Submission status: Wrong Answer

Solution code:
#inport
import sys
import numpy as np
from collections import deque


#input
N,K = [int(x) for x in sys.stdin.readline().split()]
g = [int(x) for x in sys.stdin.readline().split()]

#f = open('./sub1_03.txt')
#N,K = [int(x) for x in f.readline().split()]
#g = [int(x) for x in f.readline().split()]
#f.close()

tmp = [1]
dest = 1
n_for_loop = 0
n_loop = 0
new_start = 0

for i in range(N):
    tmp.append(g[dest - 1])
    n_for_loop = n_for_loop + 1
    #テレポ後にいる位置
    dest = g[dest - 1]
 #   print(tmp)
  #  print(g[dest-1])
   # print(np.any(np.array(tmp) == g[dest-1]))
    if np.any(np.array(tmp) == g[dest-1]):
        new_start = g[dest -1]
        break

#print(new_start)
#print(tmp)
#ループ開始街からのループ回数
dest = new_start
tmp = []
for i in range(N):
    tmp.append(g[dest - 1])
    n_loop = n_loop +1
    dest = g[dest -1 ]
    if g[dest -1] == new_start:
        n_loop = n_loop + 1
        tmp.append(g[dest - 1])
        break

n_start = n_for_loop - ( n_loop - 1 )
K = K - n_start
#print(n_loop)
#print(K)
#print(tmp)
ans = tmp[int(K%n_loop)-1]
#print(tmp[int(K%n_loop)-2])
#print("ans = ")
print(ans)
#print(tmp[int(K%n_loop)])

Submission status: Wrong Answer

Solution code:
#inport
import sys
import numpy as np
from collections import deque


#input
N,K = [int(x) for x in sys.stdin.readline().split()]
g = [int(x) for x in sys.stdin.readline().split()]

tmp = []
dest = 1
n_loop = 0

for i in range(N):
    tmp.append(g[dest - 1])
    n_loop = n_loop + 1
    dest = g[dest - 1]
    if g[dest - 1] == 1 :
        tmp.append(g[dest - 1])
        n_loop = n_loop + 1
        break

ans = tmp[int(K%n_loop)-1]
print(ans)

Submission status: Wrong Answer

Solution code:
#inport
import sys
import numpy as np
from collections import deque


#input
N,K = [int(x) for x in sys.stdin.readline().split()]
g = [int(x) for x in sys.stdin.readline().split()]

tmp = []
dest = 1
n_loop = 0

print(K)
print(np.size(g))
for i in range(N):
    tmp.append(g[dest - 1])
    n_loop = n_loop + 1
    dest = g[dest - 1]
    print(dest)
    if g[dest - 1] == 1 :
        tmp.append(g[dest - 1])
        n_loop = n_loop + 1
        break

ans = tmp[int(K%n_loop)-1]
print(ans)

Submission status: Wrong Answer

Solution code:
#inport
import sys
import numpy as np
from collections import deque


#input
N,K = [float(x) for x in sys.stdin.readline().split()]
g = [int(x) for x in sys.stdin.readline().split()]

tmp = []
dest = 1
n_loop = 0

for i in range(int(N)):
    tmp.append(g[dest - 1])
    n_loop = n_loop + 1
    dest = g[dest - 1]
    if g[dest - 1] == 1 :
        tmp.append(g[dest - 1])
        n_loop = n_loop + 1
        break

ans = tmp[int(K%n_loop)-1]
print(ans)

Submission status: Wrong Answer

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

    num = 1
    l = []
    s = [1]*n

    for _ in range(n+1):
        if not s[num-1]:
            break

        else:
            l.append(num)
            s[num-1] = 0
            num = a[num-1]


    l2 = l[l.index(num):]
    kk = (k-len(set(l)-set(l2)))%len(l2)
    print(l2[kk])

main()

Submission status: Wrong Answer

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

    num = 1
    l = []

    my_append = l.append
    for _ in range(n+1):
        if num in set(l):
            l2 = l[l.index(num):]
            break

        else:
            my_append(num)
            num = a[num-1]

    kk = (k-len(set(l)-set(l2)))%len(l2)
    print(l2[kk])

main()

Submission status: Wrong Answer

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

    num = 1
    l = []

    my_append = l.append
    for _ in range(n+1):
        if num in l:
            l2 = l[l.index(num):]
            break

        else:
            my_append(num)
            num = a[num-1]

    kk = (k-len(set(l)-set(l2)))%len(l2)
    print(l2[kk])

main()

Submission status: Wrong Answer

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

num = 1
l = []

for _ in range(n+1):
    if num in l:
        l2 = l[l.index(num):]
        break

    else:
        l.append(num)
        num = a[num-1]

kk = (k-len(set(l)-set(l2)))%len(l2)
print(l2[kk])

Submission status: Wrong Answer

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

num = 1
l = set()
l2 = []

for _ in range(n*n):
    if num in l2:
        break

    elif num in l:
        l2.append(num)
        num = a[num-1]

    else:
        l.add(num)
        num = a[num-1]

kk = (k-len(l-set(l2)))%len(l2)
print(l2[kk])

Submission status: Wrong Answer

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

num = 1
l = []
l2 = []

for _ in range(k*k):
    if num in l2:
        break

    elif num in l:
        l2.append(num)
        num = a[num-1]


    else:
        l.append(num)
        num = a[num-1]

kk = (k-len(set(l)-set(l2)))%len(l2)
print(l2[kk])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
visit=[0]*n
visit[0]=1
now=0
c=0
for i in range(k):
  now_=now
  now=a[now]-1
  if visit[now]==0:
    visit[now]=visit[now_]+1
  else:
    c=1
    break
if c==1:
  t=(k+2-visit[now])%(visit[now_]-visit[now]+1)
  if t==0:
    print(now_+1)
  else:
    i=now
    for _ in range(t-1):
      i=a[i]
    print(i+1)  
else:
  u=now+1
  print(u)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
visit=[0]*n
visit[0]=1
now=0
c=0
for i in range(n):
  now_=now
  now=a[now]-1
  if visit[now]==0:
    visit[now]=visit[now_]+1
  else:
    c=1
    break
if c==1:
  t=(k+2-visit[now])%(visit[now_]-visit[now]+1)
  if t==0:
    print(now_+1)
  else:
    i=now
    for _ in range(t-1):
      i=a[i]
    print(i+1)  
else:
  u=now+1
  print(u)

Submission status: Wrong Answer

Solution code:
import sys
n, k = [int(i) for i in sys.stdin.readline().split()]
a_s = [int(i)-1 for i in sys.stdin.readline().split()]
cnt = 0
ind = 0
ls = [ind]
last_pos = {0:0}
_set = {ind}
while cnt < k:
    ind = a_s[ind]
    if ind in _set:
        break
    _set.add(ind)
    ls.append(ind)
    cnt += 1
    last_pos[ind] = cnt
if cnt == k:
    print(ind+1)
else:
    start = last_pos[ind]
    end = cnt+1
    print(ls[start + (k - cnt) % (end - start)-1]+1)

Submission status: Wrong Answer

Solution code:
import sys
n, k = [int(i) for i in sys.stdin.readline().split()]
a_s = [int(i)-1 for i in sys.stdin.readline().split()]
cnt = 0
ind = 0
ls = [ind]
_set = {ind}
while cnt < k:
    ind = a_s[ind]
    if ind in _set:
        break
    _set.add(ind)
    ls.append(ind)
    cnt += 1
if cnt == k:
    print(ind+1)
else:
    start = ls.index(ind)
    end = cnt+1
    print(ls[start + (k - cnt) % (end - start)-1]+1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split(" "))
towns = list(map(lambda x: int(x)-1, input().split(" ")))
assert len(towns) == N

print(towns)

score = {0: 0}
current = 0
move = 0
while 1:
  next_town = towns[current]
  move += 1
  # print(f"move {current} to {next_town}")
  if score.get(next_town) is not None:
    # print(f"find loop!")
    loop_move = move - score.get(next_town)
    loop_town = next_town
    until_loop = score.get(next_town)
    # print(f"loop start town : ", loop_town)
    # print(f"cost to start to loop town : ", until_loop)
    # print(f"loop length : ", loop_move)
    break
  score[next_town] = move
  current = next_town

if K < until_loop:
  result = 0
  for k in range(K):
    result = towns[result]
else:
  result = loop_town
  for k in range((K - until_loop) % loop_move):
    result = towns[result]
print(result + 1)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
List = list(map(int, input().split()))
trace = [0]*N
pos = 1
period = 0
seppen = 0
res = 0
if K <= N:
  for i in range(K):
    if i == 0:
      pos = List[0]
    else:
      pos = List[pos-1]
  res = pos
      
else:
  for i in range(N):
    if i == 0:
      trace[i] = 1
      pos = List[0]
    else:
      if not trace[pos-1] == 0:
        period = i+1 - trace[pos-1]
        seppen = i
      else:
        trace[pos-1] = i+1
        pos = List[pos-1]
  K_2 = K - seppen
  med = K_2 % period
  if med == 0:
    res = pos
  else:
    for i in range (med):
      pos = List[pos-1]
    res = pos
print(res)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split())) 
b = 0
c = [0]
if n < k:
  k = n
for i in range(k+2):
  b = a[b]-1
  if b in c:
    d = c.index(b)
    e = i-d+1
    k = k - len(c)+e
    c = c[-e:]
    print(c[k%e]+1)
    exit(0)
  else:
    c.append(b)
print(b+1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split())) 
b = 0
c = []
for i in range(k):
  b = a[b]-1
  if b in c:
    e = []
    e.append(b)
    d = c.index(b)
    for j in range(i-d-1):
      b = a[b]-1
      e.append(b)
    if c[-i+d:] == e:
      n = len(c[:-i+d])
      k = k-n
      print(e)
      print(e[k%len(e)-1]+1)
      exit(0) 
  else:
    c.append(b)
print(b+1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
 
l = [1]
for i in range(n):
    l.append(a[l[i] - 1])
 
p = l.index(l[n])
 
if k <= n:
    print(l[k])
else:
    x = (k - n + 1) % (n - p)
    if x == 0:
        print(l[n - 1])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(lambda a: int(a)-1,input().split()))
CityI_inTime = [-1]*N
i = 0
for time in range(K):
    if CityI_inTime[i] == -1:
        CityI_inTime[i] = time
        if time == K-1:
            print(A[i])
            break
        i = A[i]
    else:
        shuuki = time - CityI_inTime[i]
        #CityI_inTime[i]+shuuki*x+amari = K
        amari = (K-CityI_inTime[i])%shuuki
        print(CityI_inTime.index(CityI_inTime[i] + amari) + 1)
        break

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
visited=[1]
x=0
now=1
next_point=a[now-1]
for cnt in range(n):
    now=next_point
    next_point=a[now-1]
    if now in visited:
        x=visited.index(now)
        break
    else:
        visited.append(now)
new_visited=visited[x:]
k-=len(visited)-len(new_visited)

#print(visited)
#print(new_visited)
print(new_visited[k%len(new_visited)])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
visited=[]
x=0
now=1
next_point=a[now-1]
for cnt in range(n):
    now=next_point
    next_point=a[now-1]
    if now in visited:
        x=visited.index(now)
        break
    else:
        visited.append(now)
new_visited=visited[x:]
k-=len(visited)-len(new_visited)

#print(visited)
#print(new_visited)
print(new_visited[k%len(new_visited)-1])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
visited=[]
x=0
now=1
next_point=a[now-1]
for cnt in range(k):
    now=next_point
    next_point=a[now-1]
    if now in visited:
        x=visited.index(now)
        break
    else:
        visited.append(now)
l=len(visited)
k-=x
m=l-x

#print(visited)
#print(x,l,m,k)

print(visited[x+k%m-1])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
visited=[]
now=1
next_point=a[now-1]
for cnt in range(k):
    now=next_point
    next_point=a[now-1]
    if now in visited:
        break
    else:
        visited.append(now)
#print(visited)
print(visited[k%len(visited)-1])

Submission status: Wrong Answer

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

cycle=[]
for i in range(0,N):
    cycle.append(-1)
    A[i]-=1

count=0
cycle[0]=count
town=0
mod=0
while True:
    count+=1
    K-=1
    town=A[town]
    if cycle[town]==-1:
        cycle[town]=count
    else:
        mod=count-cycle[town]
        break

K%=mod
while K>0:
    town=A[town]
    K-=1

print(town+1)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
already = ["1"]
set = {"1"}
now = 1

for i in range(n):
    now = str(a[int(now)-1])
    if now in set:
        break
    else:
        already.append(now)
        set.add(now)


ind = already.index(now)
k -= ind
cycle = already[ind:]
now = (k+1) % len(cycle)

print(cycle[now-1])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
already = ["1"]
now = 1

for i in range(n):
    now = str(a[int(now)-1])
    if now in already:
        break
    else:
        already.append(now)


ind = already.index(now)
k -= ind
cycle = already[ind:]
now = (k+1) % len(cycle)

#print(cycle)
print(cycle[now-1])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
B=[1]
m=0
j=0
for i in range(1,n+1):
    B.append(A[m])
    m=A[m]-1
    if B[i] in B[:i]:
        M=B[i]
        break

while B[j]!=M:
    j+=1

T=len(B)-j-1
print(B[k-((k-j)//T)*T])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
B=[1]
m=0
j=0
for i in range(1,n):
    B.append(A[m])
    m=A[m]-1
    if B[i] in B[:i]:
        M=B[i]
        break

while B[j]!=M:
    j+=1

T=len(B)-j-1
if n!=2:    
    print(B[k-((k-j)//T)*T])
else:
    print(B[k%2])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
B=[1]
m=0
j=0
for i in range(1,n):
    B.append(A[m])
    m=A[m]-1
    if B[i] in B[:i]:
        while B[j]!=B[i]:
            j+=1
        break
T=len(B)-j-1
if n!=2:    
    print(B[k-((k-j)//T)*T])
else:
    print(B[k%2])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
B=[1]
m=0
j=0
for i in range(1,n):
    B.append(A[m])
    m=A[m]-1
    if B[i] in B[:i]:
        while B[j]!=B[i]:
            j+=1
        break
T=len(B)-j-1
if n!=2:    
    print(B[k-((k-j)//T)*T],B)
else:
    print(B[k%2])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
B=[1]
m=0
j=0
for i in range(1,n):
    B.append(A[m])
    m=A[m]-1
    if B[i] in B[:i]:
        while B[j]!=B[i]:
            j+=1
        break
T=len(B)-j-1
print(B[k-((k-j)//T)*T])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
B=[1]
m=0
j=0
for i in range(1,n):
    B.append(A[m])
    m=A[m]-1
    if B[i] in B[:i]:
        while B[j]!=B[i]:
            j+=1
            print(j)
        break
T=len(B)-j-1
print(B[k-((k-j)//T)*T])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
B=[1]
m=0
j=0
for i in range(1,n):
    B.append(A[m])
    m=A[m]-1
    if B[i] in B[:i]:
        while B[j]!=B[i]:
            j+=1
            print(j)
    break
T=len(B)-j-1
print(B[k-((k-j)//T)*T])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
B=[]
m=0
for i in range(n):
    if i==0:
        B.append(1)
    else:
        B.append(A[m])
        m=A[m]-1
    if B[i] in B[:i]:
        j=0
        while B[j]!=B[i]:
            j+=1
        break
T=len(B)-j-1
print(B[k-((k-j)//T)*T])

Submission status: Wrong Answer

Solution code:
from collections import deque

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

adjacent_list=[[] for i in range(n+1)]
for i in range(1,n+1):
  adjacent_list[i].append(a[i-1])
#print(adjacent_list)

que=deque()
que.append(1)
cnt=0
visited=[]
while que:
  p=que.popleft()
  #print(adjacent_list[p])
  que.append(adjacent_list[p][0])
  
  if p in visited:
    cnt_=visited.index(p)
    break
  visited.append(p)
  cnt+=1
  

loop=cnt-cnt_
loopst=(k-cnt_)%loop
print(visited[cnt_+loopst])

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.buffer.readline

def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))

    now=1
    flg = 0
    path = [1]
    pathcheck ={1}
    for i in range(n+2):
        now = a[now-1]
        if now in pathcheck:
            flg = -1
            path.append(now)
            break
        path.append(now)
        pathcheck.add(now)


    if flg == 0:
        print(now)
        exit()

    x1 = path.index(now)
    x2 = len(path)-1
    x3 = (k-x1)%(x2-x1)
    print(path[x1+x3])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

now=1
flg = 0
path=[1]
while True:
    now = a[now-1]
    if now in path:
        flg = -1
        path.append(now)
        break
    path.append(now)

if flg == 0:
    print(now)
    exit()

x1 = path.index(now)
x2 = len(path)-1
x3 = (k-x1)%(x2-x1)
print(path[x1+x3])

Submission status: Wrong Answer

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

for i in range(n):
  lis.append(a[lis[i]-1])
  
for j in range(1,n):
  if len(set(lis[:j]).intersection(set(lis[j:]))) != 0:
    roopstartind = j-1
    break
    
for j in range(roopstartind+1,n):
  if lis[j] == lis[roopstartind]:
    roopendind = j
    break
    
rooplen = roopendind - roopstartind
nokori = k-roopstartind

print(lis[roopstartind + (nokori%rooplen)])

Submission status: Wrong Answer

Solution code:
import sys
import os
import math
import bisect
import collections
import itertools
import heapq
import re
import queue
from decimal import Decimal

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, K = il()
    A = il()
    tel = {n: A[n] - 1 for n in range(N)}

    order = [0]
    visited = set([0])
    next, l, cnt = tel[0], 0, 0
    while True:
        if next in visited:
            l = order.index(next)
            cnt = len(order) - l
            break
        visited.add(next)
        order.append(next)
        next = tel[next]
    print(order[l:][(K - l) % cnt] + 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np

N, K = list(map(int, input().split()))
a = [0] + list(map(int, input().split()))

done_list = np.array([1])
before = 1
start = 0
end = 0

if K <= N:
    for i in range(K):
        before = a[before]
    print(before)

else:
    start = 0
    counter = 0
    for i in range(N):
        if a[done_list[-1]] in done_list:
            start = np.where(done_list == a[done_list[-1]])[0]
            counter = i
            break
        else:
            done_list = np.append(done_list, a[done_list[-1]])
    K -= counter
    K = K % (len(done_list) - start)
    print(done_list[start + K - 1][0])

Submission status: Wrong Answer

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

done_list = [a[1]]
before = a[1]
start = 0
end = 0

if K <= N:
    for i in range(K):
        before = a[before]
    print(before)

else:
    start = 0
    counter = 0
    for i in range(N):
        if a[done_list[-1]] in done_list:
            start = done_list.index(a[done_list[-1]])
            counter = i + 1
            break
        else:
            done_list.append(a[done_list[-1]])
    K -= counter
    K = K % (len(done_list) - start)
    print(done_list[start + K - 1])

Submission status: Wrong Answer

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

done_list = [a[1]]
start = 0
end = 0

if K <= N:
    for i in range(K):
        done_list.append(a[done_list[-1]])
    print(done_list[-1])

else:
    start = 0
    counter = 0
    for i in range(N):
        if a[done_list[-1]] in done_list:
            start = done_list.index(a[done_list[-1]])
            counter = i + 1
            break
        else:
            done_list.append(a[done_list[-1]])
    K -= counter
    K = K % (len(done_list) - start)
    print(done_list[start + K - 1])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
now=1
s=set()
jnbn=[]
for x in range(n):
  sl=len(s)
  now=l[now-1]
  s.add(now)
  jnbn.append(now)
  nsl=len(s)
  if sl==nsl:
    break
fp=jnbn[0]
testset=[]
testset.extend(jnbn)
testset.pop(0)
#繰り返しの暫定長さを求めた
if fp in testset:
  l=len(s)
  an=k%l
  print(jnbn[an-1])
else:
  l=len(s)-1
  an=k%l
  jnbn.pop(0)
  print(jnbn[an-2])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
now=1
s=set()
jnbn=[]
for x in range(n):
  now=l[now-1]
  s.add(now)
  jnbn.append(now)
fp=jnbn[0]
testset=[]
testset.extend(jnbn)
testset.pop(0)
if fp in testset:
  l=len(s)
  an=k%l
  print(jnbn[an-1])
else:
  l=len(s)-1
  an=k%l
  jnbn.pop(0)
  print(jnbn[an-2])

Submission status: Wrong Answer

Solution code:
from collections import deque

N,K=map(int,input().split())
A=list(map(int,input().split()))
if A[0]==1:
    print(1)
    exit()
if K==1:
    print(A[0])
    exit()
for i in range(N):
    A[i]-=1


#S = [[0 for j in range(3)] for i in range(2)]　2行3列の場合
S = [0]*N
L = deque()
S[0]=1
L.append(1)
count=1
pos=A[0]

for i in range(N+1):
    
    if S[pos]==0:
        count+=1
        S[pos]=count
        L.append(pos+1)
        pos=A[pos]
        
    else:
        roopcount=count-S[pos]+1
        L.append(pos+1)
        break


if count>K:
    ans=L[K]

else:
    K=K-(count-roopcount)
    K=K%roopcount
    for i in range(count-roopcount):
        ret=L.popleft()
    ans=L[K]
    
print(ans,count,L)

Submission status: Wrong Answer

Solution code:
from collections import deque

N,K=map(int,input().split())
A=list(map(int,input().split()))
if A[0]==1:
    print(1)
    exit()
if K==1:
    print(A[0])
    exit()
for i in range(N):
    A[i]-=1


#S = [[0 for j in range(3)] for i in range(2)]　2行3列の場合
S = [0]*N
L = deque()
S[0]=1
L.append(1)
count=1
pos=A[0]

for i in range(N+1):
    
    if S[pos]==0:
        count+=1
        S[pos]=count
        L.append(pos+1)
        pos=A[pos]
        
    else:
        roopcount=count-S[pos]+1
        L.append(pos+1)
        break


if count-1>=K:
    ans=L[K+1]

else:
    K=K-(count-roopcount)
    K=K%roopcount
    for i in range(count-roopcount):
        ret=L.popleft()
    ans=L[K]
    
print(ans)

Submission status: Wrong Answer

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

root = [1]
loop_start = 0
for i in range(n):
    pos = A[root[i] - 1]
    if pos not in root:
        root.append(pos)
    else:
        loop_start = pos
        break

idx = root.index(loop_start)
one_way, loop = root[1: idx], root[idx: ]
print(one_way[k - 1] if k <= len(one_way) else loop[(k - len(one_way) - 1) % len(loop)])

Submission status: Wrong Answer

Solution code:
from collections import deque

N,K=map(int,input().split())
A=list(map(int,input().split()))
if A[0]==1:
    print(1)
    exit()

for i in range(N):
    A[i]-=1


#S = [[0 for j in range(3)] for i in range(2)]　2行3列の場合
S = [0]*N
L = deque()
S[0]=1
L.append(1)
count=1
pos=A[0]

for i in range(N+1):
    
    if S[pos]==0:
        count+=1
        S[pos]=count
        L.append(pos+1)
        pos=A[pos]
        
    else:
        roopcount=count-S[pos]+1
        L.append(pos+1)
        break


if count-1>=K:
    ans=L[K+1]

else:
    K=K-(count-roopcount)
    K=K%roopcount
    for i in range(count-roopcount):
        ret=L.popleft()
    ans=L[K]
    
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

N,K=map(int,input().split())
A=list(map(int,input().split()))
if A[0]==1:
    print(1)
    exit()

for i in range(N):
    A[i]-=1


#S = [[0 for j in range(3)] for i in range(2)]　2行3列の場合
S = [0]*N
L = deque()
S[0]=1
L.append(1)
count=1
pos=A[0]

for i in range(N+1):
    
    if S[pos]==0:
        count+=1
        S[pos]=count
        L.append(pos+1)
        pos=A[pos]
        
    else:
        roopcount=count-S[pos]+1
        L.append(pos+1)
        break


if count-1>=K:
    ans=L[K+1]

else:
    K=K-(count-roopcount)
    K=K%roopcount
    ans=L[K+count-roopcount]
    
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

#S = [[0 for j in range(3)] for i in range(2)]　2行3列の場合
S = [0]*N
L = []
S[0]=1
L.append(1)
count=1
pos=A[0]


if A[0]==0:
    print(1)
    exit()

for i in range(N+1):
    
    if S[pos]==0:
        count+=1
        S[pos]=count
        L.append(pos+1)
        pos=A[pos]
        
    else:
        roopcount=count-S[pos]+1
        break


if count-1>=K:
    ans=L[K+1]

else:
    K=K-(count-roopcount)
    K=K%roopcount
    ans=L[K+count-roopcount]
    
print(ans,count,roopcount)

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

#S = [[0 for j in range(3)] for i in range(2)]　2行3列の場合
S = [0]*N
L = []
S[0]=1
L.append(1)
count=1
pos=A[0]

if A[0]==1:
    print(1)
    exit()


for i in range(N+1):
    
    if S[pos]==0:
        count+=1
        S[pos]=count
        L.append(pos+1)
        pos=A[pos]
        
    else:
        roopcount=count-S[pos]+1
        break


if count-roopcount>=K:
    ans=L[K-1]

else:
    K=K-(count-roopcount)
    K=K%roopcount
    ans=L[K+count-roopcount]
    
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

#S = [[0 for j in range(3)] for i in range(2)]　2行3列の場合
S = [0]*N
L = []
S[0]=1
L.append(1)
count=1
pos=A[0]
for i in range(N+1):
    
    if S[pos]==0:
        count+=1
        S[pos]=count
        L.append(pos+1)
        pos=A[pos]
        
    else:
        roopcount=count-S[pos]+1
        break


if count-roopcount>=K:
    ans=L[K-1]

else:
    K=K-(count-roopcount)
    K=K%roopcount
    ans=L[K+count-roopcount]
    
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
li = list(map(int,input().split()))
li.insert(0,0)
townlist = []
nowtown = 1
rooptown = 0
town = 1
for i in range (0,200000):
    townlist.append(nowtown)
    nowtown = li[nowtown]
    if (rooptown == 0):
        for j in townlist:
            if (nowtown == j):
                rooptown = i
                pluspoint = len(li)

K = K % rooptown + rooptown + pluspoint
nowtown = 1
for i in range (0,K):
    nowtown = li[nowtown]
print(nowtown)

Submission status: Wrong Answer

Solution code:
import sys
def f(A,N):
    INF=10**18+1
    C=[0 for _ in [0]*N]
    cp=0
    for i in range(1,N+1):
        if C[cp]>0:return (cp,C[cp]-1,i-C[cp])
        else:C[cp],cp=i,A[cp]
    return (-1,0,INF)
def main():
    N,K=tuple(map(int,sys.stdin.readline().split()))
    A=list(map(lambda x:int(x)-1,sys.stdin.readline().split()))
    a,n,m=f(A,N)
    cp=0 if a==-1 else a
    for _ in range((K-n)%m):cp=A[cp]
    print(cp+1)
if __name__=='__main__':main()

Submission status: Wrong Answer

Solution code:
import sys
def f(A,N):
    INF=sys.maxsize
    C=[0 for _ in [0]*N]
    cp=0
    for i in range(1,N+1):
        if C[cp]>0:return (cp,C[cp]-1,i-C[cp])
        else:C[cp],cp=i,A[cp]
    return (-1,0,INF)
def main():
    N,K=tuple(map(int,sys.stdin.readline().split()))
    A=list(map(lambda x:int(x)-1,sys.stdin.readline().split()))
    a,n,m=f(A,N)
    cp=0 if a==-1 else a
    for _ in range((K-n)%m):cp=A[cp]
    print(cp+1)
if __name__=='__main__':main()

Submission status: Wrong Answer

Solution code:
import sys
def f(A,N):
    INF=sys.maxsize
    C=[0 for _ in [0]*N]
    for i,a in enumerate(A,1):
        if C[a]>0:return (C[a],i-C[a]+1)
        else:C[a]=i
    return (0,INF)
def main():
    N,K=tuple(map(int,sys.stdin.readline().split()))
    A=list(map(lambda x:int(x)-1,sys.stdin.readline().split()))
    n,m=f(A,N)
    cp=0
    for _ in range(n+(K-n)%m):cp=A[cp]
    print(cp+1)
if __name__=='__main__':main()