Problem:


Score: 400 points

Problem Statement

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

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

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

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

Constraints

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

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

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

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

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

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2



Sample input:
4 5
3 2 4 1


Sample output:
4

Submission status: Wrong Answer

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

i = 0
lp = 0
step = []
while visited[i] == 0:
  visited[i] = 1
  next_visit = A[i] 
  step.append(i)
  lp += 1
  i = next_visit

lp_start = step.index(i)
syuki = lp - lp_start

if lp_start >= k:
  ans = step[k]
else:
  cnt = (k - lp_start) % syuki
  ans = step[lp_start:][cnt] + 1

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = (int(x) for x in input().split())
town = [int(i) for i in input().split()]
warp  = [1]
checklist = [0]+[-1]*n
i = 0
j = 1
while j<=n:
    #print(town[i])
    warp.append(town[i])
    #print(warp,checklist)
    if checklist[town[i]-1]!=-1:
        s = checklist[town[i]-1]
        warp = warp[s:j]
        #print(checklist[town[i]-1],j)
        #print("a",warp)
        break
    checklist[town[i]-1] = j
    i = town[i]-1
    j += 1


m = len(warp)
print(warp[(k-s)%m])

Submission status: Wrong Answer

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

takahashi = 1
roop = [takahashi]
visited = [False] * N
visited[takahashi - 1] = True

for _ in range(N):
    takahashi = A[takahashi - 1]
    if visited[takahashi - 1]:
        break
    else:
        roop.append(takahashi)
        visited[takahashi - 1] = True

first, roop = roop[:roop.index(takahashi)], roop[roop.index(takahashi):]
print(roop[(K - len(first)) % len(roop)])

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
n,k=map(int,input().split())

A=list(map(int,input().split()))
L=[None]*n
R=[0]*n
R=[1]
now=1
for i in range(n):
        now=A[now-1]
        if L[now-1] != None:
                index=R.index(now)
                break
        L[now-1]=now
        R.append(now)

if k<=index:
        print(R[k+1])
        exit()

r=(k-index)%(len(R)-index)

print(R[index:][r])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
location = [1]
nowlocate = 1
for i in range(N):
  nowlocate = A[nowlocate-1]
  location.append(nowlocate)
  if len(set(location)) != len(location):
    M = len(location) -1
    break
for i in range(M):
  if location[M] == location[i]:
    roop = M-i
    stop = i
    break
print(location[((K-stop) % roop) + stop ])

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10**7)

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, k = na()
a = na()
for i in range(n):
    a[i] -= 1
ct = [0 for i in range(n)]

def dfs(v):
    nv = a[v]
    if ct[nv] == k:
        return nv, 0
    if ct[nv] != 0:
        r = ct[v] + 1 - ct[nv]
        l = (k - ct[nv]) % r
        return nv, l
    else:
        ct[nv] = ct[v] + 1
        return dfs(nv)

nv, l = dfs(0)
for i in range(l):
    nv = a[nv]
print(nv + 1)

Submission status: Wrong Answer

Solution code:
tem1=input().split()
N = int(tem1[0])
K = int(tem1[1])
tem2 = input().split()
now =1
fact = K%N
for i in range(0,fact):
    now = int(tem2[now-1])
print(now)

Submission status: Wrong Answer

Solution code:
pi = 3.1415926

############ ---- USER DEFINED INPUT FUNCTIONS ---- ############
def inp():
    return(int(input().rstrip()))
def inlt():
    return(list(map(int,input().rstrip().split())))
def insr():
    s = input().rstrip()
    return(s[:len(s) - 1])
def invr():
    return(map(int,input().rstrip().split()))
################################################################
n,k = invr()
nums = [0] + inlt()
cycle = [nums[1]]

for i in range(2,n + 1):
    if nums[cycle[-1]] != cycle[0]:
        cycle.append(nums[cycle[-1]])
    else:
        break

if k <= len(cycle):
    print(cycle[k - 1])
else:
    remain = k % len(cycle)
    print(cycle[remain - 1])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = [int(i)-1 for i in input().split()]
l = []
d = {}
p = 0
while p not in d:
    d[p] = len(l)
    l.append(p)
    p = A[p]
