Problem:


Score: 400 points

Problem Statement

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

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

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

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

Constraints

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

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

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

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

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

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2



Sample input:
4 5
3 2 4 1


Sample output:
4

Submission status: Wrong Answer

Solution code:
N, K = [int(i) for i in input().split(' ')]
A = [0]
A.extend([int(a) for a in input().split(' ')])
check_list = [0] * (N + 1)

town = 1
check_point = 0
initial = -1
interval = -1

for k in range(K):
    if check_list[town] > 0:
        check_point = town
        initial = check_list[town]
        interval = k - initial
        break
    check_list[town] = k
    town = A[town]

remain = K - k

rem = remain % interval

check_list_ = list(map(lambda x: x - initial, check_list))

for i, c in enumerate(check_list_):
    if c == rem:
        print(i)
        exit(0)

Submission status: Wrong Answer

Solution code:
N, K = [int(i) for i in input().split(' ')]
A = [0]
A.extend([int(a) for a in input().split(' ')])
check_list = [0] * (N + 1)

town = 1
check_point = 0
initial = -1
interval = -1

print(A)

for k in range(K):
    if check_list[town] > 0:
        check_point = town
        initial = check_list[town]
        interval = k - initial
        break
    check_list[town] = k
    print(check_list)
    town = A[town]

remain = K - k

rem = remain % interval

print(rem, remain, interval, initial)

check_list_ = list(map(lambda x: x - initial, check_list))

print(check_list_)

for i, c in enumerate(check_list_):
    if c == rem:
        print(i)
        exit(0)

Submission status: Wrong Answer

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

slow = 1
fast = 1
counter = 0
for _ in range(K):
    slow = A[slow - 1]
    fast = A[A[fast-1]-1]
    counter += 1
    if slow == fast:
        break
# counter = m = lambda + k * myu
# m = lambda(開始地点) +(m - lambda)
# lambda進むとmyuの定数倍のmがlambdaから進んだ事になるのでそこがスタート地点
# get lambda
slow2 = slow
slow_start = 1
counter = 0
for _ in range(K):
    slow2 = A[slow2 - 1]
    slow_start = A[slow_start-1]
    counter += 1
    if slow2 == slow_start:
        break
lam = counter
slow3 = slow
counter = 0
for _ in range(K):
    slow3 = A[slow3 - 1]
    counter += 1
    if slow3 == slow:
        break
myu = counter
lam = lam % myu

total = lam + (K-lam) % myu
length = 0
if total < K:
    length = total
else:
    length = K
current = 1
for _ in range(length):
    current = A[current-1]
print(current)

Submission status: Wrong Answer

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

slow = 1
fast = 1
counter = 0
for _ in range(K):
    slow = A[slow - 1]
    fast = A[A[fast-1]-1]
    counter += 1
    if slow == fast:
        break
# counter = m = lambda + k * myu
# m = lambda(開始地点) +(m - lambda)
# lambda進むとmyuの定数倍のmがlambdaから進んだ事になるのでそこがスタート地点
# get lambda
slow2 = slow
slow_start = 1
counter = 0
for _ in range(K):
    slow2 = A[slow2 - 1]
    slow_start = A[slow_start-1]
    counter += 1
    if slow2 == slow_start:
        break
lam = counter
slow3 = slow
counter = 0
for _ in range(K):
    slow3 = A[slow3 - 1]
    counter += 1
    if slow3 == slow:
        break
myu = counter
lam = lam % myu
total = lam + (K-lam) % myu
current = 1
for _ in range(total):
    current = A[current-1]
print(current)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
if A[0] == 1:
    print(1)
    exit()
B = [A[0]]
num = 0
C = 0
for i in range(N):
    num = A[num-1]
    B.append(num)
    if num in B:
        c = num
        break
C = [c]
num = c
for i in range(N):
    num = A[num-1]
    if num == c:
        break
    C.append(num)
if K <= len(B):
    print(B[K-1])
    exit()
K -= len(B)
print(C[K%len(C)-1])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
if A[0] == 1:
    print(1)
    exit()
B = [A[0]]
num = 0
C = 0
for i in range(N):
    num = A[num-1]
    if num in B:
        c = num
        break
    B.append(num)
C = [c]
num = c
for i in range(N):
    num = A[num-1]
    if num == c:
        break
    C.append(num)
K -= len(B)
print(C[K%len(C)-1])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
if A[0] == 1:
    print(1)
    exit()
