Problem:


Score: 400 points

Problem Statement

The Kingdom of Takahashi has N towns, numbered 1 through N.

There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.

Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Help the king by writing a program that answers this question.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

If we start at Town 1 and use the teleporter 5 times, our travel will be as follows: 1 \to 3 \to 4 \to 1 \to 3 \to 4.

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2



Sample input:
4 5
3 2 4 1


Sample output:
4

Submission status: Wrong Answer

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

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def TI(): return tuple(map(int,input().split()))
def RN(N): return [input().strip() for i in range(N)]


def main():
    N, K = MI()
    A = LI()
    A.insert(0, 0)
    x = 1
    y = 0
    N2 = N*2
    l = [0]*N2
    for i in range(N2):
        x = A[x]
        l[i]=x
    ln = len(l)
    for i in range(N):
        for j in range(i+1,N+1):
            if l[i] == l[j]:
                y = j-i
                break
        if y != 0:
            break
    if K <= N:
        print(l[K-1])
    else:
        amari = (K - ln)%y
        ans=l[ln-1-amari]
        print(ans)

    


if __name__ == "__main__":
	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
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:
import numpy as np
n,k = map(int,input().split())
l = np.array(input().split(),dtype=int)
i = 0
routes = set()
while i < k+1:
    index = 0
    routes.add(index)
    index = l[index]-1
    if index in routes:
      while i+len(routes) <= k:
        i += len(routes)
    else:  
    	i += 1
print(l[index])

Submission status: Wrong Answer

Solution code:

n,k = map(int,input().split())
a = list(map(int,input().split()))
m = list(range(n))
d = dict(zip(m,a))
k_ = k % n
dkey = 0
print(d)
for i in range(1,k_+1):
  now = d[dkey]
  dkey = now - 1
print(now)

Submission status: Wrong Answer

Solution code:
import sys

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

history =[1]
place = A[0]

for i in range(int(k/n)):
  history.append(place)
  place = A[(int(place)-1)]
  if place == 1:
    amari = k % len(history)
    print(history[amari])
    sys.exit()

Submission status: Wrong Answer

Solution code:
n,K=list(map(int,input().split()))
a = list(map(int,input().split()))
b = []
c = [0]*len(a)
I=0
check=0
k=0
if a[k]==1:
    print(1)
else:
    for i in range(n):
        b.append(a[k])
        tes=a[k]-1
        k=a[k]-1
        if a[k]==1:
            b.append(1)
            check=-1
            break
        else:
            if c[tes]==0:
                c[tes]=i+1
            else:
                b.pop(-1)
                I = c[tes]
                break
    if check==-1:
        e=K%len(b)
        print(b[e-1])
    else:
        d=len(b)-I+1
        e=(K-I+1)%d
        print(b[I+e-1])

Submission status: Wrong Answer

Solution code:
b0=input().split()
towns=int(b0[0])
k=int(b0[1])

al=[int(i)-1 for i in input().split()]

rt=[-1 for x in range(towns)]
rt[0]=0
now=0
for x in range(1,300000):
  now=al[now]
  if rt[now]!=-1:
    stp=x
    break
  else:
    rt[now]=x

ll=stp-rt[now]
k2=k-rt[now]

if k<ll:
  print(rt.index(k))
else:
  print(rt.index(k2%ll+rt[now])+1)

Submission status: Wrong Answer

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

A = list(map(int,input().split()))
B = [1]
ex = 1

for i in range(N):
    ex = A[ex - 1]
    if ex in B:
            p = B.index(ex)
            r = ex
            Z = (K - p) % (i + 1 - p)
            break
    else:
            B.append(ex)

ans = r
for k in range(Z):
    ans = A[ans - 1]
    k = k + 1
    
    
print(ans)

Submission status: Wrong Answer

Solution code:
N, K  = map(int, input().split())
A = [int(x) for x in input().split()]
count = [0] * N
t = A[0]
count[0] = 1
for i in range(N): 
    j = A[t-1]
    t = j
    if count[t-1] > 0:
        K = K - count[t-1] + 1
        K = K % (i+2 - count[t-1] + 1)
        break
    count[t-1] = i+2

x = count[t-1]
t = A[0]
j = 0
print(K)
for i in range(K - x + 1):
    j = A[t-1]
    t = A[j-1]
print(t)

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(A[current] + 1)
        return True

      if loop_length == 1:
        print(A[current] + 1)
        return True

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

if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

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

