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()))

c = [0]*N
cc = [0]
c[0] = 1
i = 0
while True:
  c[A[i]-1] += 1  # 来た場所に1を足す
  cc.append(A[i]-1)
  if c[A[i]-1] > 1:
    memo = A[i]-1
    break
  i = A[i] - 1  # 次の場所
    
used = [i for i in range(N) if c[i] > 0]

pos = [i for i in range(len(cc)) if cc[i] == memo]

res = pos[1] - pos[0]
loop = [i for i in cc[pos[0]:pos[1]]]

if K < pos[0]+1:
  for i in range(K):
    c[A[i]-1] += 1  # 来た場所に1を足す
    i = A[i] - 1
  ans = i+1
else:
  i = (K - (pos[0])) % res
  ans = loop[i]+1
  
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200000)
L = []

def Search(pos):
  if pos - 1 not in L:
    L.append(pos - 1)
    Search(A[pos-1])
  else:
    L.append(pos - 1)
    return pos
  
N, K = map(int, input().split())
A = list(map(int, input().split()))
Search(1)
#print(L)
if K < L.index(L[-1]):
  print(L[K])
else:
  t = L.index(L[-1])
  k = len(L) - t - 1
  K -= t
  #print(t, k, K % k)
  print(L[t + (K % k)] + 1)

Submission status: Wrong Answer

Solution code:
import collections
n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=[]
now=0
for i in range(10**5+1):
        ans.append(a[now])
        now=a[now]-1
#print(ans)

ans2=collections.Counter(ans)

ac=ans2.most_common()[0][0]
#print(ac)
index=-1
index2=-1

for i in range(len(ans)):
    if ans[i]==ac:
        if index==-1:
            
            index=i
        else:
            index2=i

#print(index,index2)
#print((k-index-1)%(index2-index))
print(ans[(k-index-1)%(index2-index)+index])

Submission status: Wrong Answer

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

ma = [0]*N
pr = [0]*N
flag = 0
i = 1
ne = A[0]
#print(1)
ma[0] = 1
#print(ne)
while(flag == 0):
  if(ma[ne-1] == 0):
    ma[ne-1] = 1
    pr[ne-1] = i
  else:
    flag = 1
    break
  
  if(i == K-1):
    flag = 2
    break
  
  ne = A[ne-1]
  i+=1
  #print(ne)

#print('---')
if(flag == 1):
  #calc
  start = pr[ne-1]
  dist = i - start
  #print('dist = '+str(dist))
  tmp = (K-i)%dist
  #print('tmp = '+str(tmp))
  
  ne = A[tmp]
  #for x in range(N):
  #  if(pr[x] == tmp):
  #    ne = A[x]

print(ne)
#print(pr)
#print(ma)

Submission status: Wrong Answer

Solution code:
def main(N, K, A):
    i = 0   # 現在地
    lst_memo = []
    set_memo = set([])
    while i not in set_memo:
        lst_memo.append(i)
        set_memo.add(i)
        i = A[i] - 1    # pythonは一つずれる
    key = lst_memo.index(i)
    if K < key or key > len(lst_memo):
        print(lst_memo[K - 1])
    else:
        roop = lst_memo[key:]
        print(roop[(K - key) % len(roop)] + 1)    # pythonは一つずれる


if __name__ == '__main__':
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    main(N, K, A)

Submission status: Wrong Answer

Solution code:
import sys
N,K = map(int,input().split())
As = list(map(int,input().split()))
towns = [0]*N
route = [0]
pre = 0
i = 0
for _ in range(N):
    i = As[i]-1
    if towns[i]>0:
        pre = route.index(i)
        route = route[pre:]
        break
    route.append(i)
    towns[i] = 1
else:
    print(i+1)
    sys.exit()
mod = len(route)
print(route[(K-pre)%mod]+1)

Submission status: Wrong Answer

Solution code:
#encoding utf-8
import math

