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:
import sys

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

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

print(p)
if k <= n:
    print(p[k])
else:
    # 周期の来る前の回数
    num = p.index(p[-1])
    p = p[num:]

    # 周期
    pn = len(p)-1
    k -= num

    # 回数/周期のあまり
    ans = k%pn
    print(p[ans])

Submission status: Wrong Answer

Solution code:
# 1-index
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()]
A = [0] + A

visited = [False]*(N+1)
visited[1] = True
step = [0]*(N+1)
step[0] = 1
step[1] = A[1]
count = [0]*(N+1)
count[1] = 0
count[A[1]] = 1

now = A[1]
cnt = 1
roop = 0

for _  in range(N):
    now = A[now]
    cnt += 1
    
    if visited[now]:
        roop = cnt - count[now]
        K -= count[now]
        break
    count[now] = cnt
    step[cnt] = now
    visited[now] = True
    
ind = K % roop
print(step[ind+count[now]])

Submission status: Wrong Answer

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

now=1
flag=[-1]*(N)

for i in range(N):
    if flag[now-1]==-1:
        flag[now-1]=i
        now = A[now-1]
    else:
        loop = i - flag[now-1]
        zan=(K-(i-loop))%loop
        ans = flag.index(flag[now-1]+zan)+1
        print(ans)
        exit()

print(now)

Submission status: Wrong Answer

Solution code:
a = input().split()
N = int(a[0])
K = int(a[1])
c = input().split()
c=[int(i) for i in c]


#それぞれ頂点が向かう先。



current_num_set = set()
current_num_list=[]
#これまでの道筋
current_num = 1 #現在の位置



#今いる位置が今まで出てきていない場合に、次どこにいくのかをチェック
while (current_num not in current_num_set):
      current_num_set.add(current_num)
      current_num_list.append(current_num)
      current_num = c[current_num-1]


c = len(current_num_list)


first_index = 0
#グルグルなっているところの最初のインデックスを計算して格納
for i in range(c):
  if current_num_list[i] == current_num:
    first_index = i
    break


cicle=c-first_index
first=first_index

#cがグルグルのリストの長さ
#グルグルの始まりのインデックス
if first<K :
  index = (K+1- first) % cicle + first-1
  print(current_num_list[index])
else :
  print(current_num_list[K])

Submission status: Wrong Answer

Solution code:
n, k = [int(i) for i in input().split()]
towns = [int(i) for i in input().split()]
ind_ = 1
ts = []
visited = [0]*n
roop_s = None
while(True):
    ind_ = towns[ind_-1]
    if visited[ind_-1]:
        roop_s = ts.index(ind_)
        break
    else:
        visited[ind_-1] = 1
        ts.append(ind_)
init = len(ts[:roop_s])
roop_l = len(ts) - init
roop = ts[roop_s:]
print(roop[((k-init)%roop_l)-1])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
bl = [False]*n
now = 1
bl[now - 1] = True
ct = 1
res = [1]
ky = int()
while True:
    if bl[a[now - 1] - 1]:
        ky = a[now - 1]
        break
    now = a[now - 1]
    bl[now - 1] = True
    res.append(now)
    ct += 1
roop = ct - res.index(ky)
x = ct - roop
k -= x
if k <= res.index(ky):
    print(res[k])
else:
    print(res[x + k % roop])

Submission status: Wrong Answer

Solution code:
a = input().split()
N = int(a[0])
K = int(a[1])
c = input().split()
c=[int(i) for i in c]


#それぞれ頂点が向かう先。



current_num_set = set()
current_num_list=[]
#これまでの道筋
current_num = 1 #現在の位置



#今いる位置が今まで出てきていない場合に、次どこにいくのかをチェック
while (current_num not in current_num_set):
      current_num_set.add(current_num)
      current_num_list.append(current_num)
      current_num = c[current_num-1]


c = len(current_num_list)


first_index = 0
#グルグルなっているところの最初のインデックスを計算して格納
for i in range(c):
  if current_num_list[i] == current_num:
    first_index = i
    break


