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:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    count = 0
    visit = []
    loopstart = 0
    nextcity = 1

    for n in range(N):
        count += 1
        visit += [nextcity]
        nextcity = A[nextcity-1]
        if nextcity in visit:
            loopstart = nextcity
            break
    
    # print(visit)
    # print(f'{loopstart=}')
    
    toloop = visit.index(nextcity)
    # print(f'{toloop=}')
    loopsize = len(visit) - toloop
    # print(f'{loopsize=}')
    if loopsize > K:
        # print("loopsize > K")
        nextcity = 1
        for k in range(K):
            nextcity = A[nextcity-1]
        print(nextcity)
    else:
        # print("loopsize < K")
        res = (K - toloop) % loopsize
        # print(f"{res=}")

        nextcity = loopstart

        for k in range(res):
            nextcity = A[nextcity-1]
        
        print(nextcity)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

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

#n=6
#k=727202214173249351
#a=[6, 5, 2, 5, 3, 2]

#n=4
#k=5
#a=[3,2,4,1]

if n<k:
    ai=1
    aj=set()
    for i in range(n):
        ai=a[ai-1]
        if ai in aj:
            ic2=i
            ai2=ai
            break
        else:
            aj.add(ai)

    ai=1
    for i in range(n):
        ai=a[ai-1]
        if ai==ai2:
            ic1=i
            break

    icyc=ic2-ic1
    k2=(k-ic1)%icyc+ic1
else:
    k2=k
    
ai=1
for i in range(k2):
    ai=a[ai-1]
        
print(ai)

Submission status: Wrong Answer

Solution code:

n,k=map(int,input().split())
a=list(map(int, input().split()))
b=[1]
ba={1}
c=0
m=0
# while flag==1:
for i in range(n):
    if a[m] not in ba:
        b.append(a[m])
        ba.add(a[m])
        c+=1
        m=a[m]-1
    else:
        e=a[m]
        break
# print(b,e,c+1)
e=b.index(e)
# print(b,e)
b=b[e:c+1]
f=c+1-e
if 1 not in b:
    ans=(k%f+e-1)%f
    print(b[ans])
else:
    # print(b,f)
    ans=k%f
    print(b[ans])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
visited = [0]*n
visited[0] = 1
x = a[0]
count_before_roop = 1
while True:
    if visited[x-1] == 0:
        visited[x-1] = 1
        x = a[x-1]
        count_before_roop += 1
    else:
        roop_st = x
        break
#print(visited)
#print(count_before_roop)
#print(roop_st)
len_roop = visited.count(1)
k_mod = (k-count_before_roop) % len_roop
#print(k_mod)
here = 1
for i in range(k_mod):
    here = a[here-1]
print(here)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys
def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())
 
n,k = LI()
a = LI()

i = 0
ne = 1
f = [False]*n
c = [0]*n
f[0] = True
c[0] = 0
while(True):
    ne = a[ne-1]
    i += 1
    if(f[ne-1]):
        x = (k - i) % (i - c[ne-1])
        break
    f[ne-1] = True
    c[ne-1] = i

for y in range(x):
    ne = a[ne-1]

print(ne)

Submission status: Wrong Answer

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

is_visited = [-1] * N
order = []
now = 0
a = None
b = None
while True:
    is_visited[now] = len(order)
    order.append(now)
    now = A[now]
    if is_visited[now] >= 0:
        a = len(order) - is_visited[now]
        b = is_visited[now] - 1
        break
if K <= b:
    print(order[K] + 1)
else:
    K -= b
    K %= a
    print(order[K + b] + 1)

Submission status: Wrong Answer

Solution code:

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


s = []
ord = [-1] * (n+1)

v = 1
c = 1
l = 0
while ord[v] == -1:
    ord[v] = len(s)
    s.append(v)
    v = a[v-1]
c = len(s) - ord[v]
l = ord[v]

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

Submission status: Wrong Answer

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

visited = [0]*N
i = 0
rootin = [0]*N
rootin[0] = 1
count = 0
for _ in range(N):
  count += 1
  visited[i] += 1
  if visited[i] == 2:
    end = count-1
    break
  rootin[count] = A[i]
  i = A[i]-1
start = 0
for i in range(N):
  if rootin[i] == rootin[end]:
    start = i
    break
od = rootin[:start]
root = rootin[start:end]