def loop_find(to_list):
    start = 0
    next_town = to_list[0]
    cy = [start]
    length = 1
    while next_town -1 not in cy:
        cy.append(next_town -1)
        length += 1
        next_town = to_list[next_town - 1]
    double = next_town -1

    i = cy.index(double)
    pre_loop = cy[:i]
    loop = cy[i:]

    #print(pre_loop, loop)
    return pre_loop, loop



def main():
    N,K = map(int, input().split())
    to_list = list(map(int, input().split()))
    
    pre_loop, loop = loop_find(to_list)
    
    if K < len(pre_loop):
        print(pre_loop[K])
    K -= len(pre_loop) + 1

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

X = [-1]*N

# print(X)
ikisaki = Line[0]
X[0] = 0

# print(ikisaki)

for i in range(N+1):
    if X[ikisaki-1] == -1:
        X[ikisaki-1] = i + 1
        ikisaki = Line[ikisaki-1]
        warukazu = i+1 - X[ikisaki-1]
    else:
        warukazu = i+1 - X[ikisaki-1]
        saisyo = X[ikisaki-1]
        break
    # print(X)
# print(saisyo)
# print(warukazu)
Z = K%warukazu
# print(Z)
if saisyo >= K:
    print(X.index(K)+1)
else:
    print(X.index(Z)+1)

Submission status: Wrong Answer

Solution code:
# D - Teleporter

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

nxt = 1
visited = [False] * N
visited[0] = True
ex = 0
repeat = [1]
for _ in range(N):
    nxt = A[nxt - 1]
    if visited[nxt - 1]:
        ex = repeat.index(nxt)
        repeat = repeat[ex:]
        break
    else:
        repeat.append(nxt)
        visited[nxt - 1] = True

print(repeat[(K - ex) % len(repeat)])

Submission status: Wrong Answer

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

tmp = 0
while True:
    if A[tmp] in B:
        B.append(A[tmp])
        break
    tmp = A[tmp]
    B.append(tmp)

loopStart = 0
for i in range(len(B)-1):
    if B[i] == B[-1]:
        loopStart = i
        break

lencycle = len(B) - loopStart -1
ans = 1
A = [x+1 for x in A]

for i in range(K%lencycle):
    ans = A[ans-1]

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
dp = [0] * (n+1)
B = []
f = 1
c = 1
for e,i in enumerate(A,1):
    if dp[f-1]:
        ans = e-dp[f-1]
        bal = dp[f-1]
        break
    else:
        dp[f-1]+=c
        B.append(f)
        f = A[f-1]
        c+=1
bal-=1
k-= bal+1
# print(B,bal)
k = (k%ans+1)%ans
print(B[k+bal])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
temp = list(map(int, input().split()))
warp = {}
for i in range(1,N+1):
    warp[i] = temp[i-1]

way = []
for k in range(1,K+1):
    if k == 1:
        way.append(warp[1])
    else:
        if warp[way[k-2]] in way:
            print(way[(K-k)%len(way)])
            break
        else:
            way.append(warp[way[k-2]])
else:
    print(way[-1])

Submission status: Wrong Answer

Solution code:
paramN, paramK = list(map(lambda x: int(x), input().split(' ')))
paramA = list(map(lambda x: int(x), input().split(' ')))

route = [0] * (paramN + 1)
route[0] = 1
for i in range(1, paramN + 1):
    route[i] = paramA[route[i - 1] - 1]

loop_start = 0
history = [False] * paramN
for i, r in enumerate(route):
    if history[r - 1]:
        loop_start = i
        break
    else:
        history[r - 1] = True

x = route[loop_start]
loop_start_0 = route.index(x)

y = paramK - loop_start_0
z = y % (loop_start - loop_start_0)
print(route[loop_start_0 + z])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
A_dict = {i:a for i,a in enumerate(A,1)}

History = [1]
History_set = {1}
now = 1
for _ in range(N):
    now = A_dict[now]
    if now not in History_set:
        History.append(now)
        History_set.add(now)
    else:
        break
#print(History,now)
for e,h in enumerate(History):
    if h == now:
        #print(e)
        break
