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 = list(map(int,input().split()))
a = list(map(int,input().split()))

now=1
count=0

cities = [0]

for i in range(1,k+1):
    tmp=a[cities[i-1]-1]
    if tmp in cities[:i]:
        init_city=cities.index(tmp)
        if init_city>k:
            print(cities[k])
        else:
            print(cities[init_city:i][(k-init_city)%(i-init_city)])
        break
    else:
        cities.append(tmp)
else:
    print(cities[k])

Submission status: Wrong Answer

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

now=1
flag=[0]+[-1]*(N-1)

for i in range(N):
    now = A[now-1]
    if flag[now-1]==-1:
        flag[now-1]+=i+2
    else:
        loop = i+1 - flag[now-1]
        zan=(K-i-1) - (K-i-1)//loop * loop
        ans = flag.index(flag[now-1]+zan)+1
        print(ans)
        exit()

print(now)

Submission status: Wrong Answer

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

num = list(map(int, input().split()))
t = [1]
t_test = []

for i in range(n):
  x = num[t[-1] - 1]
  if(x not in t):
    t.append(x)
  else:
    t_test = t[t.index(x):len(t)]

list_num = list(set(t) - set(t_test))

print(t_test[(k - len(list_num)) % len(t_test)])

Submission status: Wrong Answer

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

now=1
flag=[0]+[-1]*(N-1)

for i in range(N):
    now = A[now-1]
    if flag[now-1]==-1:
        flag[now-1]+=i+2
    else:
        loop = i+1 - flag[now-1]
        zan=(K-i-1) - (K-i-1)//loop * loop
        ans = flag.index(flag[now-1]+zan)+1
        exit()
print(now)

Submission status: Wrong Answer

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

now=1
count=0

cities = [0]

for i in range(1,k+1):
    tmp=a[cities[i-1]-1]
    if tmp in cities[:i]:
        init_city=cities.index(tmp)
        if init_city>k:
            print(cities[k])
        else:
            print(cities[init_city:i][(k-init_city)%(i-init_city)])
        break
    else:
        cities.append(tmp)
else:
    print(now)

Submission status: Wrong Answer

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


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

    count = -1
    visit = [-1]*N
    loopstart = 0
    nextcity = 1
    loopsize = 0

    for n in range(N):
        count += 1
        visit[nextcity-1] = count
        nextcity = A[nextcity-1]
        if visit[nextcity-1] != -1:
            # print(f'{count=}')
            loopstart = nextcity
            loopsize = count - visit[loopstart-1] + 1
            break
    
    # print(visit)
    # print(f'{loopstart=}')
    
    toloop = visit[loopstart-1]
    # print(f'{toloop=}')
    
    # print(f'{loopsize=}')
    if loopsize > K:
        # print("loopsize > K")
        nextcity = 1
        for k in range(K):
            nextcity = A[nextcity-1]
        print(nextcity)
    else:
        # print("loopsize < K")
        res = (K - toloop) % loopsize
        # print(f"{res=}")

        nextcity = loopstart

        for k in range(res):
            nextcity = A[nextcity-1]
        
        print(nextcity)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

a={}
 
b=1
a[1]=1
count=1
while(True):
    count+=1
    b=A[b-1]
    if b in a:
        cycle=count-a[b]
        rest=K-count+1
        ans=rest%cycle-1
        for i in range(min([ans,rest])):
            b=A[b-1]
        print(b)
        break
    a[b]=count
    if count==K+1:
        print(b)
        break

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    # Failed to predict input format
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))

    city = 1
    visited = {city: 0}
    cycle_len = 1

    for i in range(10**6):
        city = A[city - 1]
        
        if visited.get(A[city - 1], False):
            offset = visited[A[city -1]]
            cycle_len = i
            break
        else:
            visited[city] = i

    if abs(cycle_len - offset) != 0:

        K = (K - offset) % (cycle_len - offset)
        city = A[city - 1]
        for i in range(cycle_len):
            city = A[city - 1]

    else:
        K = (K - offset)

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

    print(city)
    
    pass

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
a = [i-1 for i in a]
visited = [-1]*n
route = []
now = 0
hop = 0
while visited[now] == -1:
    visited[now]=hop
    route.append(now)
    now = a[now]
    hop+=1
loop_st = 0
for r in route:
    if r==now: break
    loop_st+=1
