Problem:


Score: 400 points

Problem Statement

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

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

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

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

Constraints

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

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

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

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

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

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2



Sample input:
4 5
3 2 4 1


Sample output:
4

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

N,K = map(int, input().split())
A = [int(i) for i in input().split()]
used_dict = defaultdict(int)
used_dict[0] += 1
now_town = 0

for i in range(N):
    now_town = A[now_town]-1
    if used_dict[now_town]:
        used_dict[now_town] += 1
        break
    used_dict[now_town] += 1

now_town2 = 0
cnt = 0
while 1:
    if now_town2 == now_town:
        for _ in range((K-cnt)%(i-cnt+1)):
            now_town = A[now_town]-1
        break
    now_town2 = A[now_town2]-1
    cnt += 1
print(now_town+1)

Submission status: Wrong Answer

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

b = []

# n回以内にある都市に戻る
pos = 1
for i in range(len(a)):
    pos = a[pos - 1]
    if pos in b:
        break
    b.append(pos)

# ループのスタート都市 loop_city1
loop_city1 = pos
# ループの開始位置
loop_start = b.index(loop_city1)
# ループの終了位置
loop_end = len(b) - 1

# 残りの移動回数をループの長さで割るとループの何番目で終わるか分かる
out = (k - loop_start) % (loop_end - loop_start + 1)
if out == 0:
    out = loop_end - loop_start + 1

# 最終都市
# ループ前の都市数をずらして
print(b[out + loop_start - 1])

Submission status: Wrong Answer

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

b = []

# n回以内にある都市に戻る
pos = 1
for i in range(len(a)):
    pos = a[pos - 1]
    if pos in b:
        break
    b.append(pos)

# ループのスタート都市 loop_city1
loop_city1 = pos
# ループの開始位置
loop_start = b.index(loop_city1)
# ループの終了位置
loop_end = len(b) - 1

# 残りの移動回数をループの長さで割るとループの何番目で終わるか分かる
out = (k - loop_start) % (loop_end - loop_start + 1)

# 最終都市
# ループ前の都市数をずらして
print(b[out + loop_start - 1])

Submission status: Wrong Answer

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

c = []

for i in range(len(a)):
    if a[i] - 1 != i and (i + 1) in a:
        c.append(a[i])
#print(a)
c = list(set(c))
#print(c)
d = [0] * len(c)
e = []

flag = 0
pos = 1
for i in range(k):
    pos = a[pos - 1]
    if pos in c:
        flag = 1
        for j in range(len(c)):
            if c[j] == pos:
                d[j] = 1
                e.append(pos)
        if sum(d) == len(d):
            print("loop", i)
            break
    else:
        flag = 0
        d = [0] * len(c)
        e = []
    #print(pos)


last_pos = (k - (i - len(c) + 1)) % len(c)
#print(e, i, last_pos)
print(e[last_pos - 1])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(map(lambda x: int(x) - 1, input().split()))
prev = 0
count = 0
used = {prev: count}
for _ in range(n):
  prev = A[prev]
  count += 1
  if prev in used:
    break
  used[prev] = count
for _ in range((k - used[prev]) % (count - used[prev])):
  prev = A[prev]
print(prev + 1)

Submission status: Wrong Answer

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

for i,_ in enumerate(range(N)):
    now_town = A[now_town]-1
    if now_town in used:
        break
    used.append(now_town)

roop_first = used.index(now_town)
ans_index = (K-roop_first)%(i-roop_first+1)
print(used[roop_first:][ans_index]+1)

Submission status: Wrong Answer

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

visited = [0] * (1 + n)
curr = 1
while k:
    if visited[curr]:
        break
    else:
        visited[curr] = k
        curr = a[curr]
        k -= 1
        if k == 0:
            print(curr)
            exit()
repeat = visited[curr] - k
k = k % repeat
while k:
    curr = a[curr - 1]
    k -= 1
print(curr)

Submission status: Wrong Answer

Solution code:
import sys
N , K = list(map(int, input().split()))
As = list(map(int, input().split()))
As = [0]+As
route = []
Came = [-1]*(N+1)

s = 1
i = 0

while True:
    if As[s] == s:
        print(s)
        sys.exit()
    elif Came[s]>=0:
        break
    Came[s] = i
    i+=1
    s = As[s]