l = [A[0]]
a = A[0] - 1
for _ in range(0, N):
    if A[a] not in l:
        l.append(A[a])
        a = A[a] - 1
    else:
        break
        a = A[a]
n = len(l)
print(l)
b = 0
for i in range(n):
    if l[i] == a:
        del l[0:i]
        b = i - 1
        break
N = len(A)
S = (K - b) % N
ans = A[S - 1]

print(ans)

Submission status: Wrong Answer

Solution code:

#import numpy as np
from functools import *
import sys
sys.setrecursionlimit(300000)
input = sys.stdin.readline




def acinput():
    return list(map(int, input().split(" ")))




def search(x,step,vnode):
    global L,visited,K
    
    #print(visited,x)
    #print(step)
    visited[x]=step
    vnode.append(x)
    
                
    if L[x]==x:
        print(x)
        sys.exit(0)
        
    if visited[L[x]]>=0:
        C=step+1-visited[L[x]]
        k=(K%(2*C)-visited[L[x]])%C
        if k<0:
            k=(k+C)%C
            
        #print(vnode)
        #print(k,L[x],visited[L[x]])
        vnode.append(L[x])
        print(vnode[visited[L[x]]+k])
               
        #print(step,x,visited)
        sys.exit(0)
        return
    
    if step==K+1:
        print(step)
        #print(visited)
        sys.exit(0)            
    
    search(L[x],step+1,vnode)
    
    
    

N,K=acinput()
L=[0]
L.extend(acinput())

visited=[-1]*(N+1)
#visited[1]=1
search(1,0,[])

#4 500000000000000000000000001
#2 3 4 4

Submission status: Wrong Answer

Solution code:
il = [int(k) for k in input().split()]
N = il[0]
K = il[1]
A = [int(k) for k in input().split()]
A.insert(0,0)
visited = [-1]*(N+1)

##### ループをしない場合

# k = 0
# result = 1
# while k <= K:
#     result = A[result]
#     k += 1

#### ループをする場合

k = 0
alpha = 0
result = 1
loop_length = 0
while True:
    if (visited[result] < 0):
        visited[result] = k
        result = A[result]
        k += 1
    else:
        alpha = visited[result]
        loop_length = k - visited[result]
        break

print(visited)
num = (K-alpha)%(loop_length)
print("k = "+str(k))
print("alpha = "+str(alpha))
print("num = "+str(num))

if (alpha == 0):
    # 最初からループ
    print(A[K%loop_length+1])
else:
    # ループは途中から
    print(A[((K-alpha+1)%loop_length)+1])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
r = 1
root = [1]
for _ in range(n):
    roo = A[r-1]
    root.append(roo)
for i in range(n):
    if roo == root[i]:
        roopcount = n-i-1
        break
if k <= n:
    print(root[k-1])
else:
    s = k%n
    print(root[i])

Submission status: Wrong Answer

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

b = [0]*n
fi = a[0]-1
s=0
b[fi]=1
while(1):
    t= a[fi]

    b[t-1] += 1
    s+=1
    fi = t-1
    if b[t-1]>2:
        break
j=0
for i in b:
    if i==1:
        j+=1
amari = (k-j)%s

fi = a[0]-1
if amari==0:
    if j==0:
        amari=s
    else:
        amari=j
for _ in range(amari+j-1):
    t = a[fi]
    fi = t-1
print(t)

Submission status: Wrong Answer

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

now = 1
temp_l = [0]*N
temp_count = [-1]*N
temp_count[0] = 0
for i in range(N):
    pre = now
    now = A[pre-1]
    if temp_l[now-1] == 1:
        end = now
        temp = i+1
        break
    else:
        temp_l[now-1] = 1
        temp_count[now-1] = i+1

s_index = temp_count[end-1]
repeat = temp - s_index
K -= s_index
K = K % repeat

print(temp_count.index(K+s_index)+1)

Submission status: Wrong Answer

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

history = [1]
index = -1

for i in range(2, K+1):
    current = history[-1]
    _next = A[current - 1]
    if _next in history:
        index = history.index(_next)
        break
    else:
        history.append(_next)

if index == -1:
    print(history[-1])
else:
    count = K - i - len(history) - (index + 1)
    q, mod = divmod(count, len(history))
    print(history[mod])

Submission status: Wrong Answer

Solution code:
N = [int(e) for e in input().split()]
nums = [int(t) for t in input().split()]
count=[]
t = True
k=0
check = []
for i in range(N[0]):
    count.append(0)
while t:
    t = nums[k]
    if(t in check):
        break
    else:
        check.append(t)
    k = t -1