cicle=c-first_index
first=first_index

#cがグルグルのリストの長さ
#グルグルの始まりのインデックス
if c<=K :
  index = (K+1 - first) % cicle + first-1
  print(current_num_list[index])
else :
  print(current_num_list[K])

Submission status: Wrong Answer

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

port_dic = {}
for i in range(1, N + 1):
    port_dic[i] = al[i - 1]

#print(port_dic)

route = [1]
for n in range(1, K + 1):
    tmp = route[n - 1]
    route.append(port_dic[tmp])
    if len(set(route)) != len(route):
        loop = n - route.index(port_dic[tmp])
        amari = (K - route.index(port_dic[tmp]) - 1) % loop
        ans = route[route.index(port_dic[tmp]) + amari + 1]
        print(ans)
        exit()

Submission status: Wrong Answer

Solution code:
def main():
    # 町の数N, 町iの転送先はindexがAi-1の町
    N,K = map(int, input().split())
    A = list(map(int, input().split()))

    # K回移動した時の町番号を出力
    # もし一度出てきた番号が出てきたら記憶している道順リストを使う
    # 最初は道を覚えさせる作業が必要
    now = 0
    history = [0]
    i = 0
    while True:
        # まず進む
        now = A[now]-1
        # 道順登録
        if now not in history:
            history.append(now)
            i+=1
        # 以前出てきてる町ならそこから末尾までを自動で進めてiのカウントも進める
        else:
            start = history.index(now)
            r = start + (K-1-i)%(len(history)-start)
            now = history[r]
            break

    print(now+1)

main()

Submission status: Wrong Answer

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

port_dic = {}
for i in range(1, N + 1):
    port_dic[i] = al[i - 1]

#print(port_dic)

route = [1]
for n in range(1, K + 1):
    tmp = route[n - 1]
    route.append(port_dic[tmp])
    if len(set(route)) != len(route):
        loop = n - route.index(port_dic[tmp])
        amari = (K - route.index(port_dic[tmp]) - 1) % loop
        ans = route[route.index(port_dic[tmp]) + amari + 1]
        print(ans)
        break

Submission status: Wrong Answer

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

if N == 1:
    print(A[0])
else:
    loop = 1
    des = 0
    while des != A[-1]:
        des = A[des-1]
        loop += 1

    print(A[K % loop + 1])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = [0]+list(map(int,input().split()))
b = []
c = a[1]
d = 0
length = 0
first = 0
ans = 0

while c not in b:
    b += [c]
    c = a[c]
    d = c

length = len(b)-b.index(d)
first = b.index(d)
ans = b[(k-first)%length+first-1]
print(ans)

Submission status: Wrong Answer

Solution code:
# 1-index
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()]
A = [0] + A

visited = [False]*(N+1)
visited[1] = True
step = [0]*(N+1)
step[0] = 1
step[1] = A[1]
count = [0]*(N+1)
count[1] = 0
count[A[1]] = 1

now = A[1]
cnt = 1
roop = 0

for _  in range(N):
    now = A[now]
    cnt += 1
    
    if visited[now]:
        roop = cnt - count[now]
        break
    count[now] = cnt
    step[cnt] = now
    visited[now] = True
    
ind = K % roop
print(step[ind])

Submission status: Wrong Answer

Solution code:
def main():
    # n,k = map(int, input().split())
    # town = list(map(int, input().split()))
    n,k, = 5,3
    town = [2,3,4,5,1]
    town = [i-1 for i in town]
    visited = [-1] * n
    current_town = 0
    current_order = 0
    memo = []
    while visited[current_town] == -1:
        visited[current_town] = current_order
        memo.append(current_town)
        current_town = town[current_town]
        current_order += 1
    loop_start = visited[current_town]
    if k < len(memo):
        print(memo[k]+1)
    else:
        loop_length = len(memo) - loop_start
        k =  (k - loop_start) % loop_length
        print(memo[k+loop_start]+1)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, k = [int(i) for i in input().split()]
towns = [int(i) for i in input().split()]
ind_ = 1
ts = []
roop_s = None

