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:
#create date: 2020-07-03 11:44

import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n, k = na()
    a = na()
    visited_time = [-1] * n
    visited_time[0] = 0
    x, t = 0, 0
    while True:
        x = a[x] - 1
        t += 1
        if visited_time[x] > 0:
            b = visited_time[x]
            r = t - visited_time[x]
            break
        else:
            visited_time[x] = t
    c = (k-b) % r
    print(visited_time.index(b+c) + 1)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
town=[0]*n
info=[1]
now=1
while town[now-1]==0:
    town[now-1]+=1
    now=a[now-1]
    info.append(now)
loop_list=list(info[info.index(now):-1])
loop_len=len(loop_list)
before_loop_list=list(info[:info.index(now)])
if k<=len(before_loop_list):
    print(before_loop_list[k-1])
else:
    print(loop_list[((k-len(before_loop_list))%loop_len)])

Submission status: Wrong Answer

Solution code:
n,k = (int(a) for a in input().split())
A = list(map(int,input().split()))
t = [1]
x = 1
for i in range(n) :
    t.append(A[x-1])
    x = t[-1] 
    if len(t) != len(set(t)) :
        break
l = [i for i , x in enumerate(t) if x == t[-1]]
k = k-l[0]
ans = k%(l[1]-l[0])
print(t[l[0] + ans])

Submission status: Wrong Answer

Solution code:
n,k = (int(a) for a in input().split())
A = list(map(int,input().split()))
t = [1]
x = 1
for i in range(n) :
    if A[x-1] not in t :
        t.append(A[x-1])
        x = t[-1] 
    else : 
        t.append(A[x-1])
        break
l = [i for i , x in enumerate(t) if x == t[-1]]
k = k-l[0]
ans = k%(l[1]-l[0])
print(t[l[0] + ans])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
G = [[] for _ in range(N)]
for i, a in enumerate(map(int, input().split())):
    G[i] = a - 1
now = 0
seen = set()
#ループが始まる町とそれまでの移動回数iを求める
for i in range(1, K+1):
    seen.add(now)
    now = G[now]
    if now in seen:
        break
#ループを移動回数jを求める。
seen = set()
for j in range(1,K+1):
    seen.add(now)
    now = G[now]
    if now in seen:
        break
#ループの長さで割った余りｒを求める。
r = (K - i) % j
for _ in range(r):
    now = G[now]
print(now + 1)

Submission status: Wrong Answer

Solution code:
import sys

def main():
    n, k = map(int, sys.stdin.buffer.readline().split())
    a = [0] + list(map(int, sys.stdin.buffer.readline().split()))

    visited = [0]*(n+1)
    town = 1
    for i in range(k+1):
        town = a[town]
        if visited[town] == 0:
            visited[town] = i
        else:
            print(visited.index( (k - visited[town])%(i - visited[town]) + visited[town]))
            exit()
    print(town)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys

def main():
    n, k = map(int, sys.stdin.buffer.readline().split())
    a = list(map(int, sys.stdin.buffer.readline().split()))

    towns = [1]
    i = 0
    while a[i] not in towns and len(towns) < k:
        towns.append(a[i])
        i = a[i] - 1
    if len(towns) == k:
        print(towns[-1])
    else:
        ti = towns.index(a[i])
        print(towns[(k - ti)%(len(towns) - ti) + ti])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

towns = []
i = 0
while a[i] not in towns:
    towns.append(a[i])
    i = a[i] - 1
ti = towns.index(a[i])
print(towns[(k - ti)%(len(towns))-1])

Submission status: Wrong Answer

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

towns = []
i = 0
while a[i] not in towns:
    towns.append(a[i])
    i = a[i] - 1
ti = towns.index(a[i])
print(a[(k - ti)%(len(towns) - ti)+ti])

Submission status: Wrong Answer

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