ans = (N[1] - nums.index(t)-1)%(len(nums)-nums.index(t)-1)
ans1 = nums.index(t) + ans
print(nums[ans1])

Submission status: Wrong Answer

Solution code:
n,k =map(int,input().split())
a = list(map(int,input().split()))
l = 0  
count = 0
 
for i in range(10000000) :
    l = a[l]-1 
    count = +1
    if l == 0 :
        x = k%count
        for i in range(x) :
            l = a[l]-1
            
        print(l+1)
        exit()

Submission status: Wrong Answer

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

list_order = [1]
idx = 0
loop_trigger = -1
for i in range(1, k + 1):
    next_city = a_list[idx]
    if next_city in list_order:
        loop_trigger = next_city
        break
    list_order.append(next_city)
    idx = next_city - 1
if loop_trigger == -1:
    print(a_list[idx])
else:
    count_before = list_order.index(loop_trigger)
    count_loop = len(list_order) - count_before
    print(list_order[((k - count_before) % count_loop) + count_before])

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)
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)
print(root[(k-index) % (cnt-index) + index])

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 = A[loopStart] + 1
A = [x+1 for x in A]

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

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
A = list(map(int, input().split()))
A.insert(0, 0)
root = [0 for i in range(N + 1)]
root[1] = 1
now = A[1]
roop = 0
getroop = 0
startroop = -1
mae_now = -1
for j in range(K):
    root[now] += 1
    if root[now] == 3:
        startroop = now
        break
    if root[now] == 2:
        roop += 1
    if root[now] == 1:
        getroop += 1
    mae_now = now
    now = A[now]
if startroop == -1:
    print(mae_now)
if startroop != -1:
    nokoriroop = (K-(getroop-roop)) % roop
    for i in range(nokoriroop):
        now = A[now]
    print(now)

Submission status: Wrong Answer

Solution code:
import math

n, k = map(int, input().split(" "))
aL = list(map(int, input().split(" ")))
aFL = [False for _ in range(n)]

crnt = 1
cnt1 = 0
while True:
    if aFL[crnt - 1]:
        break
    cnt1 += 1
    aFL[crnt - 1] = True
    crnt = aL[crnt - 1]

wCrnt = crnt
cnt2 = 0
while True:
    crnt = aL[crnt - 1]
    cnt2 += 1
    if crnt == wCrnt:
        break

gp = cnt1 - cnt2

cr = aL[0] - 1
for i in range((k - gp) % n):
    cr = aL[cr - 1]

print(aL[cr - 1])

Submission status: Wrong Answer

Solution code:
N, K = list(map(int,input().split()))
move_list = list(map(int,input().split()))
 
  
test = 1
find_f = {}
for i in range(N):
  find_f[i+1]=[]
 
find_f[1].append(0)
for i in range(K):
  test = move_list[test-1]
  find_f[test].append(i+1)
  if len(find_f[test]) == 2 and find_f[test][1]-find_f[test][0]<=100000:
    state ,f = test, find_f[test]
    break
try:
  K = K-f[0]
  f = f[1]-f[0]
  remain = K%f
  if remain == 0:
    print(state)
 
  for i in range(remain):
    state = move_list[state-1]
 
  print(state)
except:
  print(test)
#state = 1

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
pa = {}
p = [1]
i = 0
cnt = 0
N = 0
while True:
    if not i in pa:
        pa[i] = cnt
        p.append(a[i])
        i = a[i]-1
    else:
        N = cnt
        cnt = cnt - pa[i]
        del p[-1]
        break
    cnt += 1
N = N - cnt 
if len(p) >= k:
    print(p[k-1])
else:
    print(p[k%(len(p)-N)+N])

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]

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]+1)) % res
  ans = cc[i]
  
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=[1]
t=0
for i in range(n):
    for j in range(len(b)):
        if b[j]==a[b[i]-1]:
            r=j
            t=1
            break
    if t==1:
        break
    b.append(a[b[i]-1])
b1=b[:r]
b2=b[r:]
print(b1)
print(b2)
print(r)
if k<=r:
    print(b1[k-1])
else:
    print(b2[(((k-r+1)%len(b2)+len(b2))-1)%len(b2)])

Submission status: Wrong Answer

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

l_set = set()
tele_list = [0]
current_city = 0


for i in range(N):
    if current_city in l_set:
        break

    

    else:
        current_city = A_list[current_city] - 1

        tele_list.append(current_city)
        l_set.add(current_city)

