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:
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 = []
visited = [False]*(10**6)
curr = 1
if k <= n:
	curr = 1
	count = 0
	while count != k:
		curr = dic[count]
		count+=1
	exit(print(curr))
while True:
	cycle.append(curr)
	visited[curr] = True
	curr = dic[curr]
	#k -= 1
	if visited[curr]:
		break

csize = 0
ind = 0
for i in range(len(cycle)-1,-1,-1):
	if cycle[i] == curr:
		ind = i + 1
		break
	csize += 1
offset = len(cycle) - csize - 1
#print(cycle,csize+1, k , curr,offset)
k -= offset
k %= csize + 1
print(cycle[offset + k])

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 = []
visited = [False]*(10**6)
curr = 1
if k <= n:
	curr = 1
	count = 0
	while count != k:
		curr = dic[count]
		count+=1
	exit(print(curr))
while True:
	cycle.append(curr)
	visited[curr] = True
	curr = dic[curr]
	#k -= 1
	if visited[curr]:
		break

csize = 0
for i in range(len(cycle)-1,-1,-1):
	if cycle[i] == curr:
		break
	csize += 1
'''
offset = len(cycle) - csize - 1
#print(cycle,csize+1, k , curr,offset)
k -= offset
k %= csize + 1
print(cycle[offset + k])
'''
k%=csize+1
count = 0
curr = 1
count = 0
while count != k:
	curr = dic[count]
	count+=1
exit(print(curr))

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 = []
visited = [False]*(10**6)
curr = 1
if k <= n:
	curr = 1
	count = 0
	while count != k:
		curr = dic[count]
		count+=1
	exit(print(curr))
while True:
	cycle.append(curr)
	visited[curr] = True
	curr = dic[curr]
	#k -= 1
	if visited[curr]:
		break

csize = 0
for i in range(len(cycle)-1,-1,-1):
	if cycle[i] == curr:
		break
	csize += 1

offset = len(cycle) - csize - 1
#print(cycle,csize+1, k , curr,offset)
k -= offset
k %= csize + 1
print(cycle[offset + k])

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 = []
visited = [False]*(10**6)
curr = 1
while True:
	cycle.append(curr)
	visited[curr] = True
	curr = dic[curr]
	#k -= 1
	if visited[curr]:
		break

csize = 0
ind = 0
for i in range(len(cycle)-1,-1,-1):
	if cycle[i] == curr:
		ind = i
		break
	csize += 1

offset = len(cycle) - csize - 1
#print(cycle,csize+1, k , curr)
k -= offset
k %= csize + 1
print(cycle[offset + k])

Submission status: Wrong Answer

Solution code:
import sys
n, k = map(int, input().split())
l = list(map(int, input().split()))
count = 0
li = [0 for _ in range(n)]
b = 1
for i in range(1,n + 1):
    print(b)
    if li[b-1] != 0:
        break
    li[b-1] = i
    b = l[b-1]
    count += 1
#     print('count',count)


d = count - li[b-1] + 1
intercep = li[b-1] -1
print('d', d, 'intercep', intercep)
if k <= intercep:
    b = 1
    for i in range(k):
        b = l[b-1]
    print(b)
    sys.exit()
    
mod = (k - intercep) % d
b = 1
for i in range(intercep + mod):
    print('b',b)
    b = l[b-1]
print(b)

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)
ind = 0
offset = 0
for i in range(len(cycle)):
	if cycle[i] == cyele:
		ind = i
		break
	offset+=1
#rem = k - offset
csize = len(cycle[ind:])
rem=k%csize

count = 0
curr = 1
while count != rem:
	curr = dic[curr]
	count+=1
print(curr)

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)
ind = 0
offset = 0
for i in range(len(cycle)):
	if cycle[i] == cyele:
		ind = i
		break
	offset+=1
rem = k - offset
csize = len(cycle[ind:])
rem=rem%csize

count = 0
curr = 1
while count != rem+offset:
	curr = dic[curr]
	count+=1
print(curr)

Submission status: Wrong Answer

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

flag = [False] * n

town = 1
towns = [1]
flag[0] = True