if -1 not in Came[1:]:
    T = N
    Q = K%N
    print(Came.index(Q))
    sys.exit()
#print(Came , s)
T = i - Came[s]

L = K - Came[s]

Q = L % T
#print(T , L  , Q)
print(Came.index(Came[s]+Q))

Submission status: Wrong Answer

Solution code:
import sys
N , K = list(map(int, input().split()))
As = list(map(int, input().split()))
As = [0]+As
route = []
Came = [-1]*(N+1)

s = 1
i = 0

while True:
    if As[s] == s:
        print(s)
        sys.exit()
    elif Came[s]>=0:
        break
    Came[s] = i
    i+=1
    s = As[s]
#print(Came , s)
T = i - Came[s]

L = K - Came[s]

Q = L % T
#print(T , L  , Q)
print(Came.index(Came[s]+Q))

Submission status: Wrong Answer

Solution code:
N , K = list(map(int, input().split()))
As = list(map(int, input().split()))
As = [0]+As
route = []
Came = [-1]*(N+1)

s = 1
i = 0
while True:
    if Came[s]>=0:
        break
    Came[s] = i
    i+=1
    s = As[s]
#print(Came , s)
T = i - Came[s]

L = K - Came[s]

Q = L % T
#print(T , L  , Q)
print(Came.index(Came[s]+Q))

Submission status: Wrong Answer

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


def fib_dynamic(n):
    # 結果を保持する辞書
    cal_result = {}

    # 初期値の設定
    cal_result[0] = 1

    ans = 0
    if n == 0:
        return 1

    for i in range(1,n+1):
        cal_result[i] = a[cal_result[i-1]-1]
        if i == k:
            print(cal_result[i])
            break
        elif cal_result[i] == cal_result[i-1]:
            ans = cal_result[i]

            break
        elif i>1 and cal_result[i] == cal_result[i-2]:
            if (k-i) % 2 ==0:
                ans = cal_result[i]
            else:
                ans = cal_result[i-1]

            break
        elif i >2 and cal_result[i] == cal_result[i-3]:
            if (k-i)%3==0:
                ans = cal_result[i]
            elif (k-i)%3 ==1:
                ans = cal_result[i-2]
            else:
                ans = cal_result[i-1]
            break

        elif i >3 and cal_result[i] == cal_result[i-4]:
            if (k-i)%4 ==0:
                ans = cal_result[i]
            elif (k-i)%4 ==1:
                ans = cal_result[i-3]
            elif (k-i)%4 ==2:
                ans = cal_result[i-2]
            else:
                ans = cal_result[i-1]
            break

        elif i >4 and cal_result[i] == cal_result[i-5]:
            if (k-i)%5==0:
                ans = cal_result[i]
            elif (k-i)%5==1:
                ans = cal_result[i-4]
            elif (k-i)%5==2:
                ans = cal_result[i-3]

            elif (k-i)%5==3:
                ans = cal_result[i-2]

            elif (k-i)%5==4:
                ans = cal_result[i-1]
            break

    return ans

print(fib_dynamic(k))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = [1]
C = [1] + [0] * (N - 1)
i = 0

while C[A[i] - 1] == 0:
    B.append(A[i])
    C[A[i] - 1] = 1
    i = A[i] - 1

ans = 0
for j in range(len(B)):
    if B[j] == A[i]:
        ans += j

if (len(B) - ans != 0) and (K - ans > 0):
    ans += (K - ans) % (len(B) - ans)

print(B[ans])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = [1]
C = [1] + [0] * (N - 1)
i = 0

while C[A[i] - 1] == 0:
    B.append(A[i])
    C[A[i] - 1] = 1
    i = A[i] - 1

ans = 0
for j in range(len(B)):
    if B[j] == A[i]:
        ans += j

if len(B) - ans != 0:
    ans += (K - ans) % (len(B) - ans)

print(B[ans])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
def main():
    n, k = map(int, input().split(" "))
    a = list(map(int, input().split(" ")))
    s = []
    ord = [-1]*(n+1)
    u = 1
    while ord[u] == -1:
        ord[u] = len(s)
        s.append(u)
        u = a[u-1]
    l = len(s) - ord[u]
    if k < l:
        print(s[k])
    else:
        print(s[(k-ord[u])%l+ord[u]])