B = [A[0]]
C = []
num = A[0]
Flag = 1
for i in range(N):
    num = A[num-1]
    if num == A[0]:
        Flag = 0
        break
    B.append(num)
if Flag == 1:
    B.reverse()
    C.append(B[0])
    for b in B[1:]:
        if B[0] == b:
            break
        C.append(b)
    C.reverse()
    l = len(C)
    K -= len(B)-len(C)
    print(C)
    print(C[K%l-1])
else:
    l = len(B)
    print(B[K%l-1])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
if A[0] == 1:
    print(1)
    exit()
B = [A[0]]
num = A[0]
for i in range(N):
    num = A[num-1]
    if num == A[0]:
        break
    B.append(num)
l = len(B)
print(B[K%l-1])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
teleport=list(map(int,input().split()))
double_counter=set()
# mojiretu="1"
mojiretu=["1"]
index=1
double_counter.add(index)
flag=False
for i in range(k):
    tmp=teleport[index-1]
    if tmp in double_counter:
        flag=True
        break
    else:
        double_counter.add(tmp)
        # mojiretu+=str(tmp)
        mojiretu.append(str(tmp))
        index=tmp
else:
    print(tmp)

if flag:
    mojiretu="".join(mojiretu)
    index=mojiretu.index(str(tmp))
    roop_str=mojiretu[index:]
    length=k%len(roop_str)-len(mojiretu[:index])%len(roop_str)
    print(roop_str[length])

    # index = mojiretu.index(str(tmp))
    # roop_str=mojiretu[index:]
    # length=k%len(roop_str)-len(mojiretu[:index])%len(roop_str)
    # print(roop_str[length])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
teleport=list(map(int,input().split()))
double_counter=set()
mojiretu="1"
# mojiretu=["1"]
index=1
double_counter.add(index)
flag=False
for i in range(k):
    tmp=teleport[index-1]
    if tmp in double_counter:
        flag=True
        break
    else:
        double_counter.add(tmp)
        mojiretu+=str(tmp)
        # mojiretu.append(str(tmp))
        index=tmp
else:
    print(tmp)

if flag:
    index=mojiretu.index(str(tmp))
    roop_str=mojiretu[index:]
    length=k%len(roop_str)-len(mojiretu[:index])%len(roop_str)
    print(roop_str[length])

    # index = mojiretu.index(str(tmp))
    # roop_str=mojiretu[index:]
    # length=k%len(roop_str)-len(mojiretu[:index])%len(roop_str)
    # print(roop_str[length])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
teleport=list(map(int,input().split()))
double_counter=set()
mojiretu="1"
index=1
double_counter.add(index)
flag=False
for i in range(k):
    tmp=teleport[index-1]
    if tmp in double_counter:
        flag=True
        break
    else:
        double_counter.add(tmp)
        mojiretu+=str(tmp)
        index=tmp
else:
    print(tmp)

if flag:
    # index=mojiretu.find(str(tmp))
    # roop_str=mojiretu[index:]
    # length=k%len(roop_str)-len(mojiretu[:index])%len(roop_str)
    # print(roop_str[length])

    Li = mojiretu.find(str(tmp))
    Ri = len(mojiretu)
    idx = Li + (k - Li) % (Ri - Li)
    print(mojiretu[idx])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
teleport=list(map(int,input().split()))
double_counter=set()
mojiretu="1"
index=1
double_counter.add(index)
flag=False
for i in range(k):
    tmp=teleport[index-1]
    if tmp in double_counter:
        flag=True
        break
    else:
        double_counter.add(tmp)
        mojiretu+=str(tmp)
        index=tmp
else:
    print(tmp)

if flag:
    index=mojiretu.find(str(tmp))
    roop_str=mojiretu[index:]
    # length=k%len(roop_str)-len(mojiretu[:index])%len(roop_str)
    Ri=len(mojiretu)
    length=index+(k-index)%(Ri-index)
    # print(roop_str[length])
    print(mojiretu[length])

Submission status: Wrong Answer

Solution code:
import sys
N,K = list(map(int,input().split()))
a = list(map(int,input().split()))
c = 1
mod = 0
s=[1]
d = 0
tmp =0
if K<N:
	for i in range(K):
		c =a[c-1]
	print(c)
	sys.exit()
for i in range(K):
	c =a[c-1]
	if c in s:
		s.append(c)
		tmp = c
		d ==1 
		break
	s.append(c)
