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 = [0]+list(map(int,input().split()))
visited = [False for _ in range(n+1)]
pos = 1
x = []
flg = False
for i in range(k):
    if visited[pos] == False:
        x.append(pos)
        visited[pos] = True
        pos = A[pos]
    else:
        flg = True
        pre = x.index(pos)
        loop = x[pre:]
        break
print(loop[(k-pre)%len(loop)] if flg else x[-1])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = [0] + list(map(int, input().split()))
B = [False for _ in range(n+1)]
B[1] = True
C = [1]

for i in range(1,n):
    C.append(A[C[-1]])
    #print(C)
    B[C[-1]] = True
    if B[A[C[-1]]] == True:
        C.append(A[C[-1]])
        break

num = i + 1
s = C.index(C[-1])
N = num - s

if k < N:
    print(C[k])
else:
    print(C[k%N])

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 = 1
    for i in range(1, K + 1):
        nxt_pos = A[cur_pos]
        if (j := last_visit[nxt_pos]) != -1:
            print(last_visit.index(j + (K - j) % (i - j)))
            break
        cur_pos = nxt_pos
        last_visit[cur_pos] = i


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 = 1
    for i in range(1, K + 1):
        nxt_pos = A[cur_pos]
        if (j := last_visit[nxt_pos]) != -1:
            print(last_visit.index(j + (K - i) % (i - j)))
            break
        cur_pos = nxt_pos
        last_visit[cur_pos] = i


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np
n, k = [int(k) for k in input().split()]
a = np.array([int(k) for k in input().split()], dtype=int)
used = np.full(n, -1)
r = 1
for i in range(n):
    r = a[r - 1]
    if used[r - 1] != -1:
        k = k % (i - used[r - 1])
        break
    used[r - 1] = i
r = 1
for i in range(k):
    r = a[r - 1]
print(r)

Submission status: Wrong Answer

Solution code:
import numpy as np
n, k = [int(k) for k in input().split()]
a = np.array([int(k) for k in input().split()], dtype=int)
used = np.full(n, -1)
r = 1
for i in range(n):
    r = a[r - 1]
    if used[r - 1] != -1:
        k = i - used[r - 1]
        break
    used[r - 1] = i
r = 1
for i in range(k - 1):
    r = a[r - 1]