loop_num = len(History[e:])
#print(loop_num)
num = (K-e)%loop_num
#print(num)
print(History[e:][num])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[q-1 for q in map(int,input().split())]

if k<=n:
    #k回シミュレートすればよい
    now=0
    for i in range(1,k+1):
        now=a[now]
    print(now+1)
    exit()

now=0
visited=[-100]*n
visited[now]=0
for i in range(1,2*10**5+1):
    now=a[now]
    if visited[now]==-100:
        visited[now]=i
    else:
        m=i-visited[now]
        lft=visited[now]
        break
k=k%m
if k<=lft:
    k+=m

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

Submission status: Wrong Answer

Solution code:
from collections import Counter
n, k = map(int, input().split())
a = list(map(int, input().split()))
path = []
path.append(1)
x = a[0]
path.append(x)
for i in range(n-2):
    x = a[x-1]
    path.append(x)
#print(path)
c = Counter(path)
roop1 = 0
for key, v in c.items():
    if v == 2:
        roop1 = key
roopidx = 0
roopidx2 = n
if roop1 != 0:
    roopidx = path.index(roop1)
    roopidx2 = path.index(roop1, roopidx+1)
#print(roopidx,roopidx2)
k_mod = (k-roopidx) % (roopidx2-roopidx)
print(path[roopidx+k_mod])

Submission status: Wrong Answer

Solution code:
import sys


def test():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    if N >= K:
        print(A[K-1])
        sys.exit()

    K_N = K - N
    if N >= K_N:
        print(A[K_N+1])
        sys.exit()

    N_M_1 = N-1
    ODD = (K_N // N_M_1) % 2 == 0
    MOD = K_N % N_M_1
    if MOD > 0 and ODD:
        print(A[-MOD+1])
    elif MOD > 0 and not ODD:
        print(A[MOD+1])
    else:
        print(A[len(A)-1])


if __name__ == "__main__":
    test()

Submission status: Wrong Answer

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

ind = 1
tl = [1]
ts = {1}
for i in range(n):
    ind = a[ind-1]
    if ind in ts:
        break
    
    tl.append(ind)
    ts.add(ind)

tl2 = tl[tl.index(ind):len(tl)]
ans = tl[k] if k < len(tl) else (k - len(tl)) % len(tl2)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)
from numba import njit


@njit
def make_my_route(route,A,N):
    pre = 1
    for i in range(N):
        now = A[pre-1]
        temp = np.sum(route == now)
        if temp == 1:
            break
        route = np.append(route,now)
        pre = now
    return (route,now)


def main():
    N,K = map(int,input().split())
    A = list(map(int,input().split()))
    A = np.array(A)

    route = np.array([1])

    route,now = make_my_route(route,A,N)
    start = int(np.where(route == now)[0])

    if start+1 != N:
        K -= start
        loop = route[start:]
        # print(loop,K)
        M = len(loop)
        K %= M
        res = loop[K]
    else:
        res = route[K]

    print(res)



main()

Submission status: Wrong Answer

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

citys = [False] * n
now = 0
citys[0] = True
costt = 0
while True:
    now = a[now] - 1
    if citys[now]:
        costt += 1
        break
    else:
        citys[now] = True
        costt += 1

citys = [False] * n
citys[now] = True
check = [0] * n
check[now] = 1
cost = 0
while True:
    now = a[now] - 1
    if citys[now]:
        cost += 1
        break
    else:
        citys[now] = True
        cost += 1
        check[now] = cost + 1

costt -= cost
d = ((k - costt) % cost + 1) % cost
if costt > k:
    i = 0
    while i != k:
        now = a[now] - 1
        i += 1
    print(now+1)
    quit()
ans = check.index(d)
print(ans+1)

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
a=[int(x)-1 for x in list(input().split())]
way=[0]
while True:
        now=way[-1]
        way.append(a[now])
        
        if way[-1] in way[0:-1]:
                break


last=way[-1]
st=way.index(last)
en=len(way)-1
step=en-st
rest=k%step
if k>=st:
        ans=way[rest]+1

