Problem:


Score: 400 points

Problem Statement

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

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

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

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

Constraints

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

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

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

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

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

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2



Sample input:
4 5
3 2 4 1


Sample output:
4

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
cyc=[]
a=1
while a not in cyc:
    cyc.append(a)
    a=l[a-1]
for i in range(len(cyc)):
    if cyc[i]==a:
        leng=len(cyc)-i
        dist=i
        break
if n<=dist-1:
    print(cyc[n])
else:
    print(cyc[(n-dist)%leng+dist])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
cyc=[]
a=1
while a not in cyc:
    cyc.append(a)
    a=l[a-1]
for i in range(len(cyc)):
    if cyc[i]==a:
        leng=len(cyc)-i
        dist=i
        break
if n<=dist-1:
    print(cyc[n])
else:
    print(cyc[(n+1-dist)%leng])

Submission status: Wrong Answer

Solution code:
#入力:N,M(int:整数)
def input2():
	return map(int,input().split())

#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))
  
n,k=input2()
A=input_array()

# ある地点から循環に陥る
tmps=[1] #循環手前
loops=[] #循環
count=0 #参照地点

for i in range(2*(10**5)+1):
	num=A[count]
	if num not in tmps:
		tmps.append(num)
	elif (num in tmps) and (num not in loops):
		loops.append(num)
	elif num in loops:
		break
	count=num-1

tmps=tmps[:-(len(loops))]


if k<=len(tmps):
	print(tmps[k-1])
else:
	ans=(k-(len(tmps)))%len(loops)
	print(loops[ans])

Submission status: Wrong Answer

Solution code:
#入力:N,M(int:整数)
def input2():
	return map(int,input().split())

#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))
  
n,k=input2()
A=input_array()

# ある地点から循環に陥る
tmps=[1] #循環手前
loops=[] #循環
count=0 #参照地点

for i in range(2*(10**5)+1):
	if A[count] not in tmps:
		tmps.append(A[count])
	elif (A[count] in tmps) and (A[count] not in loops):
		loops.append(A[count])
	elif A[count] in loops:
		break
	count=A[count]-1

tmps=tmps[:-(len(loops))]


if k<=len(tmps):
	print(tmps[k-1])
else:
	ans=(k-(len(tmps)))%len(loops)
	print(loops[ans])

Submission status: Wrong Answer

Solution code:
#入力:N,M(int:整数)
def input2():
	return map(int,input().split())

#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))
  
n,k=input2()
A=input_array()

# ある地点から循環に陥る
tmps=[1] #循環手前
loops=[] #循環
count=0 #参照地点

for i in range(2*(10**5)+1):
	if A[count] not in tmps:
		tmps.append(A[count])
	elif (A[count] in tmps) and (A[count] not in loops):
		loops.append(A[count])
	elif A[count] in loops:
		break
	count=A[count]-1

tmps=tmps[:-(len(loops))]
print(tmps)
print(loops)

if k<=len(tmps):
	print(tmps[k-1])
else:
	ans=(k-(len(tmps)))%len(loops)
	print(loops[ans])

Submission status: Wrong Answer

Solution code:
#入力:N,M(int:整数)
def input2():
	return map(int,input().split())

#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))
  
n,k=input2()
A=input_array()

# ある地点から循環に陥る
tmps=[1] #循環手前
loops=[] #循環
st=0 #循環が始まる地点
count=0 #参照地点

for i in range(2*(10**5)+1):
	if A[count] not in tmps:
		tmps.append(A[count])
	elif (A[count] in tmps) and (A[count] not in loops):
		loops.append(A[count])
		st=i
	elif A[count] in loops:
		break
	count=A[count]-1



if k<=st:
	print(tmps[k-1])
else:
	ans=(k-(st-1))%len(loops)
	print(loops[ans])

Submission status: Wrong Answer

Solution code:
#入力:N,M(int:整数)
def input2():
	return map(int,input().split())

#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))
  
n,k=input2()
A=input_array()

# ある地点から循環に陥る
tmps=[1] #循環手前
loops=[] #循環
st=0 #循環が始まる地点
count=0 #参照地点