before=0
roop=0
cnt={}
nxt=1
mp = []
for i in range(1, k+1):
    mp.append(nxt)
    if nxt not in cnt:
        cnt[nxt] = i
        nxt = a[nxt]
    else:
        roop = i - cnt[nxt]
        before = cnt[nxt]-1
        break
    roop=0
if roop == 0:
    print(mp[k-1])
    exit()
print(mp[before + (k-before)%roop])

Submission status: Wrong Answer

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

before=0
roop=0
cnt={}
nxt=1
mp = []
for i in range(1, k+1):
    mp.append(nxt)
    if nxt not in cnt:
        cnt[nxt] = i
        nxt = a[nxt]
    else:
        roop = i - cnt[nxt]
        before = cnt[nxt]-1
        break
    roop=1
    before=0

print(mp[before + (k-before)%roop])

Submission status: Wrong Answer

Solution code:
import sys

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A = [0] + A
town = 1
visited = [0]*(N+1)

for i in range(1, K+1):
    town = A[town]
    if visited[town] == 0:
        visited[town] = i
    else:
        loop = i - visited[town]
        town = (K - visited[town]) % loop + visited[town]
        break
print(town)

Submission status: Wrong Answer

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

#%%
ans = 0
total = 0

right = 0
for left in range(n):
    while total < k: # kを超えるまで続ける
        if right == n: # kを超えずに右終端まで来たとき
            break
        else:
            total += A[right]
            right += 1  # 右端を一つ進める

        # 終端まで行ってもkを超えないときは終了
        if total < k:
            break

        ans += n - right + 1 # kがrightまでの範囲でkをこえていれば、あとはいくら足しても条件を満たす
        total -= A[left] # 左端を一つ進める
print(ans)

Submission status: Wrong Answer

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

i = 0
telp = [1]
while True:
    if A[i] not in telp:
        telp.append(A[i])
        i = A[i] - 1
    else:
        telp.append(A[i])
        break

c = cl.Counter(telp)
lll = telp.index(c.most_common()[0][0])

K -= lll
ans = (K % len(telp[lll + 1:]))
print(telp[ans + lll])

Submission status: Wrong Answer

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

field = [(i, j) for i, j in zip(range(n), list(map(int, input().split())))]

#%%
town = 0 # 町は1始まりだが、indexの都合上-1する
order = 0 # 訪問順序
visited = {town: order} # 訪問済みの街を格納

while True:
    trans = field[town][1] - 1 # 現在のtownにあるテレポーターの移動先
    n_town = field[trans][0] # 遷移先の町. これはリストのindexになってるので-1しなくてよい
    if n_town not in visited.keys():
        order += 1
        visited[n_town] = order  # 訪問済みにする
    else: # 訪問済みを踏んだ時 TODO この実装だとループ内の数が数えられない
        st_circ = visited[n_town]
        break
    town = n_town

#%%
# ループ開始前まで
vk = list(visited.keys())
mae = len(vk[:st_circ])
circ = len(vk[st_circ:])

if k < mae:
    ans = vk[k-1]
else:
    idx = (k - mae) % circ
    ans = vk[idx + mae]

print(ans+1)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
from collections import defaultdict



#input = sys.stdin.readline

def main():
    N, K = map(int,input().split())
    A = list(map(int,input().split()))
    A = [0] +A  #transporter
    visited = [0] * (N+1)
    town = 1
    cycle = 0
    for i in range(1,K+1):
        town = A[town]
        if visited[town] ==0:
            visited[town] = i
        else:
            cycle = i - visited[town]
            place = (K - visited[town]) % cycle + visited[town]
            for j in range(N+1):
                if visited[j] == place:
                    print(j)
                    exit()
    print(town)

    print(town)



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

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

tere = {}
for i in range(n):
    tere[i+1] = a[i]

mati = deque([1])

tugi = 1
for i in range(1, k+1):
    if tere[tugi] not in mati:
        mati.append(tere[tugi])
    else:
        mati.append(tere[tugi])
        break
    tugi = tere[tugi]