for i in range(len(s)):
	if s[i] == tmp:
		b= i
		mod = len(s)-i
		break

if d == 1:

	c = 1
	for i in range (b):
		c=a[c-1]
	if K-b > mod:
		K= (K-b) % mod
	for i in range(K):
		c =a[c-1]
print(c)

Submission status: Wrong Answer

Solution code:
N,K = list(map(int,input().split()))
a = list(map(int,input().split()))
c = 1
mod = 0
s=[1]
d = 0
tmp =0
for i in range(K):
	c =a[c-1]
	if c in s:
		s.append(c)
		tmp = c
		d ==1 
		break
	s.append(c)
for i in range(len(s)):
	if s[i] == tmp:
		b= i
		mod = len(s)-i
		break

if d == 1:

	c = 1
	for i in range (b):
		c=a[c-1]
	if K-b > mod:
		K= (K-b) % mod
	for i in range(K):
		c =a[c-1]
print(c)

Submission status: Wrong Answer

Solution code:
N,K = list(map(int,input().split()))
a = list(map(int,input().split()))
c = 1
mod = 0
s=[]
d = 0
tmp =0
for i in range(K):
	c =a[c-1]
	if c in s:
		s.append(c)
		tmp = c
		d ==1 
		break
	s.append(c)
for i in range(len(s)):
	if s[i] == c:
		b= i
		mod = len(s)-i
		break

if d == 1:

	c = 1
	for i in range (b):
		c=a[c-1]
	if K-b > mod:
		K= (K-b) % mod
	for i in range(K):
		c =a[c-1]
print(c)

Submission status: Wrong Answer

Solution code:
N,K = list(map(int,input().split()))
a = list(map(int,input().split()))
c = 1
s="0"
mod = 0
d = 0
for i in range(K):
	c =a[c-1]
	if str(c) in s:
		b=s.index(str(c))
		mod = i + 1 -s.index(str(c))
		d = 1
		break
	s += str(c)

if d == 1:

	c = 1
	for i in range (b):
		c=a[c-1]
	if K-b > mod:
		K= (K-b) % mod
	for i in range(K):
		c =a[c-1]
print(c)

Submission status: Wrong Answer

Solution code:
N,K = list(map(int,input().split()))
a = list(map(int,input().split()))
c = 1
s="1"
mod = 0
d = 0
for i in range(K):
	c =a[c-1]
	if str(c) in s:
		b=s.index(str(c))
		mod = i + 1 -s.index(str(c))
		d = 1
		break
	s += str(c)
#print(mod)
#print(b)
if d == 1:
	if K > 2*mod:
		K= K % mod
	c = 1
	for i in range (b):
		c=a[c-1]
	for i in range(K-b):
		c =a[c-1]
print(c)

Submission status: Wrong Answer

Solution code:
N,K = list(map(int,input().split()))
a = list(map(int,input().split()))
c = 1
s=""
mod = 0
d = 0
for i in range(K):

	if str(c) in s and  len(s)*2<K:
		b=s.index(str(c))
		mod = i + 1 -s.index(str(c))
		d = 1
		break
	s += str(c)
	c =a[c-1]

#print(mod)
#print(b)
if d == 1:
	x= K % mod
	c = 1
	for i in range (b):
		c=a[c-1]
	for i in range(x-b):
		c =a[c-1]
print(c)

Submission status: Wrong Answer

Solution code:
N,K = list(map(int,input().split()))
a = list(map(int,input().split()))
c = 1
s="1"
mod = 0
d = 0
for i in range(K):
	c =a[c-1]
	if str(c) in s:
		b=s.index(str(c))
		mod = i + 1 -s.index(str(c))
		d = 1
		break
	s += str(c)
#print(mod)
#print(b)
if d == 1:
	x= K % mod
	c = 1
	for i in range (b):
		c=a[c-1]
	for i in range(x-b):
		c =a[c-1]
print(c)

Submission status: Wrong Answer

Solution code:
N,K = list(map(int,input().split()))
a = list(map(int,input().split()))
c = 1
s="1"
if K> N:
	K = K % N
d = 0
for i in range(K):
	c =a[c-1]

print(c)

Submission status: Wrong Answer

Solution code:
N,K = list(map(int,input().split()))
a = list(map(int,input().split()))
c = 1
s="1"
K = K % N
d = 0
for i in range(K):
	c =a[c-1]

print(c)

Submission status: Wrong Answer