else:
        ans=way[k]+1
print(ans)

Submission status: Wrong Answer

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

isPassed = [False] * N
town = 0
#2度同じところに行きつくまで繰り返し、ループの枝元探す
while True:
    if isPassed[town]==False:
        isPassed[town] = True
        town = A[town]-1
    else:
        loopPoint = town
        break

#ループまでの長さを測る
onewayLen = 0
town = 0
while town!=loopPoint:
    town = A[town]-1
    onewayLen += 1

#ループ自体の長さを測る
loopLen = 1
town = A[loopPoint] - 1
while town!=loopPoint:
    town = A[town]-1
    loopLen += 1

#解
if K <= onewayLen:
    town = 0
    for i in range(K):
        town = A[town]-1
    print(town)
else:
    loopMod = (K-onewayLen) % loopLen
    town = loopPoint
    for i in range(loopMod):
        town = A[town]-1
    print(town+1)

Submission status: Wrong Answer

Solution code:
from collections import deque
import numpy as np
n, k = map(int, input().split())
An = list(map(int, input().split()))
root = deque([1])
check = np.zeros(n, np.int)
check[0] = 1
i = 0
dup = 1
cnt = 1
for _ in range(n):
    temp = An[i]
    if check[temp-1] == 1:
        dup = temp
        break
    root.append(temp)
    check[temp-1] = 1
    i = temp - 1
    cnt += 1
index = root.index(dup)
if index == cnt - 1:
    print(dup)
elif index == cnt - 2:
    print(root[(k-index) % (cnt-index) + index - 1])
else:
    print(root[(k-index) % (cnt-index) + index])

Submission status: Wrong Answer

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


d.append(0)
for i in range(N + 10):
    p = d.pop()
    d.append(p)
    d.append(A[p] - 1)


P = []
k = 0
n = 0
for i, v in enumerate(d):
    if (v in P):
        k = i
        n = v
        break
    P.append(v)
    di[v] = i

le = k - di[n]

amari = (K - di[n]) % le

print(d[amari + di[n]] + 1)

Submission status: Wrong Answer

Solution code:
#200510D
N, K = map(int, input().split())
A = list(map(int,input().split()))
 
b = A[0]
count = 1
 
X = []
flag = 0
loop = 0
ans = 0
 
if A[0] == 1:
    ans = 1
 
elif N>=K:
    for i in range(K-1):
        c = A[b-1]
        b = c
    ans = b
else:   
    #print("a")
    for i in range(N):
        c = A[b-1]
        b = c
        l = len(X)
        for j in range(l):
            if b == X[j-1]:
                loop = i - j + 1
                flag = 1
                Z = i + 1
                #print(loop)
                break
 
 
        if flag == 1:
            break
        else:
            X.append(b)
            #print(X)
    if loop ==1:
        ans =A[Z]
    elif loop ==0:
        ans =b
        
    
    else:        
        #print(loop)        
        Y = K%loop
        #print(Y)
        #print(Z)
        #print(Z+Y-1-loop)
        ans = A[Z+Y-1-loop]
 
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
As = list(map(int, input().split()))
maps = {}

for i, a in enumerate(As):
    maps[i] = a - 1

probs = [0] * n
p = 0  # 現在位置
target = 0
h = 0  # ターゲットの初回出現位置
t = 0  # ターゲットの2回目の出現位置
s = 0

for i in range(n):
    probs[p] += 1
    if (probs[p] == 2):
        target = p
        t = i
        break
    p = maps[p]

p = 0
for i in range(n):
    if (p == target):
        h = i
        break
    p = maps[p]

s = t - h
d = (k - h) % s
ans = 0
for i in range(h + d):
    ans = maps[ans]

print(ans + 1)

Submission status: Wrong Answer

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

already = [0]*n
already[0] = 0
now = 0
count = 1
while True:
  count += 1
  now = a[now]-1
  if already[now]:
    k1 = already[now]
    k2 = count-already[now]
    break
  else:
    already[now] = count