while(True):
    ind_ = towns[ind_-1]
    if ind_ in ts:
        roop_s = ts.index(ind_)
        break
    else:
        ts.append(ind_)
init = len(ts[:roop_s])
roop_l = len(ts) - init
roop = ts[roop_s:]
print(roop[((k-init)%roop_l)-1])

Submission status: Wrong Answer

Solution code:
def main():
    n,k = map(int, input().split())
    town = list(map(int, input().split()))
    town = [i-1 for i in town]
    visited = [-1] * n
    current_town = 0
    current_order = 0
    memo = []
    while visited[current_town] == -1:
        visited[current_town] = current_order
        memo.append(current_town)
        current_town = town[current_town]
        current_order += 1
    loop_start = visited[current_town]
    if k < len(memo):
        print(memo[k])
    else:
        loop_length = len(memo) - loop_start
        k =  (k - loop_start) % loop_length
        print(memo[k+loop_start]+1)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

now=1
flag=[0]+[-1]*(N-1)

for i in range(N-1):
    now = A[now-1]
    if flag[now-1]==-1:
        flag[now-1]+=i+2
    else:
        loop = i+1 - flag[now-1]
        zan=(K-i-1)%loop
        ans = flag.index(flag[now-1]+zan)+1
        print(ans)
        exit()

print(A[now-1])

Submission status: Wrong Answer

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

point = 0
syuuki = 0
i = 0
while i < N:
  point = T_list[point]
  if point == 0:
    syuuki = i+1
    break
  i += 1

second_point = point
if syuuki != 0:
  L = K % syuuki

else:
  j = 0
  while j < N:
    point = T_list[point]
    if point == second_point:
      syuuki = j+1
      break
    j += 1
  L = (K-i) % syuuki

k = 0
while k < L:
  point = T_list[point]
  k += 1
  
print(point+1)

Submission status: Wrong Answer

Solution code:
MOD = 998244353
n, m, k = (60522, 114575, 7559)

L = [1 for i in range(n)]
for i in range(1, n):
  L[i] = L[i-1] * (m-1) % MOD
s = L[-1]
c = 1
for i in range(1, k + 1):
  c = c * (n-i) // i
  s += L[n-1-i] * c % MOD

s = s * m % MOD
print(s)

Submission status: Wrong Answer

Solution code:
import sys

# print('input >>')
N, K = map(int,(input().split()))
As = list(map(lambda x: int(x)-1,(input().split())))

visited = [0] * N
visited[0] = 1
roots = [0]
now = 0

# print('-----output-----')
while True:
    now = As[now]
    if visited[now] == 1:
        p = now
        rep_start = roots.index(now)
        move = len(roots) - rep_start
        break
    roots.append(now)
    visited[now] = 1

now = 0
if rep_start > K:
    for i in range(K):
        now = As[now]
    print(now+1)
    sys.exit()

# now = p
# for i in range(rep_start):
    # now = As[now]


K -= rep_start
K %= move
now = p

# print('K', K)
# print('now',now)

for i in range(K):
    print(now+1)
    now = As[now]

print(now+1)

Submission status: Wrong Answer

Solution code:
import sys

# print('input >>')
N, K = map(int,(input().split()))
As = list(map(lambda x: int(x)-1,(input().split())))

visited = [0] * N
visited[0] = 1
roots = [0]
now = 0

# print('-----output-----')
while True:
    now = As[now]
    if visited[now] == 1:
        p = now
        rep_start = roots.index(now)
        move = len(roots) - rep_start
        break
    roots.append(now)
    visited[now] = 1

now = 0
if rep_start > K:
    for i in range(K+1):
        now = As[now]
    print(now+1)
    sys.exit()

# now = p
# for i in range(rep_start):
    # now = As[now]


K -= rep_start
K %= move
now = p

# print('K', K)
# print('now',now)

for i in range(K):
    print(now+1)
    now = As[now]

print(now+1)

Submission status: Wrong Answer

Solution code:
#入力
a = input().split()
N = int(a[0])
K = int(a[1])
c = input().split()
c=[int(i) for i in c]