while(True):
    ntown = A[town-1]
    if flag[ntown-1]:
        l_s = towns.index(ntown)
        
        if k < l_s:
            print(towns[k-1])
            break
        else:
            l = towns[l_s:]
            k -= l_s
            print(l[k%len(l)])
            break
        
    else:
        flag[ntown-1] = True
        towns.append(ntown)
        town = ntown

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
		break
#print(cycle, cyind, len(cycle[:cyind]))
csize = len(cycle[cyind:])
offset  = len(cycle[:cyind])
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 = (int(x) for x in input().split())
A = [int(x) for x in input().split()]

flag = [False] * n

town = 1
towns = [1]
while(True):
    ntown = A[town-1]
    if flag[ntown-1]:
        l_s = towns.index(ntown)
        
        if k < l_s:
            print(towns[k-1])
            break
        else:
            l = towns[l_s:]
            k -= l_s
            print(l[k%len(l)])
            break
        
    else:
        flag[ntown-1] = True
        towns.append(ntown)
        town = ntown

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

visited = [1] + [0] * (N-1)
first_dist = [0] * N
turn = 0
pos = 0
for i in range(N+1):
    turn += 1
    pos = A[pos]
    if visited[pos] :
        loop = turn - first_dist[pos]
        break
    else:
        visited[pos] = 1
        first_dist[pos] = turn

loop_path = [pos]
for i in range(loop):
    pos = A[pos]
    loop_path.append(pos)
pos = loop_path.pop()
print(loop_path[(K - first_dist[pos])%loop] + 1)

Submission status: Wrong Answer

Solution code:
def resolve():
    n, k = map(int, input().split())
    g = list(map(int, input().split()))
    disc = [-1]*(n+1)
    s = []
    v = 1
    c = 0
    while disc[v] == -1:
        disc[v] = len(s)
        s.append(v)
        v = g[v-1]
        c = len(s) - disc[v]
    l = len(s)
    if l > k:
        print(s[k])
    else:
        r = (k - disc[v]) % c
        for i in range(r):
            v = g[v]
        print(v)
resolve()

Submission status: Wrong Answer

Solution code:
import collections
import numpy as np
import sys
import copy

N,K=map(int,input().split())
A=list(map(int,input().split()))
for i in range(N):
    A[i]-=1
B=[0]
visited=[0]*N
visited[0]=1
Flag=True
for i in range(0,N*2):
    next=A[B[i]]
    if visited[next]==0:
        B.append(next)
        visited[next]=1
    else:
        C=B[B.index(next):]
        num=B.index(next)
        break
#print(C)
if K>num:
    Ans=(K-num)%len(C)
    print(C[Ans]+1)
else:
    print(B[K])

Submission status: Wrong Answer

Solution code:
import collections
import numpy as np
import sys
import copy

N,K=map(int,input().split())
A=list(map(int,input().split()))
for i in range(N):
    A[i]-=1
B=[0]
visited=[0]*N
visited[0]=1
Flag=True
for i in range(0,N*2):
    next=A[B[i]]
    if visited[next]==0:
        B.append(next)
        visited[next]=1
    else:
        C=B[B.index(next):]
        num=B.index(next)
        break
#print(C)
if K>=len(C):
    Ans=(K-num)%len(C)
    print(C[Ans]+1)
else:
    print(B[K])

Submission status: Wrong Answer

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

def rec(now, k):
    if k == 0:
        return now
    if k == 1:
        return A[now]

    log2k = len(bin(k)) - 3

    for i in range(log2k):
        now = A[A[now]]

    return rec(now, k - 2 ** log2k)

print(rec(1, K))

Submission status: Wrong Answer

Solution code:
import collections
import numpy as np
import sys
import copy

N,K=map(int,input().split())
A=list(map(int,input().split()))
for i in range(N):
    A[i]-=1
B=[0]
visited=[0]*N
visited[0]=1
Flag=True
for i in range(0,N*2):
    next=A[B[i]]
    if visited[next]==0:
        B.append(next)
        visited[next]=1
    else:
        C=B[B.index(next):]
        num=B.index(next)
        break