print(r)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import copy
def main():
    N, K = map(int, input().split())
    A = [(int(i) - 1) for i in input().split()]
    d = []
    for i in range(N):
        d.append(-1)
    d[0] = 0
    now = 0
    cycle = 0
    ans = -1
    a = 0
    flag = True
    while a < K:
        if flag:
            if d[A[now]] == -1:
                d[A[now]] = d[now] + 1
            else:
                cycle = (d[now] + 1) - d[A[now]]
                K = (K - (d[now] + 1)) % cycle
                a = 0
                flag = False
        a += 1
        now = A[now]
    print(now + 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

idx = a[0]
hist = [1]
passed = [0] * n
flag = 0
for i, _ in enumerate(range(k), start=1):
    if passed[idx-1] == 1:
        hist.append(idx)
        passed[idx-1] = 1
        flag = 1
        break
    hist.append(idx)
    passed[idx-1] = 1
    idx = a[idx-1]
    
    
if flag:
    l_start, l_end = hist.index(idx), i
    surplus = (k - l_start) % (l_end - l_start)
    print(hist[l_start + surplus])
else:
    #print(idx)
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import copy
def main():
    N, K = map(int, input().split())
    A = [(int(i) - 1) for i in input().split()]
    d = []
    for i in range(N):
        d.append(-1)
    d[0] = 0
    now = 0
    cycle = 0
    ans = -1
    while ans < 0:
        if d[A[now]] == -1:
            d[A[now]] = d[now] + 1
        else:
            cycle = (d[now] + 1) - d[A[now]]
            ans = (K - (d[now] + 1)) % cycle
        now = A[now]
    while ans != 0:
        now = A[now]
        ans -= 1
    print(now + 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

idx = a[0]
hist = [1]
passed = [0] * n
flag = 0
for i, _ in enumerate(range(k), start=1):
    if passed[idx-1] == 1:
        hist.append(idx)
        passed[idx-1] = 1
        flag = 1
        break
    hist.append(idx)
    passed[idx-1] = 1
    idx = a[idx-1]
    
if flag:
    l_start, l_end = hist.index(idx), i
    surplus = (k-l_start) % (l_end - l_start)
    print(hist[l_start+surplus])
else:
    print(idx)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
move=[1]
for i in range(k):
  next=move[i]
  move.append(next)
  
print(move[k])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[0]+list(map(int,input().split()))
pointer=1
visited=[1]
for i in range(n):
    if a[pointer] in visited:
        loopstart=visited.index(a[pointer])
        looplength=len(visited)-visited.index(a[pointer])
        break
    else:
        visited.append(a[pointer])
        pointer = a[pointer]
        
print(visited[loopstart+(k-loopstart)%looplength])

Submission status: Wrong Answer

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

idx = a[0]
hist = [1]
flag = 0
for i, _ in enumerate(range(k), start=1):
    if idx in hist:
        hist.append(idx)
        flag = 1
        break
    hist.append(idx)
    idx = a[idx-1]
    
if flag:
    l_start, l_end = hist.index(idx), i
    surplus = (k-l_start) % (l_end - l_start)
    print(hist[l_start+surplus])
else:
    print(idx)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
visited = [False for _ in range(n)]
x = []
now = 0
flg = False

for i in range(k):
    if visited[now] == False:
        visited[now] = True
        x.append(now)
        now = A[now] - 1
    else:
        flg = True
        t = x.index(now)
        pre = x[:t]
        T = x[t:]
        break
if flg == False:
    print(x[-1]+1)
else:
    k -= len(pre)
    k %= len(T)
    print(T[k] + 1)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
A = [0]+list(map(int,input().split()))
visited = [False for _ in range(n+1)]
to = 1
pre = -1
for e, i in enumerate(range(n)):
    to = A[to]
    if visited[to] == False:
        visited[to] = e+1
    else:
        pre = visited[to] - 1
        dif = (e+1) - visited[to]
        break
if pre >= 0:
    idx = (m - pre) % dif
    if idx==0:
        idx = pre + dif
    else:
        idx = idx + pre
    print(visited.index(idx))
else:
    print(visited.index(m))

Submission status: Wrong Answer

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

i = 0
aa = 0
visited = {}
for i in range(k):
    aa = a[aa] - 1
    if aa in visited:
        s, e = visited[aa], i
        loop = e - s
        k = s + ((k - s) % loop)
        print(a[k])
        exit(0)
    visited[aa] = i

print(aa+1)

Submission status: Wrong Answer

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

i = 0
aa = 0
visited = {}
for i in range(k):
    aa = a[aa] - 1
    if aa in visited:
        s, e = visited[aa], i
        loop = e - s
        k = s + ((k - s) % loop)
        exit(0)
    visited[aa] = i

print(aa + 1)

Submission status: Wrong Answer

Solution code:
# Author : code_marshal


n, k = map(int, input().split())
teleport = list(map(int, input().split()))
i = s = 0
while k:
    s = teleport[i]
    k-=1
print (s)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
An = list(map(int,input().split()))
a = An[0]
b = [0]
counter = 1
for i in range(N):
    a = An[a-1]
    b += [a]
    counter += 1
    if a in b[:counter-1]:
        c = b[b.index(a):]
        print(c)
        d = (K - counter) % (len(c)-1)
        if d == 0:
            print(a)
            break
        else:
            for k in range(d-1):
                a = An[a-1]
                print(a)
            break
    elif i == K-2:
        print(a)
        break

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
An = list(map(int,input().split()))
a = An[0]
b = [0]
counter = 1
for i in range(N):
    a = An[a-1]
    b += [a]
    counter += 1
    if a in b[:counter-1]:
        c = b[b.index(a):]
        print(c)
        d = (K - counter) % (len(c)-1)
        if d == 0:
            print(a)
        else:
            for k in range(d-1):
                a = An[a-1]
                print(a)
        break
    elif i == K-2:
        print(a)
        break

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
An = list(map(int,input().split()))
a = An[0]
b = [0]
counter = 0
for i in range(N):
    a = An[a-1]
    b += [a]
    counter += 1
    if a in b[:counter-1]:
        c = b[b.index(a):]
        d = (K - counter) % (len(c)-1)
        if d == 0:
            print(a)
        for k in range(d-1):
            a = An[a-1]
        print(a)
        break
    elif i == K-2:
        print(a)
        break

Submission status: Wrong Answer

Solution code:
n,k,*a=map(int,open(0).read().split())
d=[2*k]*n
s=0
while k:s=a[s]-1;d[s]=k=-k%(~d[s]+k)
print(s+1)

Submission status: Wrong Answer

Solution code:


n, m = map(int,input().split())

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

visited = [0]

now = 0
next = -1

while True:
    next = A[now]-1
    if next in visited:
        break
    else:
        now = next
        visited.append(now)

print(next)
if m < len(visited):
    print(visited[m]+1)
else:
    loop = len(visited)-visited.index(next)
    m-=(len(visited)-loop)
    print(visited[m%loop+visited.index(next)]+1)

Submission status: Wrong Answer

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

s=[]
d=[-1]*(n+1)

v=1
while(d[v]==-1):
    d[v]=len(s)
    s.append(v)
    v=a[v-1]
c=len(s)-d[v]
l=d[v]

if(k<l):print(s[k])
else:
    k-=l
    k%=c
    print(k+l)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
An = list(map(int,input().split()))
a = An[0]
b = [0]
counter = 0
for i in range(K-1):
    a = An[a-1]
    b += [a]
    counter += 1
    if a in b[:counter-1]:
        c = b[b.index(a):]
        d = (K - counter + 1) % len(c)
        for k in range(d-1):
            a = An[a-1]
        print(a)
        break
    elif i == K-2:
        print(a)
        break

Submission status: Wrong Answer

Solution code:
import numpy as np
n, k = [int(k) for k in input().split()]
a = np.array([int(k) for k in input().split()], dtype=int)
used = np.full(n, -1)
r = 1
used[0] = -1
for i in range(n):
    r = a[r - 1]
    if used[r - 1] != -1:
        r = i - used[r - 1]
        break
    used[r - 1] = i
k = k % r
# print(k)
r = 1
for i in range(k):
    r = a[r - 1]
print(r)

Submission status: Wrong Answer

Solution code:
def function():
    _N,K = map(int, input().split())
    teleportes = list(map(int, input().split()))

    # ループを見つける
    i,count = 0,0
    visited = [-1] * len(teleportes) # 訪れた街にチェック
    while True :
        if visited[i] == -1: # 最初に訪れた
            visited[i] = count # 何番目に訪れたかを記録
            i = teleportes[i] - 1
            count += 1
        else:
            loops = count - visited[i] # 以前訪れてから何回旅したか
            break
    
    if K <= visited[i]:
        K = visited[i]
    else:
        K -= visited[i]
        K %= loops
    for _k in range(K):
        i = teleportes[i] - 1
    print(i+1)

if __name__ == '__main__':
    function()

Submission status: Wrong Answer

Solution code:
def function():
    _N,K = map(int, input().split())
    teleportes = list(map(int, input().split()))

    # ループを見つける
    i,count = 0,0
    visited = [-1] * len(teleportes) # 訪れた街にチェック
    while True :
        if visited[i] == -1: # 最初に訪れた
            visited[i] = count # 何番目に訪れたかを記録
            i = teleportes[i] - 1
            count += 1
        else:
            loops = count - visited[i] # 以前訪れてから何回旅したか
            break
    
    if K <= visited[i]:
        K = visited[i]
    K -= visited[i]
    K %= loops
    for _k in range(K):
        i = teleportes[i] - 1
    print(i+1)

if __name__ == '__main__':
    function()

Submission status: Wrong Answer

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

L = [[0]*2 for i in range(N)]
here = 1
start = -1
startT = -1
end = -1
endT = -1
repeat = -1
count = -1
town = -1
flag = 0

for i in range(N):
    if L[A[here-1]-1][0] == 0:
        L[A[here-1]-1][0] = 1
        L[A[here-1]-1][1] = i
        here = A[here-1]

    else:
        start = L[A[here-1]-1][1] + 1
        startT = here
        end = i
        flag = 1
        break


count = (K - start + 1) % (end - start + 1)

if count == 0:
    count = end - start + 1

if flag == 1:
    for i in range(count+1):
        if L[A[here-1]-1][1] != start + count - 2:
            here = A[here-1]
        else:
            town = A[here-1]
else:
    twon = here

print(town)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
An = list(map(int,input().split()))
a = An[0]
b = []
counter = 0
for i in range(K-1):
    a = An[a-1]
    b += [a]
    counter += 1
    f = list({a,a}&set(b))
    if f == [a,a]:
        c = b[b.index(a):]
        d = (K - counter - b.index(a) ) % len(c)
        for i in range(d-1):
            a = An[a-1]
        print(a)
        break
    else:
        print(a)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
d=[]
t=1
for i in range(1,n+1):
    d.append(t)
    if(a[t-1] in d):
        break
    t=a[t-1] 
idx=d.index(a[t-1])
print(d)
d=d[idx:]
print(d[(k-i)%len(d)])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
d=[1]
t=a[0]
for i in range(1,n+1):
    d.append(t)
    if(a[t-1] in d):
        break
    t=a[t-1] 
idx=d.index(a[t-1])
print(d)
d=d[idx:]
print(d[(k-i-1)%len(d)])

Submission status: Wrong Answer

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

town = []
dic = {}

i = 0
j = 0

while i not in dic and j < K:
    print('i', i)
    dic[i] = j
    town.append(i)
    i = X[i] - 1
    j += 1
    
if j == K:
    output = i + 1
    
else : 
    num = dic[i]
    loop = j - num
    output = town[((K-num) % loop) +num] + 1
    
print(output)

Submission status: Wrong Answer

Solution code:
_, k = list(map(int, input().split()))
a = list(map(int, input().split()))
l = []
pre = []
d = {0: None}
s = a[0] - 1
while True:
    l.append(s)
    if s in d:
        ind = l.index(s) + 1
        break
    else:
        d[s] = None
        s = a[s] - 1
if ind == len(l):
    pass
else:
    pre = l[: ind]
    l = l[ind:]
if len(pre) > k:
    ans = pre[k] + 1
else:
    num = (k - len(pre)) % len(l) - 1
    ans = l[num] + 1
print(ans)

Submission status: Wrong Answer

Solution code:
#template
def inputlist(): return [int(k) for k in input().split()]
import sys
sys.setrecursionlimit(10**6)
#template
N,K = inputlist()
A = inputlist()
seen = [False]*(N+1)
dic = {n:0 for n in range(1,N+1)}
for i in range(N):
    dic[i+1] = A[i]

def search(i,count):
    seen[i] = True
    next_i = dic[i]
    if seen[next_i]:
        count+=1
        return [next_i,count]
    count+=1
    return search(next_i,count)
key,count_sum = search(1,0)

def find(i,count):
    if i == key:
        return count
    count+=1
    return find(dic[i],count)

tmp = find(1,0)
d = count_sum-tmp
K -=tmp

mod = K % d

loop = []
loop.append(key)
def dfs(i):
    next_i = dic[i]
    if next_i == key:
        return False
    loop.append(next_i)
    return dfs(next_i)
dfs(key)

print(loop[mod])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
t=a[0]
d=[t]
for i in range(1,k):
    if(a[t-1] in d):
        break
    t=a[t-1]
    d.append(t)
print(d[(k-i-1)%len(d)])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
t=a[0]
d=[]
for i in range(1,k):
    if(a[t-1] in d):
        break
    t=a[t-1]
    d.append(t)
print(d[(k-i-1)%len(d)])

Submission status: Wrong Answer

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

now = 1
ss = set([1])
sw = 0
x = 0
for i in range(k):
    now = a[now-1]
    ss.add(now)
    if len(ss) != i+2:
        b = now
        x = i+1
        sw = 1
        break
y = 0
z = 0
q = 1
now2 = -1
for j in range(x-1):
    if now2 == b:
        y = j
        if q == 1:
          z = j
          q = 0
    now2 = a[now2-1]
if sw == 1:
    L = (k-z)%(x-y)
    for i in range(L):
        now = a[now-1]
print(now)
#print(x,y,z,L)

Submission status: Wrong Answer

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

now = 1
ss = set([1])
sw = 0
x = 0
for i in range(k):
    now = a[now-1]
    ss.add(now)
    if len(ss) != i+2:
        b = now
        x = i+1
        sw = 1
        break
y = 0
now2 = -1
for j in range(x-1):
    if now2 == b:
        y = j+1
    now2 = a[now2-1]
if sw == 1:
    L = (k-y)%(x-y)
    for i in range(L):
        now = a[now-1]
print(now)

Submission status: Wrong Answer

Solution code:
if __name__ == "__main__":
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    history = []
    visited = [False] * n 
    for i in range(n):
        a[i] -= 1
    
    now = 0

    while k > 0 and not visited[now]:
        visited[now] = True
        history.append(now)

        now = a[now]
        k -= 1
    
    if k == 0:
        print(now)
    else:
        index = 0
        while history[index] != now:
            index += 1
        
        size = len(history) - index
        print(history[index + (k % size)] + 1)

Submission status: Wrong Answer

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

now = 1
ss = set([1])
sw = 0
for i in range(n):
    now = a[now-1]
    ss.add(now)
    if len(ss) != i+2:
        b = now
        x = i+1
        sw = 1
        break
y = 0
now2 = -1
for j in range(x-1):
    if now2 == b:
        y = j+1
    now2 = a[now2-1]
if sw == 1:
    L = (k-y)%(x-y)
    for i in range(L):
        now = a[now-1]
print(now)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())         # n=町の個数 , k=テレポート回数
a_list = list(input().split())         # 各町のテレポート先リスト
dum = a_list[0]                        # dum=次のテレポート先
k = k - 1                              # テレポート回数をデクリメント
ans_list = "1"                         # 訪れた町を ans_list に格納
while(1):
    #print(dum,ans_list)
    if ans_list.count(dum) == 1:
        ind = ans_list.index(dum)
        loop = ans_list[ind:]
        len_loop = len(loop)
        k = k % len_loop
        ans_list = ""
        #print("ok")

    if k == 0:
        print(int(dum))
        break
    ans_list = ans_list + dum
    dum = a_list[int(dum)-1]
    k = k - 1

Submission status: Wrong Answer

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

    doubling = [[0]*(N+1) for _ in range(60)]
    for i,a in enumerate(A):
        doubling[0][i+1] = a
    for i in range(1,60):
        for j in range(1,N+1):
            doubling[i][j] = doubling[i-1][doubling[i-1][j]]
            
    t = 1
    for c in bin(K)[2:]:
        t = doubling[int(c)][t]
    print(t)
main()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())         # n=町の個数 , k=テレポート回数
a_list = list(input().split())         # 各町のテレポート先リスト
dum = a_list[0]                        # dum=次のテレポート先
k = k - 1                              # テレポート回数をデクリメント
ans_list = "1"                         # 訪れた先（インデックス）を ans_list に格納
while(k>=0):
    #print(dum,ans_list)
    if ans_list.count(dum) == 1:
        ind = ans_list.index(dum)
        loop = ans_list[ind:]
        len_loop = len(loop)
        k = k % len_loop
        ans_list = ""

    if k == 0:
        print(dum)
        break
    ans_list = ans_list + dum
    dum = a_list[int(dum)-1]
    k = k - 1