len_tele_list = len(tele_list)
index_last = len_tele_list % K - 1

print(tele_list[index_last] + 1)

Submission status: Wrong Answer

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

route = []
route = [1]
appearance = set()
index = 1
flg = False
for _ in range(n * 10):
    if index in appearance:
        flg = True
        break
    appearance.add(index)
    next_city = A[index - 1]
    route.append(next_city)
    index = next_city

if flg is False:
    print(route[k])
    sys.exit()

# 繰り返し個数
duplicate = route[route.index(index):len(route) - 1]
print(duplicate[(k - route.index(index)) % (len(duplicate))])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
cnt = 0
c = [0] * n
nex = 1
ans = 0
ane = 1
d=0
for i in range(n):
    if c[nex - 1] == 1:
        break
    c[nex - 1] += 1
    ane = nex
    nex = a[nex - 1]
    cnt += 1
    if cnt ==k:
        print(a[ane-1])

cnt2 = 0
nex2 = nex
anex = nex
for i in range(n):
    if c[nex2 - 1] == 2:
        break
    c[nex2 - 1] += 1
    anex = nex2
    nex2 = a[nex2 - 1]
    cnt2 += 1
#print(nex, cnt)
#print(nex2, cnt2)
if k < cnt + cnt2:
    d = k // cnt
    d = k - cnt * d
    #print(123,d)
    if d==0:
        print(a[ane - 1])
        exit(0)
    for j in range(d):
        #print(ans)
        ans = a[nex - 1]
        nex = ans
    print(ans)
    exit(0)
d = k - cnt
d2 = d // cnt2
d2 = d - cnt2 * d2

#print(d2)
if d2 == 0:
    print(a[anex - 1])
    exit(0)
for i in range(d2):
    #print(ans)
    ans = a[nex2 - 1]
    nex2 = ans
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
A = list(map(int,input().split()))
roop = 0
port = [A[0]]
for _ in range(len(A)):
    port.append(A[port[-1]-1])
    if port.count(A[port[-2]-1]) == 2:#A in port
        port.pop()
        roop = len(port)-port.index(A[port[-1]-1])
        break
K = K-len(port)
print(port[K % roop])

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 = [1]
for i in range(paramN):
    route.append(paramA[route[len(route) - 1] - 1])

towns = []
loop_start = 0
for i, r in enumerate(route):
    if r in towns:
        loop_start = i
        break
    else:
        towns.append(r)

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()))
visited = [False for _ in range(n)]
nx = A[0] - 1
x = []
p = -1
for e, i in enumerate(range(n)):
    if visited[nx] == False:
        visited[nx] = True
        x.append(nx)
        nx = A[nx] - 1
    else:
        p = x.index(nx)
        q = x[:p]
        r = x[p:]
        break
if p!=-1:
    k = (k-len(q))%len(r)
    print(r[k-1]+1)
else:
    print(x[k]+1)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))

    tree = [[] for _ in range(n)]
    for idx, a in enumerate(A):
        tree[idx].append(a - 1)

    start = []
    depth = [0 for _ in range(n)]

    def dfs(v, p, d, cnt):
        depth[v] = d
        for u in tree[v]:
            if u == p:
                continue

            if visited[u]:
                start.append([u + 1, depth[u], cnt + 1])
                return
            else:
                visited[u] = True
                dfs(u, v, d + 1, cnt + 1)
        return

    visited = [False for _ in range(n)]
    visited[0] = True
    dfs(0, -1, 0, 0)

    if not start:
        res = A[0] if k % 2 != 0 else 1
        print(res)
    else:
        if k <= start[0][1]:
            print(depth.index(k) + 1)
        else:
            cnt = (k - start[0][1]) % (start[0][2] - start[0][1])
            res = start[0][1] + cnt
            print(depth.index(res) + 1)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
#import numpy as np#not for pypy

def main():
    ##get int
    first_input = list(map(lambda x:int(x), input().split(" ")))
    second_input = list(map(lambda x:int(x), input().split(" ")))

    ##get float
    #first_input = list(map(lambda x:float(x), input().split(" ")))
    #second_input = list(map(lambda x:float(x), input().split(" ")))

    ##get string
    #first_input = input().split(" ")
    #second_input = input().split(" ")

    N = first_input[0]
    K = first_input[1]
    A_n_list = second_input

    current_i = N-1
    chain = [A_n_list[current_i]]
    while True:
        current_i = A_n_list[current_i]-1
        if A_n_list[current_i] in chain:
            sentou_index = chain.index(A_n_list[current_i])
            chain_roop = chain[sentou_index:]
            break
        chain.append(A_n_list[current_i])

    print(chain_roop)
    if sentou_index>=K:
        print(chain[K])
    else:
        print(chain_roop[(K-sentou_index+1)%len(chain_roop)])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

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

