Problem:


Score: 400 points

Problem Statement

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

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

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

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

Constraints

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

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

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

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

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

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2



Sample input:
4 5
3 2 4 1


Sample output:
4

Submission status: Wrong Answer

Solution code:
from copy import copy, deepcopy
from collections import Counter
from math import sqrt, floor, factorial
from itertools import permutations, combinations, combinations_with_replacement
from operator import mul
from functools import reduce
import bisect

MOD = 10**9 + 7
INF = float('inf')

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

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

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

mem = [0]*N
mem2 = [0]*N

idx = 0
count1 = 0
while True:
    mem[idx] += 1
    if mem[idx] == 2:
        break
    count1 += 1
    idx = A[idx]


idx2 = idx
count2 = 0

count2 += 1
idx2 = A[idx2]

while True:
    if idx2 == idx:
        break
    count2 += 1
    idx2 = A[idx2]

tmp = count1 - count2
print(tmp, count2)

if tmp >= K:
    ans = 0
    for _ in range(K):
        ans = A[ans]
else:
    e = (K - tmp) % count2 + tmp
    ans = 0
    for _ in range(e):
        ans = A[ans]

print(ans+1)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
l=[0]*2*10**5
count=0
i=0
for n in range(2*10**5+1):
    if A[i] in l:
        count=l.index(A[i])
        break
    else:
        l[n]=(A[i])
        i=A[i]-1
        
print(A[(K-count)%(n)])

Submission status: Wrong Answer

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

know = []
now = 1
cnt = 0
while 1:
    if now in know: break
    know.append(now)
    now = a[now]

start = a[know[-1]]
for i,num in enumerate(know):
    if num == start: loop = know[i:] ; break

print(know,start,loop)

cnt = len(loop)
if k>=len(know):
    k -= len(know)
    ans = loop[k%cnt]

else:
    ans = know[k]

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
l =[0]+ list(map(int,input().split()))
q=l[1]
cnt=1
d=[]
d.append(q)
while True and cnt<=n:
    q=l[q]
    cnt+=1
    if q in d:
        d.append(q)
        break
    else:
        d.append(q)
a=d.index(d[-1])
la=len(d)-1
cir=la-a
k=k-a
k=k%cir

print(d[a+k-1])

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10**6)
readline = sys.stdin.readline

def main():
    N, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    lenA = len(A)

    tmp = []
    path = []
    setpath = set([])
    cnt = 0
    i = 1
    for _ in range(1, N+10):
        setpath.add(i)
        path.append(i)
        if len(setpath) != len(path):
            path.pop()
            itmp = path.index(i)
            break
        i = A[i-1]

    lenpath = len(path)
    if K >= lenpath:
        jtmp = (K-itmp)%(lenpath-itmp)
        ans = path[itmp+jtmp]
    else:
        ans = path[K-1]
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
import sys

N, K = [int(x) for x in stdin.readline().rstrip().split()]
A = list(map(int, input().split()))
B = [i for i in range(1, N+1)]
d = dict(zip(B, A))
t = [0 for i in range(N)]
d1 = dict(zip(B, t))
temp = 1
cnt = 0
ans = []
for i in range(K+1):
    d1[d[temp]] = d1.get(d[temp], 0)+1
    if d1.get(d[temp], 0) == 2:
        cnt += 1
        ans.append(d[temp])
        # print(i, d[temp])
    if d1.get(d[temp], 0) == 3:
        break
    temp = d[temp]
    if i == K:
        print(temp)
        sys.exit()
cnt1 = 0
for i, v in enumerate(d1):
    if v == 1:
        cnt1 += 1
# print(cnt, cnt1, d1, temp, ans)
k = K-1-cnt1
print(int(ans[k % cnt]))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
p=list(map(int,input().split()))
a=[0]+p
flag=0
bef=1
town=[1]
town_set=set(town)
cnt=0
while flag==0 and cnt<k:
    if not a[bef] in town_set:
        town.append(a[bef])
        town_set.add(a[bef])
        bef=a[bef]
    else:
        flag=1
        memo=a[bef]
    cnt+=1
if cnt==k:
    print(bef)
    exit()
for j in range(len(town)):
    if town[j]==memo:
        num=j
        break