Submission status: Wrong Answer

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

def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    l = [-1]*200010
    now = 0
    l[0] = 0
    ans = -100

    for i in range(n):
        k -= 1
        now = a[now] - 1 #転送された場所
        l[now] += 1
        if l[now] == 2: #3ループ目の頭
            loop = l.count(1) + 1
            k %= loop
        if k == 0:
            ans = now + 1
            break

    print(max(ans, -1))

main()

Submission status: Wrong Answer

Solution code:
n,k,*a=map(int,open(0).read().split())
d={}
s=1
while k:k-=1;s=a[s-1];k%=d.get(s,2**60)-k;d[s]=k
print(s)

Submission status: Wrong Answer

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

    t = 1
    ans = [1]
    while True:
        if a[t-1] in ans :
            start = ans.index(a[t-1])
            break
        ans.append(a[t-1])
        t = a[t-1]

    if k > (len(ans)-start):
        index = k % (len(ans)-start)
        t = ans[index]
    else:
        t = ans[k]
    return print(t)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, K = [int(n) for n in input().split()]
A = {i : int(n) for i, n in enumerate(input().split())}

next_city = 1
visited = []

counter = 0
while counter < K:
  if counter == 0:
    cur = 0
    visited.append(cur)
  else:
    cur = next_city -1  
  next_city = A[cur]
  counter += 1

  if next_city in visited:
    start = visited.index(next_city)
    end = len(visited)
    cycle_len = end - start
    counter += (K // cycle_len - 1) * cycle_len
  visited.append(next_city)

print(next_city)

Submission status: Wrong Answer

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

    t = 1
    ans = [1]
    for _ in range(n):
        if a[t-1] in ans :
            start = ans.index(a[t-1])
            break
        ans.append(a[t-1])
        t = a[t-1]

    if k > (len(ans)-start):
        index = k % (len(ans)-start)
        t = ans[index]
    else:
        t = ans[k]
    return print(t)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())         # n=町の個数 , k=テレポート回数