for i in range(10**5):
	if A[count] not in tmps:
		tmps.append(A[count])
	elif (A[count] in tmps) and (A[count] not in loops):
		loops.append(A[count])
		st=i
	else:
		break
	count=A[count]-1


print(tmps)
print(loops)
print(st)

if k<=st:
	print(tmps[k-1])
else:
	ans=(k-(st-1))%len(loops)
	print(ans)
	print(loops[ans])

Submission status: Wrong Answer

Solution code:
#入力:N,M(int:整数)
def input2():
	return map(int,input().split())

#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))
  
n,k=input2()
A=input_array()

# ある地点から循環に陥る
tmps=[1] #循環手前
loops=[] #循環
st=0 #循環が始まる地点
count=0 #参照地点

for i in range(10**5):
	if A[count] not in tmps:
		tmps.append(A[count])
	elif (A[count] in tmps) and (A[count] not in loops):
		loops.append(A[count])
		st=i
	else:
		break
	count=A[count]-1




if k<st:
	print(tmps[k])
else:
	ans=(k-(st-1))%len(loops)
	print(loops[ans])

Submission status: Wrong Answer

Solution code:

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

b = {}
for i in a:
    b[i] = 0

use_tele = [0]*n
for i in range(k):
    if i == 0:
        start = 1
        t = a[start-1]
    use_tele[t-1] += 1
    b[t] += 1
    t = a[t-1]
    if use_tele[t-1] == 1:
        break

m = use_tele.count(1)

c = []
for i in b:
    if b[i] == 1:
        c.append(i)
print(c[k%m-1])

Submission status: Wrong Answer

Solution code:
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))

n,k=nii()
a=lnii()

x=0
x_list=[1]

for i in range(1,k):
  nx=a[x]

  if nx in x_list:
    inx=x_list.index(nx)
    loop=x_list[inx:]
    zan=k-i

    q=zan%len(loop)

    nx=loop[q]
    break
  else:
    x=nx-1
    x_list.append(nx)

print(nx)

Submission status: Wrong Answer

Solution code:
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))

n,k=nii()
a=lnii()

x=0
x_list=[0]

for i in range(1,k):
  nx=a[x]
  nx-=1

  if nx in x_list:
    inx=x_list.index(nx)
    loop=x_list[inx:]
    zan=k-i

    q=zan%len(loop)

    nx=loop[q]
    break
  else:
    x=nx
    x_list.append(nx)

print(nx+1)

Submission status: Wrong Answer

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

path = [0]*(N+1)
visited = set()

pos = 1
for i in range(N+1):
    if pos in visited: break
    path[i] = pos
    visited.add(pos)
    pos = A[pos]

loc = path.index(pos)
head = path[:loc]
rep = path[loc:i]

print(rep[(K - len(head)) % len(rep)])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
data=[0]*n
data[0]=1
ans=[1]
res=0
d=0
for _ in range(n):
  if data[a[res]-1]==0:
    ans.append(a[res])
    res=a[res]-1
    data[res]=1
  else:
    d=ans.index(a[res])
    break
#print(ans,d)
m=len(ans)-d
l=k-d
if l//m!=0:
  p=l%m
  print(ans[d+p])
else:
  print(ans[k-1])

Submission status: Wrong Answer

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

memo = []
setm = set(memo)
cnt = 0
to = 1
while cnt <= k:
    to = a[to-1]
    if to not in setm:
        cnt += 1
        memo.append(to)
        setm.add(to)
    else:
        break
if cnt==k:
    print(memo[-1])
else:
    idx = memo.index(to)
    kidx = ((k - len(memo))%(len(memo)-idx))
    print(memo[idx:][kidx-1])

Submission status: Wrong Answer

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

memo = []
setm = set(memo)
cnt = 0
to = 1
while cnt <= k:
    to = a[to-1]
    if to not in setm:
        memo.append(to)
        setm.add(to)
        cnt += 1
        print("cnt = {},to = {}".format(cnt,to))
    else:
        break