loop = route[loop_st:]
if k-1 < len(route):
    print(route[k-1]+1)
else:
    print(loop[(k-loop_st)%len(loop)]+1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
ar = list(map(int, input().strip().split(' ')))
route=[]
 
Town=1
X=0
Y=0
loop = False
loopy = False
A=K-1


while loop == False:
      for i in range(0,K):
            X=ar[(Town-1)]
            Town=(X)
            if i == (A):
                  loop=True
            if K > 100:
                  if Town in route:
                        loop=True
                        loopy=True
                        break
            route.append(Town)

      
S=len(route)
Z=K % S
Y=Z-1

while loopy == True:
      for i in range(0,Z):
            X=ar[(Town-1)]
            Town=X
            if i == Y:
                  loopy = False
       
      
print(Town)

Submission status: Wrong Answer

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

N=6
K=727202214173249351
A=[6,5,2,5,3,2]

a={}
 
b=1
a[1]=1
count=1
while(True):
    count+=1
    b=A[b-1]
    if b in a:
        cycle=count-a[b]
        rest=K-count+1
        ans=rest%cycle
        for i in range(min([ans,rest])):
            b=A[b-1]
        print(b)
        break
    a[b]=count
    if count==K+1:
        print(b)
        break

Submission status: Wrong Answer

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

n, k = map(int, input().split())
a_list = list(map(int, input().split()))
step_list = [1]
next = 1

while(1):
    next = a_list[next - 1]
    if next in step_list:
        break;
    else:
        step_list.append(next)

i = step_list.index(next)
surplus_list = step_list[:i]
loop_list = step_list[i:]

num = k + 1 - len(surplus_list)
surplus = num % len(loop_list)

print(loop_list[surplus - 1])

Submission status: Wrong Answer

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

T=[10**9]*N
label=[False]*(N+1)
T[0]=1
label[1]=True
for i in range(1,N):
    T[i]=A[T[i-1]-1]

    if label[T[i]]:
        sim=T[i]
        break
    else:
        label[T[i]]=True

sp=0
for i in range(N):
    if T[i]==sim and sp==0:
        poss=i
        sp=1
    elif T[i]==sim and sp==1:
        pose=i
        break

pos=K+1-poss-1
pos=pos%(pose-poss)
print(T[poss+pos])

Submission status: Wrong Answer

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

l = [1, 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:
        a = A[a]
        break
n = len(l)
print(l)
b = 0
for i in range(n):
    if l[i] == a:
        del l[0:i]
        b = i
        break
T = len(l)
print(l)
S = (K - b) % T
ans = l[S]
print(S)
print(b)

print(ans)

Submission status: Wrong Answer

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

r = [0]*N

now = 0
r[now]=1
for i in range(N):
  now = A[now]-1
  if r[now] == 0:
    r[now] = i+2
  else:
    x1 = r[now]
    x2 = i+1
    break
print(x1, x2)
print(r)
roop_num = x2 - x1 + 1
x = (K-x1+1) % roop_num
print(x)
if K < x1:
  print(r.index(K+1)+1)
else:
  print(r.index(x1 + x)+1)

Submission status: Wrong Answer

Solution code:
def warp(n):
    return a[n]

n,k = map(int,input().split())
a = list(map(lambda x: int(x)-1,input().split()))
rireki = [0 for _ in range(n)]
rireki2 = []
cnt = 0
mati = 0
while not rireki[mati]:
    rireki[mati] = 1
    rireki2.append(mati)
    mati = warp(mati)
    cnt += 1
for i in range(cnt):
    if rireki2[i] == mati:
        ccnt = i
        break

mod = cnt - ccnt
cycl = []
for i in range(ccnt,cnt):
    cycl.append(rireki2[i])
amari = (k-ccnt) % mod
if cnt > k:
    print(print(rireki2[k]+1))
else:
    print(cycl[amari]+1)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a_list = list(input().split())
dum = a_list[0]
ans_list = ""
for i in range(1,k+1):
    k = k - 1
    if k == 0:
        print(a_list[int(dum)-1])
        exit()
    ans_list = ans_list+dum
    dum = a_list[int(dum)-1]
    if ans_list.count(dum) == 1:
        break
#print(k)
#print(ans_list)
ind = ans_list.index(dum)
loop = ans_list[ind:]
#print(loop)
len_loop = len(loop)
loop_after = (k)%len_loop
#print(loop_after)
print(loop[loop_after-1])
#print(ans_list[1:])
#rint(ans_list)
#print(dum)
#print(ind)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
L = [1]
s = set()
s.add(1)
now = 1
for _ in range(N):
    if A[now - 1] in s:
        K -= L.index(A[now - 1])
        if K >= 0: K %= len(L) - L.index(A[now - 1])
        break
    L.append(A[now - 1])
    s.add(A[now - 1])
    now = A[now - 1]
print(L[K + L.index(A[now - 1])])
if len(L) - L.index(A[now - 1]) == 1:
    print("hoge")

Submission status: Wrong Answer

Solution code:
n,k=[int(i) for i in input().split()]
l=[int(i) for i in input().split()]
x=set(range(1,n+1))
dic=dict.fromkeys(x,0)
for i in x:
    dic[i]=l[i-1]
l=[-1]*n
curr=1
x=0
while x<=k:
    if l[curr-1]==-1:
        l[curr-1]=x
        x+=1
        continue
    else:
        #if l[curr-1]==0:
        l[curr-1]=x
    if l[curr-1]!=0:
        x=(k//l[curr-1])*l[curr-1]-(k%l[curr-1])
        
    curr=dic[curr]
    x+=1
print(dic[curr])

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]
order = {}
order[0] = 0
x = 0
T = -1
i = 0
while T<0:
  x = a[x]
  i+=1
  if x in visited:
    s = order[x]
    T = i - s
    break
  else:
    visited.append(x)
    order[x] = i
if k < s:
  print(visited[k])
else:
  mod = (k-s) % T
  print(visited[s+mod])

Submission status: Wrong Answer

Solution code:
#D
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
nxt = 1
loop = []
tset = set()
final = -1
while 1:
    loop.append(nxt)
    tset.add(nxt)
    nxt = a[nxt-1]
    if nxt in tset:
        final = nxt #重複した町
        break
m = -1
for i in range(len(loop)):
    if loop[i]==final:
        m = i
        break
l = len(loop)
if k < m:
    print(loop[k])
else:
    print(loop[(k-m)%(l-m)+m],k,m)
# print(loop[(k+1)%l])
print(loop)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
L=list(map(int,input().split()))
cnt=0
i=0
SET=set()
S=[]
while not(L[i] in SET):
    S.append(i+1)
    SET.add(i+1)
    i=L[i]-1

    
S.append(i+1)
SS=S


S=S[S.index(L[i]):]

cnt=len(S)



print(SS[K%(cnt)])

Submission status: Wrong Answer

Solution code:

n,k=map(int,input().split())
a=list(map(int, input().split()))
b=[1]
ba={1}
c=0
m=0
# while flag==1:
for i in range(n):
    if a[m] not in ba:
        b.append(a[m])
        ba.add(a[m])
        c+=1
        m=a[m]-1
    else:
        e=a[m]
        break
# print(b,e,c+1)
e=b.index(e)
# print(b,e)
b=b[e:c+1]
f=c+1-e
# if n>k:
#     print(b[k])
#     exit()
if 1 not in b:
    ans=(k-e)%f
    print(b[ans])
else:
    # print(b,f)
    ans=k%f
    print(b[ans])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a_list = list(map(int, input().split()))
step_list = [1]
next = 1

while(1):
    next = a_list[next - 1]
    if next in step_list:
        break;
    else:
        step_list.append(next)

i = step_list.index(next)
surplus_list = step_list[:i]
loop_list = step_list[i:]

num = k + 1 - len(surplus_list)
surplus = num % len(loop_list)

print(loop_list[surplus - 1])

Submission status: Wrong Answer

Solution code:
a = input().split()
N = int(a[0])
K = int(a[1])
c = input().split()

list1 = []

#それぞれ頂点が向かう先。
for i in range(N):
  list1.append([i + 1, int(c[i])])


current_num_set = set()
current_num_list=[]
#これまでの道筋
current_num = 1 #現在の位置



#今いる位置が今まで出てきていない場合に、次どこにいくのかをチェック
while (current_num not in current_num_set):
  for item in list1:
    if item[0] == current_num:
      current_num_set.add(current_num)
      current_num_list.append(current_num)

      current_num = item[1]
      break

c = len(current_num_list)


first_index = 0
#グルグルなっているところの最初のインデックスを計算して格納
for i in range(c):
  if current_num_list[i] == current_num:
    first_index = i
    break


cicle=c-first_index
first=first_index
if c<=K+1 :
  index = (K + 1 - first) % cicle + first-1
  print(current_num_list[index])
else :
  print(current_num_list[K])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
machi=list(map(int,input().split()))
x =  1
li = []
count = 0
for i in range(k):
    if x in li:
        owari = li.index(x)
        li2 = li[owari:count]
        print(li2[(k-count)%len(li2)])
        exit()
    else:
        li.append(x)
        x = machi[x-1]
        count += 1

Submission status: Wrong Answer

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

num_list = []
visit = [-1] * n
next_to = -1
a, b = [0, 0]

for i in range(n):
    if i == 0:
        visit[0] = i
        num_list.append(1)
        next_to = a_list[i]
    else:
        if visit[next_to - 1] != -1:
            a = visit[next_to - 1]
            b = i - a
            break
        else:
            visit[next_to - 1] = i
            num_list.append(next_to)
            next_to = a_list[next_to - 1]
num_list = num_list[a:]
print(num_list[(k - a) % b])

Submission status: Wrong Answer

Solution code:

n,k=map(int,input().split())
a=list(map(int, input().split()))
b=[1]
ba={1}
c=0
m=0
# while flag==1:
for i in range(n):
    if a[m] not in ba:
        b.append(a[m])
        ba.add(a[m])
        c+=1
        m=a[m]-1
    else:
        e=a[m]
        break
# print(b,e,c+1)
e=b.index(e)
# print(b,e)
b=b[e:c+1]
f=c+1-e
if n>k:
    k=k%f
    print(b[k])
    exit()
if 1 not in b:
    ans=(k-e)%f
    print(b[ans])
else:
    # print(b,f)
    ans=k%f
    print(b[ans])

Submission status: Wrong Answer

Solution code:
import bisect

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

T=[10**9]*N
T0=[10**9]*N
pos=0
T[0]=1
T0[0]=1
for i in range(1,N):
    T0.sort()
    T[i]=A[T[i-1]-1]
    T0[i]=T[i]

    pose=bisect.bisect_left(T0[:i],T0[i])
    if T0[pose]==T0[i] and pose!=i:
        sim=T0[i]
        break

sp=0
for i in range(N):
    if T[i]==sim and sp==0:
        poss=i
        sp=1
    elif T[i]==sim and sp==1:
        pose=i
        break

pos=K+1-poss-1
pos=pos%(pose-poss)
print(T[poss+pos])

Submission status: Wrong Answer

Solution code:
import numpy as np

def main():
    n, k = map(int, input().split())
    a = list(map(lambda x: int(x)-1, input().split()))
    count = 0
    foot = [0]
    is_loop = False

    for i in range(1,k):
        foot.append(a[foot[-1]])
        if foot.count(foot[-1]) == 2:
            st_i = foot.index(foot[-1], 0, len(foot)-1)
            lo_size = len(foot)-1 - st_i
            loop = foot[st_i:-1]

            k -= st_i
            k %= lo_size
            
            print(loop[k]+1)
            is_loop = True
            break
    
    if not is_loop:
        print(foot[-1]+1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a_list = list(map(int, input().split()))
step_list = [1]
next = 1

for i in range(200000):
    next = a_list[next - 1]
    if next in step_list:
        break;
    else:
        step_list.append(next)

surplus_list = step_list[:step_list.index(next)]
loop_list = step_list[step_list.index(next):]

num = k + 1 - len(surplus_list)
surplus = num % len(loop_list)

print(loop_list[surplus - 1])

Submission status: Wrong Answer

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

i = 1
memo = []
cnt = 0
cnt2 = 0
s = 0
while True:
    memo.append(a[i-1])
    i = a[i-1]
    if i in memo[:-1]:
        cnt2 = memo[:-1].index(i) 
        cntlist = memo[:-1][cnt2:]
        cnt = len(cntlist)
        break
s = (k - cnt2-1)%cnt
ans = cntlist[s]

print(ans)

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)
if k < first:
  print(visited[k])
else:
  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()))