a_list = list(input().split())         # 各町のテレポート先リスト
dum = a_list[0]                        # dum=次のテレポート先
k = k - 1                              # テレポート回数をデクリメント
ans_list = "1"                         # 訪れた先（インデックス）を ans_list に格納
flag = "ok"
while(k>0):
    #print(dum,ans_list)
    if ans_list.count(dum) == 1 and flag == "ok":
        ind = ans_list.index(dum)
        loop = ans_list[ind:]
        len_loop = len(loop)
        k = k % len_loop
        ans_list = ""

    if k == 0:
        print(dum)
        break
    ans_list = ans_list + dum
    dum = a_list[int(dum)-1]
    k = k - 1

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())         # n=町の個数 , k=テレポート回数
a_list = list(input().split())         # 各町のテレポート先リスト
dum = a_list[0]                        # dum=次のテレポート先
k = k - 1                              # テレポート回数をデクリメント
ans_list = "1"                         # 訪れた先（インデックス）を ans_list に格納
flag = "ok"
while(k<0):
    #print(dum,ans_list)
    if ans_list.count(dum) == 1 and flag == "ok":
        ind = ans_list.index(dum)
        loop = ans_list[ind:]
        len_loop = len(loop)
        k = k % len_loop
        ans_list = ""

    if k == 0:
        print(dum)
        break
    ans_list = ans_list + dum
    dum = a_list[int(dum)-1]
    k = k - 1

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())         # n=町の個数 , k=テレポート回数
a_list = list(input().split())         # 各町のテレポート先リスト
dum = a_list[0]                        # dum=次のテレポート先
k = k - 1                              # テレポート回数をデクリメント
ans_list = "1"                         # 訪れた先（インデックス）を ans_list に格納
flag = "ok"
while(1):
    #print(dum,ans_list)
    if ans_list.count(dum) == 1 and flag == "ok":
        ind = ans_list.index(dum)
        loop = ans_list[ind:]
        len_loop = len(loop)
        k = k % len_loop
        ans_list = ""

    if k == 0:
        print(dum)
        exit()
    ans_list = ans_list + dum
    dum = a_list[int(dum)-1]
    k = k - 1

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())         # n=町の個数 , k=テレポート回数
a_list = list(input().split())         # 各町のテレポート先リスト
dum = a_list[0]                        # dum=次のテレポート先
k = k - 1                              # テレポート回数をデクリメント
ans_list = "1"                         # 訪れた先（インデックス）を ans_list に格納
flag = "ok"
while(1):
    #print(dum,ans_list)
    if ans_list.count(dum) == 1 and flag == "ok":
        ind = ans_list.index(dum)
        loop = ans_list[ind:]
        len_loop = len(loop)
        k = k % len_loop
        ans_list = ""
        flag == "ng"

    if k == 0:
        print(dum)
        break
    ans_list = ans_list + dum
    dum = a_list[int(dum)-1]
    k = k - 1

