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:
# coding: utf-8
# Your code here!
import copy

N, K = map(int, input().split())
List = list(map(int, input().split()))
saki=[1]
ato=[]

i=0
j=1
while i<N:
    saki.append(List[j-1])
    j=List[j-1]
    i+=1

k=copy.deepcopy(N)
ato.append(List[j-1])

start=saki.index(ato[0])

SA=(len(List)-start)+1

o=0
check=[]
while o<SA:
    check.append(List[j-1])
    j=List[j-1]
    o+=1
print(check[(K-start)%SA])

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
a = input().split()
#町ごとの来た回数
hist1 = [0]*n
#町ごとの来た時間
hist2 = [0]*n
#時間ごとに行った町
hist3 = []
t = 0
c = 1
div = [1,0]
lst = []

for i in range(n):
  a[i] = int(a[i])-1
#

if 10000 < k:
  while 1:
    #前に来た時間
    flg = 0
    t = a[t]
    if hist1[t] == 1:
      flg = hist2[t]
    else:
      hist1[t] = 1
      hist2[t] = c
    if flg:
      div[0] = c - flg
      div[1] = flg % div[0]
      lst = hist3[flg-1:]
      #print(flg)
      break;
    else:
      hist3.append(t)
      c += 1
  #print(div)
  #print(lst)
  print(lst[(k+div[0]-div[1])%div[0]]+1)
else:
  for i in range(k):
    t = a[t]
  print(t+1)
#
#print(div)
#print(lst)

Submission status: Wrong Answer

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

b = 1

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

print(b)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
li=[0]+list(map(int,input().split()))
m=1
loop={1}
s="1"
mkr=0
for i in range(1,n+1):
    m=li[m]
    if m in loop:
        loopend=i
        break
    if i==k:
        mkr=1
        break
    loop|={m}
    s+=str(m)
if mkr==1:
    print(m)
else:
    loopstart=s.index(str(m))
    ind=(k+1-loopstart)%(loopend-loopstart)
    if ind==0:
        print(int(s[-1]))
    else:
        print(int(s[loopstart+ind-1]))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
m = [-1]*n
t = 0
i = 0
while True:
  if m[t] != -1:
    r = i - m[t]
    break
  m[t] = i
  t = a[t]-1
  i += 1
c = r
while True:
  if c > m[t]:
    break
  c += c
b = k%r + c
b 
t = 0
for i in range(b):
  t = a[t]-1
print(t+1)

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
for k in range(N):
    where[k+1] = A[where[k]-1]

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

        break
loopbefore = K -end

loopafter = loopbefore%loop

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

l = [1]
ind = 0
val = 0
for i in range(n):
    val = a[ind]
    l.append(val)
    ind = val - 1

cnt = 1
repeat =[]
repeat.append(l.pop())
z = 0

for j in range(n - 1, -1, -1):
    z = l.pop()
    if z == repeat[0]:
        break
    else:
        cnt += 1
        repeat.append(z)

repeat = repeat[::-1]

jouyo = (k- n - 1) % cnt

print(repeat[jouyo])

Submission status: Wrong Answer

Solution code:
[N,K]=map(int,input().split())
A = list(map(int,input().split()))
t = 1
log = []
for i in range(K):
    t = A[t-1]
    if t in log:
        n1 = log.index(t)
        n2 = len(log)-n1
        for i in range(int((K-n1)%n2)-1):
            t = A[t-1]
        break
    log.append(t)
print(t)

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    prev_p = 1
    g = []
    for i in range(len(A)-1):
        g.append(A[prev_p-1])
        prev_p = g[-1]
        
    if K <= N - 1:
        print(g[K-1])
    else:
        prev_ = g[-1]
        new_g = []
        for i in range(len(A)-1):
            new_g.append(A[prev_-1])
            prev_ = new_g[-1]
        
        K_ = (K  % len(new_g))
        print(new_g[K_-1])     


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def detect_cycle():
    r = {i:0 for i in range(n)}
    lst = []
    a = 0
    while r[a] < 2:
        r[a] += 1
        lst.append(a)
        a = an[a]

    lg = sum(1 for k in r if r[k] > 1)
    lst = lst[-lg:]
    return lst, r