else:
    print(tugi)
    exit()
print("No")

Submission status: Wrong Answer

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

tere = {}
for i in range(n):
    tere[i+1] = a[i]

mati = [1]
tugi = 1
for i in range(1, k+1):
    if tere[tugi] not in mati:
        mati.append(tere[tugi])
    else:
        mati.append(tere[tugi])
        break
    tugi = tere[tugi]
else:
    print(tugi)
    exit()
print("Yes")

Submission status: Wrong Answer

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

tere = {}
for i in range(n):
    tere[i+1] = a[i]

mati = [1]
tugi = 1
for i in range(1, k+1):
    if tere[tugi] not in mati:
        mati.append(tere[tugi])
    else:
        mati.append(tere[tugi])
        break
    tugi = tere[tugi]
else:
    print(tugi)
    exit()

cnta = len(mati)-1
i = mati.index(mati[-1]) + 1
cntroop = cnta - i + 1
roop = mati[i:]

print(roop[(k-cnta)%cntroop-1])

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
i = list(map(int, input().split()))
count = 0
town = 1

while(count <= K):
    count += 1
    town = i[town - 1]

print(town)

Submission status: Wrong Answer

Solution code:
n, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
memo = [1]
i = a[0]
while i not in memo:
    memo.append(i)
    i = a[i-1]
    k -= 1
p = i
k -= 1
memo = [i]
i = a[i-1]
while p != i:
    memo.append(i)
    i = a[i-1]
print(memo[k % len(memo)])

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
L = list(map(int,input().split()))
li = [L[0]]
a = L[0]
s = set()
s.add(a)
i = 0
while i <=n+1:
    a = L[a-1]
    li.append(a)
    
    if a in s:
        break
    i +=1
    s.add(a)
cnt = 0
for i in range(n):
    if li[-(i+2)] == li[-1]:
        cnt =i
        break
length = cnt+1
pre = len(li)-1-length
post = len(li) -pre
#print(pre)

#c = li[:pre]
d = li[pre:]
#print(c,d)
print(d[(m-pre)%length-1])

Submission status: Wrong Answer

Solution code:
n, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
memo = [1]
i = a[0]
while i not in memo:
    memo.append(i)
    i = a[i-1]
    k -= 1
p = i
k -= 1
memo = []
while p not in memo:
    i = a[i-1]
    memo.append(i)
print(memo[(k-1) % len(memo)])

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
L = list(map(int,input().split()))
li = [L[0]]
a = L[0]
s = set()
s.add(a)
i = 0
while i <=n+1:
    a = L[a-1]
    li.append(a)
    
    if a in s:
        #print(s,a,li)
        break
    i +=1
    s.add(a)
    
pre = li.index(li[-1])
li.pop()
c = li[:pre]
d = li[pre:]
#print(c,d)
print(d[(m-len(c))%len(d)-1])

Submission status: Wrong Answer

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

t_zero = [-1] * N

t = 0
a = 1

while t_zero[a-1] == -1:
  if t == K:
    break
  t_zero[a-1] = t
  t += 1
  a = A[a-1]
  #print(t_zero,a)
else:
  k = (K - t_zero[a-1]) % (t - t_zero[a-1])
  a = A[t_zero[a-1] + k]
print(a)

Submission status: Wrong Answer

Solution code:
import numpy as np
def resolve():
    N,K = map(int,input().split())
    A = list(input().split())
    current_p = 0
    loop_length = 0
    loop_start = 0
    Nlist = - np.ones(N, dtype = int)
    Nlist[0] = 0
    for i in range(1, N + 1):
        next_p = int(A[current_p]) - 1
        if Nlist[next_p] == -1:
            current_p = next_p
            Nlist[next_p] = i
        else:
            loop_start = Nlist[next_p]
            loop_length = i - loop_start
            break
    if loop_length != 0:
        ans =  loop_start + (K - loop_start) % loop_length
    else:
        ans = K 
    print(np.where(Nlist == ans)[0][0] + 1)