point = 1
cnt = 1
m = 0
mm = 0
ind = 0

l = deque([1])

for i in range(n):
    point = a[point-1]
    cnt += 1
    if point in l:
        mm = i+1
        ind = l.index(point)
        m = cnt - 1 - ind
        break
    l.append(point)

print(l[(k-mm)%m+ind])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
B = [0]*N
B[0] = 1
num1 = 10**6
num2 = 0
x = A[0]
for i in range(len(A)*2):

    if B[x-1] == 0:
        B[x-1] = 1
    elif B[x-1] == 1:
        num1 = min(i+1,num1)
        B[x-1] = 2
    elif B[x-1] == 2:
        num2 = i+1
        break
    else:
        None
    x = A[x-1]
    #print(x)
    #print(B)
print(num1,num2)
x = A[0]
if num1 >= K:
    for j in range(K-1):
        x = A[x-1]
    print(x)
else:
    for j in range((K-num1)%(num2-num1)-1):
        x = A[x-1]
    print(x)

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

loopcount=0
tmpPoint=1
vis={1:0}
for i in range(N):
    Next=A[tmpPoint-1]
    if Next in vis:
        startat=i
        #print(startat)
        #print(tmpPoint)
        #print(vis)
        #print(Next)
        #print(vis[Next]+1)
        loopcount=i - vis[Next]+1
        break
    vis[Next]=i+1
    tmpPoint=Next

if loopcount==0:
    print(tmpPoint)
    exit()
if startat+loopcount < K:
    nokori=(K-startat) % loopcount
else:
    nokori=0

tmpPoint=1
for i in range(startat+nokori):
    Next=A[tmpPoint-1]
    tmpPoint=Next
print(tmpPoint)

Submission status: Wrong Answer

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

    l = [0]
    for i in range(n):
        b = a[l[i]]-1
        l.append(b)
        if l[i] >= l[i+1]:
            break
            
    ind = l[k%(len(l)-1)]
    return ind+1


print(solve())

Submission status: Wrong Answer

Solution code:
import sys

def Destination(N, K, DestinationList, TravelHistory = [1] ):
    """
    for Num in range(N):
        Destination = DestinationList[ TravelHistory[-1]-1]
        TravelHistory.append(Destination)
        if(Destination in TravelHistory[:-1]):
            Pos = TravelHistory[:-1].index(Destination)
            Period = len(TravelHistory)-Pos-1
            break;
    """
    for Num in range(N):
        Destination = DestinationList[ TravelHistory[-1]-1]
        TravelHistory.append(Destination)
    for Num in range(N-1):
        if(TravelHistory[Num] in TravelHistory[Num+1:]):
            Pos = Num+1 + (TravelHistory[Num+1:]).index(TravelHistory[Num])
            Period = Pos-Num
            Start = Num
            print("S", TravelHistory[Num], Pos, Period)
            break;
    Times_K = (K-Start)%Period
    Pos_K = TravelHistory[Start+Times_K]
    return Pos_K


if(__name__ == '__main__'):
    In = input().split()

    N= int(In[0].strip())
    K= int(In[1].strip())

    DestinationList = []
    In = input().split()
    for Str in In:
        DestinationList.append( int(Str.strip()) ) # [M] -> value
    print(Destination(N, K, DestinationList) )

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
a = [0] + a
tele = [1]
for i in range(1, n+1):
    tele.append(a[tele[-1]])
    descover = False
    if tele[-1] in tele[:len(tele)-1]:
        for_roop_begin = tele.index(tele[-1])
        roop_begin = tele[i]
        roop_length = len(tele)-for_roop_begin-1
        descover = True
        break
    if descover == True:
        break
now = roop_begin
for i in range((k-for_roop_begin)%roop_length):
    now = a[now]
print(now)

Submission status: Wrong Answer

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

i=0
locate=1
flag=[0]*n
flag2=0
memory=0
while(True):
    flag[locate-1]+=1
    if flag[locate-1]==2 and flag2==0:
        i=0
        flag2=1
        memory=locate-1
    elif flag[memory]==3:
        break
    locate=a[locate-1]
    i+=1

k=k%i+2*i
locate=1
for j in range(k):
    locate=a[locate-1]