cycle, rt = detect_cycle()

# 循環の最初に行くまでカウント
a = 0
n = 0
while a != cycle[0]:
    a = an[a]
    n += 1
    
# 循環をショートカット
rest = (k - n) % len(cycle)
for i in range(rest):
    a = an[a]

print(a + 1)

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]

check = [0]*N
i = 0
c = 0
check = []
Flag = False
while c<=K:
    if i in check:
        flag = True
        break
    else:
        check.append(i)
        i = A[i]

X = check[check.index(i):]
print(1+X[(K-c)%len(X)] if flag==True else print(i+1))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# 初期入力
from collections import Counter
from copy import deepcopy
import sys
input = sys.stdin.readline
N,K = (int(x) for x in input().split())
A = list(map(int, input().split()))
connection ={}
for i,v in enumerate(A):
    connection[i+1] =v 

#巡回してるか
passed=[1]
nex =connection[1]
lap =[]
for i in range(1,N+1):
    passed.append(nex)
    c=Counter(passed)
    if 2  not in c.values():
        nex =connection[nex]
    else:
        lap =deepcopy(passed)
        indx_s =passed.index(nex) +1
        indx_f =len(lap)
        lap_num =indx_f -indx_s
        break

mod =(K -indx_s) % lap_num
ans =passed[indx_s +mod]
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
teleport = dict()
for i, a in enumerate(A):
    teleport[i+1] = a

state = 1
connect = [1]
looplen= 10**18
for n in range(N):
    state = teleport[state] 
    if state in connect:
        loop_init = connect.index(state)
        looplen = n-connect.index(state)+1
        break
    else:
        connect.append(state)
        
index = (K-loop_init)%looplen
end = connect[loop_init+index]
print(end)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
import numpy as np
link = set()
next = a[0]
link.add(1)
while next not in link:
    link.add(next)
    next = a[next-1]

link2 = set()
next2 = a[0]
link2.add(1)
while next not in link2:
    link2.add(next2)
    next2 = a[next2-1]

link2.remove(next)
ans = 0

rep = len(link)- len(link2)

if(k > len(link2)):
    p = (k-len(link2))%rep
    for i in range(p+rep-1):
        next = a[next-1]
else:
    next = 0
    for i in range(k-1):
        next = a[next-1]

ans = next
print(ans)

Submission status: Wrong Answer

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

b = 1

for i in range(n + 1):
    b = a[b - 1]

print(b)

Submission status: Wrong Answer

Solution code:
from decimal import Decimal

N,K = map(Decimal, input().split())
A = list(map(int, input().split()))
Visited = [False]*int(N)

S = "1"
i = 1
while not Visited[i-1]:
    Visited[i-1] = True
    i = A[i-1]
    S += str(i)

pos = S.find(S[-1])
K -= Decimal(pos)
S = S[pos:-1]
print (S[int(K % Decimal(len(S)))])

Submission status: Wrong Answer

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

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

city = 1
route = []
cnt = [0] * n
cnt[0] += 1
route.append(1)


while True:
    city = roads[city-1] 
    cnt[city-1] += 1
    if cnt[city-1] == 2:
        st = route.index(city)
        length = len(route) - st
        break
    route.append(city)

print(cnt)
print(route)
print(st)
print(length)

if k < len(route):
    print(route[k])

elif length >= 1:
    amari = (k+1-st) % length
    if amari  == 0:
        print(route[-1])
    else:
        print(route[amari+st-1])

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 = [i-1 for i in A]

check = [0]*N
i = 0
c = 0
check = []
Flag = False
while c<=K:
    if i in check:
        flag = True
        break
    else:
        check.append(i)
        i = A[i]