#それぞれ頂点が向かう先。



current_num_set = set()
current_num_list=[]
#これまでの道筋
current_num = 1 #現在の位置



#今いる位置が今まで出てきていない場合に、次どこにいくのかをチェック
while (current_num not in current_num_set):
      current_num_set.add(current_num)
      current_num_list.append(current_num)
      current_num = c[current_num-1]


c = len(current_num_list)


first_index = 0
#グルグルなっているところの最初のインデックスを計算して格納
for i in range(c):
  if current_num_list[i] == current_num:
    first_index = i
    break


cicle=c-first_index
first=first_index
#cがグルグルのリストの長さ
#グルグルの始まりのインデックス
if first<=K+1 :
  index = (K+1 - first) % cicle + first-1
  print(current_num_list[index])
else :
  print(current_num_list[K])

Submission status: Wrong Answer

Solution code:
a = input().split()
N = int(a[0])
K = int(a[1])
c = input().split()
c=[int(i) for i in c]


#それぞれ頂点が向かう先。



current_num_set = set()
current_num_list=[]
#これまでの道筋
current_num = 1 #現在の位置



#今いる位置が今まで出てきていない場合に、次どこにいくのかをチェック
while (current_num not in current_num_set):
      current_num_set.add(current_num)
      current_num_list.append(current_num)
      current_num = c[current_num-1]


c = len(current_num_list)


first_index = 0
#グルグルなっているところの最初のインデックスを計算して格納
for i in range(c):
  if current_num_list[i] == current_num:
    first_index = i
    break

print(current_num_list)
cicle=c-first_index
first=first_index
#cがグルグルのリストの長さ
#グルグルの始まりのインデックス
if first<=K+1 :
  index = (K+1 - first) % cicle + first-1
  print(current_num_list[index])
else :
  print(current_num_list[K])

Submission status: Wrong Answer