resolve()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.insert(0,0)
x = 1
past = []
cnt = 0
while True:
    if a[x] in past:
        past.append(a[x])
        break
    else:
        past.append(a[x])
        x = a[x]
#print(past)
for i in range(len(past)):
    if past[len(past)-1] == past[i]:
        x = len(past)-1-i
        break
#print(x)
b = ((k-(len(past)-1-x))%x)
#print(b)
#print(past)
del past[:len(past)-1-x]
#print(past)
print(past[b-1])

Submission status: Wrong Answer

Solution code:
import numpy as np
def resolve():
    N,K = map(int,input().split())
    A = list(input().split())
    current_p = 0
    loop_length = 0
    loop_start = 0
    Nlist = - np.ones(N, dtype = int)
    Nlist[0] = 0
    for i in range(1, N + 1):
        next_p = int(A[current_p]) - 1
        if Nlist[next_p] == -1:
            current_p = next_p
            Nlist[next_p] = i
        else:
            loop_start = Nlist[next_p]
            loop_length = i - loop_start
            break
    ans =  loop_start + (K - loop_start) % loop_length 
    print(np.where(Nlist == ans)[0][0] + 1)
resolve()

Submission status: Wrong Answer

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

seen = [False] * (n+1)
visited = []
nxt = 1
cnt = 0
while seen[nxt] == 0 or cnt == k:
    seen[nxt] = True
    visited.append(nxt)
    nxt = a[nxt]
    cnt += 1

if cnt == k:
    print(visited[-1])
    exit()
    
idx = visited.index(nxt)
print(visited[idx:][(k-idx)%(len(visited)-idx)])

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]
B=[]
cnt=[0]*n
m=0
for i in range(n+1):
    B.append(m)
    cnt[m]+=1
    if cnt[m]>1:
        break
    m=A[m]
c=len(B)-(B.index(m)+1)
print(1+B[(k-B.index(m))%c + B.index(m)])

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]
B=[0]*(n+1)
cnt=[0]*n
m=0
for i in range(n+1):
    B[i]=m
    cnt[m]+=1
    if cnt[m]>1:
        break
    m=A[m]
C=[i for i,x in enumerate(B) if x==m]
c=abs(C[0]-C[1])
print(1+B[(k-B.index(m))%c + B.index(m)])

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]
B=[0]*n
cnt=[0]*n
m=0
for i in range(n):
    B[i]=m
    cnt[m]+=1
    if cnt[m]>1:
        break
    m=A[m]
C=[i for i,x in enumerate(B) if x==m]
if len(C)>1:
    c=abs(C[0]-C[1])
else:
    c=n
print(1+B[((k-B.index(m))%c + B.index(m))%c])

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]
B=[0]*n
cnt=[0]*n
m=0
for i in range(n):
    B[i]=m
    cnt[m]+=1
    if cnt[m]>1:
        break
    m=A[m]
C=[i for i,x in enumerate(B) if x==m]
if len(C)>1:
    c=abs(C[0]-C[1])
else:
    c=n
print(1+B[(k-B.index(m))%c + B.index(m)])

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]
B=[0]*n
cnt=[0]*n
m=0
for i in range(n):
    B[i]=m
    cnt[m]+=1
    if cnt[m]>1:
        break
    m=A[m]
C=[i for i,x in enumerate(B) if x==m]
c=abs(C[0]-C[1])
print(1+B[(k-B.index(m))%c + B.index(m)])

Submission status: Wrong Answer

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

B = [0]
visited = [False] * n
for i, b in enumerate(B):
    if visited[b]:
        break
    visited[b] = True
    B.append(A[b])

j = B.index(b)
r = (k - j) % (i - j)
ans = B[j:][r] + 1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import time

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