X = check[check.index(i):]
print(1+X[(K-c)%len(X)] if flag==True else print(A[i]+1))

Submission status: Wrong Answer

Solution code:
#int(input())
n,k=map(int,input().split())
aa=list(map(int,input().split()))
f=0
d={}
for i in range(1,n+1):
    d[i]=aa[i-1]
pos=1
apos=0
steps=0
l=[1]
ch=[0]*(n+1)
while(ch[pos]<1):
    apos=d[pos]
    ch[pos]+=1
    pos=apos
    steps+=1
    l.append(apos)
l.pop()
 
pos=l[-1]
 
ll=[pos]
chh=[0]*(n+1)
stepss=0
while(ch[pos]<2):
    apos=d[pos]
    ch[pos]+=1
    pos=apos
    stepss+=1
    ll.append(apos)
ll.pop()
#print(ll)  
x=k%steps
k=k-steps

print(ll[((k%stepss)+1)%stepss])

Submission status: Wrong Answer

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

l = [1]
ind = 0
val = 0
for i in range(n):
    val = a[ind]
    l.append(val)
    ind = val - 1

print("l:", l)
cnt = 1
repeat =[]
repeat.append(l.pop())
z = 0

for j in range(n - 1, -1, -1):
    z = l.pop()
    if z == repeat[0]:
        print("yes")
        break
    else:
        cnt += 1
        repeat.append(z)

repeat = repeat[::-1]

jouyo = (k- n - 1) % cnt

print(repeat[jouyo])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
a_list=list(map(int,input().split()))
a_list.append(0)
tmp=a_list[0]
tmp_list=[]
K2=K%N
count=1
count2=0
while count<N*2-1:
    tmp=a_list[tmp-1]
    tmp_list.append(tmp)
    count+=1
    #print(count)
    #print(a_list[tmp-1])
#print(tmp_list)
while count2<N-1:
    tmp_list.pop(0)
    count2+=1
#print(tmp_list)
count3=1
tmp2=a_list[0]
if K < N:
    #print(tmp2)
    while count3 <= (N-1):
        tmp2=a_list[tmp2-1]
        count3+=1
    print(tmp2)
else:
    print(tmp_list[K2-1])

Submission status: Wrong Answer

Solution code:
from collections import OrderedDict

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

returned = None
i = 0
aa = 0
visited = OrderedDict([(0, 0)])
while i < n:
    aa = a[aa] - 1
    i = i + 1
    if aa in visited:
        returned = (visited[aa], i)
        break
    visited[aa] = i

k = (k - returned[0]) % (returned[1] - returned[0]) + returned[0]

print(list(visited.keys())[k] + 1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import sys
sys.setrecursionlimit(10**6)
n , k = map(int , input().split())
arr = list(map(int , input().split()))
dic = defaultdict(int)
for i in range(1,n+1):
	dic[i] = arr[i-1]
cycle = []
def dfs(node):
	if node in cycle:
		return node
	else:
		cycle.append(node)
		k = dfs(dic[node])
	return k
cyele = dfs(1)
cyind = 0
for i in range(len(cycle)):
	if cycle[i] == cyele:
		cyind = i
#print(cycle, cyind, len(cycle[:cyind]))
csize = len(cycle[cyind:])
offset  = len(cycle[:cyind]) + 1
rem = k - offset
#print("rem ",rem)
rem %= csize
#print("debug ",rem//csize, offset, rem)
count = 0
curr = 1
#print("t " , rem+offset)
while count != rem+offset:
	curr = dic[curr]
	#print(curr)
	count+=1

print(curr)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
p = a[0]
s = a[0]
g = [1]
q = []
for i in range(n+1):
    g.append(p)
    p = a[p-1]
g.reverse()
for i in range(n):
    if g[0] == g[i+1]:
        mod = i+1
        break
g.reverse()
for i in range(len(g)):
    if g[i] == g[i + mod]:
        d = i
        break
if k <= mod:
    for i in range(k%mod):
        q.append(s)
        s = a[s-1]
    q.reverse()
    q.append(1)
    print(q[0])
else:
    print(g[d+(k-d)%mod])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
i = 0
root = [-1] * n
j = 0
while a[i] not in root:
    root[j] = a[i]
    i = a[i] - 1
    j += 1
s_i = root.index(a[i])
x = 0
ans = root[-1]
for i in range(s_i + (k - s_i) % (j - s_i)):
    ans = a[x]
    x = a[x] - 1

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split()) #int
a = input().replace(" ","") #str
chokudai = "1" #str
fp = "1" #str
telenum = 0 #int
while True:
  chokudai = a[int(chokudai)-1]
  telenum += 1
  rooph = fp.find(chokudai) #int
  if rooph != -1:
    rooplen = len(fp)-rooph
    break
  else:
    fp = fp+chokudai