if cnt==k:
    print(memo[-1])
else:
    idx = memo.index(to)
    kidx = ((k - len(memo))%(len(memo)-idx))
    print(memo[idx:][kidx-1])

Submission status: Wrong Answer

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

memo = []
setm = set(memo)
cnt = 1
to = 1
while cnt <= k:
    to = a[to-1]
    if to not in setm:
        memo.append(to)
        setm.add(to)
        cnt += 1
    else:
        break
if cnt==k:
    print(memo[-1])
else:
    idx = memo.index(to)
    kidx = ((k - len(memo))%(len(memo)-idx))
    print(memo[idx:][kidx-1])

Submission status: Wrong Answer

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

memo = []
setm = set(memo)
cnt = 1
to = 1
while cnt <= k:
    to = a[to-1]
    if to not in setm:
        memo.append(to)
        setm.add(to)
        cnt += 1
    else:
        break
if cnt==k:
    print(memo[-1])
else:
    idx = memo.index(to)
    kidx = ((k - len(memo))%(len(memo)-idx))
    print(memo[-1] if kidx==0 else memo[idx:][kidx-1])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
data=[0]*n
data[0]=1
ans=[1]
res=0
d=0
for _ in range(n):
  if data[a[res]-1]==0:
    ans.append(a[res])
    res=a[res]-1
    data[res]=1
  else:
    d=ans.index(a[res])
    break
#print(ans,d)
m=len(ans)-d
l=k-d
if l//m!=0:
  p=l%m
  print(ans[d+p])
else:
  print(ans[k])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
data=[0]*n
data[0]=1
ans=[1]
res=0
d=0
for _ in range(n):
  if data[a[res]-1]==0:
    ans.append(a[res])
    res=a[res]-1
    data[res]=1
  else:
    d=ans.index(a[res])
    break
#print(ans,d)
m=len(ans)-d
l=k-d
p=l%m
print(ans[d+p])

Submission status: Wrong Answer

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

memo = [1]
setm = set(memo)
cnt = 1
to = 1
while cnt <= k:
    to = a[to-1]
    if to not in setm:
        memo.append(to)
        setm.add(to)
        cnt += 1
    else:
        break
        
idx = memo.index(to)
kidx = ((k - len(memo))%(len(memo)-idx))
if len(memo)>k:
    print(memo[-2])
else:
    print(memo[-1] if kidx==0 else memo[idx:][kidx])

Submission status: Wrong Answer

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

memo = [1]
setm = set(memo)
cnt = 1
to = 1
while cnt <= k:
    to = a[to-1]
    if to not in setm:
        memo.append(to)
        setm.add(to)
        cnt += 1
    else:
        break
        
idx = memo.index(to)
kidx = ((k - len(memo))%(len(memo)-idx))
if len(memo)>k:
    print(memo[-2])
else:
    print(memo[idx:][kidx])

Submission status: Wrong Answer

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

memo = [1]
setm = set(memo)
cnt = 1
to = 1
while cnt <= k:
    to = a[to-1]
    if to not in setm:
        memo.append(to)
        setm.add(to)
        cnt += 1
    else:
        break
        
idx = memo.index(to)
kidx = ((k - len(memo))%(len(memo)-idx))
if cnt ==k:
    print(memo[-1])
else:
    print(memo[idx:][kidx])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[int(i) for i in input().split()]
w=[1]
x=set([1])
for i in range(k):
    t=a[w[-1]-1]
    if t in x: break
    x.add(t)
    w.append(t)
p=w.index(t)
if p==0:
    print(w[k%len(w)])
else:#l 2,5,3
    p=w.index(t)
    k-=p-1
    loop=len(w)-p
    print(w[p+k%loop-1])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[int(i) for i in input().split()]
w=[1]
x=set([1])
for i in range(k):
    t=a[w[-1]-1]
    if t in x: break
    x.add(t)
    w.append(t)
p=w.index(t)
if p==0:
    print(w[k%len(w)])