if k > k2+k1:
  k -= k2+k1
  k %= k2
  ans = now
  for i in range(k):
    ans = a[ans-1]
else:
  ans = 1
  for i in range(k):
    ans = a[ans-1]
print(ans)

Submission status: Wrong Answer

Solution code:
from queue import Queue

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

    visited = [False] * N
    visited[0] = True
    p = [1]
    l = []
    r = []

    while(len(p) < N):
        v = p[-1] - 1
        n = A[v] - 1

        if(visited[n]):
            d = p.index(n+1)
            l = p[0:d]
            r = p[d:]
            break

        p.append(A[v])
        visited[n] = True

    if(len(l) == 0 and len(r) == 0):
        print(p[K])
    else:
        k = K - len(l)
        k %= len(r)
        print(r[k])
        

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
paramN, paramK = list(map(lambda x: int(x), input().split(' ')))
paramA = list(map(lambda x: int(x), input().split(' ')))

route = [0] * paramN
route[0] = 1
for i in range(1, paramN):
    route[i] = paramA[route[i - 1] - 1]

loop_start = 0
history = [False] * paramN
for i, r in enumerate(route):
    if history[r - 1]:
        loop_start = i
        break
    else:
        history[r - 1] = True

x = route[loop_start]
loop_start_0 = route.index(x)

y = paramK - loop_start_0
z = y % (loop_start - loop_start_0)
print(route[loop_start_0 + z])

Submission status: Wrong Answer

Solution code:
#200510D
N, K = map(int, input().split())
A = list(map(int,input().split()))
 
b = A[0]
count = 1
 
X = []
flag = 0
loop = 0
ans = 0
 
if A[0] == 1:
    ans = 1
 
elif N>=K:
    for i in range(K-1):
        c = A[b-1]
        b = c
    ans = b
else:   
    #print("a")
    for i in range(N+1):
        c = A[b-1]
        b = c
        l = len(X)
        for j in range(l):
            if b == X[j-1]:
                loop = i - j + 1
                flag = 1
                Z = i + 1
                #print(loop)
                break
 
 
        if flag == 1:
            break
        else:
            X.append(b)
            #print(X)
    if loop ==1:
        ans =A[Z]
    elif loop ==0:
        ans =b
        
    
    else:        
        #print(loop)        
        Y = K%loop
        #print(Y)
        #print(Z)
        #print(Z+Y-1-loop)
        ans = A[Z+Y-1-loop]
 
print(ans)

Submission status: Wrong Answer

Solution code:
#S=input()
#T=input()
N,K=map(int,input().split(" "))
A=[int(_) for _ in input().split(" ")]


a=[1]
t=a[0]
i=0

flg = A[t-1] not in a
#print("flg",flg)

#print("t",t)
#print("a",a)
#print("A[t-1]",A[t-1])

while flg :
    a.append(A[t-1])
    t=A[t-1]
    #print("t",t)
    #print("a",a)
    #print("A[t-1]",A[t-1])
    flg = A[t-1] not in a

#print("a",a)

c = a.index(A[t-1])
#print("c",c)

d = len(a)+ 1 - (c+1)
#print("d",d)

e=(K-(c+1))//d
#print("e",e)

f=K-(c+e*d)
#print("f",f)

g=c+f-1
#print("g",g)

ans=A[a[g]-1]

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=[0]+list(map(int,input().split()))
from collections import defaultdict

reached=defaultdict(int)
reached[1]=1
i=1
for j in range(1,n+1):
    if reached[A[i]]==0:
        reached[A[i]]=j+1
    else:
        t1=reached[A[i]]
        t2=j+1
        break
    i=A[i]
reached=dict(reached)
ama=(k-(t1-1))%(t2-t1)
ans=[s for s,h in reached.items() if h==t1+ama]
print(ans[0])

Submission status: Wrong Answer

Solution code:
import itertools

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

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

Loop=1
Next=1
From=0
To=0

found=[0]*N
while(True):
    found[Next-1]=1
    From=Next
    Next=A[Next-1]
    To=Next
    if(found[Next-1]==1):
        break
    Loop+=1

