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:
def isInt(x):
    try:
        float(x)
    except ValueError:
        return False
    else:
        return float(x).is_integer()

def main():
    try:
        str = input().split()
    except ValueError:
        return

    if isInt(str[0]):
        N = int(str[0])
    else:
        return

    if not( 2 <= N and N <= 2*10**5):
        return

    if isInt(str[1]):
        K = int(str[1])
    else:
        return

    if not( 1 <= K and K <= 10**18):
        return

    try:
        str = input().split()
    except ValueError:
        return

    A = [0] * N
    for i in range(N):
        if isInt(str[i]):
            ai = int(str[i])
            if 1 <= ai and ai <= N:
                A[i] = ai
            else:
                return
        else:
            return
        
    now = 1
    path = [1]
    for i in range(K):
        now = A[now-1]
        if now in path:
            break
        else:
            path.append(now)
            
    r = K % len(path)
    now = path[r]
    print(now)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

l = input().split()

L = {}
for i,v in enumerate(l):
    L[i+1] = int(v)
    
f = d % n 

print(L[f])

Submission status: Wrong Answer

Solution code:
def isInt(x):
    try:
        float(x)
    except ValueError:
        return False
    else:
        return float(x).is_integer()

def main():
    try:
        str = input().split()
    except ValueError:
        return

    if isInt(str[0]):
        N = int(str[0])
    else:
        return

    if not( 2 <= N and N <= 2*10**5):
        return

    if isInt(str[1]):
        K = int(str[1])
    else:
        return

    if not( 1 <= K and K <= 10**18):
        return

    try:
        str = input().split()
    except ValueError:
        return

    A = []
    for i in range(N):
        if isInt(str[i]) and 1 <= int(str[i]) and int(str[i]) <= N:
            A.append(int(str[i]))
        else:
            return
    print(A)
    now = 1
    for i in range(K):
        now = A[now-1]

    print(now)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
order = [-1] * (n+1)
v = [0] * (n+1)
v[0] = 1
order[v[0]] = 0
# iは順番
for i in range(1, n+1):
    v[i] = A[v[i-1] - 1]
    if order[v[i]] != -1:
        old_i = order[v[i]]
        c = i - old_i
        break
    order[v[i]] = i
if k < old_i:
    print(v[k-1])
else:
    print(v[(k - old_i)%c + old_i])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
l = sorted(list(map(int,input().split())))
g = K%len(l)
print(l[-(g)])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
where_go = list(map(int, input().split()))
now = 0
PATH = []
start = 0
roop = 0
count = 0

while now not in PATH and count < K:
    PATH.append(now)
    now = where_go[now] -  1
    count += 1
    
if count < K:
    start = PATH.index(now)
    roop = len(PATH) - start
    K = (K - start) % roop
    while count < K:
        now = where_go[now] -  1
        count += 1