Submission status: Wrong Answer

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

    t = 1
    ans = [1]
    for i in range(n):
        if a[t-1] in ans :
            start = ans.index(a[t-1])
            break
        ans.append(a[t-1])
        t = a[t-1]

    if k > (len(ans)-start):
        index = k % (len(ans)-start)
        t = ans[index]
    else:
        t = ans[k-1]
    return print(t)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())         # n=町の個数 , k=テレポート回数
a_list = list(input().split())         # 各町のテレポート先リスト
dum = a_list[0]                        # dum=次のテレポート先
k = k - 1                              # テレポート回数をデクリメント
ans_list = "1"                         # 訪れた先（インデックス）を ans_list に格納

while(k):
    #print(dum,ans_list)
    if ans_list.count(dum) == 1:
        ind = ans_list.index(dum)
        loop = ans_list[ind:]
        len_loop = len(loop)
        k = k % len_loop
        ans_list = ""

    if k == 0:
        print(dum)
        break
    ans_list = ans_list + dum
    dum = a_list[int(dum)-1]
    k = k - 1

Submission status: Wrong Answer

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

    t = 1
    ans = [1]
    for i in range(n):
        if a[t-1] in ans :
            start = ans.index(a[t-1])
            break
        ans.append(a[t-1])
        t = a[t-1]

    index = k % (len(ans)-start)
    t = ans[index]
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())         # n=町の個数 , k=テレポート回数
a_list = list(input().split())         # 各町のテレポート先リスト
dum = a_list[0]                        # dum=次のテレポート先
k = k - 1                              # テレポート回数をデクリメント
ans_list = "1"                         # 訪れた先（インデックス）を ans_list に格納