cnt = [0 for _ in range(N)]
cnt[0] += 1

warp_rireki= [A[0]]
roop = []
# c = 0
# print(cnt)
warp = A[0]
while True:
    # print(cnt[warp-1])
    if cnt[warp-1] == 2:
        break
    warp_rireki.append(A[warp-1])
    if cnt[warp-1] == 1:
        roop.append(A[warp-1])
    cnt[warp-1] += 1
    warp = A[warp-1]


one_root = sum(cnt)-len(roop)
# print("root", roop)
# print("one", one_root)
if K < len(warp_rireki):
    print(warp_rireki[K])
else:
    print(roop[(K-one_root)%len(roop)-1])

Submission status: Wrong Answer

Solution code:

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

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a_list = list(map(int, input().split()))
step_list = [1]
next = 1

while(1):
    next = a_list[next - 1]
    if next in step_list:
        break;
    else:
        step_list.append(next)

surplus_list = step_list[:step_list.index(next)]
loop_list = step_list[step_list.index(next):]

num = k + 1 - len(surplus_list)
surplus = num % len(loop_list)

print(loop_list[surplus - 1])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
a = list(map(int, input().split()))
loop = [0]
start = 0
index = 0
check = [False] * N
fin = False
while True:
    if check[a[index] - 1]:
        start = loop.index(a[index] - 1)
        break
    else:
        loop.append(a[index] - 1)
        check[a[index] - 1] = True
        index = a[index] - 1
        if len(loop) == K + 1:
            print(index)
            fin = True
            break
