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


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools
def main(n, k, a):
    ck=[-1]*n
    tak=0
    c=0
    ck[0]=c
    for i in range(n):
        c+=1
        tak=a[tak]-1
        if ck[tak]!=-1:
            loop=c-ck[tak]
            lps=ck[tak]
            break
        ck[tak]=c

    tak=0
    for i in range((k-lps)%(loop)+lps):
        tak=a[tak]-1

    print(tak+1)


if __name__ == '__main__':
    n,k=map(int, input().split())
    a=list(map(int, input().split()))
    main(n, k, a)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# atcoder 
# Türkçe konuşyorum
# Türkler var mı

n, k = map(int, input().split(" "))
val = list(map(int,input().split(" "))) 
dico = {}
i = 0
#for j in range(len(val)):
    #val = int(input().split(" "))
#    dico[j] = val[j]
#print(dico)

while k >= 1:
    if not i < n:
         i = 0
    i += 1
    k -= 1

print(val[i+1])

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
A =[0]
B=list(map(int, input().split()))
for m in range(N):
    A.append(B[m])
place=set([])
i=1
count=0
while i not in place:
    place.add(i)
    i=A[i]
    count+=1
else:
    loop=[i]
    start=i
    j=A[i]
    count+=1
    while j!=start:
        loop.append(j)
        j=A[j]
        count+=1
    else:
        num=(K-count)%len(loop)
        print(loop[num])

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import copy

N, K = map(int, input().split())
List = list(map(int, input().split()))
saki=[1]
ato=[]

if K<=N:
    List[K]
else:
    i=0
    j=1
    while i<N:
        #print(j)
        saki.append(List[j-1])
        #print(saki)
        j=List[j-1]
        i+=1
        
    #print("AAA")
    #print(j)
    
    k=copy.deepcopy(N)
    #print(k)
    
    #print(j)
    ato.append(List[j-1])
    #print(ato)
    #j=List[j-1]
    
    start=saki.index(ato[0])
    #print(start)
    
    SA=(len(List)-start)+1
    #print(SA)
    
    #print((K-start)%SA)
    o=0
    check=[]
    while o<SA:
        #print("AAA")
        #print(j)
        check.append(List[j-1])
        #print(check)
        j=List[j-1]
        o+=1
    print(check[(K-start)%SA])

Submission status: Wrong Answer

Solution code:
b0=input().split()
towns=int(b0[0])
k=int(b0[1])
 
al=[int(i)-1 for i in input().split()]
 
rt=[-1 for x in range(towns)]
rt[0]=0
now=0
for x in range(1,300000):
  now=al[now]
  if rt[now]!=-1:
    stp=x
    break
  else:
    rt[now]=x
 
ll=stp-rt[now]
k2=k-rt[now]
 
if k<ll:
  print(rt.index(k)+1)
else:
  print(rt.index(k2%ll+rt[now])+1)

Submission status: Wrong Answer

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

loopcount=0
tmpPoint=1
vis={1:0}
for i in range(N):
    Next=A[tmpPoint-1]
    if Next in vis:
        startat=i
        #print(startat)
        #print(tmpPoint)
        #print(vis)
        #print(Next)
        #print(vis[Next]+1)
        loopcount=i - vis[Next]+1
        break
    vis[Next]=i+1
    tmpPoint=Next

if loopcount==0:
    print(tmpPoint)
    exit()
if startat+loopcount < K:
    K=(K-startat) % loopcount

tmpPoint=1
for i in range(K):
    Next=A[tmpPoint-1]
    tmpPoint=Next
print(tmpPoint)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int,input().split()))
move_list = list(map(int,input().split()))
 
  
test = 1
find_f = {}
for i in range(N):
  find_f[i+1]=[]
 
find_f[1].append(0)
for i in range(K):
  test = move_list[test-1]
  find_f[test].append(i+1)
  if len(find_f[test]) == 2 and find_f[test][1]-find_f[test][0]<=1000000:
    state ,f = test, find_f[test]
    break