K=To-1+K%(Loop)
#print(Loop)
Next=1
for i in range(K):
    Next=A[Next-1]

print(Next)

Submission status: Wrong Answer

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

  history = []
  current = 0
  while True:
    history.append(current)
    current = A[current]
    if current in history:
      loop_length = len( history[history.index(current):] )
      if loop_length == 0:
        print(current + 1)
        return True

      length_to_loop = len( history[:history.index(current)] )
      additional_way = (K-length_to_loop)%loop_length
      print(history[length_to_loop + additional_way] + 1)
      return True

if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

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

a = [1]


for i in range(K):
    a.append(A_list[a[-1]-1])
    
print(a[K])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=[0]+list(map(int,input().split()))
from collections import defaultdict

reached=defaultdict(int)
reached[1]=1
i=1
for j in range(1,n+1):
    if reached[A[i]]==0:
        reached[A[i]]=j+1
    else:
        t1=reached[A[i]]
        t2=j+1
        break
    i=A[i]
reached=dict(reached)
print(reached)
ama=(k-(t1-1))%(t2-t1)
ans=[s for s,h in reached.items() if h==t1+ama]
print(ans[0])

Submission status: Wrong Answer

Solution code:
N,K= map(int,input().split())
A=list(map(int,input().split()))
count=0
temp=1
memo=[]
flag=True
while (temp not in memo):
    count=count+1
    memo.append(temp)
    temp=A[temp-1]
num=memo.index(temp)
roopnum=len(memo)-num
K=(K-num)%roopnum
temp=1
for i in range(K+num):
    temp=A[temp-1]
print(temp)

Submission status: Wrong Answer

Solution code:
def d():
    from collections import deque
    N, K = map(int, input().split())
    A = list(map(lambda x: int(x)-1, input().split()))
    # print('A = {}'.format(A))
    q = deque([A[0]])

    while True:
        # print('q = {}'.format(q))
        if len(q) == K+1:
            print(q[K] + 1)
            return
        if len(q) > 2:
            v = q.pop()
            if v in q:
                head = q.index(v)
                # print('head = {}'.format(head))
                for i in range(head):
                    q.popleft()
                K -= head + 1
                # print('new_q = {}'.format(q))
                # print('K = {}'.format(K))
                cursor = K % len(q)
                # print('cursor = {}'.format(cursor))
                print(q[cursor] + 1)
                return
            else:
                q.append(v)

        next = A[q[-1]]
        q.append(next)


##########
if __name__ == "__main__":
    d()

Submission status: Wrong Answer

Solution code:
#! env/bin/local python3
# -*- coding:utf-8 -*-

n, k = map(int, input().split())
location = {n+1: int(d) for n, d in enumerate(input().split())}

current = 1


if k < 10**4:
    for _ in range(k):
        current = location[current]
    print(current)
else:
    visit = {_n: [False, 0] for _n in range(1, n+1)}

    counter = 0
    while True:
        counter += 1

        if visit[current][0]:
            break

        visit[current][0] = True
        visit[current][1] = counter
        current = location[current]

    loop = counter - visit[current][1]

    itr_ = range((k - visit[current][1]) % loop + 1)
    for i in itr_:
        current = location[current]

    print(current)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
ar = list(map(int, input().strip().split(' ')))
route=[]

Town=1
X=0
loop = False

while loop == False:
      for i in range(0,K):
            X=ar[(Town-1)]
            Town=(X)
            route.append(Town)
            if Town in route:
                  loop=True
                  break
      
S=len(route)
K=K-i
if K > S:
      Z=K % S
      K=Z+S
      

for i in range(0,K):
      X=ar[(Town-1)]
      Town=X

      
print(Town)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
bl = [False]*n
now = 1
bl[now - 1] = True
ct = 1
res = [1]
ky = int()
while True:
    if ct == 2 * 10 ** 5:
        print(now)
        exit()
    if bl[a[now - 1] - 1]:
        ky = a[now - 1]
        break
    now = a[now - 1]
    bl[now - 1] = True
    res.append(now)
    ct += 1