if fin == False:  # len(loop)<=K
    if (K + 1 - start) % len(loop[start:]) == 0:
        print(loop[-1] + 1)
    else:
        print(loop[start + (K + 1 - start) % len(loop[start:]) - 1] + 1)

Submission status: Wrong Answer

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

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

cnt = [0 for _ in range(N)]
cnt[0] += 1

warp_rireki= [A[0]]
roop = []
# c = 0
# print(cnt)
warp = A[0]
while True:
    # print(cnt[warp-1])
    if cnt[warp-1] == 2:
        break
    warp_rireki.append(A[warp-1])
    if cnt[warp-1] == 1:
        roop.append(A[warp-1])
    cnt[warp-1] += 1
    warp = A[warp-1]


one_root = cnt.count(1)
# print("root", roop)
# print("one", one_root)
if K < len(warp_rireki):
    print(warp_rireki[K])
else:
    print(roop[(K-one_root)%len(roop)-1])

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split()) #右の要素に対して左の関数を適応する
a = list(map(int, input().strip().split())) #strip()は文字列の両端の空白や改行を削除する

for i in range(len(a)):
    a[i] -= 1 #入力値を添字に合わせる

visited = [-1] * n #[-1. -1. -1. …]を定義
now = 0
visited[0] = 0
i = 1
r = 1
while i <= k:
    next = a[now] #次に移動する場所
    #print('roop:',i,now+1,next+1)
    if visited[next] >= 0:
        r = i
        #print(i,r,pre_roop,next)
        #print(visited)
        break #同じ場所に帰ってきたらfor文を抜け出す
    else:
        visited[next] = i
        now = next
        #print(i,visited)
        i += 1

