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()))
A = [0] + A #indexを街のナンバーに合わせるために先頭に0を追加

loop = [] # 移動した街を１からここに格納
nextcity = 1
for i in range(1,N+1): #iを街のナンバーに合わせるために1からカウント
    if nextcity not in loop: # まだ行っていない街なら
        loop.append(nextcity)
        nextcity = A[nextcity]
    else:
        start = loop.index(nextcity) #start:ループが始まるindex:ループ突入前の街の数
        loop = loop[start:] # ループする部分だけに切る
        break

print(loop[((K+1-start)%len(loop)) -1])

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-

def solve():
    import sys

    N, K = list(map(int, sys.stdin.readline().split()))
    As = [-1] + list(map(int, sys.stdin.readline().split()))

    cycle_turn = 0  # 再度1に戻ってくるのにかかるテレポーテーションの回数
    turns = [1]

    now = 1
    for _ in range(N):
        cycle_turn += 1
        nx = As[now]
        turns.append(nx)

        if nx == 1:
            break
        now = nx

    k = K%cycle_turn
    print(turns[k])


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

warped = [ False ] * n
warped[0] = True

x = 0
cycle = 0

while not warped[warping[x]]:
    x = warping[x]
    warped[x] = True

    cycle += 1

x = warping[x]
cycle += 1

y = 0
cnt = 0

while warping[y] != x:
    y = warping[y]
    cnt += 1

y = warping[y]
cnt += 1

cycle -= cnt

mod = (k - cnt) % (cycle if cycle != 0 else cnt)

while mod:
    y = warping[y]

    mod -= 1

print(y + 1)

Submission status: Wrong Answer

Solution code:
#N,K入力
N,K= map(int,input().split())
#街のテレポート先入力
A = [int(i)-1 for i in input().split()]
judge = [-1] * N
judge[0] = 0
#初期位置
a = 0
#移動回数
step = 1

while step != K:
    #移動する街を決める
    a = A[a]
    if judge[a] > -1:
        break
    #何回目の移動なのか加える
    judge[a] = step
    step = step + 1

if step == K:
    print(a+1)
    # プログラム終了
else:
    width = step - judge[a]
    s =(K - judge[a]) % width
    print(judge.index(judge[a]+s)+1)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
A = list(map(int, input().split()))
p = 1
p_hist = []
p_hist_set = set(p_hist)
while p not in p_hist_set:
    p_hist.append(p)
    p_hist_set.add(p)
    p = A[p-1]
end_loop = p_hist[-1]
end_loop_idx = p_hist.index(end_loop)
start_loop = A[end_loop - 1]
start_loop_idx = p_hist.index(start_loop)
print(p_hist[(K-start_loop_idx)%(end_loop_idx-start_loop_idx+1)+start_loop_idx])

Submission status: Wrong Answer

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

adj = defaultdict(int)
for i in range(len(A)):
    adj[i+1] = A[i]

d = 0
D = 1
slow = adj[1]
fast = adj[adj[1]] 
#print(fast, slow)
while fast != slow:
    D += 1
    slow = adj[slow]
    fast = adj[adj[fast]]

slow = 1
while fast != slow:
    D += 1
    d += 1
    slow = adj[slow]
    fast = adj[fast]


r = (K - d) % (D - d)
#print(slow, r)
while r:
    slow = adj[slow]
    r -= 1

print(slow)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
A = list(map(int, input().split()))
p = 1
p_hist = []
while p not in p_hist:
    p_hist.append(p)
    p = A[p-1]
end_loop = p_hist[-1]

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
N, K = map(int, input().split())
A = list(map(lambda x:int(x), input().split()))
adj = defaultdict(int)
for i in range(len(A)):
    adj[i+1] = A[i]

d = 0
D = 1
slow = adj[1]
fast = adj[adj[1]] 

while fast != slow:
    D += 1
    slow = adj[slow]
    fast = adj[adj[fast]]