roop = ct - res.index(ky)
x = ct - roop
k -= x
print(res[x + k % roop])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
N=list(map(int,input().split()))
a={}

b=1
a[1]=1
count=1
while(True):
  count+=1
  b=N[b-1]
  #print(b)
  if b in a:
    #print(a)
    #print(b)
    #print(count)
    cycle=count-a[b]
    rest=K-count+1
    ans=rest%cycle
    for i in range(ans-1):
      b=N[b-1]
    print(b)
    break
  a[b]=count

Submission status: Wrong Answer

Solution code:
N,K=(int(x) for x in input().split())
s=input().split()

pos=K%N

print(s[pos])

Submission status: Wrong Answer

Solution code:
NK = list(map(int, input().split())) 
A = list(map(int, input().split())) 
e = 0
N = NK[0]
K = NK[1]
town= 1
list = []
for i in range(N+1):
  town = A[town-1]
  list.append(town)
  if i > 0 and town == list[0]:
    e += 1
    break
if e == 1:
  print(list[-1])
else:
  list1 = list[:-1]
  d = K % len(list1)
  print(list1[d-1])

Submission status: Wrong Answer

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

count = 0
i = 1
position = 1
position_list = [1]
position_history = [0] * N
start_loop = 0
while i > 0:
  position = A[position-1]
  position_history[position-1] += 1
  count += 1
  if position_history[position-1] > 1:
    start_loop = position_list.index(position)
    position_list.append(position)
    break
  else:
    position_list.append(position)
  	
if count >= K:
  print(position_list[K-1])
else:
  k = (K - start_loop) % (count - start_loop)
  print(position_list[start_loop+k])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
lity=[[0,0] for i in range(N)]
if N<K:
  hata=True
  i=0
  a=0
  j=0
  while hata:
    if lity[i][0]==1:
      a=j-lity[i][1]
      break
    lity[i][0]+=1
    lity[i][1]=j
    i=A[i]-1
    j+=1
  K-=j
  p=K%a
  while p>=1:
    i=A[i]-1
    p-=1
  print(i+1)
else:
  hata=True
  i=0
  a=0
  j=0
  while j<=K:
    if lity[i][0]==1:
      a=j-lity[i][1]
      break
    lity[i][0]+=1
    lity[i][1]=j
    i=A[i]-1
    j+=1
  print(i+1)

Submission status: Wrong Answer

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

costs = [0] * (N + 1)

loc = 1

finised = False
for i in range(1, K+1):
    next_loc = A[loc-1]

    if costs[next_loc] > 0:
        break

    costs[next_loc] = i
    loc = next_loc
else:
    finised = True

if finised:
    print(i)
else:
    loop_cost = i - costs[next_loc]

    K -= costs[next_loc]
    K %= loop_cost

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

    print(next_loc)

Submission status: Wrong Answer

Solution code:
import sys