x=len(town)-num
y=(k-num)%x
print(town[num+y])

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1


def II(): return int(input())


def MI(): return map(int, input().split())


def MI1(): return map(int1, input().split())


def LI(): return list(map(int, input().split()))


def LI1(): return list(map(int1, input().split()))


def LLI(rows_number): return [LI() for _ in range(rows_number)]


def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))


INF = float('inf')


def solve():
    n, k = MI()
    A = LI1()
    # print(A)
    memo = {0: 0}
    current = 0
    keiro = []
    for i in range(n):
        keiro.append(current)
        next = A[current]
        # print(i, current, next)
        if (i + 1) == k:
            print(current + 1)
            return

        if memo.get(next, -1) == -1:
            memo[next] = i + 1
            current = next
        else:
            pre = current
            current = next
            keiro.append(current)
            # print(pre, current)
            left = k - i - 1
            # print('aaa', memo[pre], memo[current])
            loopsize = memo[pre] - memo[current] + 1
            # print('a', left, loopsize)
            r = left % loopsize
            # print(memo)
            # print(keiro)
            # print(keiro[memo[current] + r])
            ans = keiro[memo[current] + r] + 1
            print(ans)
            return


if __name__ == '__main__':
    solve()

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]
g = [-1] * n

i = 0
j = 0
while g[j] == -1:
    if i == k:
        print(a[j])
        exit()
    g[j] = i
    i += 1
    j = a[j]

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

Submission status: Wrong Answer

Solution code:
import sys
a,b=map(int,input().split())
b=b+1
l=[0]*(a)
List=list(map(int,input().split()))
i=0
j=1
while l[i]==0 and j<b:
  l[i]+=j
  j+=1
  i=List[i]-1
if j==b:
  print(List[i])
  sys.exit()
roop=j-l[i]
c=(b-j)%roop
for y in range(len(l)):
  if l[y] ==c+l[i]:
    print(y+1)
    sys.exit()

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
A = list(map(int, input().split()))
Visited = [False]*N

S = "1"
i = 1
while not Visited[i-1]:
    Visited[i-1] = True
    i = A[i-1]
    S += str(i)

# print S
pos = S.find(S[-1])
# print pos

K -= pos
S = S[pos:-1]
# print S

ansp = K % len(S)
# print ansp
print (S[ansp])

Submission status: Wrong Answer

Solution code:
# D

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

def compute(city, K):
    for i in range(K):
        # print('\t', city)
        city = As[city-1]
    print(city)

cities_visited = [False] * N
i = 0
city = 1
city_visited_time = {}

while not cities_visited[city - 1]:
    # print(city)
    cities_visited[city - 1] = True
    city_visited_time[city-1] = i
    city = As[city-1]
    i += 1

loop_start = city_visited_time[city-1]
loop_end = i


div = (K - loop_start) // (loop_end - loop_start)
if K - loop_start - div * (loop_end - loop_start) < 0:
    compute(1, K)
compute(city, K - loop_start - div * (loop_end - loop_start))

Submission status: Wrong Answer

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

list = [0]
count = 0
next_A = 0
for i in range(len(A)):
    count = count+1
    try:
        idx = list.index(A[next_A]-1)
        idx_max = len(list)
        break
    except:
        list.append(A[next_A] - 1)
        next_A = A[next_A] - 1
        
if len(list) == K:
    print(list[-1]+1)
else:
    fin_idx = (K-idx)%(idx_max -idx)
    print(list[idx+fin_idx]+1)

Submission status: Wrong Answer

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

now = 1
r = [now]
while not A[now - 1] in r:
    r.append(A[now-1])
    now = A[now-1]
x = r.index(A[now-1])
L = len(r[x:])
i = ((K/1e9)%L*1e9)%L- x%L
print(r[x+(K-x)%L])

Submission status: Wrong Answer

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

if len(lis) == n:
  b = lis[n-1]
c = len(lis)
d = lis.index(b)
newlis = lis[d:c]
length = len(newlis)

t = k-d #最初のループを除く
s = t%length
if s ==0 :
    answer = newlis[length-1]
else:
    answer = newlis[s-1]
    
print(answer)

Submission status: Wrong Answer