Solution code:
[n, k], [*a] = [[*map(int, t.split())] for t in open(0)]
count = 0
visited = dict()
tmp = 1
while count <= k:
    visited[tmp] = count
    count += 1
    tmp = a[tmp - 1]
    if tmp in visited:
        break
if count < k:
    k = (k - count) % (count - visited[tmp])
    for _ in range(k):
        tmp = a[tmp - 1]
print(tmp)

Submission status: Wrong Answer

Solution code:
[n, k], [*a] = [[*map(int, t.split())] for t in open(0)]
count = 0
visited = dict()
tmp = 1
while count <= k:
    visited[tmp] = count
    count += 1
    tmp = a[tmp - 1]
    if tmp in visited:
        break
if count <= k:
    k = (k - count) % (count - visited[tmp])
    for _ in range(k):
        tmp = a[tmp - 1]
print(tmp)

Submission status: Wrong Answer

Solution code:
[n, k], [*a] = [[*map(int, t.split())] for t in open(0)]
count = 0
visited = dict()
tmp = 1
while count <= k and tmp not in visited:
    visited[tmp] = count
    tmp = a[tmp - 1]
    count += 1
if count <= k:
    k = (k - count) % (count - visited[tmp])
    for _ in range(k):
        tmp = a[tmp - 1]
print(tmp)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
teleport=list(map(int,input().split()))
double_counter=set()
mojiretu="1"
index=1
double_counter.add(index)
flag=False
for i in range(k):
    tmp=teleport[index-1]
    if tmp in double_counter:
        flag=True
        break
    else:
        double_counter.add(tmp)
        mojiretu+=str(tmp)
        index=tmp
else:
    print(tmp)

if flag:
    index=mojiretu.find(str(tmp))
    roop_str=mojiretu[index:]
    length=k%len(roop_str)-len(mojiretu[:index])%len(roop_str)
    print(roop_str[length])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
teleport=list(map(int,input().split()))
double_counter=set()
mojiretu="1"
index=1
double_counter.add(index)
for i in range(k):
    tmp=teleport[index-1]
    if tmp in double_counter:
        break
    else:
        double_counter.add(tmp)
        mojiretu+=str(tmp)
        index=tmp
else:
    print(tmp)

index=mojiretu.find(str(tmp))
roop_str=mojiretu[index:]
length=k%len(roop_str)-len(mojiretu[:index])%len(roop_str)
print(roop_str[length])

Submission status: Wrong Answer

Solution code:
#初期入力
N,K = (int(x) for x in input().split())
A = list(map(int, input().split()))
#あえて構造を辞書
connection ={i:0 for i in range(1,N+1)}
for i in range(1,N+1):
    connection[i] =A[i-1]
#テレポート先は1か所、N << K だからループする。戻ってくる場所を探す。
passed =[1]
passed_set =set(passed)
intersection =0
next =connection[1]
count_intersection =[]
count_loop =0
for i in range(1,N+1): #スタートは１
    if next not in passed_set:
        passed.append(next)
        passed_set.add(next)
        next =connection[next]
    else:
        intersection =next
        break
#1ループの移動回数
loop_s  =passed.index(intersection)
loop_f  =len(passed) 
loop_num =loop_f -loop_s

#ループ開始前後で場合分け
ans =0
if K < loop_f:
    ans =K
else:
    K2      =K -loop_s
    q,mod   =divmod(K2,loop_num)
    ans     =passed[loop_s +mod]
print(ans)

Submission status: Wrong Answer

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

def rooptown():
    i = 0
    telp = [1]
    for _ in range(N):
        if A[i] not in telp:
            telp.append(A[i])
            i = A[i] - 1
        else:
            telp.append(A[i])
            break

    j = 1
    while j <= len(telp):
        if telp[-1] != telp[-(j + 1)]:
            j += 1
        else:
            telp.pop(0)
            roop = telp[-j:]
            a = K - len(telp[:-j])
            ans = a % len(roop) - 1

            return roop[ans]

print(rooptown())

Submission status: Wrong Answer

Solution code:
#初期入力
N,K = (int(x) for x in input().split())
A = list(map(int, input().split()))
#あえて構造を辞書
connection ={i:0 for i in range(1,N+1)}
for i in range(1,N+1):
    connection[i] =A[i-1]