else:#l 2,5,3
    p=w.index(t)
    k-=p-1
    loop=len(w)-p
    print(p+k%loop-1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[int(i) for i in input().split()]
if (a[0]==1):
    print(1)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[int(i) for i in input().split()]
ans=chk=0
w=[1]
x=set([1])
for i in range(k):
    t=a[w[-1]-1]
    if t in x: break
    x.add(t)
    w.append(t)
    if i==k-1:
        print(t)
        exit()
if len(w)==1:
    print(1)
    exit()
mae=w.index(t)-1
k-=mae
loop=len(w)-mae-1
print(w[k%loop+mae])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[int(i) for i in input().split()]
ans=chk=0
w=[1]
x=set([1])
for i in range(k):
    t=a[w[-1]-1]
    if t in x: break
    x.add(t)
    w.append(t)
    if i==k-1:
        print(t)
        exit()
print(-1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[int(i) for i in input().split()]
ans=chk=0
w=[1]
x=set([1])
for i in range(k):
    t=a[w[-1]-1]
    if t in x: break
    x.add(t)
    w.append(t)
    if i==k-1:
        print(t)
        exit()
mae=w.index(t)-1
k-=mae
loop=len(w)-mae-1
print(w[k%loop+mae])

Submission status: Wrong Answer

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

id = [-1] * (N + 1)
x = 1
l = 0
a = []
while id[x] == -1:
    a.append(x)
    id[x] = l
    l += 1
    x = A[x]

c = l - id[x]
if K <= l - 1:
    print(a[K])
else:
    K -= l - 1
    print(a[id[x] + K % c - 1])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
c = 0
tmp = 1
List = []
List2 = [0] * N
flag = 0
for i in range(min(N+1,K)):
    List.append(A[tmp-1])
    tmp = List[i]
    if(List2[tmp-1] >= 1):
        flag = 1
        tmp2 = List2[List[i]-1]-1
        del List[-1]
        break
    else:
        c += 1
        List2[tmp-1] = i+1 
if(flag == 0):
    print(List[K-1])
else:
    List3 = List[tmp2:]
    l = len(List3)
    if(c-l == 0):
        print(List3[(K%l)-1])
    else:
        print(List3[((c-l+1+K)%l)-1])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
B=[0]*(N+1)
B[0]=1;c=0;d=0
if B[0] in A:c=B[0]
for i in range(1,N+1):
  B[i]=A[B[i-1]-1]
#  if c==B[i]:break
#  if A.count(B[i])>1 and d==0:c=B[i];d+=1

d=B.index(B[-1])+1
#print(B.index(B[N-1]))

#print(B,d)
#print(d+1+(K-d-1)%(N-d-1))
#print(B[K],K,d,(K+1-d),(N+1-d))
if K<=N:print(K);exit()
if (K+1-d)%(N+1-d)==0:print(B[-1])
else:print(B[d-1+(K+1-d)%(N+1-d)])
#print(B[d+1+(K-d-1)%(N-d-1)]-1)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
B=[0]*(N+1)
B[0]=1;c=0;d=0
if B[0] in A:c=B[0]
for i in range(1,N+1):
  B[i]=A[B[i-1]-1]
#  if c==B[i]:break
#  if A.count(B[i])>1 and d==0:c=B[i];d+=1

d=B.index(B[-1])+1
#print(B.index(B[N-1]))

#print(B,d)
#print(d+1+(K-d-1)%(N-d-1))
if (K+1-d)%(N+1-d)==0:print(B[-1])
else:print(B[d-1+(K+1-d)%(N+1-d)])
#print(B[d+1+(K-d-1)%(N-d-1)]-1)

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':

    n,k = map(int,input().split())
    A = [0]
    AA= list(map(int,input().split()))
    A = A + AA

    B = set()
    #同じ数字が出てきた瞬間に無限ループに突入と考えられる
    cnt = 0    #無限に入るまでまでのカウンタ
    ind = 1
    lastind = 0
    while True:
        if A[ind] not in B:
            B.add(ind)
            cnt += 1
            #次の飛び先
            ind = A[ind]
        else:
            B.add(ind)
            cnt += 1
            startind = A[ind]
            break

    #次は無限ループはどれだけの数字の集合か
    #開始位置はstartind
    C = set()
    ind = startind
    ANS = []
    while True:
        if A[ind] not in C:
            C.add(ind)
            ind = A[ind]
            ANS.append(ind)
        else:
            C.add(ind)
            ANS.append(A[ind])
            break

    tmp = k - cnt
    tmp = tmp % (len(C))
    print(ANS[tmp-1])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
B=[0]*(N+1)
B[0]=1;c=0;d=0
if B[0] in A:c=B[0]
for i in range(1,N+1):
  B[i]=A[B[i-1]-1]
#  if c==B[i]:break
#  if A.count(B[i])>1 and d==0:c=B[i];d+=1

d=B.index(B[-1])
#print(B.index(B[N-1]))
#print(d)
#print(B[-1])
#print(B,d)
#print(d+1+(K-d-1)%(N-d-1))
if (K-d-1)%(N-d-1)==0:print(B[N])
else:print(B[d+1+(K-d-1)%(N-d-1)])
#print(B[d+1+(K-d-1)%(N-d-1)]-1)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
c = 0
tmp = 1
List = []
List2 = [0] * N
flag = 0
for i in range(min(N,K)):
    List.append(A[tmp-1])
    tmp = List[i]
    if(List2[tmp-1] >= 1):
        flag = 1
        tmp2 = List2[List[i]-1]-1
        List.pop(-1)
        break
    else:
        c += 1
        List2[tmp-1] = i+1 
if(flag == 0):
    print(List[K-1])
else:
    List3 = List[tmp2:]
    l = len(List3)
    if(c%l == 0):
        print(List3[(K%l)-1])
    else:
        print(List3[((c-l+1+K)%l)-1])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
c = 0
tmp = 1
List = []
List2 = [0] * N
flag = 0
for i in range(min(N,K)):
    List.append(A[tmp-1])
    tmp = List[i]
    if(List2[tmp-1] >= 1):
        flag = 1
        tmp2 = List2[List[i]-1]-1
        List.pop(-1)
        break
    else:
        c += 1
        List2[tmp-1] = i+1 
if(flag == 0):
    print(List[K-1])
else:
    List3 = List[tmp2:]
    l = len(List3)
    if(c%l == 0):
        print(List3[(K%l)-1])
    else:
        print(List3[((K%l)+(c%l))%l])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
c = 0
tmp = 1
List = []
List2 = [0] * N
for i in range(N):
    List.append(A[tmp-1])
    tmp = List[i]
    if(List2[tmp-1] >= 1):
        tmp2 = List2[List[i]-1]-1
        List.pop(-1)
        break
    else:
        c += 1
        List2[tmp-1] = i+1 
List3 = List[tmp2:]
l = len(List3)
if(c%l == 0):
    print(List3[(K%l)-1])
else:
    print(List3[((K%l)+(c%l))%l])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
B=[0]*N
B[0]=1;c=0;d=0
if B[0] in A:c=B[0]
for i in range(1,N):
  B[i]=A[B[i-1]-1]
#  if c==B[i]:break
#  if A.count(B[i])>1 and d==0:c=B[i];d+=1

d=B.index(B[-1])
#print(B.index(B[N-1]))
#print(d)
#print(B,d)

print(B[d+1+(K-d-1)%(N-d-1)])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
B=[0]*N
B[0]=1;c=0;d=0
if B[0] in A:c=B[0]
for i in range(1,N):
  B[i]=A[B[i-1]-1]
  if c==B[i]:break
  if A.count(B[i])>1 and d==0:c=B[i];d+=1

d=B.index(c)
#print(B,c,d,i)

#  if B[i]==c:print(B[i]);exit()
#  if A.count(B[i])>1:

#if c==0:print(B[i-1]-1,A[B[i-1]-1])
print(B[d+(K-d)%(i-d)])

Submission status: Wrong Answer

Solution code:
def main() -> None:
    n, k = map(int, input().split())
    cities = tuple(map(int, input().split()))

    check = [1] + [0] * (n-1)
    city_next = cities[0] - 1
    for i in range(1, n+1):
        if check[city_next] == 0:
            check[city_next] = i + 1
            city_next = cities[city_next] - 1
        else:
            to_loop = check[city_next] - 1
            length_loop = i - check[city_next] + 1
            break

    k_left = (k-to_loop) % length_loop
    for _ in range(k_left):
        city_next = cities[city_next] - 1

    print(city_next + 1)
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
B=[0]*N
B[0]=1
for i in range(1,N):
  B[i]=A[B[i-1]-1]
  if B[i] in B[0:i-1]:
    d=B.index(B[i])
    c=i-B.index(B[i])    
    break
print(B[d+(K-d)%c])

Submission status: Wrong Answer

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

import numpy as np


# def input():
#     return sys.stdin.readline().rstrip()


def main():
    n, k = map(int, input().split())

    warps = list(map(int, input().split()))
    warps = [0] + warps
    warps = np.array(warps, dtype=int)

    order_num = np.zeros(len(warps) + 1, dtype=int)
    path_history = []

    node = 1
    while order_num[node] == 0:
        path_history.append(node)
        order_num[node] = len(path_history)
        node = warps[node]

    # print(order_num)
    # print(path_history)
    begin = order_num[node]
    # print(tail)

    if k <= begin:
        print(path_history[k - 1])
    else:
        # path_history = path_history[tail:]
        # print(path_history)
        leng = len(path_history) - order_num[node] + 1
        # print(leng)
        index = (k - (begin - 1)) % leng
        # print(index)
        print(path_history[begin - 1 + index])


main()

Submission status: Wrong Answer

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

import numpy as np


# def input():
#     return sys.stdin.readline().rstrip()


def main():
    n, k = map(int, input().split())

    warps = list(map(int, input().split()))
    warps = [0] + warps
    warps = np.array(warps, dtype=int)

    order_num = np.zeros(len(warps) + 1, dtype=int)
    path_history = []

    node = 1
    while order_num[node] == 0:
        path_history.append(node)
        order_num[node] = len(path_history)
        node = warps[node]

    # print(order_num)
    # print(path_history)
    tail = order_num[node] - 1
    # print(tail)

    if k <= tail:
        print(path_history[k - 1])
    else:
        path_history = path_history[tail:]
        # print(path_history)

        index = (k - tail) % len(path_history)
        print(path_history[index])


main()

Submission status: Wrong Answer

Solution code:
def main() -> None:
    n, k = map(int, input().split())
    cities = tuple(map(int, input().split()))

    check = [1] + [0] * (n-1)
    city_next = cities[0] - 1
    for i in range(1, n):
        if check[city_next] == 0:
            check[city_next] = i + 1
            city_next = cities[city_next] - 1
        else:
            to_loop = check[city_next] - 1
            length_loop = i - check[city_next] + 1
            break

    k_left = (k-to_loop) % length_loop
    for _ in range(k_left):
        city_next = cities[city_next] - 1

    print(city_next + 1)
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    D = 60
    n, k = map(int, input().split())
 
    warps = list(map(int, input().split()))
    warps = [0] + warps
 
    dp = [[0 for j in range(n + 1)] for i in range(D)]
    # print(warps)
    for j in range(1, n + 1):
        dp[0][j] = warps[j]
 
    for i in range(D - 1):
        for j in range(1, n + 1):
            dp[i + 1][j] = dp[i][dp[i][j]]
 
    # for dd in dp:
    # print(dd)
 
    node = 1
    for i in range(D, -1, -1):
        if k >= (1 << i):
            # print(i)
            k -= 1 << i
            node = dp[i][node]
 
    print(node)

Submission status: Wrong Answer

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

# ループを検索して計算量圧縮
# 0->1->...->st->st+1->...->st+lp-1-┐
#             └─────────────────────┘
#                       lp

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

nth = k     #取得位置
x = a[0]    #初期値
def next(x):#ネクスト関数
    ret = a[x-1]
    return ret

seen = set([])
seqs = []
for i in range(n+1):
    seen.add(x)
    seqs.append(x)
    x = next(x)
    if x in seen: break
start = seqs.index(x)
lp = len(seqs) - start

lpsum = sum(seqs[start:])
lptimes = (nth-(start+lp)+(lp-1))//lp
ret_sum = lpsum * lptimes
nth -= lp * lptimes
ret_sum += sum(seqs[:nth])
ret_val = seqs[nth-1]

#print(ret_sum)
print(ret_val)

Submission status: Wrong Answer

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

count = 0

stack = set()
visited = []
visited_set = set()
endf = False

current = 0
while True:
    i = A[current]-1
    visited_set.add(current)
    visited.append(current)

    if not i in visited_set:
        current = i
        count += 1
    else:
        leng = len(visited_set)
        roop_len = leng - visited.index(i)
        header_len = leng - roop_len
        break

    if count >= K:
        endf = True
        print(i)
        break

if not endf:
    roop_ind = (K - header_len + 1) % roop_len
    if roop_ind == 0:
        print(visited[-1]+1)
    else:
        print(visited[header_len + roop_ind-1]+1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
dic={}
wk=1
start=0
for i in range(1,n+1):
  wk=a[wk-1]
  if wk in dic:
    start=dic[wk]
    break
  dic[wk]=i
end=len(dic)
loopcnt=end-start+1
keys=list(dic.keys())
if k<start:
  print(keys[k-1])
else:
  chk=(k-start+1+loopcnt)%loopcnt
  print(keys[(start-1)+chk-1])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
dic={}
wk=1
start=0
for i in range(1,n+1):
  wk=a[wk-1]
  if wk in dic:
    start=dic[wk]
    break
  dic[wk]=i
end=len(dic)
loopcnt=end-start+1
keys=list(dic.keys())
if k<start:
  print(keys[k-1])
else:
  print(keys[(start-1)+(k-start+1)%loopcnt-1])

Submission status: Wrong Answer

Solution code:
import bisect


def main():
    n, k = map(int, input().split())
    a_lst = list(map(int, input().split()))

    lst = [0] * (n + 1)
    tmp = 0
    lst[0] = tmp
    for i in range(1, n + 1):
        tmp = a_lst[tmp] - 1
        lst[i] = tmp
    switch_lst = [0] * n

    first_index = 0
    second_index = n
    for i in range(n + 1):
        city_number = lst[i]
        second_index = i

        if switch_lst[city_number] == 1:
            search_lst = lst[:i]
            search_number = city_number

            first_index = bisect.bisect(search_lst, search_number) - 1
            break

        else:
            switch_lst[city_number] = 1

    cycle = second_index - first_index
    if first_index >= k:
        answer = lst[k] + 1
    else:
        k -= first_index
        k %= cycle
        answer = lst[k + first_index] + 1
    print(answer)
    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import bisect


def main():
    n, k = map(int, input().split())
    a_lst = list(map(int, input().split()))

    lst = [0] * (n + 1)
    tmp = 0
    lst[0] = tmp
    for i in range(1, n + 1):
        tmp = a_lst[tmp] - 1
        lst[i] = tmp
    switch_lst = [0] * n

    first_index = 0
    second_index = n
    for i in range(n + 1):
        city_number = lst[i]
        second_index = i

        if switch_lst[city_number] == 1:
            search_lst = lst[:i]
            search_number = city_number

            first_index = bisect.bisect(search_lst, search_number) - 1
            break

        else:
            switch_lst[city_number] = 1

    cycle = second_index - first_index
    if first_index >= k:
        answer = a_lst[k]
    else:
        k -= first_index
        k %= cycle
        answer = a_lst[k + first_index]
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

N, K = map(int, input().split())
towns = [0] + list(map(int, input().split()))
checked = [-1 for _ in range(N+1)]
checked[1] = 0
next = towns[1]
count = 1
    
while(checked[next] < 0):
  checked[next] = count
  count += 1
  next = towns[next]
loop = count - checked[next]
pos = 0
if K > count:
  pos = checked[next] + (K-checked[next])%loop
else:
  pos = K
for i in range(1, N+1):
  if checked[i] == pos:
    print(i)
    exit()

Submission status: Wrong Answer

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

visited = [0] * N
path = []
now_town = 1
while(True):
    visited[now_town-1] = 1
    path.append(now_town)
    now_town = As[now_town-1]
    if visited[now_town-1] == 1:
        break
index = path.index(now_town)
loop = path[index:len(path)]
loop_len = len(loop)
k = K - index
r = k % loop_len
print(loop[r])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
P = [0]
x = 0

for n in range(N):
  x = A[x]-1
  P+= [x]

P = list(set(P))
print(P[K%len(P)]+1)

Submission status: Wrong Answer

Solution code:
n, k = [int(x) for x in input().split()]
a_list = [int(x) - 1 for x in input().split()]
ci = 0
while k > 0:
    if k & 1:
        ci = a_list[ci]
    k >>= 1
    a_list = [a_list[i] for i in range(n)]
print(ci + 1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

b = []
bb = defaultdict(int)

# n回以内にある都市に戻る
pos = 1
for i in range(len(a)):
    pos = a[pos - 1]
    if pos in bb: #時間がかかる
        break
    bb[pos] = 1
    b.append(pos)

# ループのスタート都市 loop_city1
loop_city1 = pos
# ループの開始位置
loop_start = b.index(loop_city1)
# ループの終了位置
loop_end = len(b) - 1

# 残りの移動回数をループの長さで割るとループの何番目で終わるか分かる
out = (k - loop_start) % (loop_end - loop_start + 1)
if out == 0:
    out = loop_end - loop_start + 1 - 1

# 最終都市
# ループ前の都市数をずらして
print(b[out + loop_start - 1])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

b = []
bb = defaultdict(int)

# n回以内にある都市に戻る
pos = 1
for i in range(len(a)):
    pos = a[pos - 1]
    if pos in bb: #時間がかかる
        break
    bb[pos] = 1
    b.append(pos)

# ループのスタート都市 loop_city1
loop_city1 = pos
# ループの開始位置
loop_start = b.index(loop_city1)
# ループの終了位置
loop_end = len(b) - 1

# 残りの移動回数をループの長さで割るとループの何番目で終わるか分かる
out = (k - loop_start) % (loop_end - loop_start + 1)
#if out == 0:
    #out = loop_end - loop_start + 1

# 最終都市
# ループ前の都市数をずらして
print(b[out + loop_start - 1])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

b = []
bb = defaultdict(int)

# n回以内にある都市に戻る
pos = 1
for i in range(len(a)):
    pos = a[pos - 1]
    if pos in bb: #時間がかかる
        break
    bb[pos] = 1
    b.append(pos)
    
# ループのスタート都市 loop_city1
loop_city1 = pos
# ループの開始位置
loop_start = b.index(loop_city1)
# ループの終了位置
loop_end = len(b) - 1

# 残りの移動回数をループの長さで割るとループの何番目で終わるか分かる
out = (k - loop_start) % (loop_end - loop_start + 1)
if out == 0:
    out = loop_end - loop_start + 1

# 最終都市
# ループ前の都市数をずらして
print(b[out + loop_start - 1])

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    A = [a-1 for a in list(map(int, input().split()))]
    visited = [0] * N
    route = []
    town = A[0]
    while True:
        if visited[town] == 0:
            visited[town] = 1
            route.append(town)
            town = A[town]
        else:
            before_loop = route.index(town)
            route = route[before_loop:]
            break
    idx = ((K - before_loop) % len(route)) - 1
    print(route[idx] + 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = [0] + list(map(int, input().split()))
path = [-1] * (N+1)
nex = A[1]
path[1] = 0
cnt = 1
while path[nex] == -1:
    path[nex] = cnt
    cnt += 1
    nex = A[nex]

mod = cnt - path[nex]
n = path[nex] + (K-path[nex])%mod

ans = 1
for _ in range(n):
    ans = A[ans]

print(ans)