Solution code:
def main():
    n,k = map(int, input().split())
    town = list(map(int, input().split()))
    town = [i-1 for i in town]
    visited = [-1] * n
    current_town = 0
    current_order = 0
    memo = []
    while visited[current_town] == -1:
        visited[current_town] = current_order
        memo.append(current_town)
        current_town = town[current_town]
        current_order += 1
    loop_start = visited[current_order]
    if k < len(memo):
        print(memo[k])
    else:
        loop_length = len(memo) - loop_start
        k %= loop_length
        print(memo[k+loop_start]+1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
num_town, num_move = map(int, input().split())
teleports = list(map(lambda x: int(x) - 1, input().split()))

current = 0
memo = {}
for i in range(num_move):
    if memo.get(current) == 2:
        break
    if not memo.get(current):
        memo[current] = 1
    else:
        memo[current] += 1
    destination = teleports[current]
    current = destination

loop = {k:v for k,v in memo.items() if v==2}
minus = {k:v for k,v in memo.items() if v==1}

new_move_count = 0
if len(loop) == 0:
    new_move_count = num_move
else:
    new_move_count = (num_move - len(minus)) % len(loop)

for i in range(new_move_count):
    destination = teleports[current]
    current = destination

print(current + 1)

Submission status: Wrong Answer

Solution code:
import sys

# print('input >>')
N, K = map(int,(input().split()))
As = list(map(lambda x: int(x)-1,(input().split())))

visited = [0] * N
visited[0] = 1
roots = [0]
now = 0

# print('-----output-----')
while True:
    now = As[now]
    if visited[now] == 1:
        p = now
        rep_start = roots.index(now)
        move = len(roots) - rep_start
        break
    roots.append(now)
    visited[now] = 1

now = 0
if rep_start > K:
    print(roots[K]+1)
    # for i in range(K):
        # now = As[now]
        # print(now+1)
        # sys.exit()

# now = p
# for i in range(rep_start):
    # now = As[now]


K -= rep_start
K %= move
now = p

# print('K', K)
# print('now',now)

for i in range(K):
    print(now+1)
    now = As[now]

print(now+1)

Submission status: Wrong Answer

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

now=1
flag=[0]+[-1]*(N-1)

for i in range(N-1):
    now = A[now-1]
    if flag[now-1]==-1:
        flag[now-1]+=i+2
    else:
        loop = i+1 - flag[now-1]
        zan=(K-i-1) - (K-i-1)//loop * loop
        ans = flag.index(flag[now-1]+zan)+1
        print(ans)
        exit()

print(A[now-1])

Submission status: Wrong Answer

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

num = list(map(int, input().split()))
t = [1]
t_test = []

for i in range(n):
  x = num[t[-1] - 1]
  if(x not in t):
    t.append(x)
  else:
    t_test = t[t.index(x):len(t)]
    break

list_num = list(set(t) - set(t_test))

if(len(t_test) >= 1):
  print(t_test[(k - len(list_num)) % len(t_test)])

Submission status: Wrong Answer

Solution code:
import sys

# print('input >>')
N, K = map(int,(input().split()))
As = list(map(lambda x: int(x)-1,(input().split())))

visited = [0] * N
visited[0] = 1
roots = [0]
now = 0

# print('-----output-----')
while True:
    now = As[now]
    if visited[now] == 1:
        p = now
        rep_start = roots.index(now)
        move = len(roots) - rep_start
        break
    roots.append(now)
    visited[now] = 1

now = 0
if rep_start > K:
    for i in range(K+1):
        now = As[now]
        print(now+1)
        sys.exit()

# now = p
# for i in range(rep_start):
    # now = As[now]


K -= rep_start
K %= move
now = p

# print('K', K)
# print('now',now)

for i in range(K):
    print(now+1)
    now = As[now]

print(now+1)

Submission status: Wrong Answer

Solution code:
N = [int(e) for e in input().split()]
nums = [int(t) for t in input().split()]
count=[]
k = 0
check = []
t = nums[0]
for i in range(N[0]):
    count.append(0)
while count[t-1] == 0:
    check.append(t)
    count[t-1] += 1
    k = t -1
    t = nums[k]

s = check.index(t) + 1
if(N[1] < s):
    print(check[N[1]])
else:
    ans = (N[1] - s)%(len(check)+1-s)
    ans1 = check.index(t) + ans
    print(check[ans1])

Submission status: Wrong Answer

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


curr = 0
arr = []
visited = [False]*n
while(not visited[curr]):
    visited[curr] = True
    arr.append(curr)
    curr = a[curr]-1

visited = [False]*n
cycleSize = 0
while(not visited[curr]):
    visited[curr] = True
    cycleSize+=1
    curr = a[curr]-1

pathSize = len(arr) - cycleSize

if(pathSize>=k):
    print(arr[k])
else:
    print(arr[(k-pathSize)%cycleSize+pathSize]+1)

Submission status: Wrong Answer

Solution code:
a,b=map(int, input().split())
c=list(map(int, input().split())) 
 
l = [1]
tmp = 1
s = 0 
for i in range(a):
    l.append(c[tmp-1])
    tmp = c[tmp-1]
    d = l.count(l[i])
    if d > 1:
        s = l[i]
        break
 
flg = 0
h = 0
t = 0
for i in range(a):
    if s == l[i] and flg == 0:
        h = i + 1
        flg = 1
    elif s == l[i]:
        t = i + 1
        break
 
#print(s,h,t)
l2 = l[h-1:t-1]
#print(l2)
if s == 1:
    id = b % (t - 1)
    print(l2[id])
else:
    id = (b - h + 1) % (t - h)
    print(l2[id])

Submission status: Wrong Answer

Solution code:
# 1-index
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()]
A = [0] + A

visited = [False]*(N+1)
visited[1] = True
step = [0]*(N+1)
step[0] = 1
step[1] = A[1]
count = [0]*(N+1)
count[1] = 1

now = A[1]
cnt = 1
roop = 0

for _  in range(N):
    now = A[now]
    cnt += 1
    
    if visited[now]:
        roop = cnt - count[now] + 1
        K -= (count[now]-1)
        break
    count[now] = cnt
    step[cnt] = now
    visited[now] = True
    