print(now+1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
a=[p-1 for p in a]
l=[0]
i=a[0]
p=-1
q=-1
for x in range(n):
    if i in l:break
    l.append(i)
    i=a[i]
d=-1
for x in range(n):
    if a[x]==i:
        d=x
        break
f=l[:d]
b=l[d:]

if len(f)==0:
    print(b[k%len(b)]+1)
    exit()

if len(b)==0:
    print(f[k%len(f)]+1)
    exit()

if k<=d:
    print(l[k]+1)
    exit()

k-=len(f)
print(b[k%len(b)]+1)

Submission status: Wrong Answer

Solution code:
import sys

n, k, *aaa = map(int, sys.stdin.buffer.read().split())
aaa = [0] + aaa
p = 1
while k:
    if k & 1:
        p = aaa[p]
    aaa = [aaa[i] for i in range(n + 1)]
    k >>= 1
print(p)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
where_go = list(map(int, input().split()))
now = 0
PATH = []
start = 0
roop = 0
count = 0

while now not in PATH:
    PATH.append(now)
    now = where_go[now] -  1

start = PATH.index(now)
roop = len(PATH) - start
if roop != 0:
    K = (K - start) % roop
else:
    K = 0

while count < K:
    now = where_go[now] -  1
    count += 1

print(now+1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
where_go = list(map(int, input().split()))
now = 0
PATH = []
start = 0
roop = 0
count = 0

while now not in PATH:
    PATH.append(now)
    now = where_go[now] -  1

start = PATH.index(now)
roop = len(PATH) - start
K = (K - start) % roop

while count < K:
    now = where_go[now] -  1
    count += 1

print(now+1)

Submission status: Wrong Answer

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

now = 0
route = []
while True:
    route.append(now)
    to = A[now]
    if to == None:
        break
    else:
        A[now] = None
        now = to

loop_start = route.index(now)
loop_size = len(route) - loop_start

n_for_loop = min(K, loop_start)
n_in_loop = (K - n_for_loop) % loop_size

print(route[n_for_loop + n_in_loop] + 1)

Submission status: Wrong Answer

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

now = 0
route = []
while True:
    route.append(now)
    to = A[now]
    if to == None:
        break
    else:
        A[now] = None
        now = to

print(route)

loop_start = route.index(now)
loop_size = len(route) - loop_start

n_for_loop = min(K, loop_start)
n_in_loop = (K - n_for_loop) % loop_size

print(route[n_for_loop + n_in_loop] + 1)

Submission status: Wrong Answer

Solution code:
import sys
N,K=map(int,(input().split()))
A=list(map(int,input().split()))
A.insert(0,0)
loc=1
log=[0]
loop_flg=0
i=1
while(i<=K):
  if(loc not in log):
    # print("ログに"+str(loc)+"を追加します")
    log.append(loc)
    # print(log)
    loc=A[loc]
    i+=1
  elif(loc in log and loop_flg==0):
    # print("ループを"+str(log.index(loc))+"番目から"+str(i-1)+"番目の間に発見しました")
    loop_longth=i-log.index(loc)
    K = i+((K-(log.index(loc)-1))%loop_longth)
    # print(K)
    # print(log.index(loc)-1)
    loc=log[(log.index(loc)-1)+((K-log.index(loc))%loop_longth)]
    print(log.index(loc))
    sys.exit()
  # print(str(loc)+" "+str(i)+" "+str(K))
print(loc)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
L_path= tuple(map(int, input().split()))
loop = [1]
town = 1
while not L_path[town - 1] in loop:
    loop.append(L_path[town - 1])
    town = L_path[town - 1]
st = loop.index(L_path[town - 1])
tk = (K - st) % (len(loop) - st)
print(loop[st + tk])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())         # n=町の個数 , k=テレポート回数
a_list = list(input().split())         # 各町のテレポート先リスト
dum = a_list[0]                      # dum=次のテレポート先
k = k - 1                              # テレポート回数をデクリメント
ans_list = "1"                         # 訪れた先（インデックス）を ans_list に格納

while(1):
    if k == 0:
        print(dum)
        break
    ans_list = ans_list + dum
    dum = a_list[int(dum)-1]
    k = k - 1

    if ans_list.count(dum) == 1:
        ind = ans_list.index(dum)
        loop = ans_list[ind:]
        len_loop = len(loop)
        k = k % len_loop
        ans_list = ""

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
where_go = list(map(int, input().split()))
now = 0
now_list = []
start = 0
roop = 0
count = 0
 
while now not in now_list:
    now_list.append(now)
    now = where_go[now] -  1

start = now_list.index(now)
roop = len(now_list) - start
 
 
 
while count < (K - start) % roop:
    now = where_go[now] -  1
    count += 1

print(now+1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
where_go = list(map(int, input().split()))
now = 0
now_list = []
start = 0
roop = 0
count = 0

while now not in now_list:
    now_list.append(now)
    now = where_go[now] -  1

start = now_list.index(now)
roop = len(now_list) - start

print(start)
print(roop)
print((K - start) % roop)

while count < (K - start) % roop:
    now = where_go[now] -  1
    count += 1

print(now+1)

Submission status: Wrong Answer

Solution code:
import sys
N,K=map(int,(input().split()))
A=list(map(int,input().split()))
A.insert(0,0)
loc=1
log=[0]
loop_flg=0
i=1
while(i<=K):
  if(loc not in log):
    log.append(loc)
    loc=A[loc]
    i+=1
  elif(loc in log and loop_flg==0):
    loop_longth=i-log.index(loc)
    loc=A[log.index(loc)+((K-log.index(loc))%loop_longth)+1]
    sys.exit()

print(loc)

Submission status: Wrong Answer

Solution code:
def main():
    N, K, *A = map(int, open(0).read().split())
    visited = [-1]*N
    pos = A[0]
    visited[0] = 0
    visited[pos - 1] = 1
    step = 2
    order = 0
    start = 0
    step_to_start = 0
    while True:
        pos = A[pos - 1]
        if visited[pos - 1] == -1:
            visited[pos - 1] = step
        else:
            step_to_start = visited[pos - 1]
            order = step - step_to_start
            start = pos
            break
        step += 1
    # print(start, step_to_start, order)

    if K <= step_to_start:
        ans = 0
        for i in range(K):
            ans = A[ans - 1]
        print(ans)
        return
    remain = (K - step_to_start) % order
    ans = start
    for i in range(remain):
        ans = A[ans - 1]
    print(ans)

if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
def main():
    N, K, *A = map(int, open(0).read().split())
    visited = [-1]*N
    pos = A[0]
    visited[0] = 0
    visited[pos - 1] = 1
    step = 2
    order = 0
    start = 0
    step_to_start = 0
    while True:
        pos = A[pos - 1]
        if visited[pos - 1] == -1:
            visited[pos - 1] = step
        else:
            step_to_start = visited[pos - 1]
            order = step - step_to_start
            start = pos
            break
        step += 1
    # print(start, step_to_start, order)

    if K <= step_to_start:
        ans = 0
        for i in range(K):
            ans = A[ans - 1]
        print(ans)
        return
    order = 1
    pos = start
    while A[pos - 1] != start:
        pos = A[pos - 1]
        order += 1
    # print(order)
    remain = (K - step_to_start) % order
    ans = start
    for i in range(remain):
        ans = A[ans - 1]
    print(ans)

if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
from math import log2
n, k = map(int, input().split())
To = []
A = list(map(int, input().split()))
A = list(map(lambda x: x - 1, A))
To.append(A)
for i in range(int(log2(k))):
    N = [0 for i in range(n)]
    for j in range(n):
        N[j] = To[i][To[i][j]]
    To.append(N)

v = 0
for i in reversed(range(int(log2(k)) + 1)):
    l = 1 << i
    print(i)
    if l <= k:
        v = To[i][v]
        k -= l
    else:
        pass

print(v + 1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    D = 1
    LOG = {}
    PATH = []
    for i in range(N+1):
        PATH.append(D)
        LOG.setdefault(D,i)

        if LOG[D] == i:
            D = A[D-1]
            # print(PATH,LOG, i)
            continue
        else:
            # print("break",D, i)
            start = D
            break
    if i-LOG[D] <= K:
        K = (K-i)%(i-LOG[D])
        LAST = PATH[LOG[D]+K]
    else:
        LAST = PATH[K]
    print(LAST)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    D = 1
    LOG = {}
    PATH = []
    for i in range(N+1):
        PATH.append(D)
        LOG.setdefault(D,i)

        if LOG[D] == i:
            D = A[D-1]
            # print(PATH,LOG, i)
            continue
        else:
            # print("break",D, i)
            start = D
            break
    if i-LOG[D] < K:
        K = (K-i)%(i-LOG[D])
        LAST = PATH[LOG[D]+K]
    else:
        LAST = PATH[K]
    print(LAST)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, K, *A = map(int, open(0).read().split())
    visited = [0]*N
    pos = A[0]
    visited[0] = visited[pos - 1] = 1
    while visited[pos - 1] < 2:
        pos = A[pos - 1]
        visited[pos - 1] += 1
    # print(pos)
    begin = pos
    step_to_begin = 0
    if begin != 1:
        pos = 0
        while pos != begin:
            pos = A[pos - 1]
            step_to_begin += 1
        step_to_begin += 1
    # print(begin, step_to_begin)

    if K <= step_to_begin:
        ans = 0
        for i in range(K):
            ans = A[ans - 1]
        print(ans)
        return
    order = 1
    pos = begin
    while A[pos - 1] != begin:
        pos = A[pos - 1]
        order += 1
    # print(order)
    remain = (K - step_to_begin) % order
    ans = begin
    for i in range(remain):
        ans = A[ans - 1]
    print(ans)

if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
s = 1
c = 0
d = {}
t = []
# 順番をdictで持つ
while not s in d.keys():
    d[s] = c
    t.append(s)
    c += 1
    s = a[s-1]
l = c - d[s]
m = len(d.keys()) - l
if m >= k:
    print(t[m])
else:
    print(t[(k - m) % l + m])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())         # n=町の個数 , k=テレポート回数
a_list = list(input().split())         # 各町のテレポート先リスト
dum = a_list[0]                        # dum=テレポート先
k = k - 1                              # テレポート回数をデクリメント
ans_list = a_list[0]                   # テレポート先を ans_list に格納

while(1):
    if k == 0:
        print(dum)
        exit()
    dum = a_list[int(dum)-1]
    k = k - 1

    if ans_list.count(dum) == 1:
        ind = ans_list.index(dum)
        loop = ans_list[ind:]
        len_loop = len(loop)
        k = k % len_loop
        ans_list = ""
    ans_list = ans_list + dum

Submission status: Wrong Answer

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

D = 60
MAX_N = 200005
to = [[0]*MAX_N]*D
# to = [[0]*D]*MAX_N
# to = [D][MAX_N]

for i in range(n):
    to[0][i] = a[i]-1
    print(a[i]-1)
for i in range(D-1):
    for j in range(n):
        to[i+1][j] = to[i][to[i][j]]

v = 0
for i in range(D-1):
    l = 1<<i
    if (l <= k):
        v = to[i][v]
        k -= l
    # print(i)
    
print(v+1)

Submission status: Wrong Answer

Solution code:
def main():
    N, K, *A = map(int, open(0).read().split())
    visited = [0]*N
    pos = A[0]
    visited[0] = visited[pos - 1] = 1
    while visited[pos - 1] < 2:
        pos = A[pos - 1]
        visited[pos - 1] += 1
    # print(pos)
    begin = pos
    step_to_begin = 0
    if begin != 0:
        pos = 0
        while pos != begin:
            pos = A[pos - 1]
            step_to_begin += 1
        step_to_begin += 1
    # print(begin, step_to_begin)

    if K <= step_to_begin:
        ans = 0
        for i in range(K):
            ans = A[ans - 1]
        print(ans)
        return
    order = 1
    pos = step_to_begin
    while A[pos - 1] != step_to_begin:
        pos = A[pos - 1]
        order += 1
    # print(order)
    remain = (K - step_to_begin) % order
    ans = step_to_begin
    for i in range(remain):
        ans = A[ans - 1]
    print(ans)

if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    D = 1
    LOG = {}
    PATH = []
    for i in range(N):
        PATH.append(D)
        LOG.setdefault(D,i)

        if LOG[D] == i:
            D = A[D-1]
            # print(PATH,LOG, i)
            continue
        else:
            # print("break",D, i)
            start = D
            break
    if i-LOG[D] < K:
        K = (K-i)%(i-LOG[D])
        LAST = PATH[LOG[D]+K]
    else:
        LAST = PATH[K]
    print(LAST)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
l = list(map(int,input().split()))
memo1 = [0]*(n+1)
memo2 = [0]*(n+1)
tmp = 0
t = 1
index = 0

for i in range(n+1):
    tmp = l[t-1]
    if memo1[t-1] != tmp:
        memo1[t-1] = tmp
        memo2[i] = tmp
        t = tmp 
    else:
        memo2[i] = tmp
        indexes = [i for i, x in enumerate(memo2) if x == memo1[t-1]]
        index = indexes[1] - indexes[0]
        break

tmp2 = k - indexes[1]+1
if tmp2 <= 0:
    print(memo2[k-1])
else:
    tmp3 = tmp2%index
    if tmp3 == 0:
        print(memo2[indexes[0]+1])
    else:
        print(memo2[indexes[0]+tmp3+1])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
l = list(map(int,input().split()))
memo1 = [0]*(n+1)
memo2 = [0]*(n+1)
tmp = 0
t = 1
index = 0

for i in range(n+1):
    tmp = l[t-1]
    if memo1[t-1] != tmp:
        memo1[t-1] = tmp
        memo2[i] = tmp
        t = tmp 
    else:
        memo2[i] = tmp
        indexes = [i for i, x in enumerate(memo2) if x == memo1[t-1]]
        index = indexes[1] - indexes[0]
        break

print(memo2)
tmp2 = k - indexes[1]+1
if tmp2 <= 0:
    print(memo2[k-1])
else:
    tmp3 = tmp2%index
    if tmp3 == 0:
        print(memo2[indexes[0]+1])
    else:
        print(memo2[indexes[0]+tmp3+1])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
tele = list(map(int, input().split()))
	
city = 1
visited_cities = []
foo = [0]*n
while foo[city - 1] < 1:
	visited_cities.append(city)
	foo[city - 1] += 1
	city = tele[city - 1]
	k -= 1
	
visited_cities = visited_cities[visited_cities.index(city):]	
print (visited_cities[k % len(visited_cities)])

Submission status: Wrong Answer

Solution code:
#167
#d
n,k = map(int, input().split())
ln = list(map(int, input().split()))# まちi にいるとき次に町ln[i-1]へいく

# n,k = 10*4,10**18
# ln = list(range(2,n+1))
# ln.append(1)

visit = [0]*n 
now= 1 # 街iにいる
before =[] # ループに入る前（1回めのループ含む）
roop =[] #　ループ内
while visit[now -1] != 2:
    if visit[now -1] ==  0:
        before.append(now)
    elif visit [now-1] == 1:
        roop.append(now)
    visit[now -1]+= 1
    now = ln[now-1]
if k < len(before):
    print(before[k])
else :
    print(roop[((k-len(before))%len(roop)) -1])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
tele = input().split()
	
city = "1"
visited_cities = []
while city not in visited_cities:
	visited_cities.append(city)
	city = tele[int(city) - 1]
	k -= 1
	
visited_cities = visited_cities[visited_cities.index(city):]	
print (visited_cities[k % len(visited_cities)])

Submission status: Wrong Answer

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

#訪れた町
visited = [0]*n
visited[0]=1
root=[0]

next=a[0]-1
same=0
cnt=0
while cnt<k:
    if visited[next]==1:

        same=next

        root.append(next)

        index = root.index(same)

        break
    else:
        visited[next]=1
        root.append(next)

    next = a[next]-1
    cnt+=1

# print(visited)
# print(root)
# print(index)
if cnt==k:
    print(next)
    exit(0)

roops=len(root) - index -1 

if roops!=0:
    moves = index + ( (k-index)%roops )
else:
    moves= index

now=0

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

print(now+1)

Submission status: Wrong Answer

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

#訪れた町
visited = [0]*n
visited[0]=1
root=[0]

next=a[0]-1
same=0
cnt=0
while cnt<k:
    if visited[next]==1:

        same=next

        root.append(next)

        index = root.index(same)

        break
    else:
        visited[next]=1
        root.append(next)

    next = a[next]-1
    cnt+=1



# print(visited)
# print(root)
# print(index)
if same!=next:
    print(next)
    exit(0)

roops=len(root) - index -1 

if roops!=0:
    moves = index + ( (k-index)%roops )
else:
    moves= index

now=0

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

print(now+1)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
A = list(map(int,input().split()))
roop = 0
port = [A[0]]
 
for _ in range(N):
    port.append(A[port[-1]-1] )
    if port.count(A[port[-2]-1]) == 2:#A in port
        port.pop()
        roop = len(port)-port.index(A[port[-1]-1])
        break
if len(A)>=K:
    print(port[K-1])
else:
    Kroop = K-len(port)-1

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
A = list(map(int,input().split()))
roop = 0
port = [A[0]]
 
for _ in range(len(A)):
    port.append(A[port[-1]-1] )
    if port.count(A[port[-2]-1]) == 2:#A in port
        port.pop()
        roop = len(port)-port.index(A[port[-1]-1])
        break
if len(A)>=K:
    print(port[K-1])
else:
    Kroop = K-len(port)-1

Submission status: Wrong Answer

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

D = 60
MAX_N = 200005
to = [[0]*MAX_N]*D
# to = [[0]*D]*MAX_N
# to = [D][MAX_N]

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

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

v = 0
for i in range(D-1,0,-1):
    l = 1<<i
    if (l <= k):
        v = to[i][v]
        k -= l

print(v+1)

Submission status: Wrong Answer

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

#訪れた町
visited = [0]*n
visited[0]=1
root=[0]

next=a[0]-1
same=0

while True:
    if visited[next]==1:

        same=next

        root.append(next)

        break
    else:
        visited[next]=1
        root.append(next)

    next = a[next]-1

index = root.index(same)

# print(visited)
# print(root)
# print(index)

roops=len(root) - index -1 
moves = index + ( (k-index)%roops )
now=0
for i in range(moves):
    now=a[now]-1

print(now+1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

N, K=map(int, input().split())
A = list(map(int,input().split()))
now=0
rireki=[0]*N
isou=1
hazimete=0
for i in range(4*N):
    if rireki[now]==0:
        rireki[now]=1
        startnum=i
        start=now
    else:
        if hazimete==1:
            startnum=i
            start=now
        hazimte=1
        if rireki[now]==1:
            rireki[now]=2
        else:
            if rireki[now]==2:
                rireki[now]=3
                isou=i-startnum-1
                break
    #print("{}".format(now))
    now=A[now]-1

#print("{}".format(isou))
amari=(K-startnum-1)%isou

for i in range(amari):
    now=A[now]-1
#print("{}".format(amari))
if K>isou:
    print("{}".format(now+1))
else:
    now=0
    for i in range(K):
        now=A[now]-1
    print("{}".format(now+1))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

N, K=map(int, input().split())
A = list(map(int,input().split()))
now=0
rireki=[0]*N
isou=1
hazimete=0
for i in range(4*N):
    if rireki[now]==0:
        rireki[now]=1
        startnum=i
        start=now
    else:
        if hazimete==1:
            startnum=i
            start=now
        hazimte=1
        if rireki[now]==1:
            rireki[now]=2
        else:
            if rireki[now]==2:
                rireki[now]=3
                isou=i-startnum-1
                break
    #print("{}".format(now))
    now=A[now]-1

#print("{}".format(isou))
amari=(K-startnum-1)%isou

for i in range(amari):
    now=A[now]-1
#print("{}".format(amari))
if K>isou:
    print("{}".format(now+1))
else:
    now=0;
    for i in range(K):
        now=A[now]-1
    print("{}".format(now+1))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
tele = {}
i = 1
for j in input().split():
	tele[i] = int(j)
	i += 1
	
city = 1
visited_cities = []
while city not in visited_cities:
	visited_cities.append(city)
	city = tele[city]
	k -= 1
	
visited_cities = visited_cities[visited_cities.index(city):]	
print (visited_cities[k % len(visited_cities)])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

N, K=map(int, input().split())
A = list(map(int,input().split()))
now=0
rireki=[0]*N
isou=1
hazimete=0
for i in range(4*N):
    if rireki[now]==0:
        rireki[now]=1
        startnum=i
        start=now
    else:
        if hazimete==1:
            startnum=i
            start=now
        hazimte=1
        if rireki[now]==1:
            rireki[now]=2
        else:
            if rireki[now]==2:
                rireki[now]=3
                isou=i-startnum-1
                break
#    print("{}".format(now))
    now=A[now]-1

#print("{}".format(isou))
amari=(K-startnum-1)%isou

for i in range(amari):
    now=A[now]-1
#print("{}".format(amari))
print("{}".format(now+1))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

N, K=map(int, input().split())
A = list(map(int,input().split()))
now=0
rireki=[0]*N
isou=1
hazimete=0
for i in range(3*N):
    if rireki[now]==0:
        rireki[now]=1
        startnum=i
        start=now
    else:
        if hazimete==1:
            startnum=i
            start=now
        hazimte=1
        if rireki[now]==1:
            rireki[now]=2
        else:
            if rireki[now]==2:
                rireki[now]=3
                isou=i-startnum-1
                break
#    print("{}".format(now))
    now=A[now]-1

#print("{}".format(isou))
amari=(K-startnum-1)%isou

for i in range(amari):
    now=A[now]-1
#print("{}".format(amari))
print("{}".format(now+1))

Submission status: Wrong Answer

Solution code:
def search_loop(list_route, now):
    list_route.append(list_a[now-1])
    for start in range(len(list_route)):
        for end in range(start+1, len(list_route)):
            if list_route[start] == list_route[end]:
                return start, end - start
    return search_loop(list_route, list_a[now-1])


N, K = map(int, input().split())
list_a = list(map(int, input().split()))
if list_a[0] == 1:
    print(1)
    quit()
list_route = [1]
loop_start, loop_length = search_loop(list_route, 1)
t = (K-loop_start+1) % (loop_length+1)
print(list_route[t])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split())) 
 
x=[0]*(n)
before = 0
now = 0
while x[now] == 0:
    x[now]=1+x[before]
    before=now
    now = a[now]-1
    
y=x[now]-x[0]
z=x[before]-y
if k>y:
    l=(k-y)%z+1
else :
    l=k-y
for i in range(n):
    if x[i] == l+y:
        print(1+i)
        break

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split())) 
 