Solution code:
def main(N, K, A):
    i = 0   # 現在地
    memo = []
    while i not in memo:
        memo.append(i)
        i = A[i] - 1    # pythonは一つずれる
    roop = memo[memo.index(i):]
    print(roop[(K - memo.index(i)) % len(roop)] + 1)    # pythonは一つずれる


if __name__ == '__main__':
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    main(N, K, A)

Submission status: Wrong Answer

Solution code:
def main():
	n, k = [int(x) for x in input().split(" ")]
	a = [int(x) for x in input().split(" ")]
	d = {}
	pos = 0
	step = 0
	cycle = False
	way = []
	while step < k and cycle == False:
		if pos in d:
			cycle = True
		else:
			d[pos] = step
			way.append(pos)
			pos = a[pos] - 1
			step += 1

	k -= step
	if k > 0:
		cycle = way[d[pos]:step]
		x = k % len(cycle)
		print(cycle[x] + 1)

main()

Submission status: Wrong Answer

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

s = [0] * N
p = 1
h = []
while True:
    h.append(p)
    s[p-1] = 1
    p = A[p-1]

    if s[p-1] != 0:
        break

last = h.index(p)
leng = len(h) - last
rem = (K - last) % leng
print(h[last:][rem])

Submission status: Wrong Answer

Solution code:



arr1 = list(map(int,list(input().split())))
arr2 = list(map(int,list(input().split())))
arr2.insert(0,0)

N=arr1[0]
K=arr1[1]

cnt=0

x=1
times=0
for _ in range(N):
	times+=1
	x=arr2[x]
	if(x==1):
		break
#print(times)

last=K%times
for _ in range(last):
	x=arr2[x]
print(x)

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    prev_p = 1
    g = []
    for i in range(len(A)-1):
        g.append(A[prev_p-1])
        prev_p = g[-1]
        
    if K <= N - 1:
        print(g[K-1])
    else:
        prev_ = g[-1]
        new_g = []
        for i in range(len(A)-1):
            new_g.append(A[prev_-1])
            prev_ = new_g[-1]
        
        K_ = (K - N) % len(new_g)
        print(new_g[K_])     


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

visited = [0] * N

order = []

cur = 1
cnt = 0

while True:
    if visited[cur-1] == 0:
        visited[cur-1] += 1
        if cnt == K:
            print(cur)

    elif visited[cur-1] == 1: # cur == 2
        
        order_from_cur = order[order.index(cur):]
        r = (K-cnt) % len(order_from_cur)

        final = order_from_cur[r]
        print(final)
        break
    order.append(cur)
    cur = A[cur - 1]
    cnt += 1

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
tmp = a[0] - 1
b = k % n-1
while b >= 0:
    tmp = a[tmp]
    b -= 1
print(tmp)

Submission status: Wrong Answer

Solution code:
n , k = map(int,input().split())
S = list(map(int,input().split()))
l = [1]
for i in range(2*n):
    l.append(S[l[i]-1])

L = len(l)
def f(s, e):
    a = s
    b = s

    while True:
        a += 2
        b += 1
        if a >= e:
            return None
        if l[a] == l[b]:
            break

    a = 0
    while True:
        a += 1
        b += 1
        if b >= e:
            return None
        if l[a] == l[b]:
            break

    return a, b

c, d = f(0, L)
loop = l[c:d]

m = d - c 
A = c + 1
B = d + 1
if k <=  n:
    print(l[k])
else:
   K = (k - A-1) % m
   print(loop[K-1])

Submission status: Wrong Answer

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

j=[]
for i in range(n):
  j.append(0)
l=[]
s=1
while j[s-1]==0:
  l.append(s)
  j[s-1]=1
  s=a[s-1]  

e=l.index(s)
c=len(l)-e
f=(k+1-e)%c

print(l[e+f-1])

Submission status: Wrong Answer

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

i = 0
P = [1]

while True:
  if a[i] in P:
    break
  else:
    P.append(a[i])
    i = a[i] - 1

loop = P[ P.index(a[i]): ]
print(loop[(k - (P.index(a[i])) ) % len(loop)])

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
row = []
been = [0]*n
now = 0
nex = 0
t = 0
l = [-1]*n
prev = 0
while been[now] == 0:
    l[now] = t
    row.append(now)
    been[now] = 1
    now = a[now]-1
    t += 1
    if been[now] == 1:
        prev = l[now]