if K <= len(l):
    print(l[K % len(l)] + 1)
    exit()
K -= d[p]
K %= len(l) - d[p]
print(l[K + d[p]] + 1)

Submission status: Wrong Answer

Solution code:
#import numpy as np
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial#, gcd
from bisect import bisect_left #bisect_left(list, value)
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
def pri(x): print('\n'.join(map(str, x)))

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

dist = [0]*N
dist[0] = 0
cnt = 0
cp = 0
while cnt < N:
    np = A[cp]-1
    if np == cp: # ikidomari
        print(np+1)
        exit()
    elif dist[np]>0 or np == 0: # loop
        lstep = cnt - dist[np] + 1
        cp = np
        break
    dist[np] = cnt + 1
    cp = np
    cnt += 1

K -= dist[cp]
K %= lstep
cnt = 0
while cnt < K:
    np = A[cp]-1
    cp = np
    cnt += 1
    
print(cp+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]
c=A[A[0]]

s=set()
s.add(A[0])
d=[]
d.append(A[0])
 
while True:
  if c in s:
    break
  s.add(c)
  d.append(c)
  c=A[c]

a=d.index(c)
K-=a
 
mod=len(s)-a
K%=mod
 
ans=A[0]
for i in range(a):
  ans=A[ans]

for i in range(K-1):
  ans=A[ans]
print(ans+1)

Submission status: Wrong Answer

Solution code:
import itertools
from collections import defaultdict
import collections
import sys
sys.setrecursionlimit(200000)
mod = 1000000007

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

tf = [False] * (n + 1)
tf[1] = True
ss = [1]
cnt = 0
while cnt < n ** 2:
    if len(ss) != 1 and tf[a[ss[cnt] - 1]]:
        d = a[ss[cnt] - 1]
        break
    tf[a[ss[cnt] - 1]] = True
    ss.append(a[ss[cnt] - 1])
    cnt += 1


print(ss[k % (len(ss) - ss.index(d))])

Submission status: Wrong Answer

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

visited = [0] * N
cnt = 0
i = 0
loop = 0
while True:
    visited[i] = cnt
    i = A[i] - 1
    cnt += 1
    if visited[i] > 0 or i == 0:
        loop = cnt - visited[i]
        break

for _ in range((K - visited[i]) % loop):
    i = A[i] - 1
print(i+1)

Submission status: Wrong Answer

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

cnt = [0]*N
town = 1
for i in range(K):
    if cnt[town-1] ==1:
        break

    cnt[town-1] += 1
    town = A[town-1]


ans1 = sum(cnt)

import copy
town2 = copy.deepcopy(town)

cnt2 = [0]*N

for i in range(K):
    if cnt2[town2-1] ==1:
        break

    cnt2[town2-1] += 1
    town2 = A[town2-1]
    
ans2 = sum(cnt2)

if K <= (ans1-ans2):
    print(town)
else:
    town3 = 1
    cnt3=[0]*N
    K2 = ((K-(ans1-ans2))%(ans2))+(ans1-ans2)
    print(K2)
    for i in range(K2):
        if cnt3[town3-1] ==1:
            break

        cnt[town3-1] += 1
        town3 = A[town3-1]
    print(town3)

Submission status: Wrong Answer

Solution code:
from sys import stdin
n,k = [int(x) for x in stdin.readline().rstrip().split()]
a = [int(x) for x in stdin.readline().rstrip().split()]
b = [-1]*n
b[0] = 0
tf = True
t = 1
g = 0
while tf:
    if b[a[g]-1] == -1:
        b[a[g]-1] = t
    else:
        m = b[a[g]-1]
        g = a[g]-1
        break
    t += 1
    g = a[g]-1
k -= t
mo = t-m
k = k % mo
while k > 0:
    g = a[g]-1
    k -= 1
print(g+1)

Submission status: Wrong Answer

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

ans=[]
judge={}
j=0 #A進み
while A[j] not in judge.keys() or judge[A[j]]!=2:
    ans.append(A[j])
    if A[j] not in judge.keys():
        judge[A[j]]=0
    judge[A[j]]+=1
    j=A[j]-1
ans.append(A[j])