queue = {}

start = -1
count = 1

now_town = 1

for i in range(K):
    if town_list[now_town - 1] in queue:
        start = town_list[now_town - 1]
        break
    queue[town_list[now_town - 1]] = count
    now_town = town_list[now_town - 1]
    count += 1

if start == -1:
    print(now_town)
    sys.exit()

roop = len(queue) - (queue[start] - 1)
before_len = len(queue) - roop

print(list(queue.keys())[(K - before_len) % roop + before_len - 1])

Submission status: Wrong Answer

Solution code:
import sys
import time

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

queue = {}

start = -1
count = 1

now_town = 1

for i in range(N):
    if town_list[now_town - 1] in queue:
        start = town_list[now_town - 1]
        break
    queue[town_list[now_town - 1]] = count
    now_town = town_list[now_town - 1]
    count += 1

if start == -1:
    print(queue[now_town])
    sys.exit()

roop = len(queue) - (queue[start] - 1)
before_len = len(queue) - roop

print(list(queue.keys())[(K - before_len) % roop + before_len - 1])

Submission status: Wrong Answer

Solution code:
import sys
import time

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

queue = {}

start = -1
count = 1

now_town = 1

for i in range(N):
    if town_list[now_town - 1] in queue:
        start = town_list[now_town - 1]
        break
    queue[town_list[now_town - 1]] = count
    now_town = town_list[now_town - 1]
    count += 1

if start == -1:
    print(queue[now_town])
    sys.exit()

before_len = len(queue) - queue[start] - 1
roop = len(queue) - before_len - 1

print(list(queue.keys())[(K - before_len) % roop + before_len])

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
u = [-1] * (n+1)
c = 0  #回数
g = 1  #位置
p = 0
while 1:
    if u[g] == -1:
        u[g] = c
        g = a[g-1]
        c += 1
    else:
        p = c - u[g]
        break
#print(u[g] + ((k - u[g]) % p))
#print(u.index((u[g] + ((k - u[g]) % p))))
#print(u)
pl = k - u[g]
cl = u[g] + ((k - u[g]) % p)
m = 1
for i in range(cl):
    m = a[m-1]
print(m)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np


def solve(N, K, xs):
    "void()"
    town_to_time = np.zeros(N, np.uint16)
    time_to_town = np.zeros(N, np.uint16)
    cur = 1
    for i in range(K):
        cur = xs[cur - 1]
        # print(town_to_time, time_to_town, cur)
        if town_to_time[cur - 1]:
            # visited before
            period = i + 1 - town_to_time[cur - 1]
            rest = K - i - 1
            rest %= period
            # print(rest, town_to_time[cur - 1], town_to_time[cur - 1] + rest)
            print(time_to_town[town_to_time[cur - 1] + rest - 1])
            return

        town_to_time[cur - 1] = i + 1
        time_to_town[i] = cur
    print(cur)


IN1 = """
4 5
3 2 4 1
"""

IN2 = """
6 727202214173249351
6 5 2 5 3 2
"""

IN3 = """
6 100
2 3 4 5 6 1
"""


def main():
    """
    >>> as_input(IN1)
    >>> main()
    4
    >>> as_input(IN2)
    >>> main()
    2
    >>> as_input(IN3)
    >>> main()
    5
    """
    N, K = map(int, input().split())
    xs = np.array(list(map(int, input().split())))
    solve(N, K, xs)


def _test():
    import doctest
    as_input(IN1)
    main()
    doctest.testmod()


def as_input(s):
    "use in test, use given string as input file"
    import io
    global read, input
    f = io.StringIO(s.strip())
    input = f.readline
    read = f.read


USE_NUMBA = False
if (USE_NUMBA and sys.argv[-1] == 'ONLINE_JUDGE') or sys.argv[-1] == '-c':
    print("compiling")
    from numba.pycc import CC
    cc = CC('my_module')
    cc.export('solve', solve.__doc__.strip().split()[0])(solve)
    cc.compile()
    exit()