while(1):
    #print(dum,ans_list)
    if ans_list.count(dum) == 1:
        ind = ans_list.index(dum)
        loop = ans_list[ind:]
        len_loop = len(loop)
        k = k % len_loop
        ans_list = ""

    if k == 0:
        print(dum)
        break
    ans_list = ans_list + dum
    dum = a_list[int(dum)-1]
    k = k - 1

Submission status: Wrong Answer

Solution code:
def f(x):
    return int(x)-1
n,k=map(int,input().split())
A=list(map(f,input().split()))
B=[0]*n
a=0
i=0
while True:
    i+=1
    if B[a]!=0:
        j=B[a]
        break
    B[a]+=i
    a=A[a]
if k<i:
    print(B.index(k-1)+1)
else:
    k=(k-j+1)%(i-j)+j
    print(B.index(k)+1)

Submission status: Wrong Answer

Solution code:
def f(x):
    return int(x)-1
n,k=map(int,input().split())
A=list(map(f,input().split()))
B=[0]*n
a=0
i=0
while True:
    i+=1
    if B[a]!=0:
        j=B[a]
        break
    B[a]+=i
    a=A[a]
if k<i:
    print(B.index(k)+1)
else:
    k=(k-j+1)%(i-j)+j
    print(B.index(k)+1)