x=[0]*(n)
before = 0
now = 0
while x[now] == 0:
    x[now]=1+x[before]
    before=now
    now = a[now]-1
    
y=x[now]-x[0]
z=x[before]-y
if k>y:
    l=(k-y)%z+1
else :
    l=k
for i in range(n):
    if x[i] == l+y:
        print(1+i)
        break

Submission status: Wrong Answer

Solution code:
#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #ここにコード
    #input
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    #output
    #ループしたらそのループの周期でKを割る。

    loop = {i:-1 for i in range(N)}
    loop[0] = 0
    position = 0

    for i in range(N+1):
        position = A[position] - 1
        if loop[position] != -1:
            ini = loop[position]
            cycle = i+1 - ini
            break
        else:
            loop[position] = i+1

    M = (K-ini) % cycle

    answer = 0
    for j in range(M+ini):
        answer = A[answer] -1

    answer += 1
    print(answer)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N,K = [int(i) for i in input().split()]
A = [int(i)-1 for i in input().split()]
place = [0]
for _ in range(N):
    place.append(A[place[-1]])
a = place.index(place[-1])
K = a + ((K-a)%(N-a))
print(place[K]+1)

Submission status: Wrong Answer

Solution code:
#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #ここにコード
    #input
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    #output
    #ループしたらそのループの周期でKを割る。

    loop = {i:-1 for i in range(N)}
    loop[0] = 0
    position = 0

    for i in range(N):
        position = A[position] - 1
        if loop[position] != -1:
            ini = loop[position]
            cycle = i+1 - ini
            break
        else:
            loop[position] = i+1

    M = (K-ini) % cycle

    answer = 0
    for j in range(M+ini):
        answer = A[answer] -1

    answer += 1
    print(answer)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N,K = [int(i) for i in input().split()]