else:
    input = sys.stdin.buffer.readline
    read = sys.stdin.buffer.read

    if (USE_NUMBA and sys.argv[-1] != '-p') or sys.argv[-1] == "--numba":
        # -p: pure python mode
        # if not -p, import compiled module
        from my_module import solve  # pylint: disable=all
    elif sys.argv[-1] == "-t":
        _test()
        sys.exit()
    elif sys.argv[-1] != '-p' and len(sys.argv) == 2:
        # input given as file
        input_as_file = open(sys.argv[1])
        input = input_as_file.buffer.readline
        read = input_as_file.buffer.read

    main()

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np


def solve(N, K, xs):
    "void()"
    town_to_time = np.zeros(N, np.int16)
    time_to_town = np.zeros(N, np.int16)
    cur = 1
    for i in range(K):
        cur = xs[cur - 1]
        # print(town_to_time, time_to_town, cur)
        if town_to_time[cur - 1]:
            # visited before
            period = i + 1 - town_to_time[cur - 1]
            rest = K - i - 1
            rest %= period
            # print(rest, town_to_time[cur - 1], town_to_time[cur - 1] + rest)
            print(time_to_town[town_to_time[cur - 1] + rest - 1])
            return

        town_to_time[cur - 1] = i + 1
        time_to_town[i] = cur
    print(cur)


IN1 = """
4 5
3 2 4 1
"""

IN2 = """
6 727202214173249351
6 5 2 5 3 2
"""


def main():
    """
    >>> as_input(IN1)
    >>> main()
    4
    >>> as_input(IN2)
    >>> main()
    2
    """
    N, K = map(int, input().split())
    xs = np.array(list(map(int, input().split())))
    solve(N, K, xs)


def _test():
    import doctest
    as_input(IN1)
    main()
    doctest.testmod()


def as_input(s):
    "use in test, use given string as input file"
    import io
    global read, input
    f = io.StringIO(s.strip())
    input = f.readline
    read = f.read


USE_NUMBA = False
if (USE_NUMBA and sys.argv[-1] == 'ONLINE_JUDGE') or sys.argv[-1] == '-c':
    print("compiling")
    from numba.pycc import CC
    cc = CC('my_module')
    cc.export('solve', solve.__doc__.strip().split()[0])(solve)
    cc.compile()
    exit()
else:
    input = sys.stdin.buffer.readline
    read = sys.stdin.buffer.read

    if (USE_NUMBA and sys.argv[-1] != '-p') or sys.argv[-1] == "--numba":
        # -p: pure python mode
        # if not -p, import compiled module
        from my_module import solve  # pylint: disable=all
    elif sys.argv[-1] == "-t":
        _test()
        sys.exit()
    elif sys.argv[-1] != '-p' and len(sys.argv) == 2:
        # input given as file
        input_as_file = open(sys.argv[1])
        input = input_as_file.buffer.readline
        read = input_as_file.buffer.read

    main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
pl=list(map(int,input().split()))
cl=[0]*2*n
order=[]
next=0
for i in range(k):
  if cl[next]==0:
    cl[next]=1
    next=pl[next]-1
  elif cl[next]==1:
    cl[next]=2
    order.append(next+1)
    next=pl[next]-1
  else:
    kk=len(order)
    hasuu=cl.count(1)
    newk=k-hasuu
    ans=newk%kk
    print(order[ans])
    break
else:
  print(pl[next])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
pl=list(map(int,input().split()))
cl=[0]*2*n
order=[]
next=0
for i in range(k):
  if cl[next]==0:
    cl[next]=1
    next=pl[next]-1
  elif cl[next]==1:
    cl[next]=2
    order.append(pl[next])
    next=pl[next]-1
  else:
    kk=len(order)
    hasuu=cl.count(1)
    newk=k-hasuu
    ans=newk%kk
    print(order[ans])
    break