st=ans.index(A[j])
ans=ans[st:-1]
K-=st
l=len(ans)
#print(ans)
print(ans[K%l-1])

Submission status: Wrong Answer

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

import sys
from collections import defaultdict, deque
from heapq import heappush, heappop
import math
import bisect

# 再起回数上限変更
sys.setrecursionlimit(10**7)

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

first = 0
second = 0
cnt = 0
visited = [0] * N
city = [None] * N

def nex_node(x):
    global first
    global second
    global cnt
    city[cnt] = x
    cnt += 1

    if visited[x] > 0:
        first = visited[x]
        second = cnt
        val = K - first +1
        idx = val % (second - first)
        # print(first, second, idx)
        # print(city)
        print(city[first - 1 +idx] + 1)
        sys.exit()

    visited[x] = cnt

    # print(x, cnt, a[x])
    if cnt == K:
        print(x)
        sys.exit()
    if a[x] == x:
        print(x+1)
        sys.exit()

    nex_node(a[x])


nex_node(0)

Submission status: Wrong Answer

Solution code:
def main():
    n,k = map(int,input().split())
    a = list(map(int,input().split()))
    has = {}
    for i in range(n):
        has[i]=[]
    v = a[0]-1
    visited = [1]
    for i in range(1,n):
        if v+1 in visited:
            mov = i
            ind = visited.index(v+1)
            vi = visited[ind:]
        else:
            has[i]+= [v+1]
            visited.append(v+1)
            v = a[v]-1
    if mov>=k:
        print(visited[k])
    else:
        k = k-mov
        print(vi[(k)%len(vi)])



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq
import itertools



sys.setrecursionlimit(10**7)
#N, Q = map(int, input().split())
#G = [list(input()) for i in range(H)]
#INF = V * 10001
#A = [int(i) for i in input().split()]
#AB = [list(map(int, input().split())) for _ in range(K)]

N, K = map(int,input().split())
A = [int(i) for i in input().split()]
Temp = [0]*N
imp = [0]*3
ans = [0]

def rec(next, kore):
    #print(kore)
    if(kore==K):
        ans[0] = next
        return
    if(Temp[next-1] == 0):
        Temp[next-1] = kore
        rec(A[next-1], kore+1)
    else:
        imp[0] = kore-Temp[next-1]
        imp[1] = kore
        imp[2] = next
    return

rec(0,1)
#print("fin")

if(ans[0] != 0):
    print(ans[0])
else:
    K -= imp[1]
    K = K%imp[0]
    if(K==0):
        print(imp[2])
    else:
        rec(imp[2], 1)
        print(ans[0])

Submission status: Wrong Answer

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

num = 1
l = []
l.append(num)
i = 0
while i < K-1:
  num = A[num-1]
  if num not in l:
    l.append(num)
    i += 1
  else:
    i = K - (K-1-i) % (i+1-(l.index(num)))
    break;
    
while i < K-1:
  num = A[num-1]
  i += 1
  
print(num)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
l=list(map(int,input().split()))
from collections import deque
d=deque([0])
li=[0]*n
c=1
lis=[]
for i in range(n):
  now=d.pop()
  ne=l[now]
  li[now]=c
  lis.append(now)
  if li[ne-1]!=0:
    z=ne-1
    break
  else:
    d.append(ne-1)
x=lis.index(z)
if m<x+1:
  print(x)
else:
  print(lis[(m-x)%(len(lis)-x)+x]+1)

Submission status: Wrong Answer

Solution code:
def main():
  n,k=map(int,input().split())
  a=list(map(int,input().split()))
  #n=100000
  #k=n*14
  #a=list(range(2,n+1))
  #a.append(1)
  b=a[0]
  if b==1:
    print(b)
    exit()
  seen=[-1]*n
  seen[0]=0
  c=1
  while seen[b-1]==-1:
    seen[b-1]=c
    b=a[b-1]
    c+=1
  start=seen[b-1]
  end=c
  k=start+(k-start)%(end-start)
  print([i+1 for i,s in enumerate(seen) if s==k][0])

if __name__=='__main__':
  main()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split(" "))