A = [int(i)-1 for i in input().split()]
place = [0]
for _ in range(N-1):
    place.append(A[place[-1]])
a = place.index(place[-1])+1
K = a + ((K-a)%(N-a))
print(place[K]+1)

Submission status: Wrong Answer

Solution code:
N,K = [int(i) for i in input().split()]
A = [int(i)-1 for i in input().split()]
place = [0]
for _ in range(N-1):
    place.append(A[place[-1]])
a = place.index(place[-1])+1
K = a + ((K-a)%(N-a))
print(A[K]+1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
tele = {}
for i in range(1, n+1):
	tele[i] = a[i - 1]
	
city = 1
visited_cities = []
while city not in visited_cities:
	visited_cities.append(city)
	city = tele[city]
	k -= 1
	
visited_cities = visited_cities[visited_cities.index(city):]
		
print (visited_cities[k % len(visited_cities)])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
tele = {}
for i in range(1, n+1):
	tele[i] = a[i - 1]
	
city = 1
visited_cities = []
while city not in visited_cities:
	visited_cities.append(city)
	city = tele[city]
	k -= 1
	
for _ in range(visited_cities.index(city)):
	del (visited_cities[0])
		
print (visited_cities[k % len(visited_cities)])

Submission status: Wrong Answer

Solution code:
N,K = [int(i) for i in input().split()]
A = [int(i)-1 for i in input().split()]
place = [0]
for _ in range(N):
    place.append(A[place[-1]])
a = place.index(place[-1])
K = a + ((K-a)%(N-a))
print(A[K]+1)