if i > k:
    print(now + 1) #今いる場所を出力
else:
    l = k % r
    i = 0
    p = 0
    for _ in range(l):
        p = a[p]
        i +=1

    print(p+1)

Submission status: Wrong Answer

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

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

cnt = [0 for _ in range(N)]
cnt[0] += 1

warp_rireki= [A[0]]
roop = []
# c = 0
# print(cnt)
warp = A[0]
while True:
    # print(cnt[warp-1])
    if cnt[warp-1] == 2:
        break
    warp_rireki.append(A[warp-1])
    if cnt[warp-1] == 1:
        roop.append(A[warp-1])
    cnt[warp-1] += 1
    warp = A[warp-1]


one_root = cnt.count(1)
# print("root", roop)
# print("one", one_root)
if K < len(warp_rireki):
    print(warp_rireki[K])
print(roop[(K-one_root)%len(roop)-1])

Submission status: Wrong Answer

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

rec = [0] * (N + 1)
costs = [0] * (N + 1)

loc = 1
rec[loc] = 1

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

    if rec[next_loc] == 1:
        break

    costs[next_loc] = i
    rec[next_loc] = 1
    loc = next_loc
else:
    finished = True

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

    final_loc = costs.index((K - loop_index_cost) % loop_cost + loop_index_cost)
    print(final_loc)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