a = list(map(int,input().split(" ")))
memory = [1]
i = 1
while True:
  if not a[i-1] in memory:
    memory.append(a[i-1])
    i = a[i-1]
  else:
    start = memory.index(a[i-1])
    break
if k < len(memory):
  print(memory[k-1])
else:
  loop = len(memory)-start
  town = (k - start) % loop
  print(memory[town + start])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
seq=list(map(int,input().split()))
v=[False]*n
path=[1]
i=0
v[0]=True
while True:
        if v[seq[i]-1]==False:
                v[seq[i]-1]=True
                path.append(seq[i])
                i=seq[i]-1
                
        else:
                t=seq[i]
                break

ind=path.index(t)
path2=path[ind:]
path1=path[:ind]
k-=len(path1)
#print(t,ind,path1,path2)
print(path2[(k%len(path2))])

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    A = [int(x) for x in input().split()]
    L=[1]
    a = A[0]
    for _ in range(N):
        L.append(a)
        a=A[a-1]
        if a==1:
            break
    g = K%len(L)
    print(L[g])
    
main()

Submission status: Wrong Answer

Solution code:
def find_size(A):
    visited = set()
    visited_index = dict()
    now = 0
    size = 0
    while True:
        if now in visited:
            return visited_index[now], size - visited_index[now]
        visited.add(now)
        visited_index[now] = size
        now = A[now]
        size += 1
    return None

def find_n(A, N):
    now = 0
    for _ in range(N):
        now = A[now]
    return now

int1 = lambda s: int(s)-1

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

start, size = find_size(A)
if K <= start + size:
    print(find_n(A, start + size) + 1)
else:
    print(find_n(A, (K % size) - start + (start + size)) + 1)

Submission status: Wrong Answer

Solution code:

n, k = map(int, raw_input().split(' '))
adj = [-1] + map(int, raw_input().split(' '))

seen = {}
cur = 1
count = 0
k -= 1
while(k):
	
	if cur not in seen:
		seen[cur] = count
		count += 1
	nex = adj[cur]
	if nex not in seen:
		k -= 1
		cur = nex
	elif nex in seen:
		if seen[nex] > seen[cur]:
			k -= 1
			cur = nex

		elif seen[nex] < seen[cur]:
			k %= seen[cur] - seen[nex] +1
			cur = nex

print cur

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
tele=[0 for _ in range(n+1)]
cnt=1
v=1
while True:
    if tele[v]!=0:
        break
    else:
        tele[v]=cnt
        cnt+=1
        v=A[v-1]

circle_in=tele[v]
circle=[]
for idx,t in enumerate(tele):
    if t<circle_in:
        continue
    else:
        circle.append((t,idx))
circle.sort()

circle_len=len(circle)
if circle[0][1]==1:
    circle_idx=k%(circle_len)
    ans=circle[circle_idx][1]

elif k<circle_in:
    ans=tele.index(k)
else:
    k=k-(circle_in-1)
    circle_idx=k%(circle_len)
    ans=circle[circle_idx][1]

print(ans)

Submission status: Wrong Answer

Solution code:
n, k, *a = map(int, open(0).read().split())
a = [None] + a
done = [None] + [0] * n
t = 0
p = 1
while not done[p]:
    done[p] = t
    p = a[p]
    t += 1

loop = t - done[p]
orig = done[p]

k = (k - orig) % loop + orig
p = 1
while k:
    p = a[p]
    k -= 1

print(p)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

visited = [0] * (n + 1)
now = 1
loop = 0
while k > 0:
    now -= 1
    if visited[now] == 1:
        break
    else:
        now = a[now]
        visited[now] = 1
        k -= 1
        loop += 1

k %= loop
while k > 0:
    now -= 1
    now = a[now]
    k -= 1

ans = now
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
for i in range(n):
    a[i] -= 1
used = [True] * n
used[0] = False
i = a[0]
k1 = 1
g = [0]
while True:
    if used[i]:
        used[i] = False
        k1 += 1
        g.append(i)
        i = a[i]
    else:
        break
f = False
g1 = []
for j in g:
    if j == i:
        f = True
    if f:
        g1.append(j)