#テレポート先は1か所、N << K だからループする。戻ってくる場所を探す。
passed =[1]
intersection =0
next =connection[1]
count_intersection =[]
count_loop =0
for i in range(1,N+1): #スタートは１
    if next not in passed:
        passed.append(next)
        next =connection[next]
    else:
        intersection =next
        break
#1ループの移動回数
loop_s  =passed.index(intersection)
loop_f  =len(passed) 
loop_num =loop_f -loop_s

#ループ開始前後で場合分け
ans =0
if K < loop_f:
    ans =K
else:
    K2      =K -loop_s
    q,mod   =divmod(K2,loop_num)
    ans     =passed[loop_s +mod]
print(ans)

Submission status: Wrong Answer

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

def rooptown():
    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
    return telp

def f(telp):
    j = 1
    while True:
        if telp[-1] != telp[-(j + 1)]:
            j += 1
        else:
            telp.pop(0)
            ans = telp[(K - len(telp[:-j])) % len(telp[-j:]) - 1]
            return ans

print(f(rooptown()))

Submission status: Wrong Answer

Solution code:
[n, k], [*a] = [[*map(int, t.split())] for t in open(0)]
count = 0
visited = dict()
tmp = 1
while count <= k and tmp not in visited:
    visited[tmp] = count
    tmp = a[tmp - 1]
    count += 1
if count < k:
    k = (k - count) % (count - visited[tmp])
    for _ in range(k):
        tmp = a[tmp - 1]
print(tmp)

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    if a[0] == 1:
        print(1)
        exit(0)
    if a[0] == a[a[0]-1]:
        print(a[0])
    pos = a[0]
    num = 1
    nums = set([1])
    while pos not in nums:
        nums.add(pos)
        num += 1
        pos = a[pos-1]
    lastnum = pos
    prefix = 0
    pos = 1
    while pos != lastnum:
        prefix += 1
        pos = a[pos-1]
    num -= prefix
    if num < 1:
        num = 10**20
    if k > prefix:
        k = (k-prefix)%num+prefix
    pos = 1
    for _ in range(k):
       pos = a[pos-1]
    print(pos)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    if a[0] == 1:
        print(1)
        exit(0)
    if a[0] == a[a[0]-1]:
        print(a[0])
    pos = a[0]
    num = 1
    nums = set([1])
    while pos not in nums:
        nums.add(pos)
        num += 1
        pos = a[pos-1]
    lastnum = pos
    prefix = 0
    pos = 1
    while pos != lastnum:
        prefix += 1
        pos = a[pos-1]
    num -= prefix
    if k > prefix:
        k = (k-prefix)%num+prefix
    pos = 1
    for _ in range(k):
       pos = a[pos-1]
    print(pos)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    if a[0] == a[a[0]-1]:
        print(1)
        exit(0)
    pos = a[0]
    num = 1
    nums = set([1])
    while pos not in nums:
        nums.add(pos)
        num += 1
        pos = a[pos-1]
    lastnum = pos
    prefix = 0
    pos = 1
    while pos != lastnum:
        prefix += 1
        pos = a[pos-1]
    num -= prefix
    if k >= prefix:
        k = (k-prefix)%num+prefix
    pos = 1
    for _ in range(k):
       pos = a[pos-1]
    print(pos)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    if a[0] == a[a[0]-1]:
        print(1)
        exit(0)
    pos = a[0]
    num = 1
    nums = set([1])
    while pos not in nums:
        nums.add(pos)
        num += 1
        pos = a[pos-1]
    lastnum = pos
    prefix = 0
    pos = 1
    while pos != lastnum:
        prefix += 1
        pos = a[pos-1]
    num -= prefix
    if k > prefix:
        k = (k-prefix)%num+prefix
    pos = 1
    for _ in range(k):
       pos = a[pos-1]
    print(pos)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
result=[1]
temp=1
for i in range(n):
  temp=a[temp-1]
  result.append(temp)
temp=result.index(result[len(result)-1])
c=len(result)-temp-1
if k<n:
  print(result[k+1])
else:
  print(result[temp+(k-temp)%c])

Submission status: Wrong Answer

Solution code:

def main():
    N, K = map(int,input().split(' '))
    to = list(map(int,input().split(' ')))
    to.insert(0, 0)
    dist = [0] * (N+1) # 頭からの距離
    distance = 0
    node = 1 # start
    mod = 1
    for _ in range(N):
        node = to[node]
        K-=1
        distance += 1
        if node == 1:
            mod = distance
            break
        elif dist[node] == 0:
            dist[node] = distance
        else:
            mod = distance - dist[node]
            break
    K %= mod
    for _ in range(K):
        node = to[node]
    print(node)

    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