def test():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    if N >= K:
        print(A[K-1])
        sys.exit()

    K_N = K - N
    if N >= K_N:
        print(A[K_N+1])
        sys.exit()

    N_M_1 = N-1
    ODD = (K_N // N_M_1) % 2 == 0
    MOD = K_N % N_M_1
    if MOD > 0 and ODD:
        print(A[-MOD])
    elif MOD > 0 and not ODD:
        print(A[MOD])
    else:
        print(A[N-1])


if __name__ == "__main__":
    test()

Submission status: Wrong Answer

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

now = 1
step = 1
marking = [0] * N
  
while marking[town[now - 1] - 1] == 0:
    marking[now -1] = step
    now = town[now - 1]
    step += 1

marking[now -1] = step
loop = marking[now - 1] - marking[town[now - 1] - 1]

first = marking[town[now - 1] - 1] - 1
set = loop + 1

last = (K - first) % set

now = town[now - 1]
step = 1


while step <= last:
    now = town[now - 1]
    step += 1

print(now)

Submission status: Wrong Answer

Solution code:
N,K=map(int, input().split())
A = list(map(int, input().split()))
where = [0 for k in range(N)]
where[0]=1
loop=0
li = []
for k in range(N):
    where[k+1] = A[where[k]-1]
    li.append(where[k])

    if where[k+1] in li:
        
        end = k+1
        start = li.index(where[k+1])
        loop = end -start
        break
loopbefore = K -end

loopafter = loopbefore%loop

where2 = where[start:end]

if K < end:
    ans = where[K-1]
else:
    K = K - end
    j = K % loop
    ans = where2[j]
 
print(ans)
'''
where2 = [where[end]]
for k in range(loopafter):
    where2.append(A[where2[k]-1])
print(where2[-1])
'''

Submission status: Wrong Answer

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


to = As[0]
# print("------")
# print("1",end="->")
for i in range(K):
    # print(str(to), end="->")

    if to == As[to-1]:
        print(to)
        break
    
    elif As_io[to-1] > -1:
        # print("\n")
        # print("now loop")
        cycle = i+1-As_io[to-1]
        # print(cycle)
        remain =  (K-(i+1)) % cycle
        
        # print(remain)
        for j in range(remain):
            to = As[to-1]
        print(to)
        break
        
    else:
        As_io[to-1] = i+1
        to = As[to-1]

Submission status: Wrong Answer

Solution code:
aa = input().split(" ")

a = int(aa[0])
k = int(aa[1])
p = input().split(" ")

def finito(firstStep, currentStep):
  loopLen = currentStep -firstStep
  stepsRemaining = (k-currentStep) % loopLen
  return stepsRemaining
  
d = {}
current = 1
for i in range(k):
  g = d.get(current)
  if g:
    remaining = finito(g,i)
    for ii in range(remaining):
      current = int(p[current- 1])
    print(current)
    break
      
  else:
    d[current] = i
  current = int(p[current-1])

Submission status: Wrong Answer

Solution code:
from queue import Queue

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

    visited = [False] * N
    visited[0] = True
    p = [1]
    l = []
    r = []

    while(len(p) < N):
        v = p[-1] - 1
        n = A[v] - 1

        if(visited[n]):
            d = p.index(n+1)
            l = p[0:d]
            r = p[d:]
            break

        p.append(A[v])
        visited[n] = True


    k = K - len(l)
    k %= len(r)

    print(r[k])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

dic={}

i=0
dic[1]=i
next_town=1

while True:
    next_town=A[next_town-1]
    if next_town in dic:
        lp_st=dic[next_town]
        lp_len=i+1-lp_st
        break

    else:
        dic[next_town]=i+1
        i +=1
if K<=i:
    for j in dic:
        if dic[j]==K:
            print(j)

else:
    if lp_len==1:
        L=lp_st
    elif lp_st==0:
        lp_st=lp_len
        L=(K-lp_st+1)%lp_len+lp_st-1
    else:
        L=(K-lp_st+1)%lp_len+lp_st-1

    for j in dic:
        if dic[j]==L:
            print(j)

Submission status: Wrong Answer

Solution code:
n, k = [int(x) for x in input().split()]
a = [int(x)-1 for x in input().split()]

visited = [0]
x = 0
T = -1
i = 0
while T < 0:
  x = a[x]
  i += 1
  if x in visited:
    for j in range(i):
      if visited[j] == x:
        first = j
        break
    T = i - j
    break
  else:
    visited.append(x)
mod = (k-first) % T
print(a[first+mod]+1)

Submission status: Wrong Answer

Solution code:

n,k=map(int,input().split())
a=list(map(int, input().split()))
b=[1]
c=0
m=0
# while flag==1:
for i in range(n):
    if a[m] not in b:
        b.append(a[m])
        c+=1
        m=a[m]-1
    else:
        e=a[m]
        break

e=b.index(e)
# print(b,e)
b=b[e:c+1]
f=c+1-e
if 1 not in b:
    ans=(e+2+k)%f
    print(b[ans])
else:
    ans=k%f
    print(b[ans])