if K > len(od):
  K -= len(od)
  if K%len(root) == 0:
    ans = root[-1]
  else:
    ans = root[K%len(root)]
else:
  ans = od[K-1]
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
As = list(map(int, input().split()))
maps = {}

for i, a in enumerate(As):
    maps[i] = a - 1

probs = [0] * n
p = 0  # 現在位置
target = 0  # はじめて2回出現する場所
h = 0  # ターゲットの初回出現時のワープ回数
t = 0  # ターゲットの2回目の出現時のワープ回数

# はじめて2回出現する場所(target)と、その時点でのワープ回数を取得
for i in range(n + 1):
    probs[p] += 1
    if (probs[p] == 2):
        target = p
        t = i
        break
    p = maps[p]

# 初回出現位置を取得(めんどうくさいのでもう一回回した。)
p = 0
for i in range(n):
    if (p == target):
        h = i
        break
    p = maps[p]

s = t - h  # 周期
d = (k - h) % s  # targetに到着後にワープすべき回数

# 実際に必要な回数だけワープ
p = 0
for i in range(h + d):
    p = maps[p]

print(p + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
lis = list(map(lambda z: int(z) - 1, map(int, input().split())))

s = {0}
res = [0]

while lis[res[-1]] not in s:
    res.append(lis[res[-1]])
    s.add(res[-1])

x, y = res.index(res[-1]), len(res)

if m < y:
    print(res[m]+1)
else:
    mm = (m - x) % (y - x)
    print(res[mm + x]+1)

Submission status: Wrong Answer

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

visited = [0]*N
i = 0
rootin = [0]*N
rootin[0] = 1
count = 0
for _ in range(N):
  count += 1
  visited[i] += 1
  if visited[i] == 2:
    end = count-1
    break
  rootin[count] = A[i]
  i = A[i]-1
start = 0
for i in range(N):
  if rootin[i] == rootin[end]:
    start = i
    break
od = rootin[:start]
root = rootin[start:end]

if K > len(od):
  K -= len(od)
  if K%len(root) == 0:
    ans = root[-1]
  else:
    ans = root[K%len(root)-1]
else:
  ans = od[K-1]
print(ans)

Submission status: Wrong Answer

Solution code:
import collections
from collections import OrderedDict

n,k = map(int,input().split())
L = list(map(int,input().split()))
od = collections.OrderedDict()

t = L[0]
od [t] = t
r = 0
i = 0
c = 0
b = True
for j in range(k):
    #print(od)
    t = L[t-1]
    if ｔ in od:
        l = list(od.keys())
        i = l.index(t)
        r = len(od) - i
        b= False
        break
    else:
        od[t] = t
#print(r)
#print(i)


#print(k)
l = list(od.keys())
#print((k - i)//r)
if b:
    print(t)
else:
    k2 = (k - i)%r
    print(l[i+k2-1])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
flag=0
now=0
i=0
count=0
c=[0]*N
r=[0]
c[0]=1
while flag==0:
  now=A[now]-1
  count+=1
  if count==K:
    flag=1
  elif c[now]==1:
    flag=2
  c[now]=1
  r.append(now)
print(r)
if flag==1:
  print(now+1)
else:
  o=[i for i, x in enumerate(r) if x == now]
  print(o[0],o[1])
  print(r[(K-o[0])%(o[1]-o[0])+o[0]]+1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
lis = list(map(lambda z: int(z) - 1, map(int, input().split())))

s = {0}
res = [0]

while lis[res[-1]] not in s:
    res.append(lis[res[-1]])
    s.add(res[-1])

x, y = res.index(res[-1]), len(res)

if m < y:
    print(res[m])
else:
    mm = (m - x) % (y - x)
    print(res[mm + x])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = input()
#  = int(input())
n, k = map(int, input().split())
a = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

move_history = [0]
current = 0
st_loop = -1
is_loop = False
for i in range(10**18): 
    current = a[current] - 1

    if current in move_history:
        st_loop = move_history.index(current)
        is_loop = True
        break

    if len(move_history) - 1 > k:
        move_history.append(current)
        break

    move_history.append(current)

if is_loop:
    loop_town = move_history[st_loop:]
    nlty = len(move_history[st_loop:])
    nltn = len(move_history[:st_loop])

    k -= nltn
    print(loop_town[k % len(loop_town)] + 1)

else:
    print(move_history[-1] + 1)

Submission status: Wrong Answer

Solution code:
N = [int(e) for e in input().split()]
nums = [int(t) for t in input().split()]
count=[]
h = 0
k = 0
check = []
t=1
for i in range(N[0]):
    count.append(0)
while count[t-1] == 0:
    t = nums[k]
    if(count[t-1] == 1):
        break
    else:
        check.append(t)
        count[t-1] += 1
    k = t -1
    h+=1
s = check.index(t)
if(N[1] < s):
    print(check[N[1]])
else:
    ans = (N[1] - s)%(len(check)-s)
    ans1 = check.index(t) - 1 + ans
    print(check[ans1])

Submission status: Wrong Answer

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

vis = set()
vis.add(1)
cnt = 0
cnt2town = {}
town2cnt = {}
cnt2town[0] = 1
town2cnt[1] = 0

cnt = 1
prev = 1
while cnt <= K:
    a = A[prev - 1]

    if a in vis:
        c = town2cnt[a]

        rem = K - cnt
        num_loop = cnt - c
        
        r = rem % num_loop
        print(cnt2town[c + r])

        exit()

    cnt2town[cnt] = a
    town2cnt[a] = cnt
    vis.add(a)

    prev = a
    cnt += 1

Submission status: Wrong Answer

Solution code:
#coding:utf-8

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

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

track = [0] * (N)

state = 1

for i in range(N):
    
    if track[state-1] == 1:
        break
    else:
        track[state-1] = 1

    state = A[state-1]

numnum = 0
path = 0
stopper = state

for i in track:
    numnum += i

state = 1

for i in range(N):
    
    if state == stopper:
        break
    else:
        path += 1
        numnum -= 1

    state = A[state-1]

root = int((K - path) % numnum) + path 
state = 1

for i in range(root):
    state = A[state-1]

print("{}".format(state))

Submission status: Wrong Answer

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

t = list(map(int,input().split()))
p = 1
roop = 0
fp = []
fp.append(p)
for i in range(n):
    p = t[p-1]
    if p in fp:
        roop = i+1 - fp.index(p)
        break
    else:
        fp.append(p)
ans = fp[fp.index(p):]
print(ans[(k-fp.index(p))%roop])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

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

for i in range(N):
    A[i] -= 1

tele = [0]
#tele = [0,A[0]-1]
z = 0
s = 0
#B = {0:0,A[0]-1:1}
B = {0:0}
for i in range(N):
    if A[tele[-1]] in B and len(B) != 1:
        s = B[A[tele[-1]]]
        break
    B[A[tele[-1]]] = i+1
    tele.append(A[tele[-1]])
    z = tele[-1]

if K <= s:
    print(tele[K])
else:
    K -= s
    amari = K%(len(tele[s:len(tele)]))
    tele = tele[s:len(tele)]*2
    print(tele[amari]+1)

Submission status: Wrong Answer

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

check = [0]*N
i = 0
c = 0
check = []
visited = [0]*N
flag = False
while c<=K:
    if visited[i] == 1:
        flag = True
        break
    else:
        check.append(i)
        visited[i] = 1
        i = A[i]
        c += 1
if flag==True:
    X = check[check.index(i):]
    print(1+X[(K-check.index(i))%(len(check)-check.index(i))])
else: 
    print(i+1)

Submission status: Wrong Answer

Solution code:
s = input().split()
n = int(s[0])
k = int(s[1])
count =0

t = list(map(int,input().split()))
p = 1
roop = 0
fp = []
fp.append(p)
for i in range(n):
    p = t[p-1]
    if p in fp:
        roop = i+1 - fp.index(p)
        break
    else:
        fp.append(p)
ans = fp[fp.index(p):]
print(ans[(k-fp.index(p))%roop])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
flag=0
now=0
i=0
count=0
c=[0]*N
r=[0]
c[0]=1
while flag==0:
  now=A[now]-1
  count+=1
  if count==K:
    flag=1
  elif c[now]==1:
    flag=2
  c[now]=1
  r.append(now)
if flag==1:
  print(now+1)
else:
  o=[i for i, x in enumerate(r) if x == now]
  print(r[K%(o[1]-o[0])+o[0]+1]+1)

Submission status: Wrong Answer

Solution code:
import sys

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

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

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



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

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


K -= rep_start
K %= move
now = p

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

print(now+1)

Submission status: Wrong Answer

Solution code:
N, K, *A = map(int, open(0).read().split())

A = [0] + A
visited = [-1] * (N + 1)

pre = 0
cycle = 0

cur = 1
for i in range(N):
    visited[cur] = i

    cur = A[cur]
    if visited[cur] >= 0:
        pre = visited[cur]
        cycle = i + 1 - visited[cur]
        break

cur = 1
for _ in range(pre + ((K - pre) % cycle)):
    cur = A[cur]

print(cur)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import collections
from collections import OrderedDict

n,k = map(int,input().split())
L = list(map(int,input().split()))
od = collections.OrderedDict()

t = L[0]
od [t] = t
r = 0
i = 0
c = 0
for j in range(n*2):
    #print(od)
    t = L[t-1]
    if ｔ in od:
        l = list(od.keys())
        i = l.index(t)
        r = len(od) - i
        break
    else:
        od[t] = t
#print(r)
#print(i)

k2 = (k - i)%r
#print(k)
l = list(od.keys())
#print((k - i)//r)
if r == 0:
    print(t)
else:
    print(l[i+k2-1])

Submission status: Wrong Answer

Solution code:
N, K, *A = map(int, open(0).read().split())

A = [0] + A
visited = [-1] * (N + 1)

pre = 0
cycle = 0

cur = 1
for i in range(N):
    visited[cur] = i

    cur = A[cur]
    if visited[cur] > 0:
        pre = visited[cur]
        cycle = i + 1 - visited[cur]
        break

cur = 1
for _ in range(pre + ((K - pre) % cycle)):
    cur = A[cur]

print(cur)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
towns = [int(i) for i in input().split()]
arrivedList = [1]
loopCount = 0
previous = 0
for i in range(N):
    # print(arrivedList[-1],arrivedList[len(arrivedList)-1])
    current = towns[arrivedList[-1]-1]
    arrivedList.append(current)
# print(arrivedList)
if K < N + 1:
    print(arrivedList[-1])
    exit()
loopStart = N - 1
while (arrivedList[N] != arrivedList[loopStart]):
    loopStart -= 1
remain = (K-loopStart) % (N - loopStart)
loopList = arrivedList[loopStart+1:]
# print(loopList,remain)
print(loopList[remain-1])

Submission status: Wrong Answer

Solution code:
N = [int(e) for e in input().split()]
nums = [int(t) for t in input().split()]
count=[]
h = 0
k = 0
check = []
for i in range(N[0]):
    count.append(0)
while h < N[0]:
    t = nums[k]
    if(count[t-1] == 1):
        break
    else:
        check.append(t)
        count[t-1] += 1
    k = t -1
    h+=1
s = check.index(t)
if(N[1] < s):
    print(check[N[1]])
else:
    ans = (N[1] - s)%(len(check)-s)
    ans1 = check.index(t) - 1 + ans
    print(check[ans1])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  a = b
  b = A[a-1]
  B.append(b)
  if len(B) == n:
    ans = 0
aaa = k%n
print(B[aaa-1])

Submission status: Wrong Answer

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

visit=set()
order=[-1]*(N+1)

p=0
cnt=0
while not (p in visit):
    visit.add(p)
    order[cnt]=p
    p=A[p]-1
    cnt+=1

ind=order.index(p)

if K>N:
    K=(K-ind)%(cnt-ind)

while K>0:
    p=A[p]-1
    K-=1

print(p+1)

Submission status: Wrong Answer

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

A = list(map(int, input().split()))

cnt = [0 for _ in range(N)]
cnt[0] += 1
roop = []
# print(cnt)
warp = A[0]
while True:
    # print(cnt[warp-1])
    if cnt[warp-1] == 2:
        break
    if cnt[warp-1] == 1:
        roop.append(A[warp-1])
    cnt[warp-1] += 1
    warp = A[warp-1]


one_root = cnt.count(1)
# print("root", roop)
# print("one", one_root)
print(roop[(K-one_root)%len(roop)-1])

Submission status: Wrong Answer

Solution code:
n, k = list(map(int,input().split()))
a = list(map(int,input().split()))
# 循環を調べる
c=0
count=0
init_city=0
tmp_city = []

for i in range(2,k+2):
    tmp=a[c]
    if tmp in tmp_city:
        init_city=tmp_city.index(tmp)+1
        tmp_city=[1]+tmp_city
        print(tmp_city[init_city:][(k-len(tmp_city[:init_city]))%len(tmp_city[init_city:])])
        break
    tmp_city.append(tmp)
    c=tmp-1
else:
    print(tmp_city[init_city+1])

Submission status: Wrong Answer

Solution code:
import collections
from collections import OrderedDict

n,k = map(int,input().split())
L = list(map(int,input().split()))
od = collections.OrderedDict()

t = L[0]
r = 0
i = 0
c = 0
for j in range(n*2):
    #print(L2)
    t = L[t-1]
    if ｔ in od:
        l = list(od.keys())
        i = l.index(t)
        r = len(od) - i
        break
    else:
        od[t] = t
#print(r)
#print(i)

k2 = (k - i)%r
#print(k)
l = list(od.keys())
#print((k - i)//r)
if r == 0:
    print(t)
else:
    print(l[i+k2-1])

Submission status: Wrong Answer

Solution code:
def d():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    print('A = {}'.format(A))
    warps = [1]

    for i in range(K):
        city = A[warps[-1] - 1]
        # print('city, warps = {}, {}'.format(city, warps))
        if city in warps:
            loop_start_index = warps.index(city)
            loop = warps[loop_start_index:]
            K -= loop_start_index
            print(loop[K % len(loop)])
            return
        else:
            warps.append(city)
    print(warps[-1])


##########
if __name__ == "__main__":
    d()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
s=1
B=[1]
import sys
for i in range(K):
  s=A[s-1]
  if s in B:
    u=B.index(s)
    u_=len(B)-u#周期の長さ
    t=(K-u+1)%u_
    if t>=1 and u>=1:
      print(B[u+t-1])
      sys.exit()
    elif u==0:
      t=(K+1)%u_
      print(B[t-1])
      sys.exit()
    else:
      print(B[u])
      sys.exit()
  else:
    B.append(s)
 
print(s)

Submission status: Wrong Answer

Solution code:

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

#n=6
#k=727202214173249351
#a=[6, 5, 2, 5, 3, 2]

#n=4
#k=5
#a=[3,2,4,1]

if n<k:
    ai=1
    aj=set()
    for i in range(n):
        aim=ai
        ai=a[ai-1]
        if ai in aj:
            ic2=i
            ai2=ai
            break
        else:
            aj.add(ai)

    ai=1
    for i in range(min(n,k)):
        ai=a[ai-1]
        if ai==ai2:
            ic1=i
            break

    icyc=ic2-ic1
    k2=(k-ic1)%icyc+ic1
else:
    k2=k
    
ai=1
for i in range(k2):
    ai=a[ai-1]
        
print(ai)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
As = list(map(int, input().split()))
maps = {}

for i, a in enumerate(As):
    maps[i] = a - 1

probs = [0] * n
probs[0] = 1
p = 0  # 現在位置
target = 0  # はじめて2回出現する場所
h = 0  # ターゲットの初回出現時のワープ回数
t = 0  # ターゲットの2回目の出現時のワープ回数

# はじめて2回出現する場所(target)と、その時点でのワープ回数を取得
for i in range(n):
    if (probs[p] == 2):
        target = p
        t = i
        break
    p = maps[p]
    probs[p] += 1

# 初回出現位置を取得(めんどうくさいのでもう一回回した。)
p = 0
for i in range(n):
    if (p == target):
        h = i
        break
    p = maps[p]

s = t - h  # 周期
d = (k - h) % s  # targetに到着後にワープすべき回数

# 実際に必要な回数だけワープ
p = 0
for i in range(h + d):
    p = maps[p]

print(p + 1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import defaultdict
N,K=map(int, sys.stdin.readline().split())
A=[None]+map(int, sys.stdin.readline().split())

visit=defaultdict(lambda: 0)    #訪問済みリスト
L=[1]       #移動順番をリストに入れる
visit[1]=1
x=1         #スタート地点は常に1
while True:
    to=A[x]
    if visit[to]==0:    #未訪問だったら
        visit[to]=1     #訪問済みに変更
        L.append(to)
        x=to
    else:   #次の移動先が1だったらスタートに戻るのでループを打ち切る
        break

to_idx=L.index(to)  #循環する配列の先頭の位置を調べる
loop_L=L[to_idx:]   #循環している部分を抜き出す

len_all=len(L)
len_loop=len(loop_L)
len_non_loop=len_all-len_loop

if K<=len_non_loop: #Kが循環に入る手前だったら
    print L[len_non_loop]
else:               #Kが循環に入っているならば
    K-=len_non_loop #非循環の長さを引く
    print loop_L[K%len_loop]

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
ar = list(map(int, input().strip().split(' ')))
route=[]

Town=1
X=0
loop = False
loopy = False
A=(K-1)

while loop == False:
      for i in range(0,K):
            X=ar[(Town-1)]
            Town=(X)
            if i == A:
                  loop=True
            if K > 100:
                  if Town in route:
                        loopy=True
                        loop=True
                        break
            route.append(Town)

if loopy==True:
      S=len(route)
      K=K-i
      if K > S:
            Z=K % S
            print(Z)
            K=Z+S
            print(K)
      

      for i in range(0,K):
            X=ar[(Town-1)]
            Town=X

Submission status: Wrong Answer

Solution code:
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    a = [a[i] - 1 for i in range(n)]
    next_i = 0
    seen = [-1] * n
    count = 0
    while True:
        if seen[next_i] != -1:
            start = next_i
            l = count - seen[next_i]
            break
        seen[next_i] = count
        count += 1
        next_i = a[next_i]
    if k > start:
        k = ((k - start) % l) + start
    for i in range(n):
        if seen[i] == k:
            print(i + 1)
            break


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Wrong Answer

Solution code:

n,k=map(int,input().split())
a=list(map(int, input().split()))
b=[1]
ba=set()
c=0
m=0
# while flag==1:
for i in range(n):
    if a[m] not in ba:
        b.append(a[m])
        ba.add(a[m])
        c+=1
        m=a[m]-1
    else:
        e=a[m]
        break

e=b.index(e)
# print(b,e)
b=b[e:c+1]
f=c+1-e
if 1 not in b:
    ans=(k%f+e-1)%f
    print(b[ans])
else:
    ans=k%f
    print(b[ans])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
def tele(i):
    ans = A[i-1]
    return ans
i = tele(1)
if N >= K:
    for j in range(K-1):
        i = tele(i)
        print(i)
else:
    l = [i]
    for j in range(N-1):
        i = tele(i)
        if i not in l:
            l.append(i)
        else:
            a = l.index(i)
            loop = len(l) - a
            ans = (K - a) % loop
            if ans != 0:
                q = l[a+ans-1]
            else:
                q = l[a+loop-1]
            print(q)
            exit()

Submission status: Wrong Answer

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

town = [0 for i in range(N)]

def next(A,now):
    return(A[now-1])

now = 1
i = 0
while True:
    if town[now-1] != 0:
        roop_start = town[now-1]
        roop_end = i
        break
    town[now-1] = i+1
    now = next(A,now)
    i += 1

roop = roop_end - roop_start + 1
teleport = roop_start + (K - roop_start)%roop

now = 1
for i in range(teleport):
    now = next(A,now)

print(now)

Submission status: Wrong Answer

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

visit=set()
order=[-1]*(N+1)

p=0
cnt=0
while not (p in visit):
    visit.add(p)
    order[cnt]=p
    p=A[p]-1
    cnt+=1

ind=order.index(p)
if K>ind:
    K=(K-ind)%(cnt-ind)

while K>0:
    p=A[p]-1
    K-=1

print(p+1)

Submission status: Wrong Answer

Solution code:
import logging

logging.basicConfig(level=logging.INFO, format="%(message)s")
#logging.disable(logging.CRITICAL)


def main():
    N, K = map(int, input().split())
    A_i = list(map(int, input().split()))
    B_i = [0 for i in range(N)]
    cycle_list = []
    
    cycle = 0
    diff = 0
    counter = 0
    machi = 1

    while counter < N:
        if B_i[machi - 1] == 1:
            diff = cycle_list.index(machi)
            cycle = counter - diff
            cycle_list = cycle_list[diff:]
            break
        else:
            counter += 1
            B_i[machi - 1] = 1
            cycle_list.append(machi)
            machi = A_i[machi - 1]


    mod_K = (K - diff) % cycle
    print(cycle_list[mod_K])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

town = [0 for i in range(N)]

def next(A,now):
    return(A[now-1])

now = 1
i = 0
while True:
    if town[now-1] != 0:
        roop_start = town[now-1]
        roop_end = i
        break
    town[now-1] = i+1
    now = next(A,now)
    i += 1

roop = roop_end - roop_start + 1
teleport = roop_start + (K - roop_start)%roop
print(teleport)
now = 1
for i in range(teleport):
    now = next(A,now)

print(now)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
for i in range(n):
  a[i] = a[i] - 1
b = 0
c = []
d = []
e = 0
f = 0
for i in range(k):
  b = a[b]
  d.append(b)
  if b not in d:
    e = e + 1
  elif b in d:
    for j in range(i):
      b = a[b]
      if b in c:
        break
      c.append(b)
    else:
      continue
    break
g = (k - e) % len(c)
print(c[g] + 1)

Submission status: Wrong Answer

Solution code:
def d():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    print('A = {}'.format(A))
    warps = [1]

    for i in range(K):
        city = A[i]
        print('i, city, warps = {}, {}, {}'.format(i, city, warps))
        if city in warps:
            loop_start_index = warps.index(city)
            loop = warps[loop_start_index:]
            K -= loop_start_index
            print(loop[K % len(loop)])
            return
        else:
            warps.append(city)
    print(warps[-1])


##########
if __name__ == "__main__":
    d()

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()))
    visited = {i:0 for i in range(N)}
    visited[0] = 1
    count = 0
    pre_count = 0
    cicle_idx = 0
    is_cicle = False
    idx = 0
    ans = -1
    while not is_cicle:
        count += 1
        idx = A[idx]-1
        if count == K:
            ans = A[idx]
        if visited[idx] == 1:
            visited[idx] = 2
            is_cicle = True
            cicle_idx = idx
            break
        visited[idx] = 1
    if ans == -1:
        cicle = []
        will_push = False
        while True:
            idx = A[idx]-1
            if idx == cicle_idx:will_push = True
            if len(cicle) > 0 and visited[idx] == 2:break
            if will_push:
                cicle.append(idx)
        pre_count = count - len(cicle)
        ans_idx = (K - pre_count) % len(cicle)
        print(cicle[ans_idx]+1)
    else:
        print(ans)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

check = [0]*N
i = 0
c = 0
check = []
visited = [0]*N
flag = False
while c<=K:
    if visited[i] == 1:
        flag = True
        break
    else:
        check.append(i)
        visited[i] = 1
        i = A[i]
        c += 1
if flag==True:
    X = check[check.index(i):]
    print(1+X[(K-check.index(i))%len(X)])  
else: 
    print(i+1)

Submission status: Wrong Answer

Solution code:
def Main(N, K, A):
    l = []
    for i in range(N):
        l.append([0,0])
    ind = A[0]-1
    l[ind][0] = 0
    l[ind][1] = 1
    detected = 0
    first = 0
    second = 0
    path = [0]
    path.append(ind)
    for i in range(1, N):
        ind = A[ind]-1
        if l[ind][1] == 1:
            detected = ind
            first = l[detected][0]
            second = i
            break
        else:
            l[ind][0] = i
            l[ind][1] = 1
            path.append(ind)
    if K < first:
        return path[K]+1
    else:
        return path[first + (K-first)%(second-first)]+1

def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    print(Main(N, K, A))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

journey = [1]

for i in range(N):
    nxt = a[journey[-1]]
    journey.append(nxt)

if K < N:
    print(journey[K])
    exit()

loop_start = N-1
loop_end   = N

finished = False
for i in range(N):
    for j in range(i+1,N+1):
        if journey[i] == journey[j]:
            loop_start = i
            loop_end   = j
            finished = True
            break
    if finished:
        break

period = loop_end - loop_start

ans = K % period
print(journey[ans])

Submission status: Wrong Answer

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

cache={}
log=[]
A = list(map(lambda x:int(x)-1, input().split(' ')))

now=0

while(True):
  log.append(now)
  cache[now]=1
  now=A[now]
  if(cache.get(now)):break

loop_start=log.index(now)
loop_end=len(log)
loop=loop_end-loop_start

if K<len(log):
  print(log[K]+1)
else:
  print(log[K%loop]+1)