if K<=N:
    next_city=0
    for i in range(K):
        next_city=A[next_city]
else:
    next_city=0
    visited=[-1]*N
    visited[0]=0
    for num in range(1,N):
        next_city=A[next_city]-1
        if visited[next_city]!=-1:
            break
        visited[next_city]=num
    K=(K-num)%(num-visited[next_city])
    for i in range(K):
        next_city=A[next_city]-1
print(next_city+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    if a[0] == a[a[0]-1]:
        print(1)
        exit(0)
    pos = a[0]
    num = 1
    nums = set([1])
    while pos not in nums:
        nums.add(pos)
        num += 1
        pos = a[pos-1]
    lastnum = pos
    prefix = 0
    pos = 1
    while pos != lastnum:
        prefix += 1
        pos = a[pos-1]
    num -= prefix
    k = (k-prefix)%num+prefix
    pos = 1
    if k == 0:
        print(1)
        exit(0)
    for _ in range(k):
       pos = a[pos-1]
    print(pos)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    if a[0] == a[a[0]-1]:
        print(1)
        exit(0)
    pos = a[0]
    num = 1
    nums = set([1])
    prev = 1
    while pos not in nums:
        nums.add(pos)
        num += 1
        prev = pos
        pos = a[pos-1]
    lastnum = prev
    prefix = 0
    while pos == lastnum:
        prefix += 1
        pos = a[pos-1]
    k = (k-prefix)%num+prefix
    if k == 0:
        print(1)
        exit(0)
    for _ in range(k):
       pos = a[pos-1]
    print(pos)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
 
n, k = map(int, readline().split())
a = np.int32(read().split())
 
a -= 1

b = [] * n
b.append(0)
t = 0

for i in range(1, n+1):
    t = a[t]
    if t in b:
        d = b.index(t)
        r = len(b) - d
        break
    else:
        b.append(t)

print(b[((k-d) % r) + d] + 1)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
 
n, k = map(int, readline().split())
a = np.int32(read().split())
 
a -= 1

b = [] * n
b.append(0)
t = 0

for i in range(1, n+1):
    t = a[t]
    if t in b:
        r = len(b) - b.index(t)
        d = b.index(t)
        break
    else:
        b.append(t)

print(b[((k-d) % r) + d] + 1)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
 
n, k = map(int, readline().split())
a = np.int32(read().split())
 
a -= 1

b = [] * n
b.append(0)
t = 0
i = 1

while i < n:
    t = a[t]
    if t in b:
        r = len(b) - b.index(t)
        d = b.index(t)
        break
    else:
        b.append(t)
        i += 1

print(b[((k-d) % r) + d] + 1)

Submission status: Wrong Answer

Solution code:

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
 
n, k = map(int, readline().split())
a = list(map(int, read().split()))
 
for i in range(n):
    a[i] -= 1

b = [] * n
b.append(0)
t = 0
i = 1

while i < n:
    t = a[t]
    if t in b:
        r = len(b) - b.index(t)
        d = b.index(t)
        break
    else:
        b.append(t)
        i += 1

print(b[((k-d) % r) + d] + 1)

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    pos = a[a[0]-1]
    if pos == a[0]:
        print(1)
        exit(0)
    num = 1
    while pos != a[0] and num < n:
        num += 1
        pos = a[pos-1]
    k %= num
    pos = a[0]
    if k == 0:
        print(1)
        exit(0)
    for _ in range(k-1):
       pos = a[pos-1]
    print(pos)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    pos = a[a[0]-1]
    if pos == a[0]:
        print(1)
        exit(0)
    num = 1
    while pos != a[0] and num < n:
        num += 1
        pos = a[pos-1]
    k %= num
    pos = a[0]
    if k == 0:
        k = num-1
    for _ in range(k-1):
       pos = a[pos-1]
    print(pos)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    pos = a[a[0]-1]
    if pos == a[0]:
        print(pos)
        exit(0)
    num = 1
    while pos != a[0] and num < n:
        num += 1
        pos = a[pos-1]
    k %= num
    if k == 0:
        k = num-1
    for _ in range(k-1):
       pos = a[pos-1]
    print(pos)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    pos = a[a[0]-1]
    if pos == a[0]:
        print(pos)
        exit(0)
    num = 1
    while pos != a[0]:
        num += 1
        pos = a[pos-1]
    k %= num
    if k == 0:
        k = num-1
    for _ in range(k-1):
       pos = a[pos-1]
    print(pos)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    pos = a[0]
    k %= n
    for _ in range(k):
       pos = a[pos-1]
    print(pos)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#ダブリング
n,k=map(int,input().split())
d=60
to=[[0]*n]*d
to[0]=[i-1 for i in map(int,input().split())]
for i in range(1,d):
  for j in range(n):
    to[i][j]=to[i-1][to[i][j]]
v=0
for i in reversed(range(d)):
  if i<=k:
    v=to[i][v]
    k-=i

print(v+1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
s=[]
v=1
ord=[-1]*(n+1)
c=1
l=0
while ord[v]==-1:
  ord[v]=len(s)
  s.append(v)
  v=A[v-1]
print(s)
print(ord)
c=len(s)-ord[v]
l=ord[v]

if k<l:
  print(s[k])
else:
  k-=l
  k%=c
  print(s[l+k])

Submission status: Wrong Answer

Solution code:
import sys
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
def resolve():
    n, k = map(int, input().split())
    f = list(map(lambda x : int(x) - 1, input().split()))
    step = [-1] * n
    step[0] = 0
    now = 0
    while step[f[now]] == -1:
        step[f[now]] = step[now] + 1
        now = f[now]
        if step[now] == k:
            print(now)
            return

    k -= step[now]
    loop = step[now] - step[f[now]] + 1
    k %= loop
    for _ in range(k):
        now = f[now]
    print(now + 1)
resolve()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
V=[]
p=0
x=0
X=[]
for i in range(n):
  if A[p] in V:
    x=V.index(A[p])
    X=V[:x+1]
    V=V[x:]
    break
  V.append(A[p])
  p=A[p]-1
#print(V)
print(V[(k-x)%len(V)-1] if k>x else X[k])

Submission status: Wrong Answer

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

num = list(map(int, input().split()))
t = [-1] * n
count = 0
s = 1

while t[s - 1] == -1:
  if count == k:
    break

  t[s - 1] = count
  count += 1
  s = num[s - 1]

print(t.index(t[s - 1] + ((k - t[s - 1]) % (count - t[s - 1]))) + 1)

Submission status: Wrong Answer

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

num = list(map(int, input().split()))
t = [-1] * n
count = 0
s = 1

while t[s - 1] == -1:
  if count == k:
    break

  t[s - 1] = count
  count += 1
  s = num[s - 1]
else:
  z = (k - t[s - 1]) % (count - t[s - 1])
  print(t.index(t[s - 1] + z) + 1)

Submission status: Wrong Answer

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

num = list(map(int, input().split()))
t = [-1] * n
count = 0
s = 1

while t[s - 1] == -1:
  if count == k:
    break

  t[s - 1] = count
  count += 1
  s = num[s - 1]
else:
  print(t.index(t[s - 1] + ((k - t[s - 1]) % (count - t[s - 1]))) + 1)

Submission status: Wrong Answer

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

num = list(map(int, input().split()))
t = [-1] * n
count = 0
s = 1

while t[s - 1] == -1:
  if t == k:
    break

  t[s - 1] = count
  count += 1
  s = num[s - 1]
else:
  print(t.index(t[s - 1] + ((k - t[s - 1]) % (count - t[s - 1]))) + 1)

Submission status: Wrong Answer

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

num = list(map(int, input().split()))
t = [0] * n
count = 0
s = 1

while t[s - 1] == 0:
  if t == k:
    break

  t[s - 1] = count
  count += 1
  s = num[s - 1]
else:
  print(t.index(t[s - 1] + ((k - t[s - 1]) % (count - t[s - 1]))) + 1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
visited = [False]*N
path = [0]
at = 0
loop = 0
visited[0]=True
for i in range(N):
    at = A[at]-1
    if visited[at]:
        loop = i+1-path.index(at)
        break
    else:
        visited[at]=True
        path.append(at)
if K <= len(path):
    print(path[K-1]+1)
else:
    print(path[(K-path.index(at))%loop+path.index(at)]+1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
path = [0]
at = 0
loop = 0
for i in range(N):
    at = A[at]-1
    if at in path:
        loop = i+1-path.index(at)
        break
    else:
        path.append(at)

print(path[(K-path.index(at))%loop+path.index(at)]+1)