slow = 1
while fast != slow:
    D += 1
    d += 1
    slow = adj[slow]
    fast = adj[fast]


r = (K - d) % (D - d)

while r:
    slow = adj[slow]
    r -= 1
print(slow)

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip() # input string
ni = lambda: int(readline().rstrip()) # input int
nm = lambda: map(int, readline().split()) # input multiple int 
nl = lambda: list(map(int, readline().split())) # input multiple int to list

N, K = nm()
A = nl()

dir = []
now = 0
cnt = 0
emerged = [False] * N
while True:
    cnt += 1
    if cnt == K: break
    now = A[now] - 1
    if emerged[now]: 
        dir.append(now)
        break
    emerged[now] = True
    dir.append(now)
if len(dir) == 2:
    print(1)
elif cnt == K:
    print(now + 1)
else:
    indexes = [i for i, x in enumerate(dir) if x == dir[-1]]
    a, b = indexes[0], indexes[1]
    print(dir[(K - a) % (b - a) + a - 1] + 1)

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip() # input string
ni = lambda: int(readline().rstrip()) # input int
nm = lambda: map(int, readline().split()) # input multiple int 
nl = lambda: list(map(int, readline().split())) # input multiple int to list

N, K = nm()
A = nl()

dir = []
now = 0
cnt = 0
emerged = [False] * N
while True:
    cnt += 1
    if cnt == K: break
    now = A[now] - 1
    if emerged[now]: 
        dir.append(now)
        break
    emerged[now] = True
    dir.append(now)
if len(dir) == 2:
    print(1)
elif cnt == K:
    print(now)
else:
    indexes = [i for i, x in enumerate(dir) if x == dir[-1]]
    a, b = indexes[0], indexes[1]
    print(dir[(K - a) % (b - a) + a - 1] + 1)

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip() # input string
ni = lambda: int(readline().rstrip()) # input int
nm = lambda: map(int, readline().split()) # input multiple int 
nl = lambda: list(map(int, readline().split())) # input multiple int to list

N, K = nm()
A = nl()

dir = []
now = 0
cnt = 0
emerged = [False] * N
while cnt < K:
    cnt += 1
    now = A[now] - 1
    if emerged[now]: 
        dir.append(now)
        break
    emerged[now] = True
    dir.append(now)
if len(dir) == 2:
    print(1)
elif cnt == K:
    print(now)
else:
    indexes = [i for i, x in enumerate(dir) if x == dir[-1]]
    a, b = indexes[0], indexes[1]
    print(dir[(K - a) % (b - a) + a - 1] + 1)

Submission status: Wrong Answer

Solution code:
import sys
#N,K入力
N,K= map(int,input().split())
#街の番号入力
A = [int(i) for i in input().split()]

# 重複した要素があるかどうかを判定
def has_duplicates(seq):
    return len(seq) == len(set(seq))

#移動経路
warp = [1]
a = 1

#同じ街にたどり着くまでループ
while has_duplicates(warp):
    a = A [a - 1]
    warp.append(a)
    #同じ街にたどり着かなかった場合はこっち
    if len(warp) == K +1:
        print(warp[K])
        sys.exit()

l = [i for i, x in enumerate(warp) if x == a]

# 何回ワープしたらループする街に戻ってくるか
width = l[1] - l[0]

#　ループのどこに行くか
d = (K - l[0]) % width

#結果の出力
print(A[d + l[0]])

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip() # input string
ni = lambda: int(readline().rstrip()) # input int
nm = lambda: map(int, readline().split()) # input multiple int 
nl = lambda: list(map(int, readline().split())) # input multiple int to list

N, K = nm()
A = nl()

dir = []
now = 0
cnt = 0
emerged = [False] * N
while True:
    now = A[now] - 1
    if emerged[now]: 
        dir.append(now)
        break
    emerged[now] = True
    dir.append(now)