# print(prev,t)
print(row[prev + (k-prev)%(t-prev)]+1)
# print(l)
# print(row)

Submission status: Wrong Answer

Solution code:
def main():
    n, k = [int(x) for x in input().split()]
    teleporter = [int(x) - 1 for x in input().split()]
    route = []
    route_append = route.append
    seen = [False] * n
    traveled, now_i = 0, 0

    while traveled < n + 100:
        if seen[now_i]:
            # テレポートがループしたら，ルート調査は終了する。
            break
        route_append(now_i)
        seen[now_i] = True
        now_i = teleporter[now_i]
        traveled += 1

    # 調査終了後，now_1 はループ部分の頭。ループ前にイントロがある。
    # ループ頭は何番目に尋ねるか？
    intro_length = [i for i, x in enumerate(route) if x == now_i][-1]
    # intro_length = route.index(now_i)
    loop_length = len(route) - intro_length
    arriving_i = intro_length + (k - intro_length) % loop_length
    return route[arriving_i] + 1


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

Submission status: Wrong Answer

Solution code:
N,K=map(int, input().split())
A = list(map(int, input().split()))
where = [0 for k in range(N)]
where[0]=1
loop=0
for k in range(N):
    where[k+1] = A[where[k]-1]
    print(where)
    if where[k+1] in where[0:k]:
        li = where[0:k]
        end = k+1
        start = li.index(where[k+1])
        loop = end -start
        print(start, end, loop)
        break
loopbefore = K -start

loopafter = loopbefore%loop

where2 = [where[end]]
for k in range(loopafter):
    where2.append(A[where2[k]-1])
print(where2[-1])

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 = 1
    current = 1

    n1 = n2 = 0
    while(step<len(A)):
        #print(current)
        seq.append(current)
        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
        step += 1
        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:
import sys

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

for _ in range(n):
    tmp = l[t-1]
    if memo1[t-1] != tmp:
        memo1[t-1] = tmp
        memo2.append(tmp)
        t = tmp 
    else:
        memo2.append(tmp)
        indexes = [i for i, x in enumerate(memo2) if x == memo1[t-1]]
        index = indexes[1] - indexes[0]
        exit
print(memo2)

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

Submission status: Wrong Answer

Solution code:
import sys

if __name__ == '__main__':
    n, k = list(map(lambda x: int(x), sys.stdin.readline().split(' ')))
    a = list(map(lambda x: int(x)-1, sys.stdin.readline().split(' ')))
    visited = [-1 for i in range(n)]
    rireki = [-1 for i in range(n)]
    
    p = 0
    visited[0] = 0
    rireki[0] = 0
    offset, loop = -1, -1
    for i in range(k):
        i += 1
        p = a[p]
        rireki[i] = p
        if visited[p] != -1:
            #print(i, visited[p])
            offset = visited[p]
            loop = i - offset+1
            break
        visited[p] = i
    
    k = k % loop
    print(rireki[k+offset]+1)

Submission status: Wrong Answer

Solution code:
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	if n<r:
		return 0
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=qb
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)
def dq():
	return queue.deque()
class UnionFind():
	def __init__(self, n):
		self.n = n
		self.parents = [-1]*n

	def find(self, x): # root
		if self.parents[x]<0:
			return x
		else:
			self.parents[x] = self.find(self.parents[x])
			return self.parents[x]

	def union(self,x,y):
		x = self.find(x)
		y = self.find(y)

		if x==y:
			return

		if self.parents[x]>self.parents[y]:
			x,y = y,x

		self.parents[x]+=self.parents[y]
		self.parents[y]=x

	def size(self,x):
		return -1*self.parents[self.find(x)]

	def same(self,x,y):
		return self.find(x)==self.find(y)

	def members(self,x): # much time
		root = self.find(x)
		return [i for i in range(self.n) if self.find(i)==root]

	def roots(self):
		return [i for i,x in enumerate(self.parents) if x<0]

	def group_count(self):
		return len(self.roots())

	def all_group_members(self):
		return {r: self.members(r) for r in self.roots()} # 1~n