L=list(map(int,input().split()))
cnt=0
i=0
SET=set()
S=[]
while not(L[i] in SET):
    S.append(i+1)
    SET=set(S)
    i=L[i]-1

    
S.append(i+1)
SS=S


S=S[S.index(L[i]):]

cnt=len(S)



print(SS[K%(cnt)])

Submission status: Wrong Answer

Solution code:
mNK = map(int, input().split())
lNK = list(mNK)

mTel = map(int, input().split())
lTel = list(mTel)

lWent = [-1 for i in range(lNK[0])]
nTown = 0
loopCnt = -1

for i in range(lNK[1]):
    nextTown = lTel[nTown] - 1
    lWent[nTown] = i
    if(lWent[nextTown] != -1):
        loopCnt = i - lWent[nextTown] + 1
        surplus = (lNK[1] - i + 2) % loopCnt
        nTown = lWent.index(lWent[nextTown] + surplus)
        break
    nTown = nextTown

print(nTown + 1)

Submission status: Wrong Answer

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

now=1
to=0
t=k
l=[0]*n
l[0]=1
cnt=2

while t>0:
    to=a[now-1]
    t-=1
    if l[to-1]!=0:
        break
    else:
        l[to-1]=cnt
        cnt+=1
    now=to

if t==0:
    print(now)
else:
    loop=max(l)-l[to-1]+1
    start=l[to-1]-1
    ans=l.index((k-start)%loop+start+1)+1
    print(ans)

Submission status: Wrong Answer

Solution code:
n,k,*a=map(int,open(0).read().split())
b=[1]
s_b={1}
for i in range(n):
  tmp=a[b[-1]-1]
  if tmp in s_b:
    idx=b.index(tmp)
    break
  else:
    b.append(tmp)
    s_b|={tmp}
mod=len(b[idx:])
d=len(b[:idx])
print(b[d+(k-d)%mod])

Submission status: Wrong Answer

Solution code:
n,k,*a=map(int,open(0).read().split())
b=[1]
s_b={1}
for i in range(n):
  tmp=a[b[-1]-1]
  if tmp in s_b:
    idx=b.index(tmp)
    break
  else:
    b.append(tmp)
    s_b|={tmp}
mod=len(b[idx:])
d=len(b[:idx])
print(d,(k-d)%mod)
print(b[d+(k-d)%mod])

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(visited[first+mod]+1)

Submission status: Wrong Answer

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

teleport = [-1] * (N+1)
teleport[0] = 1
visited = [(False, -1)] * (N+1)
visited[1] = (True, 0)

for i in range(1, N+1):
    next_town = A[teleport[i-1]-1]
    if visited[next_town][0]:
        before = visited[next_town][1]
        break
    teleport[i] = next_town
    visited[next_town] = (True, i)


period = i - before
offset = (K - before) % period
print(teleport[before + offset])

Submission status: Wrong Answer

Solution code:

def solve(N,K,A):
    idx = [-1 for _ in range(len(A))]
    now = 0
    period = 0
    bias = 0
    tmp = []
    for i in range(K+1):
        tmp.append(now+1)
        if idx[now] == -1:
            idx[now] = i
        else:
            period = i - idx[now]
            cycle = tmp[-period-1:-1]
            bias = i - period
            break

    if K <= bias:
        now = 0
        for _ in range(K):
            now = A[now] - 1
        print(now + 1)
    else:
        K -= bias
        K = K % period
        print(cycle[K])

if __name__=="__main__":
    # import random
    # import numpy as np
    # for N in range(2, 10):
    #     A = list(range(1, N+1))
    #     random.shuffle(A)
    #     for K in range(1, 10**18):
    #         solve(N, K, A)
    N,K = [int(a) for a in input().split()]
    A = [int(a) for a in input().split()]
    solve(N, K, A)

Submission status: Wrong Answer

Solution code:
def resolve():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    check=[-1]*n
    iti=0
    rireki=[]
    while True:
        iti=a[iti]-1
        if check[iti]==0:
            mae=rireki.index(iti)
            Mae=rireki[:mae]
            Loop=rireki[mae:]
            roop=len(Loop)
            break
        rireki.append(iti)

        check[iti]=0
    if k <= mae:
        print(Mae[k]+1)
    else:
        k-=mae
        print(Loop[(k%roop)-1]+1)