else:
  print(pl[next])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
pl=list(map(int,input().split()))
cl=[0]*2*n
order=[]
next=0
for i in range(k):
  if cl[next]==0:
    cl[next]=1
    next=pl[next]-1
  elif cl[next]==1:
    cl[next]=2
    order.append(pl[next])
    next=pl[next]-1
  else:
    kk=len(order)
    hasuu=order.count(1)
    newk=k-hasuu
    ans=newk%kk
    print(order[ans])
    break
else:
  print(pl[next])

Submission status: Wrong Answer

Solution code:
import sys

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

roop = 0
queue = [1]

start = -1

for now_town in queue:
    if town_list[now_town - 1] in queue:
        start = town_list[now_town - 1]
        break
    queue.append(town_list[now_town - 1])

if start == -1:
    print(queue[K])
    sys.exit()

before_len = queue.index(start)
roop = len(queue) - before_len

print(queue[before_len + (K - before_len) % roop])

Submission status: Wrong Answer

Solution code:
import sys

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

roop = 0
queue = [1]

start = -1

for now_town in queue:
    if not town_list[now_town - 1] in queue:
        queue.append(town_list[now_town - 1])

    # 循環した
    else:
        start = town_list[now_town - 1]
        break

if start == -1:
    print(queue[K])
    sys.exit()

before_len = queue.index(start)
roop = len(queue) - before_len

print(queue[before_len + (K - before_len) % roop])

Submission status: Wrong Answer

Solution code:
import sys

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

graph = {}
for index, value in enumerate(input().split()):
    graph[index + 1] = int(value)

queue = [1]
now_pos = 1
start = 0

for i in range(N + 1):
    next_pos = graph[now_pos]
    if not next_pos in queue:
        queue.append(next_pos)
        now_pos = next_pos

    # 循環した
    else:
        start = next_pos
        break

before_len = len(queue[:queue.index(start)])
roop = queue[queue.index(start):]

for i in range(len(roop)):
    if (K - before_len) % len(roop) == i:
        print(roop[i])
        sys.exit()

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
u = [-1] * (n+1)
c = 0
g = 1
p = 0
while 1:
    if u[g] == -1:
        u[g] = c
        g = a[g-1]
        c += 1
    else:
        p = c - u[g]
        break
print(u.index((u[g] + ((k - u[g]) % p))))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = list('n' for i in range(n))
b[0] = 'y'
c = 1
d = []

for i in range(k):
    c = a[c - 1]
    if b[c - 1] == 'y':
        j = c
        d.append(a[j - 1])
        j = a[j - 1]
        while True:
            if d[0] == a[j - 1]:
                m = len(d)
                print(d[(k - i + 1) % m])
                exit(0)
            else:
                d.append(a[j - 1])

                j = a[j - 1]

    else:
        b[c - 1] = 'y'

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = list('n' for i in range(n))
c = 1
d = []

for i in range(k):
    c = a[c - 1]
    if b[c - 1] == 'y':
        j = c
        d.append(a[j - 1])
        j = a[j - 1]
        while True:
            if d[0] == a[j - 1]:
                m = len(d)
                print(d[(k - i + 1) % m])
                exit(0)
            else:
                d.append(a[j - 1])
                j = a[j - 1]

    else:
        b[c - 1] = 'y'

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = [1]
a = 0
i = 0
while a == 0:
  if A[i] in B:
    b = B.index(A[i])
    C = B[:b]
    D = B[b:]
    a = 1
  else:
    B.append(A[i])
  i = A[i] - 1
c = len(C)
d = len(D)
if K <= c:
  ans = C[K-1]
else:
  ans = D[(K - c) % d]
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
from collections import Counter
c=Counter()
b=[]
i=0
b.append(1)
cir_start=-1
cnt=0
while cnt<4*10**5:
  if c[a[i]]>0:
    cir_start=a[i]
    break
  b.append(a[i])
  c[a[i]]+=1
  i=a[i]-1
  cnt+=1