n,k = readInts()
a = readInts()
ex = [0]*n
ex[0] = 0
ex_r = []
f = 1
l = 1
count = 0
if a[0]==1:
	print(1)
	exit()
while 1:
	l = a[l-1]
	ex_r.append(l)
	if ex[l-1]==1:
		break
	ex[l-1]=1

first = ex_r.index(ex_r[-1])
#print(ex_r)
p2 = ex_r[:-1]
#print(p2)
repeat = len(p2)-first
#print(first,repeat)

if k<=len(p2):
	print(ex_r[k-1])
else:
	y = (k-first)%repeat
	print(ex_r[first+y-1])

Submission status: Wrong Answer

Solution code:
import sys

n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
m=[0]*n
pla=a[0]
for i in range(k-1):
    if a[i] in m:
        q=i-m.index(a[i])
        del a[0:m.index(a[i])]
        p=len(a)%q
        print(a[p])
        sys.exit()
    else:
        m[i]=a[i]
        pla=a[pla-1]
print(pla)

Submission status: Wrong Answer

Solution code:
N , K = map(int,input().split())
B = {}
C = {}
D = {0}
A = list(map(int,input().split()))
place = 0
num = 0
C[0] = 0
while True:
    place = int(A[place]) - 1
    num += 1   
    if place in D:
        Y = int(num) - int(C[place])
        break
    elif num == K:
        break
    else:
        B[num] = place
        C[place] = num
        D.add(place)
try:
    X = (K - num) % Y
    print(B[C[place] + X] + 1)
except:
    print(A[place])

Submission status: Wrong Answer

Solution code:
import sys


def solve():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    b_lst = [False] * N
    b_lst[0] = True
    ans = 1
    save = -1
    counnnt = 0
    while True:
        counnnt += 1
        ans = A[ans-1]
        if b_lst[ans - 1]:
            break
        else:
            b_lst[ans-1] = True
    counter = 0
    save = ans
    while True:
        counter += 1
        ans = A[ans-1]
        if ans == save:
            break
    K -= (counnnt-counter)
    if K < 0:
        ans = 1
        for i in range(K):
            ans = A[ans-1]
        print(ans)
        sys.exit(0)
    K %= counter
    for i in range(K):
        ans = A[ans-1]
    print(ans)


solve()

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    cycle = 0
    flag = True
    visited = [0] * N
    visited[0] = 1
    visited_num = [0] * N
    before_city = 0
    visited_city = 0
    periodic = 0
    while flag:
        next_city = A[before_city] - 1
        cycle += 1
        if visited[next_city] == 1:
            visited_city = next_city
            periodic = cycle - visited_num[next_city]
            flag = False
        else:
            visited[next_city] = 1
            visited_num[next_city] = cycle
            before_city = next_city
    K_new = (K - cycle) % periodic
    before_city = visited_city
    for i in range(K_new):
        next_city = A[before_city] - 1
        before_city = next_city
    print(next_city+1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# D

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

def compute(city, K):
    for i in range(K):
        # print('\t', city)
        city = As[city-1]
    print(city)

cities_visited = [False] * N
i = 0
city = 1
city_visited_time = {}

while not cities_visited[city - 1]:
    # print(city)
    cities_visited[city - 1] = True
    city_visited_time[city-1] = i
    city = As[city-1]
    i += 1

loop_start = city_visited_time[city-1]
loop_end = i

if K < loop_start:
    compute(1, K)

div = (K - loop_start) // (loop_end - loop_start)
compute(city, K - loop_start - div * (loop_end - loop_start))

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split()) #int
a = input().replace(" ","") #str
chokudai = "1" #str
fp = "1" #str
telenum = 0 #int
while True:
  chokudai = a[int(chokudai)-1]
  telenum += 1
  rooph = fp.find(chokudai) #int
  if rooph != -1:
    rooplen = len(fp)-rooph
    break
  else:
    fp = fp+chokudai
print(fp[rooph+k%rooplen])

Submission status: Wrong Answer

Solution code:
import sys

temp = sys.stdin.readline().split(" ")
n = int(temp[0])
k = int(temp[1])

temp = sys.stdin.readline().split(" ")
telep = [int(i) for i in temp]