print(locate)

Submission status: Wrong Answer

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

for s_point in range(N):
  point = s_point
  count = 0
  i = 0
  while i < N:
    point = T_list[point]
    if point == s_point:
      count = i+1
      break
    i += 1
  count_list.append(count)

print(count_list)

point = 0
while count_list[point] == 0:
  point = T_list[point]
  count += 1

print(count,point)
L = K % count_list[point]
point = 0
i = 0
while i < L:
  point = T_list[point]
  i += 1
print(point+1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

N, K = map(int,input().split())
A = list(map(int,input().split()))
town = [False] * (N+1)
current = 1

cnt = 0

for i in range(N):
    #print(current,'!')
    if town[current]:
        #current = A[current-1]
        break
    else:
        town[current] = True
        cnt += 1
        current = A[current-1]

town2 = [False] * (N+1)

T = 0
for i in range(N):
    #print(current,'?')
    if town2[current]:
        #current = A[current-1]
        break
    else:
        town2[current] = True
        T += 1
        current = A[current-1]
if current != 1:
    current = A[current-1]
if K <= cnt:
    c = 1
    for i in range(K):
        c = A[c-1]
        #print(c)
    ans = c
else:
    K -= cnt
    K %= T
    c = current
    for i in range(K):
        c = A[c-1]
        #print(c)
    ans = c
#print(cnt,T)
#print(current)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
A = [0] + list(map(int,input().split()))
t = 1
ans = 1
L = [0] * (N + 1)
loop = [0]
l1 = 0
l2 = 0
count = 0

while True:
  t = A[t]
  if L[t] == 1:
    l2 += 1
    L[t] += 1
    loop.append(t)
  elif L[t] == 0:
    L[t] += 1
    l1 += 1
    l2 += 1
  elif L[t] == 2:
    ans = t
    break
l = l2 - l1
loop[0] = loop[-1]

if K <= l1:
  while count < K:
    ans = A[ans]
    count += 1
else:
  ans = loop[(K - l1) % l]

print(ans)

#print(l1)
#print(l2)
#print(loop)

Submission status: Wrong Answer

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

ans = [1]
for i in range(n):
    if i == 0:
        tmp = a[i]
        ans.append(tmp)
        continue
    ans.append(a[tmp-1])
    tmp = a[tmp-1]

#print(ans)

for i in range(n):
    b = ans[i]
    for j in range(i+1,n):
        if b == ans[j]:
            f = j - i
            f_1 = i
#print(f,f_1)
k -= f_1
yatta = k % f
print(ans[yatta+f_1])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# スペース区切りの整数の入力
N, K = map(int, input().split())
# 文字列の入力
A = list(map(int, input().split()))
#開業されたN個要素を受け付ける
#list = [input() for i in range(N)]
#print(A)
ans=1
rireki = [1]
for i in range(N):
    if not A[ans-1] in rireki:
        rireki.append(A[ans-1])
        ans=A[ans-1]
        #print(ans)
        #print(rireki)
    else:
        #print(rireki)
        #print(A[ans-1])
        n = rireki.index(A[ans-1])
        #print(n)
        l = len(rireki[n:])
        #print(l)
        #print(K-i-1)
        a=(K-i-1)%l
        ans=rireki[n:][a]
        break

print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

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

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

Loop=1
Next=1

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

K=K%(Loop)

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

print(Next)

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 = 0
step = 0
for i in range(k):
  g = d.get(current)
  if g:
    remaining = finito(g-1,step-1)
    for ii in range(remaining+1):
      current = int(p[current- 1])
    print(current)
    break
      
  else:
    d[current] = step
  current = int(p[current-1])
  step += 1

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
l=[int(i) for i in input().split()]
c=1
ini=l[0]
k=l[0]
while True:
    # print('*',l[k])
    k=l[k-1]
    c+=1
    if k==ini:
        break
print(l[b%(c-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(K):
        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]
    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:
import sys
input = sys.stdin.readline

def main():
    N, K = map(int, input().split())
    A = [int(v) for v in input().split()]
    i = 0
    p = 1
    memo = [0] * N
    loop = 0
    for i in range(K):
        p = A[p - 1]
        if memo[p - 1] == 0:
            memo[p - 1] = i
        else:
            loop = memo[p - 1] - i
            break
    
    if i < K:
        print(p)
    else:
        s = (K-i)%loop
        for i in range(s, K - 1):
            p = A[p - 1]
        print(p)
    


if __name__ == '__main__':
    main()