if len(dir) == 2:
    print(1)
else:
    indexes = [i for i, x in enumerate(dir) if x == dir[-1]]
    a, b = indexes[0], indexes[1]
    print(dir[(K - a) % (b - a) + a - 1] + 1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
S = []
S.append(1)
S.append(A[0])
while True:
    idx = S[-1]
    loc = A[idx-1]
    if loc not in S:
        S.append(loc)
    else:
        start = S.index(loc)
        end = len(S)
        span = end - start
        break

n = K%span
print(S[n])

Submission status: Wrong Answer

Solution code:
n, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
route_list = []
current_index = n
while current_index not in route_list:
    route_list.append(current_index)
    value = a[current_index-1]
    current_index = value
cycle_route = route_list[route_list.index(current_index):]
temp = len(route_list[:route_list.index(current_index)])
cycle_route[(k - temp) % len(cycle_route)-1]

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip() # input string
ni = lambda: int(readline().rstrip()) # input int
nm = lambda: map(int, readline().split()) # input multiple int 
nl = lambda: list(map(int, readline().split())) # input multiple int to list

N, K = nm()
A = nl()

dir = []
now = 0
cnt = 0
emerged = [False] * N
while True:
    now = A[now] - 1
    if emerged[now]: 
        dir.append(now)
        break
    emerged[now] = True
    dir.append(now)
indexes = [i for i, x in enumerate(dir) if x == dir[-1]]
a, b = indexes[0], indexes[1]

print(dir[(K - a) % (b - a) + a - 1] + 1)

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip() # input string
ni = lambda: int(readline().rstrip()) # input int
nm = lambda: map(int, readline().split()) # input multiple int 
nl = lambda: list(map(int, readline().split())) # input multiple int to list

N, K = nm()
A = nl()

dir = []
now = 0
cnt = 0
while True:
    cnt += 1
    now = A[now] - 1
    dir.append(now)
    if len(dir) != len(set(dir)): break
indexes = [i for i, x in enumerate(dir) if x == dir[-1]]
a, b = indexes[0], indexes[1]

print(dir[(K - a) % (b - a) + a - 1] + 1)

Submission status: Wrong Answer

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

city = a[1]
array = [1, a[1]]
checked = [False for _ in range(n+1)]
checked[1] = True
while True:
  tmp = a[city]
  city = tmp
  if checked[city]:
    break
  else:
    checked[city] = True
    array.append(tmp)
p = array.index(city)
q = len(array) - p
print(array[p+(k-p)%q])

Submission status: Wrong Answer

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

n=0
for _ in range(k):
    n-=1
    n=a[n]

print(n+1)

Submission status: Wrong Answer

Solution code:
            
import sys
stdin=sys.stdin

ns = lambda:stdin.readline().rstrip()
ni = lambda:int(stdin.readline().rstrip())
nm = lambda:map(int,stdin.readline().split())
nl = lambda:list(map(int,stdin.readline().split()))

N,K=nm()
A=nl()

city_l=[0]
city_l.append(1)
city_c=[0]*(N+1)
city_c[1]+=1
n_city=A[0]
flag=True
r_start=-1
r_end=-1
while(flag):
    city_c[n_city]+=1
    city_l.append(n_city)
    if(city_c[n_city]>1):
        r_start=city_l.index(n_city)
        r_end=len(city_l)-1
        city_l.append(A[n_city-1])
        flag=False
    n_city=A[n_city-1]

if(K<=r_end):
    print(city_l[K])


else:
    K2=K-r_end+1
    K2=K2%(r_end-r_start)
    if(K2==0):
       print(city_l[r_end])
    else:
       print(city_l[r_start+K2])

Submission status: Wrong Answer

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

seen = {1: 0}
a = 1

for i in range(1, n+1):
  if seen.setdefault(a:=A[a-1], i) != i:
    break

p = seen[a]
q = i - seen[a]

k = (k - p) % q + p

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

print(a)

Submission status: Wrong Answer

Solution code:
            
import sys
stdin=sys.stdin

ns = lambda:stdin.readline().rstrip()
ni = lambda:int(stdin.readline().rstrip())
nm = lambda:map(int,stdin.readline().split())
nl = lambda:list(map(int,stdin.readline().split()))

N,K=nm()
A=nl()

city_l=[0]
city_l.append(1)
city_c=[0]*(N+1)
city_c[1]+=1
n_city=A[0]
flag=True
r_start=-1
r_end=-1
while(flag):
    city_c[n_city]+=1
    city_l.append(n_city)
    if(city_c[n_city]>1):
        r_start=city_l.index(n_city)
        r_end=len(city_l)-1
        city_l.append(A[n_city-1])
        flag=False
    n_city=A[n_city-1]

if(K<=r_end):
    print(city_l[K])


else:
    K2=K-r_end+1
    K2=K2%(r_end-r_start)
    if(K2==0):
       print(city_l[r_end-1])
    else:
       print(city_l[r_start+K2])

Submission status: Wrong Answer

Solution code:
def solve(string):
    n, k, *a = map(int, string.split())
    a, t = [_a - 1 for _a in a], [0] * n
    p = c = 0
    while not t[a[p]]:
        c += 1
        t[a[p]] = c
        p = a[p]
    k = (k - t[a[p]] + 1) % (t[p] - t[a[p]] + 1)
    for _ in (range(k)):
        p = a[p]
    return str(p + 1)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Wrong Answer

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

seen = {1: 0}

a = 1
for i in range(1, n+1):
  a = A[a-1]
  if a in seen:
    break
  else:
    seen[a] = i

p = seen[a]
q = i - seen[a]

k = (k - p) % q + p

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

print(a)

Submission status: Wrong Answer

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

N,K = map(int,input().rstrip().split())
A = [int(x)-1 for x in input().rstrip().split()]

now = 0
d = {}

for i in range(0,N):
    if now in d:
        K -= d[now]
        K %= i-d[now]
        for j in range(K):
            now = A[now]
        break
    else:
        d[now] = i

    now = A[now]

print(now+1)

Submission status: Wrong Answer

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

visited = [-1]*N
next = 1

for i in range(K+1):
  present = next
  if i == K:
    print(present)
    exit(0)

  visited[present-1] = i
  next = A[present-1]
  if  visited[next-1] != -1:
    l_loop = visited[present-1] - visited[next-1] + 1
    cnt = i
    break

rest = (K-cnt) % l_loop
if rest == 0:
  print(present)
else:
  present = visited.index(visited[next-1] + rest - 1)
  print(present)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
cnt = 0
visited = []
next = 1
for i in range(1, K+1):
  present = next
  if i == K:
    print(present)
    exit(0)

  visited.append(present)
  next = A[present-1]
  if next in visited:
    ind = visited.index(next)
    l = len(visited[ind:])
    cnt = i
    break

rest = (K-cnt) % l
present = visited[ind + rest]
print(present)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys,pprint,readline

av = {}
passed = set()

(N,K) = [int(x) for x in input().split(" ")]

arrivalary = list(map(int,input().split(" ")))

'''
N =  200000
K = 12345678901234567
arrivalary = []
for i in range(1,N):
    arrivalary.append(i+1)
arrivalary.append(1)
'''

i = 0
loopflg = 0
pos = 1
nextpos = 0
loopstartpos = 0
loopstartnum = 0
loopcount = 0
while i < K:
    passed.add(pos)
    nextpos = arrivalary[pos-1]
    #print(i,pos,nextpos,loopflg,loopstartpos,loopcount,file=sys.stderr)
    if loopflg == 0 and nextpos in passed:
        loopflg = 1
        loopstartpos = nextpos
        loopstartnum = i
        loopcount = 1
    elif loopflg == 1 and nextpos == loopstartpos:
        left = (K-loopstartnum) % loopcount
        i = K-left
    elif loopflg == 1:
        loopcount += 1
    pos = nextpos
    i += 1

print(pos)

Submission status: Wrong Answer

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

loop = []
nextcity = 1
for i in range(1,N+1): #Kデカすぎるので途中でbreak ループするところを保存したいな
    if nextcity in loop:
        start = loop.index(nextcity)
        loop = loop[start:]
        break
    loop.append(nextcity)
    nextcity = A[nextcity]
    
print(start)
print(loop[((K-start+1)%len(loop))-1])

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys,pprint,readline

av = {}
passed = set()

(N,K) = [int(x) for x in input().split(" ")]

arrivalary = input().split(" ")
i = 1
while len(arrivalary) > 0:
    a = int(arrivalary.pop(0))
    av[i] = a
    i += 1

i = 0
loopflg = 0
pos = 1
nextpos = 0
loopstartpos = 0
loopstartnum = 0
loopcount = 0
while i < K:
    passed.add(pos)
    nextpos = av[pos]
    #print(i,pos,nextpos,loopflg,loopstartpos,loopcount,file=sys.stderr)
    if loopflg == 0 and nextpos in passed:
        loopflg = 1
        loopstartpos = nextpos
        loopstartnum = i
        loopcount = 1
    elif loopflg == 1 and nextpos == loopstartpos:
        left = (K-loopstartnum) % loopcount
        i = K-left
    elif loopflg == 1:
        loopcount += 1
    pos = nextpos
    i += 1

print(pos)

Submission status: Wrong Answer

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

loop = []
nextcity = 1
for i in range(1,N+1): #Kデカすぎるので途中でbreak ループするところを保存したいな
    if nextcity in loop:
        break
    loop.append(nextcity)
    nextcity = A[nextcity]

print(loop[K%len(loop)])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
cnt = 0
visited = []
next = 1
while True:
  present = next
  cnt += 1
  if cnt == K:
    print(present)
    break

  visited.append(present)
  next = A[present-1]
  if next in visited:
    ind = visited.index(next)
    l = len(visited[ind:])
    break

if cnt != K:
  rest = (K-cnt) % l
  present = visited[ind + rest]
  print(present)

Submission status: Wrong Answer

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

p = 1
points = [p]
for _ in range(N):
    p = A[p]
    if p in points:
        break
    points.append(p)

head = len(points[:points.index(p)])
cycle = len(points[points.index(p):])

for _ in range((K - head) % cycle):
    p = A[p]

print(p)

Submission status: Wrong Answer

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

visited = set([1])
loop = [1]
now = 1

for _ in range(N):
    now = A[now]
    if now in visited:
        break
    else:
        loop.append(now)
        visited.add(now)

x = loop.index(now)

if K < x:
    if x+1 < len(loop):
        print(loop[x+1])
    else:
        print(loop[0])
else:
    K -= x
    loop = loop[x:]
    K %= len(loop)
    print(loop[K])

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
A = [int(i)-1 for i in input().split()]

visited = [0]
visited_flg = [0 for _ in range(N)]
visited_flg[0] = 1
now_city = 0

next_city = A[0]
# visited.append(A[0])

# if K <= N:
#     p = 0
#     for _ in range(K):
#         p = A[p]
#     print(p + 1)
#     exit()


while True:
    next_city = A[now_city] 
    if visited_flg[next_city]:
        break
    
    now_city = next_city
    visited.append(now_city)
    visited_flg[now_city] = 1
    

loop_start = next_city
loop_city =  visited[visited.index(loop_start):]
k =  (K- len(visited)) % len(loop_city)
print(loop_city[k]+1)

Submission status: Wrong Answer

Solution code:
N, K = [int(i) for i in input().split()]
A = [int(i)-1 for i in input().split()]

i = 0
s = "0"
while True:
    if str(A[i]) in s:
        r = s.index(str(A[i]))
        break
    s +=str(A[i])
    i = A[i]

print( int( s[r:][ (K-r)%(len(s)-r) ] )+1 )

Submission status: Wrong Answer

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


visited = [1]
next_city = A[0]
visited.append(A[0])

if K <= N:
    p = 0
    for _ in range(K):
        p = A[p]
    print(p + 1)
    exit()


while True:
    next_city = A[next_city-1] 
    if next_city in visited:
        break
    visited.append(next_city)
loop_start = next_city
loop_city =  visited[visited.index(loop_start):]
k =  (K- ( N - len(loop_city) - 1 )) % len(loop_city)
print(loop_city[k])

Submission status: Wrong Answer

Solution code:
def test():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    p = [1]
    s = {1, }
    point = 1
    for i in range(n):
        point = a[point-1]
        if point in p:
            break

        p.append(point)
        s.add(point)

    for l in range(len(p)):
        if point == p[l]:
            i = l
            break

    k -= i
    m = len(p) - i
    p = p[i:]
    mod = k % m
    print(p[mod])


if __name__ == "__main__":
    test()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
from decimal import Decimal
k=Decimal(k)
r=[-1]*n
b=[]
lambda_1=lambda s:int(s)-1
a=list(map(lambda_1, input().split()))
c=0
for ii in range(2*len(a)):
	b.append(a[c])
	if r[c]!=-1:
		a0=b.index(r[c])
		k0=(k-(a0+1))%(len(b)-(a0+1))+a0
		k0=int(k0)
		"""print(c,a0,k0,b)
		print(r)"""
		print(b[k0]+1)
		exit()
	r[c]=a[c]
	c=a[c]

#print(a[len(a)-1])

Submission status: Wrong Answer

Solution code:
import sys
stdin=sys.stdin

ns = lambda:stdin.readline().rstrip()
ni = lambda:int(stdin.readline().rstrip())
nm = lambda:map(int,stdin.readline().split())
nl = lambda:list(map(int,stdin.readline().split()))

N,K=nm()
A=nl()

city_l=[]
city_l.append(1)
city_c=[0]*(N+1)
city_c[1]+=1
n_city=A[0]
flag=True
r_start=-1
r_end=-1
while(flag):
    city_c[n_city]+=1
    city_l.append(n_city)
    if(city_c[n_city]>1):
        r_start=city_l.index(n_city)+1
        r_end=len(city_l)-1
        flag=False
    n_city=A[n_city-1]

if(K<=r_end):
    print(city_l[K-1])


else:
    K2=K-r_end
    K2=K2%(r_end-r_start+1)
    if(K2==0):
       print(A[city_l[r_end-1]-1])
    else:
       print(A[city_l[r_start+K2-2]-1])

Submission status: Wrong Answer

Solution code:
def main():
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    warp_idx = [-1] * (len(A) + 1)
    ans_list = []

    c = 1
    i = 0
    while warp_idx[c] == -1:
        warp_idx[c] = i
        ans_list.append(c)
        c = A[c - 1]
        i += 1
    start = warp_idx[c]
    loop = i - start
    if K < loop:
        print(ans_list[K])
    else:
        step = K - start
        step %= loop
        print(ans_list[start+step])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


visited = [1]
next_city = A[0]
visited.append(A[0])
while True:
    next_city = A[next_city-1] 
    if next_city in visited:
        break
    visited.append(next_city)
loop_start = next_city
loop_city =  visited[visited.index(loop_start):]
k =  (K-(N-len(loop_city)-1))%len(loop_city)
print(loop_city[k])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
from decimal import Decimal
k=Decimal(k)
r=[-1]*n
b=[]
lambda_1=lambda s:int(s)-1
a=list(map(lambda_1, input().split()))
c=0
for ii in range(2*len(a)):
	b.append(a[c])
	if r[a[c]]!=-1:
	    a0=b.index(r[a[c]])
	    k0=(k-(a0+1))%(len(b)-(a0+1))+a0
	    k0=int(k0)
	    #print(b)
	    #print(r)
	    print(b[k0]+1)
	    exit()
	c=a[c]
	r[c]=c

#print(a[len(a)-1])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
r=[-1]*n
b=[]
lambda_1=lambda s:int(s)-1
a=list(map(lambda_1, input().split()))
c=0
for ii in range(2*len(a)):
	b.append(a[c])
	if r[a[c]]!=-1:
	    a0=b.index(r[a[c]])
	    k0=(k-(a0+1))%(len(b)-(a0+1))+a0
	    #print(b)
	    #print(r)
	    print(b[k0]+1)
	    exit()
	c=a[c]
	r[c]=c

#print(a[len(a)-1])

Submission status: Wrong Answer

Solution code:
import math

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

    state = 1
    chklist = [0] * (N + 1)
    chklist_flag = [False] * (N + 1)
    chkmod = 0

    chklist_flag[0] = True

    for i in range(1, K + 1):
        state = A[state - 1]
        if chklist_flag[state - 1]:
            chkmod = (K - chklist[state - 1]) % (i - chklist[state - 1]) 
            break
        chklist[state - 1] = i 
        chklist_flag[state - 1] = True

    for i in range(chklist[state - 1] + chkmod):
        state = A[state - 1]

    print(state)

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import math

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

    state = 1
    chklist = [0] * (N + 1)
    chklist_flag = [False] * (N + 1)
    chkmod = 0

    chklist_flag[0] = True

    for i in range(1, K + 1):
        print(chklist)
        state = A[state - 1]
        if chklist_flag[state - 1]:
            chkmod = (K - chklist[state - 1]) % (i - chklist[state - 1]) 
            break
        chklist[state - 1] = i 
        chklist_flag[state - 1] = True

    for i in range(chklist[state - 1] + chkmod):
        state = A[state - 1]

    print(state)

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

done_list = [1]
cur_place = 1
flag = True
for i in range(K):
    if a_list[cur_place-1] not in done_list:
        cur_place = a_list[cur_place-1]
        done_list.append(cur_place)
    else:
        period = len(done_list) - done_list.index(a_list[cur_place-1])
        print(done_list[done_list.index(a_list[cur_place-1]) + (K - i + 1) % period])
        flag = False
        break

if flag:        
    print(cur_place)

Submission status: Wrong Answer

Solution code:
import math

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

    state = 0
    chklist = [0] * N
    chklist_flag = [False] * N
    chkmod = 0

    chklist_flag[0] = True

    for i in range(1, K + 1):
        if chklist_flag[state - 1]:
            chkmod = (K - chklist[state - 1]) % (i - chklist[state - 1]) 
            break
        state = A[state]
        chklist[state - 1] = i 
        chklist_flag[state - 1] = True
 

    for i in range(chklist[state - 1] + chkmod):
        chklist[state - 1] = i 
        state = A[state- 1]

    print(state)

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import math

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

    state = 0
    chklist = [0] * N
    chklist_flag = [False] * N
    chkmod = 0

    chklist_flag[0] = True

    for i in range(1, K + 1):
        if chklist_flag[state - 1]:
            chkmod = (K - chklist[state - 1]) % (i - chklist[state - 1]) 
            break
        chklist[state - 1] = i 
        chklist_flag[state - 1] = True
        state = A[state]

    for i in range(chklist[state - 1] + chkmod):
        chklist[state - 1] = i 
        state = A[state- 1]

    print(state)

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

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

t_sort = sorted(t)
for i in range(n):
    if t_sort[i] == t_sort[i + 1]:
        b = t_sort[i]
        break

count = 0
for i in range(n):
    if t[i] == b:
        if count == 0:
            count += 1
            s_first = i
        else:
            s_second = i
            break
s = s_second - s_first

for i in range(n - s + 1):
    if t[i] == t[i + s]:
        break

if k < i:
    print(t[k])
else:
    print(t[(k - 1) % s + i - 1])

Submission status: Wrong Answer

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

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

t_sort = sorted(t)
for i in range(n):
    if t_sort[i] == t_sort[i + 1]:
        b = t_sort[i]
        break

count = 0
for i in range(n):
    if t[i] == b:
        if count == 0:
            count += 1
            s_first = i
        else:
            s_second = i
            break
s = s_second - s_first

for i in range(n - s + 1):
    if t[i] == t[i + s]:
        break

if k < i:
    print(t[k])
else:
    print(t[(k - 1) % s + i])

Submission status: Wrong Answer

Solution code:
import math

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

crnt = 1
st = set()
wl = []
key = crnt
cy = 0
mg = 0


for i in range(n):
    if crnt in st:
        key = crnt
        cy = i - wl.index(crnt)
        mg = wl.index(crnt)
        break
    st.add(crnt)
    wl.append(crnt)
    crnt = li[crnt - 1]

k -= mg
if cy != 0:
    k %= cy
crnt = key
for i in range(k):
    crnt = li[crnt - 1]

print(crnt)

Submission status: Wrong Answer

Solution code:
import math

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

crnt = 1
st = set()
wl = []
key = crnt
cy = 0
mg = 0

for i in range(n):
    if crnt in st:
        key = crnt
        cy = i - wl.index(crnt)
        mg = wl.index(crnt)
        break
    st.add(crnt)
    wl.append(crnt)
    crnt = li[crnt - 1]

k -= mg
if cy != 0:
    k %= cy
crnt = key
for i in range(k):
    crnt = li[crnt - 1]

print(crnt)

Submission status: Wrong Answer

Solution code:
import logging
logging.basicConfig(level=logging.INFO)
info = logging.getLogger(__name__).info

N,K = [int(x) for x in input().split()]
An = [int(x) for x in input().split()]

Aline = [1]
info(An)

count = 1
Ai = An[0]
while not Ai in Aline:
  info("Ai:{}".format(Ai))
  count += 1
  Aline.append(Ai)
  Ai = An[Ai-1]

Aline.append(Ai)
loopi = Aline.index(Ai)
step = len(Aline) - 1 - loopi
info("Ai:{} and loop {} to {} {}steps".format(Ai,loopi, len(Aline)-1,step))

info("Aloop: {}".format(Aline))
info("K:{} loopi:{} loop:{}".format(K,loopi, (len(Aline)-loopi - 1)))
k = (K - loopi) % (step)
info(k)

print(An[loopi+k])

Submission status: Wrong Answer

Solution code:
n,k = (int(x) for x in input().split())
a = tuple([int(x) for x in input().split()])
 
c = [0] + [-1]*(n-1)
l = [1]
now = 1
dupl = -1

if n >= k:
  for i in range(n):
    now = a[now - 1]
  print(now)
  exit()

for i in range(k):
  now = a[now - 1]
  c[now - 1] += 1
   
  if c[now - 1] == 1:
    dupl = now
    break
  else:
    l.append(now)
       
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()))

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

for i in range(n):
    for j in range(i + 1, n):
        if t[i] == t[j]:
            s = j - i
            break
    else:
        continue
    break

if k < i:
    print(t[k])
else:
    print(t[(k - i) % s + i])

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    a = list(map(lambda x: int(x)-1, input().split()))
    flag = [-1 for i in range(n)]
    now = 0
    cnt = 0

    while flag[now] == -1:
        flag[now] = cnt
        cnt += 1
        now = a[now]
    # print(cnt, now, flag[now])
    k = (k-flag[now]) % (cnt-flag[now])

    for i in range(k):
        now = a[now]
    print(now+1)
    return


if __name__ == "__main__":
    main()