#telep = [i for i in range(n)]
# print(telep)

alreadyVisited = []

now = 1
piv = -1
counter = 0

for i in range(k):
    alreadyVisited.append(now)
    now = telep[now - 1]
    if now in alreadyVisited:
        piv = now
        break
    else:
        counter += 1
        pass

if piv != -1:
    index = alreadyVisited.index(piv)
    nl = alreadyVisited[index:]

    #print(nl)

    looptime = len(nl)
    beforeloop = index

    #print(beforeloop)
    #print(looptime)

    hoge = k - beforeloop
    #print(hoge)

    #print(divmod(hoge, looptime))

    _, h2 = divmod(hoge, looptime)
    #print(h2)

    print(nl[h2])

Submission status: Wrong Answer

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

    table = [0] * n
    j = 1
    i = 1
    while True:
        if table[j - 1] != 0:
            break
        table[j - 1] = i
        j = a[j - 1]
        i += 1

    t_max = max(table)
    ini_roop = a[table.index(t_max)] - 1
    roop = t_max - ini_roop
    k_ini = k - table[ini_roop] + 1

    amari = k_ini % roop
    if ini_roop + amari + 1 in table:
        print(table.index(ini_roop + amari + 1) + 1)

    else:
        print(ini_roop + 1)
resolve()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
visited=[0]*N#訪問フラグ
ima_pos=0#現在地
mirai_pos=0#移動先

count_first=0#一巡目のループ
first_DP=[]
while(visited[ima_pos]==0):
  visited[ima_pos]=1#訪れたら更新
  mirai_pos=A[ima_pos]-1
  ima_pos=mirai_pos
  first_DP.append(ima_pos)
  count_first+=1#何回移動したか

second_start=ima_pos#2巡目ループのスタート地点
count_second=0#2巡目以降のループ
second_DP=[]

while(True):#スタート地点に戻ってきたら抜ける
  visited[ima_pos]=2#訪れたら更新
  mirai_pos=A[ima_pos]-1
  ima_pos=mirai_pos
  second_DP.append(ima_pos)
  count_second+=1#何回移動したか
  if ima_pos==second_start:
    break
#各DPには何番目(0-n-1)の町に行くか　が書かれている
if K<=count_first and count_second==0:
  print(first_DP[K-1])
elif K>count_first:
  cc=K-count_first
  if cc%count_second==0:
    print(second_DP[-1]+1)
  else:
    print(second_DP[cc%count_second-1]+1)

Submission status: Wrong Answer

Solution code:
# Problem D - Teleporter

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

# initialization
visit_list = [0]*N
visit_list[0] = 1
pos = a_nums[0]
visit_count = 0

while visit_list[pos-1]==0 and visit_count<K:
    visit_count += 1
    visit_list[pos-1] = visit_count
    pos = a_nums[pos-1]
visit_count += 1

# calc 1
if visit_count<K:
    tmp_1 = 0
    if pos==1:
        tmp_1 = visit_count - visit_list[pos-1] + 1 # 周期
    else:
        tmp_1 = visit_count - visit_list[pos-1]
    tmp_2 = (K-visit_count) % tmp_1 # 何周するのか
    # tmp_2回分ループ処理を行う
    for i in range(tmp_2):
        pos = a_nums[pos-1]

# output
print(pos)

Submission status: Wrong Answer

Solution code:
import sys


def solve():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    b_lst = [False] * N
    b_lst[0] = True
    ans = 1
    save = -1
    counnnt = 0
    while True:
        counnnt += 1
        ans = A[ans-1]
        if b_lst[ans - 1]:
            break
        else:
            b_lst[ans-1] = True
    counter = 0
    save = ans
    while True:
        counter += 1
        ans = A[ans-1]
        if ans == save:
            break
    K -= (counnnt-counter)
    if K < 1:
        ans = 1
        for i in range(K):
            ans = A[ans-1]
        print(ans)
        sys.exit(0)
    K %= counter
    for i in range(K):
        ans = A[ans-1]
    print(ans)


solve()

Submission status: Wrong Answer

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

for i in range(2*n):
    if a[now[i]-1] in now:
        break
    now.append(a[now[i]-1])

    