if rooplen < k:
  print(fp[rooph+(k-rooph)%rooplen])
else:
  print(fp[k])

Submission status: Wrong Answer

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

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

P=0
L=[0]*int(N+1)
D=[0]*int(N+1)
c=0
R=0
while(1):
    c+=1
    P=A[P]-1
    L[c]=P
    if D[P]>0: 
        R=D[P]
        break
    D[P]=c
    if P==0: break
    

#print(L)
K-=R
K%=(c-R)
#print(R,K)
ans=L[R+K]+1

print(ans)

Submission status: Wrong Answer

Solution code:
[N, K] = list(map(int, input().split()))
A = list(map(int, input().split()))
count2 = 0
x = 1
y = [1]
for i in range(2000000):
    x = A[x-1]
    if x in y:
        count2 = y.index(x)
        break
    else:
        y.append(x)
for i in range(count2):
    y.pop(0)
ans_ind = (K-count2) % len(y)
if len(y) > N:
    print(y[N])
else:
    print(y[ans_ind])

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:]
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()))
cnt = 0
tmp = a_list[0]

while cnt != k:
    for i in range(n):
        if i == tmp:
            tmp = a_list[i]
    cnt += 1
    
print(i+1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
i=0
s=set()
s.add(1)
order=[1]
while 1:
    if a[i] in s:
        order.append(a[i])
        break
    else:
        order.append(a[i])
        s.add(a[i])
    i=a[i]-1
for i in range(len(order)):
    if order[-1]==order[i]:
        idx=i
        break
#print(order)
k-=idx+1
cnt=len(order)-(idx+1)
if len(order)>k:
    print(order[k])
else:
    order=order[idx+1:]
   # print(cnt)
    #print(order)
    k%=cnt
    #print(k)
    print(order[k])
#print(order)

Submission status: Wrong Answer

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

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

T = []

a = A[0]

while a not in T:
    T.append(a)
    a = A[a-1]

T_index = T.index(a)
p = (K-T_index)%(len(T)-T_index)
if K <= T_index:
    print(T[T_index-1])
else:
    if p == 0:
        print(T[-1])
    else:
        print(T[T_index + p - 1])

Submission status: Wrong Answer

Solution code:
def getlooptime(warpmap, start='1'):
    warpmap = warpmap.copy()    
    counter = 0

    for _ in range(N):

        counter += 1

        nextmap = warpmap[start]

        if type(nextmap) == int:
            return counter - 1, counter - nextmap
        
        warpmap[start] = counter
        start = nextmap

def getterminal(warpmap, n, start):
    warpmap = warpmap.copy()

    if n == 0:
        return start
    else:
        for _ in range(1, n + 1):
            nextmap = warpmap[start]
            start = nextmap

        return nextmap


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

warpmap = {}

for town_num, to_warp in enumerate(A):
    
    town_num = town_num + 1
    warpmap[f"{town_num}"] = f"{to_warp}"


pasttime, looptime = getlooptime(warpmap=warpmap, start='1')
print(getterminal(warpmap=warpmap, n=(K - pasttime) % looptime, start=getterminal(warpmap=warpmap, n=pasttime, start='1')))

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

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

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
m = [-1]*n
t = 0
i = 0
while True:
  if m[t] != -1:
    r = i - m[t]
    break
  m[t] = i
  t = a[t]-1
  i += 1
b = k%r + m[t]
t = 0
for i in range(b):
  t = a[t]-1
print(t+1)

Submission status: Wrong Answer

Solution code:
a=input()
a0=a.split()
n=int(a0[0])
k=int(a0[1])
b=input()
b0=b.split()
p=1
c=0
d=0
if k>=100000:
    l=list(range(n))
    l1=list(range(n))
    for i in range(k):
        c=c+1
        l[p-1]=l[p-1]+1
        p=int(b0[p-1])
        for x in range(n):
            if l[x]==l1[x]+2:
                break
        if l[x]==l1[x]+2:
            break
    for i in range(k):
        d=d+1
        p=int(b0[p-1])
        if p==d:
            break
    r=c-d
    s=(k-d)%r
    if s<=n:
        print(b0[s+1])
    else:
        print(b0[0])
else:
    for i in range(k):
        p=int(b0[p-1])
    print(p)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
s = [1]
while True:
    l = A[s[-1]-1]
    if l in s:
        h = s.index(l)
        new = s[h:]
        side = len(new)

        break
    else:
        s.append(l)
print(new[(k-h)%side])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
A = list(map(int, input().split()))

# ループ解析
step_on = [-1] * N
current = 1
min_mv_count = K
i = 0
#for i in range(N + 1):
looped = False
while(not looped):
    #print(current)
    if step_on[current - 1] == -1:
        step_on[current - 1] = i
        current = A[current - 1]
        i += 1
    else:
        #print("else")
        looped = True
        period = i - step_on[current - 1]
        pre_mv = i - period
        min_mv_count = pre_mv + ((K - pre_mv) % period)
        #print("period {} pre_mv {}".format(period, pre_mv))
        #print("min_mv_count {}".format(min_mv_count))
        break

current = 1
#for i in range(K + 1): # 正攻法
for i in range(min_mv_count):
    current = A[current - 1]
print(current)

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(abs((K-num1)%(num2-num1)-1)):
        x = A[x-1]
    print(x)

Submission status: Wrong Answer

Solution code:
def main():
    N,K = map(int,input().split())
    As = list(map(int,input().split()))
    route = [0]
    pre = 0
    i = 0
    r_append = route.append
    for _ in range(N):
        i = As[i]-1
        if i in route:
            pre = route.index(i)
            route = route[pre:]
            break
        r_append(i)
    # print(route)
    mod = len(route)
    print(route[(K-pre)%mod]+1)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from  collections import Counter

N, K = map(int, input().split())
A = list(map(lambda x: int(x) -1, input().split()))


roop_num = 0
roop_num_index = 0
tmp_index = 0
tmp = A[tmp_index]
cnt = 0
print(A)

dic_cnt = Counter()
dic_order = {}


dic_cnt[0] = 1
dic_order[0] = 0

first = 0

for i in range(N):
    tmp_index = tmp
    tmp = A[tmp]
    dic_cnt[tmp] += 1
    if dic_cnt[tmp] == 2:
        first = dic_order[tmp]
        roop_num_index = tmp_index
        if tmp == 0:
            roop_num = (i+1) - dic_order[tmp] +1
        else:    
            roop_num = (i+1) - dic_order[tmp]
        break
    else:
        dic_order[tmp] = i+1

if K <= N:
    tmp = A[0]
    for i in range(N):
        tmp = A[tmp]
    print(tmp+1)
else:
    K = K - first
    div = K % roop_num
    tmp_index = A[tmp_index]
    tmp = A[tmp_index]
    if tmp_index == 0:
        for d in range(div):
            tmp_index = tmp
            tmp = A[tmp]
    else:
        for d in range(div-1):
            tmp_index = tmp
            tmp = A[tmp]
    print(tmp_index+1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split(" "))
towns = [int(i)-1 for i in input().split(" ")]
loopCount = 0
nowPos = 0
posList = [0]
while not(nowPos==0 and loopCount!=0):
    nowPos = towns[nowPos]
    posList.append(nowPos)
    for i in range(0,len(posList)+1):
        for j in range(i+1,len(posList)):
            length = j - i
            for l in range(len(posList)-length):
                if posList[l: l + length] == posList[i: j] and i != l:
                    remain = K % loopCount - i
                    nowPos = posList[j-1]
                    for k in range(remain):
                        nowPos = towns[nowPos]
                    print(nowPos + 1)
                    exit()
    loopCount = loopCount + 1
    if loopCount == K:
        print(nowPos + 1)
        exit()
print(nowPos + 1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
l = [int(i) for i in input().split()]
check = [0]*n
count = 0
now = 1
mod = 0
start = 0
loop = []
while True:
  count += 1
  if check[now-1] == 0:
    check[now-1] += count
    loop.append(now)
    now = l[now-1]
  else:
    mod = count-check[now-1]
    loop = loop[check[now-1]-1:]
    start = check[now-1]-1
    break
x = (k-start)%mod
print(loop[x])

Submission status: Wrong Answer

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

x=[0]*(n)
before = 0
now = 0
while x[now] == 0:
    x[now]=1+x[before]
    before=now
    now = a[now]-1
    
y=x[now]-x[0]
z=x[before]-y
l=(k-y)%z+1
for i in range(n):
    if x[i] == l+y:
        print(1+i)
        break

Submission status: Wrong Answer

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

new_a = []
city = 1
ipponmichi_last = 1
for _ in range(n+1):
    if city in new_a:
        ipponmichi_last  = city
        break
    else:
        new_a.append(city)
        city = a[city - 1]

lastcitynum  = new_a.index(ipponmichi_last)
if a[0] == 1:
    print(1)
elif lastcitynum == len(new_a)-1:
    print(new_a[lastcitynum])
else:
    print(new_a[lastcitynum:][(k-lastcitynum) % (len(new_a) - lastcitynum)])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
p = 0
a = 1
ret = [1]
init_l = list()
rec_l = list()
while p < K:
    a = A[a-1]
    if a in ret:
        ind = ret.index(a)
        init_l = ret[:ind]
        rec_l = ret[ind:]
        break
    ret.append(a)
if init_l == [] and rec_l ==[]:
    print(a)
else:
    m = K - len(init_l)
    print(rec_l[m % len(rec_l)])

Submission status: Wrong Answer

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

n = a[0]
k = a[1]

src = 0
passed = [1]
turn = []
flag = 0

for i in range(k):
  dest = b[src]
  if dest in passed:
    if dest in turn:
      count = len(turn)
      now = i + 1
      rep = (k - now) % count
      ans = turn[rep]
      print(ans)
      flag = 1
      break
    else:
      turn.append(dest)
  else:
    passed.append(dest)
  src = dest - 1

if flag == 0:
  print(src)
  
print(passed)
print(turn)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a_l = list(map(int, input().split()))
a_m = dict()
tmp = list()  # startまでの距離を計測用

f = 1

while True:
    if f in a_m:
        # すでにfrom-toが登録済みであればループのstartを登録終了
        start = f
        pre = tmp.index(f)
        break
    tmp.append(f)
    t = a_l[f-1]  # to
    a_m[f] = t  # from-toを登録
    f = t

if k > pre:
    cycle_len = len(a_m) - pre
    k_mod = (k - pre) % cycle_len

    result = start

else:
    result = 1
    k_mod = k

for _ in range(k):
    result = a_m[result]

print(result)

Submission status: Wrong Answer

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

ans_list_part = [1]
target_tmp = a_list[0]
seen = []

while target_tmp not in ans_list_part:
    ans_list_part.append(target_tmp)
    target_tmp = a_list[target_tmp-1]

index = k % len(ans_list_part)
print(ans_list_part[index])

Submission status: Wrong Answer

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

l = [int(i) for i in input().split()]
a = set()
flag = False
s = 0
while(l[s] - 1 not in a):
	if l[s] == 1:
		flag = True
		s = l[s] - 1
		a.add(s)
		break
	s = l[s] - 1
	a.add(s)
	#print(l[s], a)
if flag:
	print(l[k % len(a)])
else:
	print(l[(k - 1) % len(a)])

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(A[port[K % roop]])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A_s = list(map(int, input().split()))
# それぞれの街に訪れた時のカウント
cache = [0]*N
# cacheの逆引き辞書
MAP = {}
# 町カーソル
i=0
# テレポーターのカウント
cnt=1
# 閉路のサイズ
rplng = 0
nolp = False
while(True):
    if(cache[i]!=0):
        rplng = cnt - cache[i]
        break
    if(cnt > K):
        nolp = True
        break
    MAP[cnt] = i+1
    cache[i] = cnt
    i = A_s[i]-1
    cnt+=1
if(nolp):
    print(MAP[cnt-1])
else:
    ans = MAP[(K-cnt+1)%rplng+cache[i]]
    print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split()) #int
a = input().replace(" ","") #str
chokudai = "1" #str
fp = "1" #str
telenum = 0 #int
while True:
  chokudai = a[int(chokudai)-1]
  telenum += 1
  rooph = fp.find(chokudai) #int
  if rooph != -1:
    rooplen = len(fp)-rooph
    break
  else:
    fp = fp+chokudai
print(fp)
if rooplen < k:
  print(fp[rooph+(k-rooph)%rooplen])
else:
  print(fp[k])

Submission status: Wrong Answer

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

a.insert(0, 'hoge')
amari = k % n

tmp = 1
if k >= n:
    for i in range(1, n + amari + 1):
        tmp = a[tmp]
        # print(i, tmp)
else:
    for i in range(1, k+1):
        if tmp != k:
            tmp = a[tmp]
        elif tmp == k:
            print(tmp)
            exit()

print(tmp)

Submission status: Wrong Answer

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

V = [0 for i in range(n)]
P = [-100 for i in range(n)]
cp = 1
t = 0
while 1:
    #print('V = ', V)
    V[cp - 1] += 1
    if V[cp - 1] < 2:
        P[cp - 1] = t
    else:
        break
    cp = A[cp - 1]
    t += 1

pret = P[cp - 1]
t = t - pret
#print('pret = ', pret)
#print('t = ', t)
#print('P = ', P)

if k <= pret:
    pass
else:
    k = k - pret
    P = list(map(lambda x: x - pret, P))
#print('P = ', P)

l = k % t
ans = 0
for i in range(n):
    if P[i] == l:
        ans = i + 1
        break
    else:
        pass
print(ans)

Submission status: Wrong Answer

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


def readstr():
    return input().strip()


def readint():
    return int(input())


def readnums():
    return map(int, input().split())


def readstrs():
    return input().split()


def main():
    N, K = readnums()
    A = list(readnums())
    '''
    setでinを回したい
    -> l = set()
    順序も保持したい
    dictで保持？
    {1: 0, 3: 1, 4: 2, 1: 3, ...}
    a in l = Trueのとき
    dict[a]
    '''
    l = {A[0]}
    dic = {A[0]: 0}
    a = A[0]
    for i in range(1, N):
        a = A[a - 1]
        if a in l:
            d = dic[a]
            loop = list(dic.keys())[d:]
            K -= d
            print(loop[K % len(loop) - 1])
            break
        else:
            l.add(a)
            dic[a] = i
    else:
        print(list(dic.keys())[K - 1])


if __name__ == "__main__":
    main()

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+1)

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

print(A[Next-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-1])
    sys.exit()

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