'''
def main():
    n, k = map(int, input().split(" "))
    a = list(map(lambda i: int(i)-1, input().split(" ")))
    s = [0]
    d = defaultdict(lambda:0)
    x = a[0]
    for i in range(n):
        s.append(x)
        x = a[x]

    bb=None
    for i in range(n):
        d[s[i]] += 1
        if d[s[i]] ==2:
            bb=s[i]
            break

    cc = s.index(bb)
    s[cc]=-1
    dd = s.index(bb)
    loop_len = dd-cc
    s[cc]=s[dd]
        
    if bb ==None or k < cc:
        print(s[k]+1)
    else:
        y = (k-cc) % loop_len
        print(s[y+cc]+1)
'''
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
def main():
    n, k = map(int, input().split(" "))
    a = list(map(lambda i: int(i)-1, input().split(" ")))
    s = [0]
    d = defaultdict(lambda:0)
    x = a[0]
    for i in range(n):
        s.append(x)
        x = a[x]

    bb=None
    for i in range(n):
        d[s[i]] += 1
        if d[s[i]] ==2:
            bb=s[i]
            break
    if bb == None:
        print(s[k]+1)
    else:
        cc = s.index(bb)
        s[cc]=-1
        dd = s.index(bb)
        loop_len = dd-cc
        s[cc]=s[dd]
        

        y = (k-cc) % loop_len
        print(s[y+cc]+1)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
def main():
    n, k = map(int, input().split(" "))
    a = list(map(lambda i: int(i)-1, input().split(" ")))
    s = [0]
    d = defaultdict(lambda:0)
    x = a[0]
    for i in range(n):
        s.append(x)
        x = a[x]
    aa = 0
    bb=None
    for i in range(n):
        d[s[i]] += 1
        if d[s[i]] ==2:
            bb=s[i]
            break
    cc = s.index(bb)
    s[cc]=-1
    dd = s.index(bb)
    loop_len = dd-cc
    s[cc]=s[dd]
    

    y = (k-cc) % loop_len
    print(s[y+cc]+1)

    



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:

def main():
    n, k = map(int, input().split(" "))
    a = list(map(lambda i: int(i)-1, input().split(" ")))
    s = [0]
    u = [0]
    f = True
    loop_len = 1
    z = 0
    x = a[0]
    while f:
        if x in s:
            f = False
            z = s.index(x)
            s = s[z:]
            loop_len = len(s)
            
        else:
            s.append(x)
            x = a[x]
    y = (k-z) % loop_len
    print(s[y]+1)

    



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:

def main():
    n, k = map(int, input().split(" "))
    a = list(map(lambda i: int(i)-1, input().split(" ")))
    s = [0]
    u = [0]
    f = True
    loop_len = 1
    z = 0
    x = a[0]
    while f:
        if x in s:
            f = False
            z = s.index(x)
            s = s[z:]
            loop_len = len(s)
            
        else:
            s.append(x)
            x = a[x]
    y = (k-z) % loop_len
    print(s)
    print(s[y]+1)

    



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    inlis = list(map(int, input().split()))
    flag = 0
    trans = 0
    now = 1
    indic = dict()
    indic[1] = 0

    while flag < 1:
        if trans < k:
            trans += 1
            tsugi = inlis[now-1]
            now = tsugi
            if tsugi not in indic:
                indic[tsugi] = trans
                #print(tsugi, indic)
            else:
                flag = 1
                loop = trans - indic[tsugi]
                yokei = indic[tsugi]
                #print(loop, yokei)

        if trans == k:
            #print(tsugi)
            exit()
    
    loopamari = (k-yokei) % loop

    for _ in range(loopamari):
        tsugi = inlis[now-1]
        now = tsugi
    
    print(now)
    




if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from copy import deepcopy

def main():
    n, k = map(int, input().split())
    a = [int(x) for x in input().split()]
    visited = set()
    i = 1
    cnt = 0
    # 途中で前に行ったところに行ったら終わり
    # kが小さくてcnt == kになってもおわり
    while True:
        if i in visited:
            break
        visited.add(i)
        cnt += 1
        i = a[i-1]
        if cnt == k:
            print(cnt)
            exit()
    k -= cnt
    period = list()
    period.append(i)
    i = a[i-1]
    # 何回で同じ場所に戻るか
    while True:
        if i == period[0]:
            break
        period.append(i)
        i = a[i-1]
    # 余り
    mod = k % len(period)
    ans = period[mod]
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from copy import deepcopy