t = len(now)-now.index(a[now[i]-1])
f = now.index(a[now[i]-1])


if k >= f + t:
    p = (k-f)%t
    k = f + p
    
for i in range(k):
      ans = a[ans]
print(ans)

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 = [1]
    loopstart = 0
    nextcity = 1

    for n in range(N):
        count += 1
        visit += [nextcity]
        nextcity = A[nextcity-1]
        if nextcity in visit:
            loopstart = nextcity
            break
    
    lo = visit.index(nextcity)
    
    res = (K - lo + 1) % (len(visit) - lo)

    nextcity = loopstart

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




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
cnt = 0
vectors = []

n, k = map(int, input().split())
A = [int(i) - 1 for i in input().split()]
check = [0]
now = 0
while True:
    nxt = A[now]
    if (now, nxt) in vectors:
        break
    elif cnt == k:
        print(A[now])
        exit()
    else:
        cnt += 1
        check.append(nxt)
        vectors.append((now, nxt))
        now = nxt
idx = vectors.index((now, nxt))  # loop開始idx
loop = len(vectors) - idx  # loop長
# print(loop)
edge = len(vectors) - loop
# print(edge)
ans_idx = k % loop
# print(ans_idx)
print(A[ans_idx] + 1)

Submission status: Wrong Answer

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

    p_list = [1]
    ne_p = a[0]
    p_bool = [False] * n
    p_bool[0] = True
    check = False
    step = 0
    for _ in range(n):
        p_bool[ne_p-1] = True
        p_list.append(ne_p)
        step = 0
        if step == k:
            #print(ne_p)
            sys.exit()
        ne_p = a[ne_p-1]
        # if ne_p in p_list:
        #     ind = p_list.index(ne_p)
        #     break
        if p_bool[ne_p-1]:
            check = True
            break
        
    #print(p_list)
    #print(p_bool)
    if check:
        ind = p_list.index(ne_p)
    else:
        ind = 0
    k = k - ind
    #print(ind)
    #print(k)
    #print(p_list)
    #print(k%(len(p_list)-ind))
    print(p_list[ind + k%(len(p_list)-ind)])

solve()

Submission status: Wrong Answer

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

flag = [0]*(N+5) # 何回目に初めて訪れたか 
flag[0] = 1

tmp = 0
period = 1
period_pre = 0 # 周期が始まるまでのワープ回数
now = 0
for i in range(N):
  now = A[now]-1
  #print(now+1)
  #print(flag)
  #print()
  if(flag[now] != 0):
    period_pre = flag[now] 
    period = (i+2)-flag[now] 
    tmp = 1
    break
  flag[now] = i+2
if(tmp == 0):
  period = N
  
#print('period',period)  
#print('period_pre',period_pre) 
if(period_pre >= K):
  num = period_pre
else:
  num = period_pre+((K-period_pre)%period)
#print('num',num)

now = 0
for i in range(num):
  now = A[now]-1
ans = now+1 # index→town_no  
print(ans)

Submission status: Wrong Answer

Solution code:
n, k_in=map(int, input().split())
a_l=list(map(int, input().split()))

def f(s):
    a = s
    b = s
    i=1
    while True:
        a = a_l[a-1]
        a = a_l[a-1]
        b = a_l[b-1]
        if a == b:
            break
        i+=1

    j=1
    a = a_l[0]
    while True:
        a = a_l[a-1]
        b = a_l[b-1]
        if a == b:
            break
        j+=1

    k=1
    while True:
        a = a_l[a-1]
        a = a_l[a-1]
        b = a_l[b-1]
        if a == b:
            break
        k+=1

    return j, k

lam, myu = f(a_l[0])
# print(lam,myu)
pos=[]
pos.append(a_l[0])
for i in range(n):
    pos.append(a_l[pos[i]-1])

if k_in<=lam:
    print(pos[lam-1])
else:
    k_in=(k_in-lam)%myu
    print(pos[lam+k_in-1])

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])
    if len(order) >= K:
      print(order[K-1]+1)
    else:
      K -= order.index(M[s])
      order = order[order.index(M[s]):]
      print(order[K%len(order)]+1)
    break
  order.append(M[s])
  sets.add(M[s])
  s = M[s]