ind = K % roop
print(step[ind])

Submission status: Wrong Answer

Solution code:
cin = open(0).read().strip().split('\n')

n,k = map(int, cin[0].split(' '))
a = list(map(int, cin[1].split(' ')))

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

if n >= k:
    print(a[k])
else:
    times = [-1] * n
    for i in range(n):
        if times[history[i]-1] == -1:
            times[history[i]-1] = i
        else:
            cycle = i-times[history[i]-1]
            pre = times[history[i]-1]
            mod = (k-pre)%cycle
            print(history[pre+mod])
            break

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a_list = list(input().split())
dum = a_list[0]
ans_list = ""
for i in range(1,k+1):
    k = k - 1
    ans_list = ans_list+dum
    dum = a_list[int(dum)-1]
    if ans_list.count(dum) == 1:
        break
ind = ans_list.index(dum)
loop = ans_list[ind:]
len_loop = len(loop)
loop_after = (k)%len_loop
print(loop[loop_after-1])

Submission status: Wrong Answer

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

cities = [int(i) for i in input().split()]

next_city = cities[0]

if K <= N:
    for _ in range(K):
        next_city = cities[next_city-1]        
    output = next_city
else : 
    city_logs = [1, next_city]
    for _ in range(K):
        next_city = cities[next_city-1]
        if next_city in city_logs:
            break
        city_logs.append(next_city)
    
    loop_start = city_logs.index(next_city)
    
    if K < loop_start:
        output = city_logs[K-1]
    else :
        repeat_city = city_logs[loop_start:]   
        output_index = (K-loop_start) % len(repeat_city)
        output = repeat_city[output_index]
        
print(output)

Submission status: Wrong Answer

Solution code:
n,k=[int(i) for i in input().split()]
l=[int(i) for i in input().split()]
x=set(range(1,n+1))
dic=dict.fromkeys(x,0)
for i in x:
    dic[i]=l[i-1]
l=[-1]*n
curr=1
x=0
while x<=k:
    if l[curr-1]==-1:
        l[curr-1]=x
        x+=1
        continue
    else:
        #if l[curr-1]==0:
        l[curr-1]=x
    if l[curr-1]!=0:
        x=k-((k-x)%l[curr-1])
        
    curr=dic[curr]
    x+=1
print(dic[curr])

Submission status: Wrong Answer

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

now=1
flag=[0]+[-1]*(N-1)

for i in range(N-1):
    now = A[now-1]
    if flag[now-1]==-1:
        flag[now-1]+=i+2
    else:
        loop = i+1 - flag[now-1]
        zan=(K-i-1) - (K-i-1)//loop * loop
        ans = flag.index(flag[now-1]+zan)+1
        print(ans,now,flag,i)
        exit()

print(A[now-1])

Submission status: Wrong Answer

Solution code:
import sys

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

# kが小さい時
if k <= n:
    l = [0]
    for i in range(n):
        l.append(a[l[i]])
    print(l[k-1]+1)
    sys.exit()

z = 1
p = [1]
for _ in range(n):
    z = a[z-1]
    if z in p:
        p.append(z)
        break
    p.append(z)

# 周期の来る前の回数
num = p.index(p[-1])

p = p[num:]

# 周期
pn = len(p)-1

k -= num

# 回数/周期のあまり
anum = k%pn
print(p[anum])

Submission status: Wrong Answer

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

visited = []
order = {}
x = 1
T = -1
while T < 0:
  x = a[x-1] #添字に直す
  if x not in visited:
    visited.append(x)
    order[x] = len(visited)-1
  else:
    T = len(visited) - order[x]
    s = order[x]
if k < s:
  print(visited[k])
else:
  mod = (k-s) % T
  print(visited[s+mod])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