try:
  K = K-f[0]
  f = f[1]-f[0]
  remain = K%f
  if remain == 0:
    print(state)
 
  for i in range(remain):
    state = move_list[state-1]
 
  print(state)
except:
  print(test)
#state = 1

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
is_good = True
graph=[0]
s = 0
while is_good:
    next = a[graph[-1]]-1
    if  next in graph:
        next_ = graph.index(next)
        loop = len(graph) - next_
        is_good = False
    else:
        graph.append(next)


if k <= next_:
    print(graph[k-1] + 1)

else:
    k -= next_
    x = k%loop
    print(graph[x+next_]+1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
m = [-1]*n
t = 0
i = 0
while True:
  if m[t] != -1:
    r = i - m[t]
    break
  m[t] = i
  t = a[t]-1
  i += 1
b = k%r+i+1
t = 0
for i in range(b):
  t = a[t]-1
print(t+1)

Submission status: Wrong Answer

Solution code:
from collections import deque, defaultdict
d = deque()
di = defaultdict(int)
N, K = map(int, input().split())
A = list(map(int, input().split()))


d.append(0)
for i in range(N * 2):
    p = d.pop()
    d.append(p)
    d.append(A[p] - 1)


P = []
k = 0
n = 0
for i, v in enumerate(d):
    if (v in P):
        k = i
        n = v
        break
    P.append(v)
    di[v] = i

le = k - di[n]
amari = K % le

print(d[le + amari] + 1)

Submission status: Wrong Answer

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

b = 1

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

print(b)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
li=[0]+list(map(int,input().split()))
m=1
loop={1}
s="1"
mkr=0
for i in range(1,n+1):
    m=li[m]
    if m in loop:
        loopend=i
        break
    if i==k:
        mkr=1
        break
    loop|={m}
    s+=str(m)
if mkr==1:
    print(m)
else:
    loopstart=s.index(str(m))
    ind=(k+1-loopstart)%(loopend-loopstart)
    if ind==0:
        print(s[-1])
    else:
        print(int(s[loopstart+ind-1]))

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
A = list(map(int,input().split()))
ans = A[0]
for _ in range(K):
    ans = A[ans-1]
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
L=list(map(int,input().split()))
cnt=0
i=0
S=[]
while not(L[i] in S):
    S.append(i+1)

    i=L[i]-1
    cnt=cnt+1
S.append(i+1)

print(S[K%(cnt+1)])

Submission status: Wrong Answer

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

start = 0
atesaki = 0
start_li = [0]
atesaki_li = []
count = 0
target = atesaki
for i in range(k):
    atesaki = li[start]-1
    start = atesaki
    if atesaki in atesaki_li:
        count = i+1
        break
    else:
        atesaki_li.append(atesaki)
    #print(atesaki,'tes')

first_ind = atesaki_li.index(atesaki)+1
#print(atesaki_li,atesaki,first_ind)

atesaki_li[(k-len(atesaki_li))%(len(atesaki_li)-first_ind+1)]
print(atesaki_li[(k-len(atesaki_li))%(len(atesaki_li)-first_ind+1)]+1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import copy

N, K = map(int, input().split())
List = list(map(int, input().split()))
saki=[1]
ato=[]

if K<N:
    List[K]
else:
    i=0
    j=1
    while i<N:
        #print(j)
        saki.append(List[j-1])
        #print(saki)
        j=List[j-1]
        i+=1
        
    #print("AAA")
    #print(j)
    
    k=copy.deepcopy(N)
    #print(k)
    
    #print(j)
    ato.append(List[j-1])
    #print(ato)
    #j=List[j-1]
    
    start=saki.index(ato[0])
    #print(start)
    
    SA=(len(List)-start)+1
    #print(SA)
    
    #print((K-start)%SA)
    o=0
    check=[]
    while o<SA:
        #print("AAA")
        #print(j)
        check.append(List[j-1])
        #print(check)
        j=List[j-1]
        o+=1
    print(check[(K-start)%SA])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
p = a[0]
s = a[0]
g = []
q = []
for i in range(n+1):
    g.append(p)
    p = a[p-1]
g.reverse()
for i in range(n):
    if g[0] == g[i+1]:
        mod = i+1
        break
g.reverse()
for i in range(len(g)):
    if g[i] == g[i + mod]:
        d = i
        break
if k <= mod:
    for i in range(k%mod):
        q.append(s)
        s = a[s-1]
    q.reverse()
    q.append(1)
    print(q[0])
else:
    print(g[d+(k-d)%mod+2])

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]

mod = [0]
flag = [0 for i in range(n)]
flag[0] = 1
idx = A[0]
f = False

for i in range(n):
    if flag[idx] == 1:
        idx = mod.index(idx)
        f = True
        break
    flag[idx] = 1
    mod.append(idx)
    idx = A[idx]

if f:
    r = len(mod) - idx
    k -= idx
    mod = mod[idx:]
    print(mod[k % r] + 1)
else:
    print(mod[k]+1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[int(i)-1 for i in input().split()]
ed=[False]*n
go=0
c=0
cc=0
while not ed[go]:
    ed[go]=True
    go=a[go]
    c+=1
go=c
ed=[False]*n
while not ed[go]:
    ed[go]=True
    go=a[go]
    cc+=1
go=c
for i in range(c):
    go=a[go]
print(go+1)

Submission status: Wrong Answer

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

#インデックスを1オリジンに
A.insert(0,-1)

accessed_node = set([1])
accessed_node_list=[1]
loop_root = []
loop_madeno_size = 0
loop_size=0
t=1
#ループ検出
for _ in range(1,K+1):
  if not A[t] in accessed_node:
    accessed_node.add(A[t])
    accessed_node_list.append(A[t])
    #次の場所を決める
    t=A[t]
  else:
    #ループ路を確定する
    loop_start_index=accessed_node_list.index(A[t])
    loop_root = accessed_node_list[loop_start_index:]
    loop_madeno_size = loop_start_index
    loop_size=len(loop_root)
    break
#print(accessed_node)
#print(loop_madeno_size)
if loop_madeno_size <= K:
  print(t)
  exit()
K -= loop_madeno_size
print(loop_root[K%loop_size])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
towns = list(map(int, input().split()))
place_count = [0] * (n + 1)
next_place = 1
teleport_count = 0
for i in range(n * 2):
  place_count[next_place] += 1
  next_place = towns[next_place - 1]
one_move_place = place_count.count(1)
for place in place_count:
  if place > 1:
    teleport_count += 1
next_place = 1
for _ in range(one_move_place + (k - one_move_place) % teleport_count):
  next_place = towns[next_place - 1]
print(next_place)

Submission status: Wrong Answer

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

# 1から順に探索
visited=[-1]*(1+n)
cl=[0]*(1+n)
visited[1]=0
c=0
now=1
t=0

start=0

for i in range(1,n+1):
    if visited[al[now]]==-1: # まだきてない
        visited[al[now]]=0
        c+=1
        cl[al[now]]=c
        now=al[now]
    else:  
        start=cl[al[now]]
        t=i-start
        break
cl2=cl[1:]
print(cl2.index(start+(k-start)%t)+1)

Submission status: Wrong Answer

Solution code:
#dt = {} for i in x: dt[i] = dt.get(i,0)+1
#dt = {k:v for k,v in sorted(x.items(), key=lambda i: i[1])}
ipnl = lambda n: [int(input()) for _ in range(n)]
inp = lambda :int(input())
ip = lambda :[int(w) for w in input().split()]
mp = lambda :map(int,input().split())

n,k = mp()
x = ip()
if x[0] == 1:
    print(1)
    exit()
cyc = [1]
vis = {}
i = 1
while not vis.get(i,0):
    vis[i] = 1
    i = x[i-1]
    cyc.append(i)
#print(cyc)
if k <= len(cyc):
    print(cyc[k])
last = cyc[-1]
ind = cyc.index(last)
kk = ind
k -= kk
ind = (k-1) % (len(cyc)-kk-1) + 1
print(cyc[kk+ind])

Submission status: Wrong Answer

Solution code:
import numpy as np
n, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
b = []
m = 0
flag = 0
for l in range(n):
    b.append(m)
    m = a[m] - 1
    for p in range(len(b)):
        if b[p] == m:
            r = len(b) + 1 - p
            s = p - 1
            flag += 1
            break
    if flag == 1:
        break
if k <= s:
    for l in range(k-1):
        m = a[m] - 1
    print(m + 1)
else:
    c = (k - 1 - s) % r
    if c == 0:
        for l in range(s + p - 1):
            m = a[m] - 1
        print(m + 1)
    else:
        for l in range(s + c):
            m = a[m] - 1
        print(m + 1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
lst = list(map(int, input().split()))
lst.insert(0, 0)

times = [0]*(n+1)
_map = [0]*(n+1)
idx = {}

sentou = 0
num = lst[1]
first = 0
kk = 0
for i in range(1, k+1):
    times[num] += 1
    idx[i] = num
    sentou += 1
    if times[num] == 2:
        first = num
        break
    _map[num] = i
    num = lst[num]
    kk += 1

if kk == k:
    print(idx[k])
    exit()


first_idx = _map[first]
num_str = ''
for i in range(first_idx, max(_map)+1):
    num_str += str(idx[i])


aa = k - sentou
if k < sentou:
    print(num_str(aa))

ans_len = (k - sentou) % len(num_str)
print(num_str[ans_len])

Submission status: Wrong Answer

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

move1 = [1]
_num = move1[0]
for i in range(n*3):
    _num = al[_num-1]
    move1.append(_num)

move2 = move1[n:n+n]
for i in range(n, 3*n):
    if move1[i:i+n] == move2:
        _index = i
        break

# print(_index)

if k <= n+_index:
    print(move1[k])
else:
    nn = (k-n-_index)%n
    print(move2[nn])

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
n=list(map(int,input().split()))
d=0
m=[i for i in range(1,len(n)+1)]
o=dict(zip(m,n))
print(o)

f=1
while d!=b:
    f=n[f-1]
    d+=1
    print(f)

Submission status: Wrong Answer

Solution code:
n,k =map(int,input().split())
a = list(map(int,input().split()))
l = 0  
count = 0
 
for i in range(1000000) :
    l = a[l]-1 
    count = +1
    if l == 0 :
        x = k%count
        for i in range(x) :
            l = a[l]-1
            
        print(l+1)
        exit()

Submission status: Wrong Answer

Solution code:

n,k=map(int,input().split())
a=list(map(int,input().split()))
c=2
t=0
path=[]
s=set()
cy=[]
while c<=k:
    if a[t] in s:
        cy=path[path.index(a[t]):]
        k=c-1
        break
    path.append(a[t])
    s.add(a[t])
    t=a[t]-1
    c+=1
if cy==[]:
    print(a[t])
else:
    print(cy[k%len(cy)]-1)

Submission status: Wrong Answer

Solution code:

N, K = [int(x) for x in input().split()]
A = [int(x)-1 for x in input().split()]

arrived = set()
circle = [0]
circle1 = []
arrived.add(0)
pre = 0
l = 0
for i in range(N):
    nt = A[pre]
    if nt not in arrived:
        arrived.add(nt)
        circle.append(nt)
    else:
        l = len(circle)
        #print(circle1)
        if not circle1:
            circle1.append(nt)
        elif nt != circle1[0]:
            circle1.append(nt)
        else:
            circle1.append(nt)
            break
    pre = nt
#print(circle)
#print(circle1)
index = (K - l)%len(circle1)
print(circle1[index] + 1)

Submission status: Wrong Answer

Solution code:
N ,K = map(int, input().split())
A = list(map(int, input().split()))
B = [0]
C = {} 
C[0] = 0
cnt = 0
jdg = 0
q = 0
while jdg == 0:
    i = A[q]-1
    q = i
    if q in C:
        l = C[q]
        #print(q,l)
        break
    cnt += 1
    B.append(i)
    C[i] = cnt
r = cnt+1
#print(q)
#print(B)
#print(C)
#print(l)
#print(r)
H = B[l:r]
print(H) 
M = (K-l)%(r-l)
#print(N-l,M)
if K >= r:
	print(H[M]+1)
else:
  print(B[K]+1)

Submission status: Wrong Answer

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

for j in range(2*N):
    i = A[i-1]
    if i in B:
        I = A[i-1]
        break
    else:
        B.append(i)
        
count = 0
i = 1
for j in range(N):
    if I == A[i-1]:
        CT = count
        break
    else:
        count += 1
        i = A[i-1]
        
flag = True
i = I
C = [I]

for j in range(N):
    if A[i-1] == I:
        break
    else:
        i = A[i-1]
        C.append(i)

if K >= CT:
    D = C[(K-CT-1)%(len(C))]
    print(str(D))
else:
    print(str(B[K-1]))

Submission status: Wrong Answer

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

V = [0 for i in range(n)]
P = [-100 for i in range(n)]
cp = 1
t = 0
while 1:
    #print('V = ', V)
    V[cp - 1] += 1
    if V[cp - 1] < 2:
        P[cp - 1] = t
    else:
        break
    cp = A[cp - 1]
    t += 1

pret = P[cp - 1]
t = t - pret
print('pret = ', pret)
print('t = ', t)
print('P = ', P)

if k <= pret:
    pass
else:
    k = k - pret
    P = list(map(lambda x: x - pret, P))
print('P = ', P)

l = k % t
ans = 0
for i in range(n):
    if P[i] == l:
        ans = i + 1
        break
    else:
        pass
print(ans)

Submission status: Wrong Answer

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

def detect_cycle():
    r = {i:0 for i in range(n)}
    lst = []
    a = 0
    while r[a] < 2:
        r[a] += 1
        lst.append(a)
        a = an[a]

    lg = sum(1 for k in r if r[k] > 1)
    lst = lst[-lg:]
    return lst, r

cycle, rt = detect_cycle()

# 循環の最初に行くまでカウント
a = 0
n = 0
while a != cycle[0]:
    a = an[a]
    n += 1
    
# 循環をショートカット
rest = (k - n) % len(cycle)
#if (k - n) // len(cycle) == 0:
#    a = cycle[-1]
for i in range(rest):
    a = an[a]

print(a + 1)

Submission status: Wrong Answer

Solution code:
def calc(i):
    ans_list = [i]
    ans_set = set([i])
    count = 1
    i = a[i]
    while (i not in ans_set) & (count <= 2 * n):
        ans_list.append(i)
        ans_set.add(i)
        count += 1
        i = a[i]
    index = ans_list.index(i)
    ans_list.append(i)
    return count, ans_list

n, k = map(int, input().split())
a = [int(i) - 1 for i in input().split()]

count, ans_list = calc(0)
if count <= 2 *n:
    index = ans_list.index(ans_list[-1])
    k = k - index
    ans_list = ans_list[index + 1:]
    print(ans_list[k % len(ans_list) - 1] + 1)
else:
    index = ans_list[-1]
    ans_list = ans_list[index:]
    k = k - index
    k = k % len(ans_list)
    print(ans_list[k] + 1)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))

    tree = [[] for _ in range(n)]
    for idx, a in enumerate(A):
        tree[idx].append(a - 1)

    start = []
    depth = [0 for _ in range(n)]

    def dfs(v, p, d, cnt):
        depth[v] = d
        for u in tree[v]:
            if u == p:
                continue

            if visited[u]:
                start.append([u + 1, depth[u], cnt + 1])
                return

            visited[u] = True
            dfs(u, v, d + 1, cnt + 1)
        return

    visited = [False for _ in range(n)]
    visited[0] = True
    dfs(0, -1, 0, 0)

    if not start:
        print(1)
    else:
        if k <= start[0][1]:
            print(depth.index(k) + 1)
        else:
            cnt = (k - start[0][1]) % (start[0][2] - start[0][1])
            res = start[0][1] + cnt
            print(depth.index(res) + 1)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:

n,k=map(int,input().split())
a=list(map(int, input().split()))
b=[1]
c=0
m=0
# while flag==1:
for i in range(n):
    if a[m] not in b:
        b.append(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=(e+2+k)%f
    print(b[ans])
else:
    ans=k%f
    print(b[ans])

Submission status: Wrong Answer

Solution code:
n = input().split()
N = int(n[0])
K = int(n[1])

lista = []
listb = [1]
C = 1
A = list(map(int, input().split()))

for i in range(N):
    lista.append(int(A[i]))
                 
for j in range(20000000):
    C = lista[C-1]
    if C not in listb:
        listb.append(C)
    else:
        break
                 
for i in range(len(listb)):
    if listb[i] == C:
        D = C
        I = i

if K <= N:
    print(C)
elif K > N and C == N:
    print(listb[K % N -1])
else:
    if C-D == 1 or C-D == 0:
        print(C)
    else:
        M = (K-I) % (C-D)
        print(listb[I+M-1])

Submission status: Wrong Answer

Solution code:

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

#ループする場合
flag=[0]*N
patt=[]
aaaa=0
i=0
while True:
    patt.append(i)
    if flag[i]==1:
        #del patt[-1]
        break
    flag[i]=1
    i=A[i]-1


srt = patt.index(patt[-1])
#print('srt',srt)
end = len(patt)-2
ptrn = patt[srt:end+1]

if K<len(ptrn)+1:
    print(patt[K]+1)
else:
    B = (K-srt) % len(ptrn)
    #print('B',B)

    print(ptrn[B]+1)
    #print('0000000')

    #print('ptrm',ptrn)
    #print(patt)

Submission status: Wrong Answer

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

# N, K = 6, 727202214173249351
# A = [6, 5, 2, 5, 3, 2]

now = 1
loop_node = []
first_loop = 0
l = [0 for _ in range(N)]
while True:
    l[now - 1] += 1
    if l[now - 1] == 3:
        break

    if l[now - 1] == 2:
        loop_node.append(now)

    now = A[now - 1]
    first_loop += 1
loop_num = len(loop_node)

print(loop_node[((K) - (first_loop - loop_num * 2)) % loop_num])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
lst = list(map(int, input().split()))
ans = []
dict = {}
at = 1
amari = 0
loop = 0
dict[1] = 1

while 1 :
    at = lst[at - 1]
    if at in dict.keys() :
        dict[at] += 1
    else :
        dict[at] = 1
    if max(dict.values()) == 1:
        amari += 1
    if max(dict.values()) == 2:
        ans += [at]
        loop += 1
    if max(dict.values()) == 3:
        break

value = (k - amari) % loop
print(lst[value])

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
cnt=1       #配列の長さを数える
while True:
    to=A[x]
    if visit[to]==0:    #未訪問だったら
        visit[to]=1     #訪問済みに変更
        cnt+=1
        L.append(to)
        x=to
    else:   #次の移動先が1だったらスタートに戻るのでループを打ち切る
        break

loop_fro=L.index(to)
loop_to=cnt-1
not_loop_len=loop_fro-1

if K<=not_loop_len:
    print L[K-1]
else:
    tmp=K-not_loop_len
    print L[K%(loop_to-loop_fro+1)]

Submission status: Wrong Answer

Solution code:
import sys

n,k = (int(i) for i in input().split())

a = [int(i)-1 for i in input().split()]

hantei = [True]*(n+1)
otozure = [None]*(n+1)
mokuteki = a[0]
moto = k
kazoe = 0
otozure[0] = 0
while k>0:
        
    if hantei[mokuteki] == False: #二週目突入
       # print("break\nmokuteki= {}\nk = {}".format(mokuteki,k))
        otozure[kazoe+1] = mokuteki
        saigo = otozure
        break

    else:
        hantei[mokuteki]= False
        otozure[kazoe+1] = mokuteki#今いるところを記憶
       # print(otozure[kazoe])
        mokuteki = a[mokuteki]#次に行くところ
        kazoe+=1
    k-=1
else:
    print(otozure[kazoe]+1)
    sys.exit()
i = 0
while True:
    if otozure[kazoe+1] == otozure[kazoe-i]:
        break
    i+=1


nokori = k%(i+1)
#print(nokori)
i=0
while otozure[i] != mokuteki:
    i+=1

if a[0]==0:
    print("1")
else:
    print(otozure[i-1+nokori]+1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,(input().split()))
x=list(map(int,input().split()))
y=0
c="0"
for i in range(k):
    c+=str(y)
    y=x[y]-1
    if c in str(y):
        d=c.find(str(y))
        ru=len(c)-d
        no=k-d
        print(d+(no%ru)-1)
print(y+1)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = [int(i) for i in input().split()]
tmp=0
tmplist=[0]
#loopしてない判定
loopinter=k%n
loopstart=0
for i in range(k):
    tmp=a[tmp]-1
    if tmp not in tmplist:
        tmplist.append(tmp)
    else:
        loopstart = tmplist.index(tmp)
        loopinter = len(tmplist)-tmplist.index(tmp)
        break
tmp=0
for i in range((k-loopstart)%loopinter+loopstart):
    tmp=a[tmp]-1
print(tmp+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()))
    li = [0]
    nt = 0
    rt = [1]
    de_num = 0

    for i in range(n):
        nt = a[nt]-1
        for j in range(len(rt)):
            if nt == rt[j]:
                de_num = j
                li[0:j] = []
                #print(li)
                break
        rt.append(nt)
        li.append(nt)

    x = (k-de_num) % len(li)
    # print(x)
    print(li[x]+1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a_list = list(map(int, input().split()))
route = []

now = 1
# next = a_list[now-1]

while True:
    if now in route:
        start = route.index(now)
        end = len(route)
        junkai = route[start:end]
        amari = route[:start]
        break
    route.append(now)
    now = a_list[now-1]
    # next = a_list[now-1]
    
hoge = (k-len(amari))%len(junkai)
print(junkai[hoge])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
M = list(map(lambda x: int(x)-1, input().split()))
s = 0
order = [s]
sets = set(order)
while True:
  if M[s] in sets:
    #print(order, M[s])
    K -= order.index(M[s])
    order = order[order.index(M[s]):]
    print(order[K%len(order)-1]+1)
    break
  order.append(M[s])
  sets.add(M[s])
  s = M[s]

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split(" "))
towns = [int(i)-1 for i in input().split(" ")]
loopCount = 0
nowPos = 0
posList = [0]
while not(nowPos==0 and loopCount!=0):
    nowPos = towns[nowPos]
    posList.append(nowPos)
    for i in range(0,len(posList)+1):
        for j in range(i+1,len(posList)):
            length = j - i
            for l in range(len(posList)-length):
                if posList[l: l + length] == posList[i: j] and i != l:
                    remain = K % loopCount - i
                    nowPos = posList[j-1]
                    for k in range(remain):
                        nowPos = towns[nowPos]
                    print(nowPos + 1)
                    exit()
    loopCount = loopCount + 1
    if loopCount == K:
        print(nowPos + 1)
        exit()

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
is_good = True
graph=[0]
s = 0
while is_good:
    next = a[graph[-1]]-1
    if  next in graph:
        next_ = graph.index(next)
        loop = len(graph) - next_
        is_good = False
    else:
        graph.append(next)


if k <= next_:
    print(graph[k-1] + 1)

else:
    k -= next_
    x = k%loop
    print(graph[x+next_]+1)

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
town = list(map(int,input().split()))
cnt = 1
step = 1
now = town[0]
via = [1]
while((now not in via)):
  via.append(now)
  now = town[now -1]
  cnt += 1
  step += 1

loop = via[via.index(now):]
print(loop[(k - cnt)%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

while loop == False:
      for i in range(0,K):
            X=ar[(Town-1)]
            Town=(X)
            route.append(Town)
            if Town in route:
                  loop=True
                  break
      
S=len(route)
K=K-i
Z=K % S
Z=Z+1    
for i in range(0,Z):
      X=ar[(Town-1)]
      Town=X

      
print(Town)

Submission status: Wrong Answer

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

ind = 0
#for i in range(k):
#   ind = A[ind]-1


print(k%(n/2))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
posit=0
rireki=[]
for i in range(k):
    rireki.append([i,posit])
    posit=A[posit]-1
    for j in range(len(rireki)):
        if rireki[j][1]==posit:
            loopend=i+1
            loopst=j
            k=(k-j)%(i+1-j)+j
            print(A[rireki[j][1]])
            quit()
    print(A[posit])
    quit()

Submission status: Wrong Answer

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

A = list(map(int, input().split()))
d = {}

d[1] = A[0]

idx = 1
start = 0
count = 0
while True:
  count += 1
  d[idx] = A[idx-1]
  idx = A[idx-1]
  if idx in d:
    start = idx
    break


e = {}
l = []
while True:
  e[start] = A[start-1]
  l.append(e[start])
  start = A[start-1]
  if start in e:
    break


amari = ((K-count-len(l)) % len(l))
if amari != 0:
  print(l[amari-1])
else:
  print(l[-1])

Submission status: Wrong Answer

Solution code:
def m():
    n, k = map(int, input().split())
    a = [0] + list(map(int, input().split()))
    visited = [[] for _ in range(n + 1)]
    visited[1].append(0)
    cur = 1
    i = 1
    while True:
        cur = a[cur]
        visited[cur].append(i)
        i += 1
        if len(visited[cur]) >= 2:
            break

    if k <= visited[cur][0]:
        now = 1
        for p in range(1, k+1):
            now = a[now]
        return now
    else:
        tmp = k - visited[cur][0]
        MOD = visited[cur][1] - visited[cur][0]
        count = tmp % MOD
        now = 1
        for p in range(1, count+1+tmp):
            now = a[now]
        return now


print(m())

Submission status: Wrong Answer

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


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    # Failed to predict input format
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))

    visited = [0] * (N+10)
    seq = []

    step = 0
    current = 1

    n1 = n2 = 0
    while(step<len(A)+1):
        #print(current)
        seq.append(current)
        step += 1
        visited[current] = step
        next_pos = A[current-1]
        if visited[next_pos]>0:
            n1 = visited[next_pos]
            n2 = step - n1 + 1
            break
        else:
            visited[next_pos] = step
        current = next_pos

    #print(n1, n2)
    #print(n1-1, ((K-(n1-1))%n2))

    print(seq[n1-1+((K-(n1-1))%n2)])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[int(i) for i in input().split()]
senni=[1]
senni_set=set(senni)
dabu=False
while dabu == False:
    foo = a[senni[-1]-1]
    if foo in senni_set:
        dabu = True
        tmp=senni.index(foo)
        t=len(senni)-tmp
        break
    senni.append(foo)
    senni_set.add(foo)
if t<=k and k%t < tmp:
    ad=0
    while k%t+ad < tmp:
        ad += t
    ans = senni[k%t+ad]
else:
    ans=senni[k%t]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

def main():
    ans = 0
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    next = a[0]
    for i in range(k%n):
        next = a[next-1]
    print(next)

if __name__ == '__main__':
    main()