print(g1[(k - (len(g) - len(g1))) % len(g1)] + 1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
a = list(map(int,input().split()))
town = [-1]*N
i=1
n=0
while True:
    if town[i-1] == -1:
        town[i-1] = n
        i = a[i-1]
    else:
        roop = i
        break
    n += 1

amari = (K-town[roop-1])%(n-town[roop-1])
print(town.index(town[roop-1]+amari)+1)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N, K = LI()
    A = LI_()

    visited = [False]*N

    city = A[0]
    teleport_cycle = [city]
    while not visited[A[city]]:
        city = A[city]
        teleport_cycle.append(city)
        visited[city] = True

    city_cycle_head = A[teleport_cycle[-1]]
    idx_cycle_head = teleport_cycle.index(city_cycle_head)
    length_cycle = len(teleport_cycle) - idx_cycle_head

    if K-1<len(teleport_cycle):
        print(teleport_cycle[K-1]+1)
    else:
        K = idx_cycle_head + (K-idx_cycle_head)%length_cycle
        print(teleport_cycle[K-1]+1)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

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

vislist=[1]
visset={1}
loopflag=True
endflag=False
donek=0
ima=1

while loopflag:
    ima=alist[ima-1]
    if ima in visset:
        loopflag=False
    else:
        vislist.append(ima)
        visset.add(ima)

        donek+=1

        if donek == k:
            loopflag=False
            endflag=True

if endflag:
    print(ima)
    sys.exit()

loopstartindex=vislist.index(ima)
looplength=len(visset)-loopstartindex
print(vislist[loopstartindex+(k+1-loopstartindex)%looplength-1])

Submission status: Wrong Answer

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


while 1:
    a = A[visted[-1]-1]
    
    if a not in visted:
        visted.append(a)
    else:
        visted.append(a)
        break
        
loop_start = visted.index(visted[-1])+1

if k-1 < loop_start:
    print(visted[k-1])
else:
    x = len(visted[:loop_start])
    visted = visted[loop_start:]
    print(visted[(k-x)%len(visted)])

Submission status: Wrong Answer

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

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

    tmp = 1
    tl = []
    tl_a = tl.append

    if k < n:
        for i in range(n):
            if not nlis[tmp - 1] in tl:
                tl_a(nlis[tmp - 1])
                tmp = nlis[tmp - 1]
            else:
                tl = tl[tl.index(nlis[tmp - 1]):]
                print(tl[((n - i) % len(tl)) - 1])
                break
    else:
        for i in range(n):
            if not nlis[tmp - 1] in tl:
                tl_a(nlis[tmp - 1])
                tmp = nlis[tmp - 1]
            else:
                tl = tl[tl.index(nlis[tmp - 1]):]
                print(tl[((k - i) % len(tl)) - 1])
                break

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
n,k = map(int,input().split())
a = list(map(int,input().split()))
ans = []
key = -1
p = 0
while a[p] != 1:
    if a[p] in ans:
        key = ans.index(a[p])
        break
    ans.append(a[p])
    p = a[p]-1
if len(ans) == 0:
    print(1)
elif key == -1:
    mod = k % len(ans)
    if mod == 0:
        print(ans[-1])
    else:
        print(ans[mod])
else:
    r = ans[key:]
    mod = (k-key)%len(r)
    if mod == 0:
        print(ans[-1])
    else:
        print(ans[mod])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
a = [1]+a
v = [-1 for _ in range(n+1)]
v[1] = 0
x = a[1]
c = 1

while k > 0 and v[x] < 0:
    y = a[x]
    v[y] = v[x]+1
    x = y
    k -= 1
    c += 1

if k==0:
    print(x)
else:
    z = c - v[x]

    k = k%z
    while k > 0:
        k -= 1
        x = a[x]
    print(x)

Submission status: Wrong Answer

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

l = list(map(int, input().split()))
for i in range(len(l)):
    d[i+1] = l[i]

city = 1
num = 0
l2 = [1]
while True:
    city = d[city]
    if city in l2:
        break
    l2.append(city)
first = l2.index(city)
l2 = l2[first:]
print(l2[(k-first)%(len(l2))])

Submission status: Wrong Answer

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

now = 1
for i in range(n+1):
    next = a[now-1]
    now = next
    if not now in town_list:
        town_list.append(now)
    else:
        idx = town_list.index(now)
        initial = town_list[:idx]
        cycle = town_list[idx:]
        break

end_idx = (k-len(initial))%len(cycle)
print(cycle[end_idx])

Submission status: Wrong Answer

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

current_town = 0

ls_1 = [0]

for n in range(N):
    after_town = A[current_town]
    if ls_1.count(after_town) == 0:
        ls_1.append(after_town)
        current_town = after_town
    else:
        ls_2 = ls_1[ls_1.index(after_town):]
        break

print(ls_2[(K-(len(ls_1)-len(ls_2))) % len(ls_2)]+1)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
A = list(map(int,input().split()))
A = [A[k]-1 for k in range(N)]
now = 0
for k in range(1000):
    if (K>>k)&1 == 1:
        now = A[now]
print(now+1)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
B = []
for i in range(len(A)):
    B.append(0)

ok = 0
i = 0
cnt1 = 0
cnt2 = 0
B[0] = 1
while ok == 0:
    i = A[i]-1
    if B[i] == 0:
        B[i] = 1
    else:
        ok = 1
        B[i] = 2
    cnt1 += 1

while ok == 1:
    i = A[i]-1
    if B[i] == 1:
        B[i] = 2
    else:
        ok = 2
    cnt2 += 1

if K > cnt1:
    K -= cnt1
    ok = 3
if K > cnt2 and ok == 3:
    K = K % cnt2

while K > 0:
    i = A[i]-1
    K -= 1

print(i+1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
now = 1
cnt = 1
loop = 0
visited = [False] * n
visited[now - 1] = cnt
while True:
    now = a[now - 1]
    cnt += 1
    if visited[now - 1]:
        loop = (cnt - visited[now - 1])
        print(a[((k - (visited[now - 1] - 1)) % loop) + (visited[now - 1] - 1)])
        exit()
    visited[now - 1] = cnt

Submission status: Wrong Answer

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

flg = True
c = [0 for i in range(n)]
i = 1
b = []
d = []
one = False
while flg:
	x = i-1
	if c[x] == 1 and a[x] == 1:
		flg = False
		one = True
		break
	elif c[x] == 3:
		flg = False
		break
	else:
		c[x] += 1
		if c[x] == 1:
			b.append(x+1)
		if c[x] == 2:
			d.append(x+1)
	i = a[x]
y = k % len(b)
ans = b[y]
if k <= len(b):
	print (ans)
else:
	if one:
		print (ans)
	else:
		k -= len(b)
		y = k % len(d)
		ans = d[y]
		print (ans)

Submission status: Wrong Answer

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

exist = [a[0]]
ex = {}

cnt = 1
while(True):
    if a[exist[-1]-1] in exist:
        idx = exist.index(a[exist[-1]-1])
        exist.append(a[exist[-1]-1])
        break
    exist.append(a[exist[-1]-1])
    ex[a[exist[-1]-1]] = 0
    
    cnt += 1
    if cnt >= k-1:
        print(exist[-1])
        exit()


#k = k-idx
#print(idx,exist,cnt)
d = (k-idx) % (cnt-idx)

print(exist[d+idx-1])

Submission status: Wrong Answer

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

if L[0]==0:
    print(1)
    exit()

seen=set()
cur=0
cnt=0

while cur not in seen:
    seen.add(cur)
    cnt+=1
    # last=cur
    cur=L[cur]
intersec=cur
cur=0
cnt2=0
while cur!=intersec:
    cur=L[cur]
    cnt2+=1
loopSize=cnt-cnt2
ans=0
for _ in range((k-cnt2)%(loopSize)+cnt2):
    ans=L[ans]
print(ans+1)

Submission status: Wrong Answer

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

ans = a[K % N]
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
n,k=map(int,input().split())

A=list(map(int,input().split()))
L=[None]*n
R=[0]*n
R=[1]
now=1
for i in range(n):
        now=A[now-1]
        if L[now-1] != None:
                index=R.index(now)
                break
        L[now-1]=now
        R.append(now)

if k<=index:
        print(L[k+1])
        exit()

r=(k-index)%(len(R)-index)

print(R[index:][r])

Submission status: Wrong Answer

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

X = 0
town = 1
counted = []
for k in range(N):
  X += 1
  counted.append(town)
  town = A[town-1]
  if town in counted:
    Q = len(counted[counted.index(town):])
    ini = X - Q
    break
    
ans = 1
if K > ini:    
  num = (K-ini) % Q + ini
  for i in range(num):
    ans = A[ans-1]
  print(ans)
else:
  for i in range(K):
    ans = A[ans-1]
    print(ans)
  print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
flag = 0
position = [0]
while flag == 0:
  temp = position[-1]
  if A[temp] -1 in position:
    break
  position.append(A[temp]-1)
x = K%len(position)
print(position[x]+1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
s=set()
l=list()
s.add(0)
d=dict()
ind=0
l.append(0)
d[0]=0
s.add(0)
ind+=1
while(True):
    if a[l[-1]]-1 in s:
        break
    d[a[l[-1]]-1]=ind
    s.add(a[l[-1]]-1)
    l.append(a[l[-1]]-1)
    ind+=1
# print(d)
ind=d[a[l[-1]]-1]
# print(ind,a[l[-1]]-1)
r=(k-len(l))%(len(l)-ind)
# print(l)
# print(r)
print(l[ind+r]+1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
a = list(map(int, input().split()))
chk = [0] * N
start = -1
ido = [0]
chk[0] = 1
index = 0
cnt = 0
while True:
    index = a[index] - 1
    if chk[index] == 0:
        chk[index] += 1
        cnt += 1
        ido.append(index)
    else:
        start = index
        break

if cnt >= K:
    print(a[ido[K]])
    exit()
tmp = ido.index(start)
ido = ido[tmp:]
K -= tmp
print(ido[K % len(ido)] + 1)

Submission status: Wrong Answer

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

loop = [1]
P = 1
for i in range(2*(10**5)):
  G = A[P-1]
  if G in loop:
    Comeback = loop.index(G)
    length = len(loop)
    looplen = length - Comeback
    Shippo = Comeback

    break
  else:
    loop.append(G)
    P = G

print(loop[((K+1)-Shippo)% looplen + Shippo-1])

Submission status: Wrong Answer

Solution code:
import sys
import re
import math
import collections
import decimal
import bisect
import itertools
import fractions
import functools
import copy

# import heapq
# from collections import deque
# import decimal

sys.setrecursionlimit(10000001)
INF = sys.maxsize
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===

def main():
    n, k = ns()
    a = na()
    a = [ai - 1 for ai in a]

    junkan_point = 0
    visited = [0, a[0]]
    next = a[0]
    
    visited_set = set(visited)
    
    for _ in range(n):
        if a[next] in visited_set:
            flg = True
            junkan_point = a[next]
            break
        else:
            visited.append(a[next])
            visited_set.add(a[next])
            next = a[next]

    junkan_visited = visited[visited.index(junkan_point):]

    tmp = len(visited) - len(junkan_visited)
    if k < tmp:
        print(k)
    else:
        idx = (k-tmp) % len(junkan_visited)
        print(junkan_visited[idx] + 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

graph = []

time = [-1] * N

for i in range(N):
    graph.append([i, A[i]])

now = 0
nex = 0
time[0] = 0
loop = 0

while True:
    now = nex
    nex = A[now] - 1

    if time[nex] == -1:
        time[nex] = time[now] + 1
    else:
        loop = nex
        break

if K > max(time):
    K %= (max(time) - time[loop] + 1)
    K -= time[loop]
    for i in range(N):
        time[i] -= time[loop]

for i in range(N):
    if time[i] == K:
        print(i + 1)

# print(time, K)

Submission status: Wrong Answer

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


def solve(N, K, A):
    visited = [1]
    vset = set()
    vset.add(1)
    now = 1
    b = 0
    l = 0
    for _ in range(N):
        now = A[now - 1]
        if now in vset:
            b = visited.index(now)
            l = len(visited) - b
            break
        else:
            visited.append(now)
            vset.add(now)
    x = (K - b) % l
    print(visited[b + x])
    return

# 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():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]
    solve(N, K, A)


if __name__ == '__main__':
    main()