cir=[]
if cir_start!=-1:
  ok=False
  for bb in b:
    if ok:
      if bb==cir_start:
        break
      cir.append(bb)
    else:
      if bb==cir_start:
        cir.append(bb)
        ok=True
  d=[]
  for bb in b:
    if bb==cir_start:
      break
    d.append(bb)
  k-=len(d)-1
  k%=len(cir)
  print(cir[k-1])
else:
  print(b[k-1])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
Nlist = {}
Nlist[1]=0
Vlist={}
count = 1
Vlist[0]=1
for A in input().split():
    A=int(A)
    if count==A:
        continue
    if A in Nlist:
        break
    Nlist[A] = count
    Vlist[count]=A
    count = count+1

print(Vlist[K%count])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
l1=set([a[0]])
num1=a[0]
num2=1
l2=[]
def al(num):
    l1.add(num)
def n2(num5):
    return num5 in l1
for i in range(n):
    num1=a[num1-1]
    al(num1)
    if num2==len(l1):
        break
    else:
        num2=len(l1)
l1=list(l1)
num=l1.index(num1)
l2=l1[num:]
l1=l1[:num]
num1=len(l1)
num2=len(l2)
if k<=num1:
    print(l1[k-1])
else:
    num3=(k-num1)%num2
    print(l2[num3-1])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
l1=set([a[0]])
num1=a[0]
num2=1
l2=[]
def al(num):
    l1.add(num)
def n2(num5):
    return num5 in l1
for i in range(n):
    num1=a[num1-1]
    al(num1)
    if num2!=len(l1):
        break
    else:
        num2=len(l1)
l1=list(l1)
num=l1.index(num1)
l2=l1[num:]
l1=l1[:num]
num1=len(l1)
num2=len(l2)
if k<=num1:
    print(l1[k-1])
else:
    num3=(k-num1)%num2
    print(l2[num3-1])

Submission status: Wrong Answer

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


def main():
    [n, k] = [int(e) for e in si().split()]
    trace = dict()
    for i in range(n):
        trace[i] = 0

    l = [int(e) for e in si().split()]
    i, cnt = 0, 0
    while True:
        k -= 1
        cnt += 1
        i = l[i]-1
        if i == 0 or trace[i] != 0:
            cy = cnt-trace[i]
            break
        trace[i] = cnt
    k %= cy
    while k > 0:
        i = l[i]-1
        k -= 1
    print(i+1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

root_list = list(map(int, input().split()))
  
#print(W)
place = 0
visited_list = []

while True:
  visited_list.append(place)
  place = root_list[place] - 1
  #print(place)
  if place in visited_list:
    break
#print(visited_list)

roop_start = visited_list.index(place)
roop_list = visited_list[roop_start:]
roops = k - roop_start
print(roop_list[roops%len(roop_list)]+1)

Submission status: Wrong Answer

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

town = 0
cnt = 0

visited = [False] * n
visited[0] = -1

start = 0
loop = 10**8
start_cnt = 0
for i in range(n*2):
    town = A[town]
    if visited[town] != False:
        start_cnt = i + 1
        loop = i - visited[town]
        break
    else:
        visited[town] = i

town = 0

for i in range((k-start_cnt)%loop+start_cnt):
    town = A[town]

print(town+1)

Submission status: Wrong Answer

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

town = 0
cnt = 0

visited = [False] * n
visited[0] = -1

start = 0
loop = 10**8
start_cnt = 0
for i in range(n*2):
    town = A[town]
    if visited[town] != False:
        start_cnt = i + 1
        loop = i - visited[town]
        break
    else:
        visited[town] = i

for i in range((k-start_cnt)%loop+start_cnt):
    town = A[town]

print(town+1)