def main():
    n, k = map(int, input().split())
    a = [int(x)-1 for x in input().split()]
    visited = set()
    i = 0
    cnt = 0
    # 途中で前に行ったところに行ったら終わり
    # kが小さくてcnt == kになってもおわり
    while True:
        if i in visited:
            break
        visited.add(i)
        cnt += 1
        i = a[i]
        if cnt == k:
            print(cnt)
            exit()
    k -= cnt
    period = list()
    period.append(i)
    i = a[i]
    # 何回で同じ場所に戻るか
    while True:
        if i == period[0]:
            break
        period.append(i)
        i = a[i]
    # 余り
    mod = k % len(period)
    ans = period[mod]
    print(ans+1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
259
N,K=map(int,input().split())
A=[0]+list(map(int,input().split()))
m=1
towns=[1]
flg=[0]*(N+1)
flg[1]=1
while 1:
    m=A[m]
    towns.append(m)
    if flg[m]==1:
        l=len(towns)-towns.index(m)-1
        break
    flg[m]=1
pl=towns.index(m)
if K<=pl:
    print(pl)
else:
    print(towns[(K-pl)%l+pl])

Submission status: Wrong Answer

Solution code:
259
N,K=map(int,input().split())
A=[0]+list(map(int,input().split()))
m=1
towns=[]
while 1:
    towns.append(m)
    m=A[m]
    if m in towns:
        l=len(towns)-towns.index(m)
        break
print(towns[K%l])

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
a=list(map(int, input().split()))
b=[False]*n
pos=0
for i in range(n):
    if b[pos]!=False:
        loop=i-b[pos]+1
        break
    b[pos]=i+1
    pos=a[pos]-1
    k-=1

for i in range(k%loop):
    pos=a[pos]-1
print(pos+1)

Submission status: Wrong Answer

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

tt = [0] * n
tt[0] = 1
ans_l = [1]
i = 1
c = 0

while True:
   # if c == k:
#        break
    c += 1
    i = a[i-1]
    if tt[i-1] == False:
        tt[i-1] = 1
        ans_l.append(i)
    else:
        break

#print(c,k)
d = ans_l.index(i)
k -= d
ans = k % (len(ans_l)-d)
print(ans_l[ans+d])

Submission status: Wrong Answer

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

tt = [False] * n
tt[0] = True
ans_l = [1]
i = 1

while True:
    i = a[i-1]
    if tt[i-1] == False:
        tt[i-1] = 1
        ans_l.append(i)
    else:
        break

d = ans_l.index(i)
k -= d
ans = k % (len(ans_l)-d)
print(ans_l[ans+d])

Submission status: Wrong Answer

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

tt = [0] * n
tt[0] = 1
ans_l = [1]
i = 1

while True:
    i = a[i-1]
    if tt[i-1] == False:
        tt[i-1] = 1
        ans_l.append(i)
    else:
        break

d = ans_l.index(i)
k -= d
ans = k % (len(ans_l)-d)
print(ans_l[ans+d])

Submission status: Wrong Answer

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

tt = [0] * n
tt[0] = 1
ans_l = [1]
i = 1

while True:
    i = a[i-1]
    if tt[i-1]:
        break
    tt[i-1] = 1
    ans_l.append(i)

#print(ans_l,i,tt)
d = ans_l.index(i)
k -= d
ans = k % (len(ans_l)-d)
print(ans_l[ans+d])

Submission status: Wrong Answer

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


tt = [0] * n
tt[0] = 1
ans_l = [1]
i = 0

while True:
    i = a[i]-1
    if tt[i]:
        break
    tt[i] = 1
    ans_l.append(i+1)

i += 1
#print(ans_l,i,tt)
d = ans_l.index(i)
k -= d
ans = k % (len(ans_l)-d)
print(ans_l[ans+d])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
a_m = max(a)
tt = [-1] * a_m
tt[0] = 0
ans_l = [1]

i = 0
c = 0
while True:
    if tt[a[i]-1] != -1:
        loop = c - tt[a[i]-1] + 1
        k -= tt[a[i]-1]
        break
    tt[a[i]-1] = c + 1
    c += 1
    ans_l.append(a[i])
    i = a[i] - 1

ans_c = k % loop
#print(ans_l,ans_c,tt,loop,k)
print(ans_l[ans_c+tt[a[i]-1]])

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    a_list = list(map(int, input().split()))
    visited_list = [-1] * n  # 何ワープ目で訪れたか
    visited_list[0] = 0
    city, loop, non_loop = 1, 0, 0  # 今いる街、何ワープでループするか、最初にループするまでのワープ数

    for i in range(1, n + 1):
        city = a_list[city - 1]
        if visited_list[city - 1] != -1:
            loop = i - visited_list[city - 1]
            non_loop = visited_list[city - 1] - 1
            break
        else:
            visited_list[city - 1] = i

    if k <= non_loop:
        for _ in range(k):
            city = a_list[city - 1]
    else:
        for _ in range(non_loop + (k - non_loop) % loop + loop):
            city = a_list[city - 1]

    print(city)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

ll = [a[0]]

for i in range(1, n):
    cnt = ll[i-1]
    if cnt == a[cnt-1]:
        print(cnt)
        exit()
    if a[cnt-1] == a[0]:
        break
    else:
        ll.append(a[cnt-1])

print(ll[k%3-1])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [a - 1 for a in A]
L = [None]*(N+1)
s = 0
L[0] = s
index = -1
for i in range(min(N,K)):
  e = i+1
  s = A[s]
  if s not in L:
    L[i+1] = s
  else:
    index = L.index(s)
    break
if index == -1:
  g = K%e
  print(L[g]+1)
else:
  g = index + (K-index)%(e-index)
  print(L[g]+1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [a - 1 for a in A]
L = []
s = 0
L.append(s)
index = -1
for i in range(N):
  s = A[s]
  if s not in L:
    L.append(s)
  else:
    index = L.index(s)
    break

if index == -1:
  g = K%len(L)
  print(L[g]-1)
else:
  g = index + (K-index)%(len(L)-index)
  print(L[g]+1)

Submission status: Wrong Answer

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

check = [0] * (n+1)
check[1] = 1
x = [1]
now = 1
while check[a[now]] == 0 and len(x)<k:
    x.append(a[now])
    now = a[now]
    check[now] = 1

if len(x)<k:
    if a[now] != x[0]: 
        k -= len(x)
        x = x[x.index(a[now]):]
print(x[k%len(x)] if k >= len(x) else x[k])

Submission status: Wrong Answer

Solution code:
from copy import deepcopy

def main():
    n, k = map(int, input().split())
    a = [int(x)-1 for x in input().split()]
    cnt = 0
    next_posi = 0
    # 何回できたことのあるところにいくか
    visited = set()
    visited.add(0)
    for _ in range(n):
        next_posi = a[next_posi]
        cnt += 1
        if next_posi not in visited:
            visited.add(next_posi)
        else:
            break
    # print('cnt', cnt)
    cnt2 = 0
    visited_posi = deepcopy(next_posi)
    for _ in range(k):
        next_posi = a[next_posi]
        cnt2 += 1
        if next_posi == visited_posi:
            break
    if k - cnt >= cnt2:       
        # print('cnt2', cnt2)
        mod = (k-cnt) % cnt2
        # print('mod', mod)
        for _ in range(mod):
            next_posi = a[next_posi]
        print(next_posi+1)
    else:
        for _ in range(k-cnt):
            next_posi = a[next_posi]
        print(next_posi+1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    a = [int(x)-1 for x in input().split()]
    cnt = 0
    next_posi = 0
    # 何回で元に戻るか
    for _ in range(n):
        next_posi = a[next_posi]
        cnt += 1
        if next_posi == 0:
            break
    mod = k % cnt
    next_posi = 0
    # 余りぶん進むと
    for _ in range(mod):
        next_posi = a[next_posi]
    print(next_posi+1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    a = [int(x)-1 for x in input().split()]
    pow_num = 43
    posi = [[0] * n for i in range(pow_num)]
    for i in range(pow_num):
        if i == 0:
            # 初期値設定。
            for j in range(n):
                posi[i][j] = a[j]
            continue
        for j in range(n):
            posi[i][j] = posi[i-1][posi[i-1][j]]
    # print(posi)
    power_count = list()
    for i in range(pow_num):
        # kを2進数で考えた時の右から1が立っているかどうか
        if k & (1 << i):
            power_count.append(i)
            continue
    next_posi = 0
    for posi_idx in power_count:
        next_posi = posi[posi_idx][next_posi]
        # print(power_count)
        # print(next_posi)

    print(next_posi+1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

visit = [0]

current_loc = 0
while warp[current_loc] != -1:
  visit.append(warp[current_loc])
  warp[current_loc] = -1
  current_loc = visit[-1]

res = visit.index(visit[-1])
cycle = visit[res + 1:]
if K <= res:
  print(visit[K])
else:
  answer = cycle[(K - res - 1) % (len(visit) - 1 - res)] + 1
  print(answer)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
rec = [0]
i = 0
flag = [0 for _ in range(n + 1)]
while True:
    if flag[rec[i]] == 0:
        flag[rec[i]] = 1
        rec.append(a[rec[i]] - 1)
    else:
        start = rec.index(a[rec[i]] - 1)
        loop = rec[start:]
        pre = rec[:start]
        break
    i += 1
if len(loop) == 0:
    print(rec[k])
else:
    count = (k - len(pre)) % len(loop)
    print(loop[count] + 1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
rec = [0]
i = 0
flag = [0 for _ in range(n + 1)]
while True:
    if flag[rec[i]] == 0:
        flag[rec[i]] = 1
        rec.append(a[rec[i]] - 1)
    else:
        start = rec.index(a[rec[i]] - 1)
        loop = rec[start:]
        pre = rec[:start]
        break
    i += 1
count = (k - len(pre)) % len(loop)
print(loop[count] + 1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
# rec = [a[0] - 1]
# i = 0
rec = [0]
for i in range(n):
    if a[rec[i]] - 1 not in rec:
        rec.append(a[rec[i]] - 1)
    else:
        start = rec.index(a[rec[i]] - 1)
        loop = rec[start:]
        pre = rec[:start]
        break
count = (k - len(pre)) % len(loop)
# print(loop)
# print(pre)
print(loop[count] + 1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
# rec = [a[0] - 1]
i = 0
rec = [i]
while True:
    if a[rec[i]] - 1 not in rec:
        rec.append(a[rec[i]] - 1)
    else:
        break
    i += 1
# print(rec)
count = (k) % len(rec)
print(rec[count] + 1)

Submission status: Wrong Answer

Solution code:
import numpy as np
N,K = map(int,input().split())
A = np.array(list(map(int,input().split())))
num = 0
position = 1
ok = False
position_list = np.array([])
for i in range(N):
    position = A[position -1]
    if position in position_list and ok == False:
        former = list(zip(*np.where(position_list == position)))[0][0]
        later = position
        ok = True
    position_list = np.append(position_list,position)
step = later - former
mod = (K - 1 - former) % step
print(int(position_list[former + mod]))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
p = 1
t = [1]
u = set()
u.add(1)
for i in range(n):
  p = a[p - 1]
  if p not in u:
    t.append(p)
    u.add(p)
  else:
    break
c = t.index(p)
r = len(t) - c
s = len(t) - r
ans = t[(k - s + 1) % r + s - 1]
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
p = 1
t = [1]
u = set()
for i in range(k):
  p = a[p - 1]
  if p not in u:
    t.append(p)
    u.add(p)
  else:
    break
c = t.index(p)
r = len(t) - c
s = len(t) - r
ans = t[(k - s + 1) % r + s - 1]
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
p = 1
t = [1]
u = set()
for i in range(n):
  p = a[p - 1]
  if p not in u:
    t.append(p)
    u.add(p)
  else:
    break
c = t.index(p)
r = len(t) - c
s = len(t) - r
ans = t[(k - s + 1) % r + s - 1]
print(ans)

Submission status: Wrong Answer

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

now = 1
path = [1]

for i in range(N + 2):
    now = A[now]
    path.append(now)

if i >= K:
    print(path[K])
    quit()

now = path.pop()
lo_start = i - 1
while path[lo_start] != now:
    lo_start -= 1

lo_len = i - lo_start
loop = path[lo_start:]

ans = loop[(K - path.index(now) - 1) % lo_len]

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
p = 1
t = [1]
u = set()
for i in range(n):
  p = a[p - 1]
  if p not in u:
    t.append(p)
    u.add(p)
  else:
    break
c = t.index(p)
r = len(t) - c
s = len(t) - r
ans = t[(k + 1 - s) % r + s - 1]
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
p = 1
t = [1]
for i in range(n):
  p = a[p - 1]
  if p not in t:
    t.append(p)
  else:
    break
c = t.index(p)
r = len(t) - c
s = len(t) - r
ans = t[(k + 1 - s) % r + s - 1]
print(ans)

Submission status: Wrong Answer

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

now = 1
path = [1]

for i in range(N + 2):
    now = A[now]
    path.append(now)

if i >= K:
    print(path[K])
    quit()

now = path.pop()
lo_start = i - 1
while path[lo_start] != now:
    lo_start -= 1

lo_len = i - lo_start
loop = path[lo_start:]

ans = loop[(K - lo_start) % lo_len]

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
p = 1
t = [1]
for i in range(k):
  p = a[p - 1]
  if p not in t:
    t.append(p)
  else:
    break
c = t.index(p)
r = len(t) - c
s = len(t) - r
ans = t[(k + 1 - s) % r + s - 1]
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
l = [0]
x = 0
for i in range(k):
    if l.count(x) >= 2:
        break
    l.append(a[x]-1)
    x = a[x]-1
else:
    print(x)
    exit()
loop = l[l.index(x):-1]
print(loop[(k-l.index(x))%len(loop)]+1)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
t = []
i = 1
iya = [0] * n

while True:
    if iya[i-1]:
        c = int(len(t)) - t.index(i)
        break
    t.append(i)
    iya[i-1] = 1
    i = a[i-1]

mmm = int(len(t)) - c
k -= mmm
ans = k % c
#print(t,c)
print(t[ans+mmm])
#print(t)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Mon Sep  7 18:05:47 2020

@author: liang
"""

N, K = map(int, input().split())
A = [int(x) for x in input().split()]
visited = [-1]*N
loop = list()

cur = 1
cnt = 0
while visited[cur-1] == -1:
#while True:
    visited[cur-1] = cnt
    loop.append(cur)
    cnt += 1
    cur = A[cur-1]
    if cnt == K:
        print(cur)
        print(loop)
        break
else:
    #print(loop)
    loop = loop[visited[cur-1]:]
    T = cnt -visited[cur-1]
    #print(loop, T)
    ans = loop[(K - cnt)%T]
    print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
t = []
i = 1
iya = [0] * n

while True:
    if iya[i-1]:
        c = int(len(t)) - t.index(i)
        break
    t.append(i)
    iya[i-1] = 1
    i = a[i-1]

c_2 = k-c
ans = c_2 % c
print(t[ans])
#print(t)

Submission status: Wrong Answer

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

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

    flag = False
    for pos in range(len(cur)):
        if cur[pos] == x:
            flag = True
            break

    if flag: break
    cur.append(x)

print(cur[(k - pos) % (len(cur) - pos) + pos] + 1)

Submission status: Wrong Answer

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

kyori = [-1] *len(A)

junban =[]

x=0
d=0
while kyori[x] == -1:
    
    junban.append(x)
    kyori[x]=d
    d+=1
    x=A[x]-1
    
    
roop_x = x
roop_d = d - kyori[x]

for k in range(100):

    if k<d:
        print(junban[k]+1)
        
    else:
        
        k0 = kyori[x]
        k1 = (k-k0)%roop_d
        
        print(junban[k0+k1]+1)

Submission status: Wrong Answer

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

kyori = [-1] *len(A)

junban =[]

x=0
d=0
while kyori[x] == -1:
    
    junban.append(x)
    kyori[x]=d
    d+=1
    x=A[x]-1
    
    
roop_x = x
roop_d = d - kyori[x]

if k<d:
    print(junban[k]+1)
    
else:
    
    k0 = kyori[x]-1
    k1 = (k-k0)%roop_d
    
    print(junban[k0+k1]+1)

Submission status: Wrong Answer

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

check = [0] * N #町を訪問するのが初めてか
check[0] = 1
li = [] #町の移動順
li.append(1)
i, j = 1, 1 #現在の町の番号と移動回数

while True:
  k = A[i-1] #移動先の町の番号
  if check[k-1] == 1:
    #前に訪問したことがある場合
    n = li.index(k) #ループに入った時点での移動回数
    break
  check[k-1] = 1
  li.append(k)
  i = k
  j += 1

if K <= n-1: print(li[K])
else:
  if j > n :
    print(li[((K - (n-1)) % (j-n)) + n - 1])
  elif j == n:
    print(li[(K % j) + n])