resolve()

Submission status: Wrong Answer

Solution code:
import math

n, k = map(int, input().split(" "))
aL = list(map(int, input().split(" ")))
d = []

crnt = 1
c = 0
diff = crnt
while True:
    if crnt in d:
        diff = c + 1 - d.index(crnt) * 2
        break
    d.append(crnt)
    crnt = aL[crnt - 1]
    c += 1

ini = c - diff
for i in range((k - ini) % diff):
    crnt = aL[crnt - 1]

print(crnt)

Submission status: Wrong Answer

Solution code:
import collections
from collections import OrderedDict

n,k = map(int,input().split())
L = list(map(int,input().split()))
od = collections.OrderedDict()

t = L[0]
od [t] = t
r = 0
i = 0
c = 0
for j in range(k):
    #print(od)
    t = L[t-1]
    if ｔ in od:
        l = list(od.keys())
        i = l.index(t)
        r = len(od) - i
        k2 = (k - i)%r
        print(l[i+k2-1])
        break
    else:
        od[t] = t
else:
    print(t)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
a = [i-1 for i in a]
visited = [-1]*n
route = []
now = 0
hop = 0
while visited[now] == -1:
    visited[now]=hop
    route.append(now)
    now = a[now]
    hop+=1
loop_st = 0
for r in route:
    if r==now: break
    loop_st+=1
loop = route[loop_st:]
if k < len(route):
    print(route[k-1])
else:
    k-=loop_st
    print(loop[k%len(loop)]+1)

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  # はじめて2回出現する場所
h = 0  # ターゲットの初回出現時のワープ回数
t = 0  # ターゲットの2回目の出現時のワープ回数

# はじめて2回出現する場所(target)と、その時点でのワープ回数を取得
for i in range(n + 1):
    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  # 周期
if (k <= h):
    p = 0
    for i in range(k):
        p = maps[p]
    print(p + 1)
    exit

d = (k - h) % s  # targetに到着後にワープすべき回数

# 実際に必要な回数だけワープ
p = 0
for i in range(h + d):
    p = maps[p]

print(p + 1)

Submission status: Wrong Answer

Solution code:
def main():
    # 町の数N, 町iの転送先はindexがAi-1の町
    N,K = map(int, input().split())
    A = list(map(int, input().split()))

    # K回移動した時の町番号を出力
    # もし一度出てきた番号が出てきたら記憶している道順リストを使う
    # 最初は道を覚えさせる作業が必要
    now = 0
    history = [0]
    i = 0
    while i<K:
        # まず進む
        now = A[now]-1
        # 道順登録
        if now not in history:
            history.append(now)
            i+=1
        # 以前出てきてる町ならそこから末尾までを自動で進めてiのカウントも進める
        else:
            r = (K-i-1)%len(history)
            now = history[r]
            break

    print(now+1)

main()

Submission status: Wrong Answer

Solution code:
import logging

logging.basicConfig(level=logging.INFO, format="%(message)s")
#logging.disable(logging.CRITICAL)


def main():
    N, K = map(int, input().split())
    A_i = list(map(int, input().split()))
    B_i = [0 for i in range(N)]
    cycle_list = []
    diff_list = []
    cycle = 0
    diff = 0
    counter = 0
    machi = 1

    while counter < N:
        if B_i[machi - 1] == 1:
            diff = cycle_list.index(machi)
            cycle = counter - diff
            diff_list = cycle_list[:diff]
            cycle_list = cycle_list[diff:]
            break
        else:
            counter += 1
            B_i[machi - 1] = 1
            cycle_list.append(machi)
            machi = A_i[machi - 1]


    if K <= diff:
        print(diff_list[K - 1])
    else:
        mod_K = (K - diff) % cycle
        print(cycle_list[mod_K])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
s=1
B=[1]
for i in range(K):
  s=A[s-1]
  if s in B:
    u=B.index(s)
    u_=len(B)-u#周期の長さ
    t=(K-u+1)%u_
    if t>=1 and u>=1:
      print(B[u+t-1])
      break
    elif u==0:
      t=(K+1)%u_
      print(B[t-1])
      break
    else:
      print(B[u])
      break
  else:
    B.append(s)
 
print(s)