Submission status: Wrong Answer

Solution code:
def f(x):
    return int(x)-1
n,k=map(int,input().split())
A=list(map(f,input().split()))
B=[0]*n
a=0
i=0
while True:
    i+=1
    if B[a]!=0:
        j=B[a]
        break
    B[a]+=i
    a=A[a]
if k<i:
    print(B.index(k)+1)
k=(k-j+1)%(i-j)+j
print(B.index(k)+1)

Submission status: Wrong Answer

Solution code:
def f(x):
    return int(x)-1
n,k=map(int,input().split())
A=list(map(f,input().split()))
B=[0]*n
a=0
i=0
while True:
    i+=1
    if B[a]!=0:
        j=B[a]
        break
    B[a]+=i
    a=A[a]
k=(k-j+1)%(i-j)+j
print(B.index(k)+1)

Submission status: Wrong Answer

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

now = 0
a = [_-1 for _ in list(map(int,input().split()))]

past = []
offset = 0
cnt = n
idx = 0
fl = False
for i in range(k):
  if fl:
    break
  past.append(now)
  now = a[now]
  try:
    idx = past.index(now)
    cnt = i-idx+1
    fl = True
    break
  except:
    pass

now = 0
for i in range(k % cnt):
  now = a[now]

print(offset + now+1)

Submission status: Wrong Answer

Solution code:
def f(x):
    return int(x)-1
n,k=map(int,input().split())
A=list(map(f,input().split()))
B=[0]
while len(B)==len(set(B)):
    B.append(A[B[-1]])
l=len(B)-1
j=B.index(B[-1])
if l==j:
    if k<l:
        print(A[k]+1)
    else:
        print(A[l]+1)
else:
    k=(k-j)%(l-j)
    print(B[k+j]+1)

Submission status: Wrong Answer

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

Tel=[0]*(2*N+1)
Tel[0]=1

for i in range(1,2*N+1):
  Tel[i]=A[ Tel[i-1] -1 ]

    
#print(Tel)
I=0
J=0


for i in range(2*N+1):
  for j in range(i+1,2*N+1):
    if (Tel[i]==Tel[j]):
      if( I==0 ):
        I=i
        J=j

#print(I,J)
#print( ((K-I)%J) +I )
print( Tel[((K-I)%J) +I-1] )