#print(C)
Ans=(K-num)%len(C)
print(C[Ans]+1)

Submission status: Wrong Answer

Solution code:
def solve():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    now = 0
    hoge= []
    bit = 0
    piyo = 0
    huga = []
    while True:
        hoge.append(now)
        now = a[now]-1
        if now in hoge:
            huga = hoge[hoge.index(now):len(hoge)]
            piyo = hoge.index(now)
            bit = 1
            break
    ans = huga[(k-piyo)%len(huga)]
    print(ans+1)
    return 0
 
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

visited = [False]*(2*10**5 + 10)
val = 1
count = 0
hold = k
while hold > 0 :
    if visited[val - 1] or val == t[val - 1]:
        break
    visited[val - 1] = True
    val = t[val - 1]
    # print('a', val)
    count += 1
    hold -= 1
if hold == 0:
    print(t[val - 1])
    exit(0)
if val == t[val - 1]:
    print(val)
    exit(0)
count0 = count
count = 1
newval = val
start = t[newval - 1]
while start != newval:
    count += 1
    start = t[start - 1]
# print('count', count, 'count0', count0)
if t[val - 1] == t[0]:
    temp = k
else:
    temp = k - count0
# print('temp', temp)
jump = (temp) % (count)
# print('jump', jump)
# print('val', val)
ans = val
for i in range(jump):
    ans = t[ans - 1]
    # print('b', ans)
print(ans)

Submission status: Wrong Answer

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

visited = [False]*(2*10**5 + 10)
val = 1
count = 0
hold = k
while hold > 0 :
    if visited[val] or val == t[val - 1]:
        break
    visited[val] = True
    val = t[val - 1]
    # print('a', val)
    count += 1
    hold -= 1
if hold == 0:
    print(t[val - 1])
    exit(0)
if val == t[val - 1]:
    print(val)
    exit(0)
count0 = count
count = 1
newval = val
start = t[newval - 1]
while start != newval:
    count += 1
    start = t[start - 1]
# print('count', count, 'count0', count0)
if t[val - 1] == t[0]:
    temp = k
else:
    temp = k - count0
# print('temp', temp)
jump = (temp) % (count)
# print('jump', jump)
# print('val', val)
ans = val
for i in range(jump):
    ans = t[ans - 1]
    # print('b', ans)
print(ans)

Submission status: Wrong Answer

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

visited = [False]*(2*10**5 + 10)
val = 1
count = 0
hold = k
while hold > 0 :
    if visited[val] or val == t[val - 1]:
        break
    visited[val] = True
    val = t[val - 1]
    # print('a', val)
    count += 1
    hold -= 1
if hold == 0:
    print(t[val - 1])
    exit(0)
count0 = count
count = 1
newval = val
start = t[newval - 1]
while start != newval:
    count += 1
    start = t[start - 1]
# print('count', count, 'count0', count0)
if t[val - 1] == t[0]:
    temp = k
else:
    temp = k - count0
# print('temp', temp)
jump = (temp) % (count)
# print('jump', jump)
# print('val', val)
ans = val
for i in range(jump):
    ans = t[ans - 1]
    # print('b', ans)
print(ans)

Submission status: Wrong Answer

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

visited = [False]*(2*10**5 + 10)
val = 1
count = 0
hold = k
while hold > 0 :
    if visited[val] or val == t[val - 1]:
        break
    visited[val] = True
    val = t[val - 1]
    # print('a', val)
    count += 1
    hold -= 1
if hold == 0:
    print(t[val - 1])
    exit(0)
count0 = count
count = 1
newval = val
start = t[newval - 1]
while start != newval:
    count += 1
    start = t[start - 1]
# print('count', count, 'count0', count0)
if t[val - 1] == t[0]:
    temp = k
else:
    temp = k - count0
# print('temp', temp)
jump = (temp) % (count)
# print('jump', jump)
# print('val', val)
ans = val
for i in range(jump):
    ans = t[ans - 1]
    print('b', ans)
print(ans)

Submission status: Wrong Answer

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