machi=list(map(int,input().split()))
x =  1
li = []
count = 0
mo = [0]*n
for i in range(k):
    if mo[x-1] == 1:
        owari = li.index(x)
        li2 = li[owari:count]
        print(li2[(k-count)%len(li2)])
        exit()
    else:
        li.append(x)
        mo[x-1] = 1
        x = machi[x-1]
        count += 1

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))
loop = []
start = 0
index = 0
check = [-1] * N
fin = False
count = 0
while True:
    if check[index] != -1:
        start = check[index]
        break
    else:
        loop.append(index)
        check[index] = count
        count += 1
        index = a[index]

        if len(loop) == K + 1:
            print(index + 1)
            fin = True
            break
if not fin:  # len(loop)<=K
    if (K + 1 - start) % len(loop[start:]) == 0:
        print(loop[-1] + 1)
    else:
        print(loop[start + (K + 1 - start) % len(loop[start:]) - 1] + 1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

import sys
import math
import collections
import itertools
import bisect
INF = 10 ** 13
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def gcd(x, y) : return x if y == 0 else gcd(y, x%y)
def lcm(x, y) : return (x * y) // gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def LS() : return input().split()
def RS(N) : return [input() for _ in range(N)]
def LRS(N) : return [input().split() for _ in range(N)]
def PL(L) : print(*L, sep="\n")
def YesNo(B) : print("Yes" if B else "No")
def YESNO(B) : print("YES" if B else "NO")

N, K = LI()
A = LI()

D = 60
to = [[-1] * N for _ in range(D)]
to[0] = [a-1 for a in A]

for i in range(D-1):
    for j in range(N):
        to[i+1][j] = to[i][to[i][j]]

now = 0
time = 0
while K > 0:
    if K&1:
        now = to[time][now]
    
    K >>= 1

print(now+1)

Submission status: Wrong Answer

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

visited = []
order = {}
x = 1
T = -1
while T < 0:
  x = a[x-1] #添字に直す
  if x not in visited:
    visited.append(x)
    order[x] = len(visited)-1
  else:
    T = len(visited) - order[x]
    s = order[x]
    break
if k < s:
  print(visited[k])
else:
  mod = (k-s) % T
  print(visited[s+mod])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
machi=list(map(int,input().split()))
x =  1#今いる街の名前
li = []#訪問した街の名前
count = 0#何回ワープしたか
mo = [0]*n#訪問したら1になる
for i in range(k):
    if mo[x-1] == 1:#訪問してたら
        owari = li.index(x)#訪問してた街の番号
        li2 = li[owari:count]#ループの最初から最後
        print(li2[(k-count)%len(li2)])#ループの個数
        exit()
    else:#訪問してなかったら
        li.append(x)
        mo[x-1] = 1#次の街へ行った記録
        x = machi[x-1]#次の街へワープ
        count += 1#ワープ数記録

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
machi=list(map(int,input().split()))
x =  1#今いる街の名前
li = []#訪問した街の名前
count = 0#何回ワープしたか
mo = [0]*n#訪問したら1になる
for i in range(k):
    if mo[x-1] == 1:#訪問してたら
        owari = li.index(x)#訪問してた街の番号
        li2 = li[owari:count]#ループの最初から最後
        print(li2[(k-count)%len(li2)])#ループの個数
        exit()
    else:#訪問してなかったら
        li.append(x)
        mo[x-1] = 1#次の街へ行った記録
        x = machi[x-1]#次の街へワープ
        count += 1

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a_list = list(map(int, input().split()))
judge_list = [0] * n
step_list = [1]
next = 1

while(1):
    next = a_list[next - 1]
    if judge_list[next - 1] == 1:
        break;
    else:
        judge_list[next - 1] += 1;
        step_list.append(next)

print(step_list)

i = step_list.index(next)
surplus_list = step_list[:i]
loop_list = step_list[i:]

num = k + 1 - len(surplus_list)
surplus = num % len(loop_list)

print(loop_list[surplus - 1])

Submission status: Wrong Answer

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

visited = []
order = {}
x = 1
T = -1
while T < 0:
  x = a[x-1] #添字に直す
  if x not in visited:
    visited.append(x)
    order[x] = len(visited)-1
  else:
    T = len(visited) - order[x]
    s = order[x]
if k < s:
  print(visited[k])
else:
  mod = (k-s) % T
  print(visited[s+mod])

Submission status: Wrong Answer

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

i = 1
memo = []
cnt = 0
cnt2 = 0
s = 0
for _ in range(2*n):
    memo.append(a[i-1])
    i = a[i-1]
    if i in memo[:-1]:
        cnt2 = memo[:-1].index(i) 
        cntlist = memo[:-1][cnt2:]
        cnt = len(cntlist)
        break
s = (k - cnt2-1)%cnt
ans = cntlist[s]

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=[int(i) for i in input().split()]
l=[int(i) for i in input().split()]
x=set(range(1,n+1))
dic=dict.fromkeys(x,0)
for i in x:
    dic[i]=l[i-1]
l=[-1]*n
curr=1
x=0
while x<=k:
    if l[curr-1]==-1:
        l[curr-1]=x
        x+=1
        continue
    else:
        #if l[curr-1]==0:
        l[curr-1]=x
    if l[curr-1]!=0:
        x=k-(k%l[curr-1])
        
    curr=dic[curr]
    x+=1
print(dic[curr])

Submission status: Wrong Answer

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

num = list(map(int, input().split()))
t = [1]
t_test = []

for i in range(n):
  x = num[t[-1] - 1]
  if(x not in t):
    t.append(x)
  else:
    t_test = t[t.index(x):len(t)]

list_num = list(set(t) - set(t_test))

if(len(t_test) >= 1):
  print(t_test[(k - len(list_num)) % len(t_test)])
else:
  print(t[-1])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
 
a={}
 
b=1
a[1]=1
count=1
while(count<=K+1):
    count+=1
    b=A[b-1]
    if b in a:
        cycle=count-a[b]
        rest=K-count+1
        ans=rest%cycle-1
        for i in range(min([ans,rest])):
            b=A[b-1]
        print(b)
        break
    a[b]=count
    if count==K+1:
        print(b)
        break

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
sys.setrecursionlimit(200000)
N,K=map(int,input().split())
A=list(map(int,input().split()))
def B(i):
  if i==0:
    return 1
  return A[B(i-1)-1]
C=[1]
for i in range(1,N+1):
  if C.count(B(i))==0:
    C.append(B(i))
  else:
    l=C.index(B(i))+1
    break
if K+1<=l:
  print(C[K])
else:
  print(C[l-2+(K+2-l)%(len(C)-l+1)])
print(C)

Submission status: Wrong Answer

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

l = [1]
tmp = 1
for i in range(a):
    l.append(c[tmp-1])
    tmp = c[tmp-1]

#print(l)
s = 0    
for i in range(a):
    d = l.count(l[i])
    if d > 1:
        s = l[i]
        break

flg = 0
h = 0
t = 0
for i in range(a):
    if s == l[i] and flg == 0:
        h = i + 1
        flg = 1
    elif s == l[i]:
        t = i + 1
        break

#print(s,h,t)
l2 = l[h-1:t-1]
#print(l2)
if s == 1:
    id = b % (t - 1)
    print(l2[id])
else:
    id = (b - h + 1) % (t - h)
    print(l2[id])

Submission status: Wrong Answer

Solution code:
a = input().split()
N = int(a[0])
K = int(a[1])
c = input().split()
c=[int(i) for i in c]


#それぞれ頂点が向かう先。



current_num_set = set()
current_num_list=[]
#これまでの道筋
current_num = 1 #現在の位置



#今いる位置が今まで出てきていない場合に、次どこにいくのかをチェック
while (current_num not in current_num_set):
      current_num_set.add(current_num)
      current_num_list.append(current_num)
      current_num = c[current_num-1]


c = len(current_num_list)


first_index = 0
#グルグルなっているところの最初のインデックスを計算して格納
for i in range(c):
  if current_num_list[i] == current_num:
    first_index = i
    break


cicle=c-first_index
first=first_index
if c<=K+1 :
  index = (K + 1 - first) % cicle + first-1
  print(current_num_list[index])
else :
  print(current_num_list[K])