visited = [False]*(2*10**5 + 10)
val = 1
count = 0
hold = k
while hold > 0 :
    if visited[val] or val == t[val - 1]:
        break
    visited[val] = True
    val = t[val - 1]
    # print('a', val)
    count += 1
    hold -= 1
count0 = count
count = 1
newval = val
start = t[newval - 1]
while start != newval:
    count += 1
    start = t[start - 1]
# print('count', count, 'count0', count0)
if t[val - 1] == t[0]:
    temp = k
else:
    temp = k - count0
# print('temp', temp)
jump = (temp) % (count)
# print('jump', jump)
# print('val', val)
ans = val
for i in range(jump):
    ans = t[ans - 1]
    print('b', ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
cnt = 0
visited = []
next = 1
while True:
  present = next
  cnt += 1
  visited.append(present)
  next = A[present-1]
  if next in visited:
    ind = visited.index(next)
    l = len(visited[ind:])
    break

rest = (K-cnt) % l
present = visited[ind + rest]
print(present)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
visit = {1}
route = [1]
i = 0
count = 0
bforlp = 0

while True:
  count += 1
  route.append(a[route[i]-1])
  visit.add(a[route[i]-1])
  visit_list = list(visit)
  if visit_list != route:
    bforlp = route.index(route[count])
    loop = list(route[bforlp:count:1])
   # print("{}".format(route[::1]))
   # print("loop")
    break
  #print("{}".format(route[::1]))
  i+=1
#print("count = {}, beforeloop = {}, loop = {}".format(count,bforlp,loop[::1]))
print("{}".format(loop[(k - bforlp) % (count-bforlp)]))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[0]+list(map(int,input().split()))
co=[0]*200100
no=1
nn=1
roop=0
tt=0
while True:
    if co[no]!=0:
        roop=nn-co[no]
        tt=co[no]-1
        break
    co[no]=nn
    no=a[no]
    nn+=1
if k>=tt:
    k%=roop

ans=1
for i in range(k):
    ans=a[ans]
print(ans)

Submission status: Wrong Answer

Solution code:
def test():
    import numpy as np
    for i in range(10):
        N = np.random.randint(100000, 200000)
        K = np.random.randint(10**17, 10**18)
        to_list = np.random.randint(1, N+1, [N])
        pre_loop, loop = loop_find(to_list)
        find(pre_loop, loop, K)
    
def find(pre_loop, loop, K):
    if K < len(pre_loop):
        print(pre_loop[K])
    else:
        K -= len(pre_loop) 
        K %= len(loop)

        #print("K", K)
        print(loop[K-1])


class Portals:
    def __init__(self, portals):
        self.portals = portals
    def __getitem__(self, i):
        return self.portals[i - 1]

def loop_find(to_list):
    
    portals = Portals(to_list)
    next_town = portals[1]
    cy = []
    cy_set = set()
    while next_town not in cy_set:
        cy.append(next_town)
        cy_set.add(next_town)
        next_town = portals[next_town]
    double = next_town

    i = cy.index(double)
    pre_loop = cy[:i]
    loop = cy[i:]

    #print(pre_loop, loop)
    return pre_loop, loop



def main():

    #test()

    N,K = map(int, input().split())
    to_list = list(map(int, input().split()))
    
    pre_loop, loop = loop_find(to_list)
    
    find(pre_loop, loop, K)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

route = [0] * (n+1)
#b->ループの始まり
#c->ループの終わり
b = 0
c = 0

nx = 1
for i in range(n):
    if nx not in set(route):
        route[i] += nx
    else:
        c = n+1 - route.count(0)
        b = route.index(nx)
        break
        
    nx = a[nx-1]
    #print(nx)
    #print(route)
    
if len(route)-route.count(0)>= k:
    print(route[k-1])
else:
    #print("b",b)
    #print("c",c)
    loop = route[b:c]
    print(loop[(k-b) % (c-b)])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
visit = {1}
route = [1]
count = 0
bforlp = 0

for i in range(n):
  count += 1
  route.append(a[route[i]-1])
  visit.add(a[route[i]-1])
  visit_list = list(visit)
  if visit_list != route:
    bforlp = route.index(route[count])
    loop = list(route[bforlp:count:1])
   # print("{}".format(route[::1]))
   # print("loop")
    break
 # print("{}".format(route[::1]))
#print("count = {}, beforeloop = {}, loop = {}".format(count,bforlp,loop[::1]))
print("{}".format(loop[(k - bforlp) % (count-bforlp)]))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
c = 1
P = [1]
for i in range(1,k+1):
    c = A[c-1]
    if i == k:
        print(c)
        exit(0)
    if c in P:
        roop_start = P.index(c)
        roop_num = i- roop_start
        a = (k - roop_start) %  roop_num
        b = P[a + roop_start]
        print(b)
    P.append(c)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[0]+list(map(int,input().split()))
co=[0]*200100
no=1
nn=1
roop=0
while True:
    if co[no]!=0:
        roop=nn-co[no]
        break
    co[no]=nn
    no=a[no]
    nn+=1
k%=roop
ans=1
for i in range(k):
    ans=a[ans]
print(ans)

Submission status: Wrong Answer

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

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

route = [1]

end = False
i = 0

if a[0] == 1:
    ans = 1
else:
  while not end:
    if a[i] not in route:
      route.append(a[i])
      i = a[i] -1
      # route[1].append(1)
    else:
      position = route.index(a[i])
      # print(position)
      break
    if len(route) == n:
      overhead = 0
      end = True

  overhead = position
  cycle_length = len(route) - overhead

  # print(route)
  # print(overhead)
  # print(cycle_length)
  if k <= overhead:
    ans = route[k-1]
  else:
    end_position = (k-overhead) % cycle_length
    # print(end_position)

    ans = route[overhead + end_position]

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
print(a[k%len(set(a))+len(a)-len(set(a))])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
def f(x):
    return A[x-1]
c = 1
P = [1]
 
for i in range(1,k+1):
    c = f(c)
    if c in P:
        roop_start = P.index(c)
        roop_num = i- roop_start
        break
    P.append(c)
    if i == k:
        roop_start = 0
        roop_num = 1
a = (k - roop_start) %  roop_num
b = P[a + roop_start]
print(b)

Submission status: Wrong Answer

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

route = []
#b->ループの始まり
#c->ループの終わり
b = 0
c = 0

nx = 1
for i in range(n):
    if nx not in set(route):
        route.append(nx)
    else:
        c = len(route)-1
        b = route.index(nx)
        break
        
    nx = a[nx-1]
    #print(nx)
    #print(route)
    
if len(route) >= k:
    print(route[k-1])
else:
    #print("b",b)
    #print("c",c)
    loop = route[b:c+1]
    print(loop[(k-b) % (c-b+1)])

Submission status: Wrong Answer

Solution code:
def test():
    import numpy as np
    for i in range(10):
        N = np.random.randint(100000, 200000)
        K = np.random.randint(10**17, 10**18)
        to_list = np.random.randint(1, N+1, [N])
        pre_loop, loop = loop_find(to_list)
        find(pre_loop, loop, K)
    
def find(pre_loop, loop, K):
    if K < len(pre_loop):
        print(pre_loop[K])
    else:
        K -= len(pre_loop) 
        K %= len(loop)

        #print("K", K)
        print(loop[K-1])


class Portals:
    def __init__(self, portals):
        self.portals = portals
    def __getitem__(self, i):
        return self.portals[i - 1]

def loop_find(to_list):
    
    portals = Portals(to_list)
    next_town = portals[1]
    cy = []
    while next_town not in cy:
        cy.append(next_town)
        next_town = portals[next_town]
    double = next_town

    i = cy.index(double)
    pre_loop = cy[:i]
    loop = cy[i:]

    #print(pre_loop, loop)
    return pre_loop, loop



def main():

    #test()

    N,K = map(int, input().split())
    to_list = list(map(int, input().split()))
    
    pre_loop, loop = loop_find(to_list)
    
    find(pre_loop, loop, K)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
 
a = list(map(int, input().split()))
 
route = [1]
 
end = False
i = 0
 
if a[0] == 1:
    ans = 1
else:
  while not end:
    if a[i] not in route:
      route.append(a[i])
      i = a[i] -1
      # route[1].append(1)
    else:
      position = route.index(a[i])
      # print(position)
      break
    if len(route) == n:
      overhead = 0
      end = True
 
  overhead = position
  cycle_length = len(route) - overhead
 
  # print(route)
  # print(overhead)
  # print(cycle_length)
 
  end_position = (k-overhead) % cycle_length
  # print(end_position)
 
  ans = route[overhead + end_position]
 
print(ans)

Submission status: Wrong Answer

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

def rec(now, k):
    # print(now, k)
    if k == 0:
        return now
    if k == 1:
        return A[now]
    return rec(rec(now, k % 2), k // 2)

print(rec(1, K))

Submission status: Wrong Answer

Solution code:
import sys
import os
import math
import bisect
import collections
import itertools
import heapq

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: x * y / math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, K = il()
    A = il()

    town = [False]*N
    town[0] = True
    tel = [0]*N
    nextT, cnt = 0, 0
    for n in range(N):
        nextT = A[nextT]-1
        cnt += 1
        if town[nextT]:
            break
        tel[n] = nextT+1
        town[nextT] = True

    mod = (K - cnt) % (len(tel)-cnt)
    print(tel[mod+1])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

route = []
#b->ループの始まり
#c->ループの終わり
b = 0
c = 0
tyofuku = []
nx = 1
for i in range(n):
    if nx not in route:
        route.append(nx)
    else:
        c = len(route)-1
        b = route.index(nx)
        break
        
    nx = a[nx-1]
    #print(nx)
    #print(route)
    
if len(route) >= k:
    print(route[k-1])
else:
    #print("b",b)
    #print("c",c)
    loop = route[b:c+1]
    print(loop[(k-b) % (c-b+1)])

Submission status: Wrong Answer

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

i = 0
T = []
S = set()
st = 0
while True:
    T.append(i)
    S.add(i)
    i = A[i]
    if i in S:
        st = T.index(i)
        break

# print(T)

idx = (K - st) % (len(T) - st)
print(T[st + idx] + 1)

Submission status: Wrong Answer

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

i = 0
T = []
S = set()
st = 0
while True:
    T.append(i)
    S.add(i)
    i = A[i]
    if i in S:
        st = T.index(i)
        break

print(T)

idx = (K - st) % (len(T) - st)
print(T[st + idx] + 1)

Submission status: Wrong Answer

Solution code:
import sys
from io import StringIO
import unittest

sys.setrecursionlimit(10**5*2)

def search(u, n, g, disc, k):
    if disc[u] != None:
        circles = n - disc[u]
        v = (k - disc[u]) % circles
        ans = u
        for i in range(v):
            ans = g[ans]
        return ans + 1
    disc[u] = n
    return search(g[u], n+1, g, disc, k)

def resolve():
    n, k = map(int, input().split())
    g = list(map(lambda x: int(x) - 1, input().split()))
    disc = [None]*n
    v = search(0, 0, g, disc, k)
    print(v)
resolve()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
sub = [0]*n
city = 0
all = []
roop = []
while sub[city] != 2:
  if sub[city] == 0:
    all.append(city)
  else:
    roop.append(city)
  sub[city] += 1
  city = A[city] -1
if len(roop)<=k:
  print(roop[(k-(len(all)-len(roop)))%len(roop)]+1)
else:
  print(all[k]+1)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
sub = [0]*n
city = 0
all = []
roop = []
while sub[city] != 2:
  if sub[city] == 0:
    all.append(city+1)
  else:
    roop.append(city+1)
  sub[city] += 1
  city = A[city] -1
if len(roop)<=k:
  print(roop[(k-(len(all)-len(roop)))%len(roop)])
else:
  print(all[k])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
log = []
visited = [False for i in range(n)]
now = a[0]-1
loop = False
for i in range(k-1):
    if visited[now] == True:
        loop = True
        break
    log.append(now)
    visited[now] = True
    now = a[now]-1
else:
    print(now+1)
if loop:
    for i in range(len(log)):
        if log[i] == now:
            first = len(log[:i])
            log_list = log[i:]
            loop_num = len(log_list)
            break
    print(log)
    k -= first
    k %= loop_num
    print(log_list[k-1]+1)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
sub = [0]*n
city = 0
all = []
roop = []
while sub[city] != 2:
  if sub[city] == 0:
    all.append(city+1)
  else:
    roop.append(city+1)
  sub[city] += 1
  city = A[city] -1
if len(roop)<k:
  print(roop[(k-(len(all)-len(roop)))%len(roop)])
else:
  print(all[k-1])

Submission status: Wrong Answer

Solution code:
import sys

temp = sys.stdin.readline().split(" ")
n = int(temp[0])
k = int(temp[1])

temp = sys.stdin.readline().split(" ")
telep = [int(i) for i in temp]

alreadyVisited = []

now = 1
piv = -1
counter = 0

for i in range(n):
    alreadyVisited.append(now)
    now = telep[now - 1]
    if now in alreadyVisited:
        piv = now
        break
    else:
        counter += 1
        pass

if piv != -1:
    index = alreadyVisited.index(piv)
    nl = alreadyVisited[index:]

    looptime = len(nl)
    beforeloop = index

    hoge = k - beforeloop

    _, h2 = divmod(hoge, looptime)

    print(nl[h2])
else:
    print(now)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(str,input().split()))
a=[0]+a

s="0"
t=1
for _ in range(1,n+1):
    s+=a[t]
    t=int(a[t])
#print(s)

record=[0]*(n+1)
flag=0
for i in range(1,n+1):
    t=int(s[i])
    if record[t]==0:
        record[t]=i
    else:
        flag=record[t]
        break

if flag==0:print(s[k%n])
else:
    if k<=n:print(s[k])
    else:
        s='0'+s[flag:i]  #上下保持一致，左边加上0
        #print(s)
        print(s[(k-(flag-1))%(i-1-flag+1)])  #去除非循环节部分后求余

Submission status: Wrong Answer

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

dic = {}
for key, a in zip(range(1, n+1), list(map(int,input().split()))):
    dic[key] = a
 
keynum = 0
log = [1]
next = dic[1]
log.append(next)
k -= 1
if k ==1:
    print(dic[next])
else:
    while k:
        next = dic[next]
        if next in log:
            keynum = log.index(next)
            k -= 1
            break
        log.append(next)
        k -= 1
    looplen = len(log[keynum:])
    res = k%looplen
    print(log[keynum+res])

Submission status: Wrong Answer

Solution code:
import numpy as np
import itertools
n, k = map(int, input().split())
a = list(map(int, input().split()))

now = 1
town_list = [0]*n
town_list[0] = 1
tn_list = [1]

while(1):
    next = a[now-1]
    now = next
    if town_list[now-1] == 0:
        town_list[now-1] = 1
        tn_list.append(now)

    else:
        idx = tn_list.index(now)
        initial = tn_list[:idx]
        cycle = tn_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=list(map(int,input().split()))

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

if n<k:
    ai=0
    aj=set()
    for i in range(n+1):
        ai=a[ai]
        if ai in aj:
            ic2=i
            ai2=ai
            break
        else:
            aj.add(ai)

    ai=0
    for i in range(n+1):
        ai=a[ai]
        if ai==ai2:
            ic1=i
            break

    icyc=ic2-ic1
    if icyc==1:
        k2=ic1+1
    else:
        k2=(k-ic1)%icyc+ic1
else:
    k2=k
    
ai=0
for i in range(k2):
    ai=a[ai]
        
print(ai+1)

Submission status: Wrong Answer

Solution code:
def main():
    n,k = map(int,input().split())
    a = list(map(int,input().split()))
    # k<=10**18 < 2**60
    r = 60
    idx = 0
    a = [i-1 for i in range(n)]
    for i in range(r):
        if (k>>i)&1 == 1:
            idx = a[idx]
        a = [a[a[j]] for j in range(n)]
    print(idx+1)    

if __name__ == '__main__':
    main()