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:
n,k=map(int,input().split())
a=list(map(int,input().split()))

s,r1,r2=0,0,0
p1,p2=[str(a[s])],[]
for i in range(k+1):
    if p1.count(str(a[s-1]))==0:
        p1.append(str(a[s-1]))
        r1+=1
    elif p2.count(str(a[s-1]))==1:
        break
    elif p1.count(str(a[s-1]))==1:
        p2.append(str(a[s-1]))
        r2+=1
    s=a[s-1]
if k<len(p1):
    print(p1[k])
else:
    k=(k-len(p1)+1)%len(p2)
    print(p2[k-1])

Submission status: Wrong Answer

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

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 or K < N:
    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())
a = list(map(int,input().split()))
ans = []
check = {}
key = -1
p = 0
while True:
    ans.append(p)
    check[p] = 0
    p = a[p]-1
    if p in check:
        key = ans.index(p)
        break
r = ans[key:]
mod = (k-key)%len(r)
print(r[mod]+1)

Submission status: Wrong Answer

Solution code:
def main():
    from builtins import int, map, list, print, len
    import sys
    sys.setrecursionlimit(10 ** 6)

    input = sys.stdin.readline
    input_list = (lambda: input().rstrip().split())
    input_number = (lambda: int(input()))
    input_number_list = (lambda: list(map(int, input_list())))

    N,K= input_number_list()
    A = input_number_list()
    route = [1]
    now = 1
    is_looop = False
    try:
        for i in range(N):
            if A[now-1] in route:
                is_looop = True
                break
            now = A[now-1]
            route.append(now)
    except Exception as e:
        pass
    if is_looop and len(route) < K:
        start = A[now-1]
        start_idx = route.index(start)
        l_count = start_idx
        r_count = len(route) - start_idx
        K -= l_count
        m = K % r_count
        print(route[start_idx + m])
    else:
        print(A[now-1])


if __name__ == '__main__':
    main()

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(lambda x: int(x)-1, input().split()))

visited = [1] + [0]*(n-1)
idx = [0] + [-1]*(n-1)
route = [0]
while True:
    now = a[route[-1]]
    if visited[now]:
        break
    visited[now] = 1
    idx[now] = len(route)
    route.append(now)

border = idx[a[route[-1]]]
non_loop = route[:border]
loop = route[border:]

if a[route[-1]] == 0:
    print(route[k%len(route)])
else:
    print(loop[(k-len(non_loop))%len(loop)]+1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict


def get_int():
    return int(input())


def get_tuple():
    return map(int, input().strip().split())


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


n, k = get_tuple()
ls = get_ls()
visited = defaultdict(int)
times = 0
town = 1
while town not in visited:
    visited[town] = times
    times += 1
    town = ls[town - 1]

period = times - visited[town]
if k<=times:
    town = 1
    ans = ls[town - 1]
    while k > 0:
        town = ans
        ans = ls[town - 1]
        k -= 1
    print(ans)
else:
    iter_req = visited[town]
    k -= visited[town]

    if k > period:
        k %= period
    iter_req += k
    town = 1
    ans = ls[town-1]
    while iter_req >1:
            town = ans
            ans = ls[town - 1]
            k -= 1
            #print(ans)
            iter_req -= 1

    print(ans)

Submission status: Wrong Answer

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

cur = 1
loop = -1
arr = []
for i in range(n): arr.append([False])

# ループする場所を調べる
while True:
    if arr[cur-1] == True:
        loop = cur
        break
    else:
        arr[cur-1] = True
        cur = a[cur-1]

cur = 1
cnt = 0
loop_cnt = 0
while True:
    cur = a[cur-1]
    cnt += 1
    if cur == loop and loop_cnt > 1: break
    if cur == loop: loop_cnt += 1
    if loop_cnt == 1: cnt = 0
    
v_cnt = 0
if loop == 1:
    v_cnt = 1
else:
    for i in range(n):
        v_cnt += 1
        if a[i] == loop: break

k %= cnt

for i in range(k + v_cnt):
    cur = a[cur-1]

print(cur)

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(ans[k % cnt])

Submission status: Wrong Answer

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

city = [i for i in range(n)]

egde = [-1] * n
tmp = a[0]
for i in range(n):
    tmp = a[tmp - 1]
    egde[tmp - 1] = i

tmp = a[0]
for i in range(k % (n - egde.count(-1)) - 1):
    tmp = a[tmp - 1]

print(tmp)

Submission status: Wrong Answer

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

slowRunner = 0
fastRunner = 0
flag = 1
while slowRunner!=fastRunner or flag:
    flag = 0
    nextSlow = listA[slowRunner]-1
    r = listA[fastRunner]-1
    nextFast = listA[r]-1
    slowRunner = nextSlow
    fastRunner = nextFast

f = 0
slowR1 = 0
slowR2 = slowRunner
while slowR1!=slowR2:
    f+=1
    nextR1 = listA[slowR1]-1
    nextR2 = listA[slowR2]-1
    slowR1 = nextR1
    slowR2 = nextR2

nodeL = []
start = slowR1
s = slowR1
flag = 1
while s!=start or flag:
    flag = 0
    nodeL.append(s+1)
    next = listA[s]-1
    s = next

amari = (k-f)%len(nodeL)
print(nodeL[amari])

Submission status: Wrong Answer

Solution code:
from collections import OrderedDict

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

returned = None
i = 0
aa = 0
visited = OrderedDict([(0, 0)])
while i < n:
    aa = a[aa] - 1
    i = i + 1
    if aa in visited:
        returned = (visited[aa], i)
        break
    visited[aa] = i

loop = returned[1] - returned[0]

if k >= len(visited):
    k = k % (returned[1] - returned[0])
    if k < returned[0]:
        k = k + returned[0]

print(list(visited.keys())[k] + 1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
aaa = [0] + list(map(int, input().split()))
cnt = [0, 1] + [0] * (n - 1)
tmp = 1
cic_num = 1
break1 = 0
for i in range(1, k + 1):
    tmp = aaa[tmp]
    if cnt[tmp] > 0:
        cic_num = i if tmp == 1 else i - cnt[tmp]
        break1 = 1
        break
    cnt[tmp] = i
num = (k - cnt[tmp] + 1) % cic_num
print(cnt.index(num + cnt[tmp] - 1) if break1 == 1 else tmp)

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_end+1:
    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:
import sys

N, K = map(int, input().split())
if K == 0:
  print(1)
  sys.exit()
A = list(map(int, input().split()))
now = 1
count = 0
start = 0

now = A[now-1]
route = [now]
count += 1

def check(now, route, count, K):
  now = A[now-1]
  if now in route:
    start = route.index(now)
    return 0   
  else:
    route.append(now)
    count += 1

check(now, route, count, K)

if K <= start:
  print(route[K-1])
else:
  K = K-start
  print(route[(K % count-start)-1+start])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = input().split()
x = 0
y = 0
for i in range(K + 1):
    y = int(x) - 1
    x = A[y]
print(x)

Submission status: Wrong Answer

Solution code:
import sys 
n,m=list(map(int,input().split(" ")))
 
arr=list(map(int,input().split(" ")))

def bfs(start,n,visited,m):
	# count=0

	# l2=[i+1]
	
	# count+=1
	moves=[]
	moves.append(start)
	visited[start]=1
	left=-1
	fl=0
	while(True):

		j=arr[start]-1
		
		if(visited[j]==0):
			moves.append(j)
			visited[j]=1
			# l2.append(j)
			# q.append(j)

			# count+=1
		else:
			left=j
			fl=1
			break
		start=j


	# print(moves,left)
		# q.popleft()
	# print(left,moves)
	if(left!=-1):
		pre=[]
		post=[]
		for i in range(len(moves)):
			if(moves[i]==left):
				pre=moves[:i]
				post=moves[i:]
				break
		if(m<=len(pre)):
			print(pre[m-1]+1)
		else:
			m-=len(pre)
			# print

			print(post[m%len(post)]+1)


	else:
		# curr=0
		# print("hdks")
		print(moves[m%len(moves)]+1)
visited=[0]*n
bfs(0,n,visited,m)

Submission status: Wrong Answer

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

pos = 0
memo = [[False, 0] for _ in range(N)] # memo[i]: [bool, tel_cnt] , [数えたか、何回移動したか]
memo[0] = [True, 0]
distance = 0
l_pos = 0
tel_cnt = 0
while True:
    next = A[pos]
    tel_cnt += 1
    if memo[next][0]: # 数えた
        r_pos, l_pos = memo[pos][1], memo[next][1]
        distance = r_pos - l_pos
        break
    memo[next][1] = tel_cnt
    memo[next][0] = True
    # index 更新
    pos = next

def find_idx_by_telcnt(tel_cnt):
    for i in range(N):
        m = memo[i]
        if m[1] == tel_cnt:
            return i

if K <= l_pos:
    print(find_idx_by_telcnt(K))
else:
    diff = K - l_pos
    # print(K)
    # print(l_pos)
    # print(diff)
    # print(distance)
    rem = diff % (distance + 1)
    ans_pos = l_pos + rem
    print(find_idx_by_telcnt(ans_pos) + 1)

Submission status: Wrong Answer

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

s2 = input().split()
A = []
for i in range(N):
  A.append(int(s2[i]))
  
now = 1
root = []
root.append(now)

for k in range(K):
  new = A[now-1]
  if new in root:
    Ka = K - root.index(new)
    new_root = root[root.index(new):]
    print(new_root[Ka%len(new_root)])
    break
  else:
    now = new
    root.append(new)

Submission status: Wrong Answer

Solution code:
def main():
    # 町の数N, 町iの転送先はindexがAi-1の町
    N,K = map(int, input().split())
    A = list(map(int, input().split()))

    # K回移動した時の町番号を出力
    # もし一度出てきた番号が出てきたら記憶している道順リストを使う
    # 最初は道を覚えさせる作業が必要
    now = 0
    history = [0]
    i = 0
    while i<K:
        # まず進む
        print(i)
        now = A[now]-1
        # 道順登録
        if now not in history:
            history.append(now)
            i+=1
        # 以前出てきてる町ならそこから末尾までを自動で進めてiのカウントも進める
        else:
            # 末尾未満
            skip = len(history)-history.index(now)-1
            now = history[history.index(now)+skip]
            i += skip

    print(now+1)

main()

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
flag = 0
start = 0
position = [0]
while flag == 0:
  temp = position[-1]
  if A[temp] -1 in position:
    flag = 1
    start = position.index(A[temp] -1)
  position.append(A[temp]-1)

x = start + (K - start)%(len(position) - start -1)

print(position[x]+1)

Submission status: Wrong Answer

Solution code:
'''
.append(要素) リストに要素を追加
.extend(リスト) リストを連結する
.insert(場所,要素) 指定した場所に要素追加
del 配列名[場所]
-1で末尾で末尾から一つ前

sort()
sorted(reverse=true)

配列の初期化
配列名=[a]*n+[b]*m

Decimal()
'''

from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy
import math
from functools import reduce
from operator import mul
from collections import Counter
import itertools
import fractions
import sys

t = [int(s) for s in stdin.readline().rstrip().split()]
n = t[0]
k = t[1]
Ak = [int(s) for s in stdin.readline().rstrip().split()]

data = [1]
tmp = 0
for el in Ak:
    data.append(Ak[tmp])
    tmp = Ak[tmp]-1
used = []
head = 0
mark = [int(s) for s in '0'*n]
for el in data:
    if (mark[el-1] == 1):
        head = el
        break
    mark[el - 1] = 1
    used.append(el)
key = used.index(head)
k -= key
print(used[key+k % (len(used) - key)])

Submission status: Wrong Answer

Solution code:
import bisect
n,k = map(int,input().split())
A = list(map(int,input().split()))
memo = [0 for _ in range(n+1)]
dict = {i:0 for i in range(1,n+1)}
memo[0] = 1
dict[1] = 1
i = 1
j = 1
loopnum = 0
ans = 0
while(j <= k):
    if dict[A[i-1]] == 1:
        loopnum = A[i-1]
        break
    a = A[i-1]
    memo[j] = a
    dict[a] = 1
    i = a
    j += 1
    ans = a
if loopnum != 0:
    if loopnum == 1:
        left = 0
    else:
        left = bisect.bisect_left(memo,loopnum)+1
    f = j - left
    diff = max(left,0)
    index = (k - diff) % f + diff
    ans = memo[index]

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
fp=[]
nw=a[0]-1
while not nw in fp:
  fp.append(nw)
  nw=a[nw]-1
ft=fp.index(nw)
lp=len(fp)-ft
idx=min((k-ft)%lp+ft,k)
print(fp[idx-1]+1)

Submission status: Wrong Answer

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

pos = 0
kiseki = [0]
while len(kiseki) < N:
    pos = A[pos] - 1
    if pos in kiseki:
        past = kiseki.index(pos)
        kiseki = kiseki[past:]
        print(kiseki[(K - past) % len(kiseki)] + 1)
        exit()
    else:
        kiseki.append(pos)
        # print(kiseki)

print(kiseki[K % len(kiseki)] + 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])
else:
	y = (k-first)%repeat
	print(ex_r[first+y-1])

Submission status: Wrong Answer

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

for i in range(n):
    c = b.count(b[i])
    if c != 1:
        break

index_num = [n for n, v in enumerate(b) if v == b[i]]
ans = index_num[0] + 1
e = index_num[1]

f = e - ans+ 1

g = k - ans
h = g % f
print(b[index_num[0]+h])

Submission status: Wrong Answer

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

citys = [False] * n
now = 0
citys[0] = True
costt = 0
while True:
    now = a[now] - 1
    if citys[now]:
        costt += 1
        break
    else:
        citys[now] = True
        costt += 1

citys = [False] * n
citys[now] = True
check = [0] * n
check[now] = 1
cost = 0
while True:
    now = a[now] - 1
    if citys[now]:
        cost += 1
        break
    else:
        citys[now] = True
        cost += 1
        check[now] = cost + 1

costt -= cost
d = (k - costt) % cost + 1
if costt > k:
    i = 0
    while i != k:
        now = a[now] - 1
        i += 1
    print(now+1)
    quit()
ans = check.index(d)
print(ans+1)
print(check)

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+1 < loop_end:
    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,m,x = (int(x) for x in input().split())
b = [0]*n
a = []
for i in range(n):
    a.append(list(map(int,input().split())))

ans = b[0]
for i in range(n):
    if min(b)>=x:
        break
    else:
        for j in range(1,m+1):
            b[j]+=a[i][j]
            ans+=a[i][0]

print(ans)
"""
n,k = (int(x) for x in input().split())
a = [int(i) for i in input().split()]
an = 0
my = [a[0]]

def has_duplicates(seq):
    return len(seq) != len(set(seq))

i = a[0]

if a[0]==1:
    print(1)

else:
    for _ in range(n):
        if has_duplicates(my) is True:
            break
        else:
            my.append(a[i-1])
            i = a[i-1]
    p = len(my)
    q = my.index(my[p-1])
    r = p-q-1
    if k<=p:
        print(my[k-1])
    else:
        s = (k-p+1)%r
        if s !=0:
            print(my[q-1+s])
        else:
            print(my[p-1])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
li = list(map(int,input().split()))
count = 2
li2 = [1]
li2.append(li[0])
for i in range(n):
    li2.append(li[li2[i+1]-1])
    count += 1
    if len(set(li2)) != count:
        break
if count == n + 2:
    print(li2[-2])
else:
    s = li2.index(li2[-1])
    del li2[0:s]
    del li2[-1]
    print(li2[(k-s) % len(li2)])

Submission status: Wrong Answer

Solution code:
from collections import deque
n, k = list(map(int, input().split()))
a = [0, ] + list(map(int, input().split()))
stack = deque([1, ])
visited = [0] * (n + 1)
hist = []
open = []
loop = []
while stack:
    now = stack.popleft()
    if visited[now] == 1:
        b = hist.index(now)
        open = hist[:b]
        loop = hist[b:]
        break
    next = a[now]
    visited[now] = 1
    hist.append(now)
    stack.append(next)
print(loop[(k - len(open)) % len(loop)])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
dic = {}
dic[1] = 1
new = 1
rest = 0
ball = 0
for i in range(1, n):
    new = a[new - 1]
    if new in dic:
        rest = dic[new] - 1
        ball = i - rest
        break
    else:
        dic[new] = i + 1
hoge = list(dic)
print(hoge[(k - (rest)) % ball + rest])

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
a = list(map(int, input().split()))
get = [1e100 for i in range(n)]

point = 0
num = 0
mod = 0

while(True):
    if(get[point]!=1e100):
        tmp = get[point]
        break
    get[point] = num
    point = a[point]-1
    num += 1


for i in get:
    if(i < tmp or i == 1e100):pass
    else:mod += 1

if(tmp == 0):
    print(get.index(m%mod)+1)

else:
    if(m <= tmp):
        print(get.index(m%mod)+1)


    newm = m - tmp
    print(get.index(newm % mod + tmp)+1)

#print(mod)
#print(get)

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()))

    city = 1
    visited = {city: 0}
    cycle_len = 1

    for i in range(N):
        city = A[city - 1]
        visited[city] = i
        if visited.get(A[city - 1], False):
            offset = visited[A[city -1]]
            cycle_len = i
            break

    K = (K - offset) % cycle_len

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

    print(city)
    
    pass

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = k % n
ans = mod
for x in range(len(str(mod))+ 1):
    ans = a[ans]
print(ans)

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(int(A[place]-1))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
now = 1
x = 0
ss = set([1])
for i in range(k):
    now = a[now-1]
    ss.add(now)
    if len(ss) != i+1:
        x = 1
        break 
if x == 1:
    aa = k%(len(ss)+1)
    if aa == 0:
        for i in range(len(ss)):
            now = a[now-1]
    else:
        for i in range(aa-1):
            now = a[now-1]
print(now)

Submission status: Wrong Answer

Solution code:
N,K=(int(x) for x in input().split())
A=list(map(int, input().split()))
if K<=2*N:
    s=1
    for i in range(K):
        s=A[s-1]
    print(s)
else:
    s=1
    S={1}
    iloop=1
    while A[s-1] not in S:
        S.add(A[s-1])
        s=A[s-1]
        iloop=iloop+1
    p=s
    loop=1
    while A[s-1] != p:
        loop=loop+1
        s=A[s-1]
    g=(K-iloop)%loop+iloop
    for i in range(g):
        s=A[s-1]
    print(s)

Submission status: Wrong Answer

Solution code:
# region header
import sys
import math
from bisect import bisect_left, bisect_right, insort_left, insort_right
from collections import defaultdict, deque, Counter
from copy import deepcopy
from fractions import gcd
from functools import lru_cache, reduce
from heapq import heappop, heappush
from itertools import accumulate, groupby, product, permutations, combinations, combinations_with_replacement
from math import ceil, floor, factorial, log, sqrt, sin, cos
from operator import itemgetter
from string import ascii_lowercase, ascii_uppercase, digits
sys.setrecursionlimit(10**6)
INF = float('inf')
MOD = 10 ** 9 + 7
def rs(): return sys.stdin.readline().rstrip()
def ri(): return int(rs())
def rf(): return float(rs())
def rs_(): return [_ for _ in rs().split()]
def ri_(): return [int(_) for _ in rs().split()]
def rf_(): return [float(_) for _ in rs().split()]
def divisors(n, sortedresult=True):
    div = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            div.append(i)
            if i != n // i:
                div.append(n//i)
    if sortedresult:
        div.sort()
    return div
# endregion
N, K = ri_()
A = ri_()
d = [0] * N
d[0] = 1
cnt = 1
loop = 0
now = 0
while True:
    now = A[now] - 1
    if d[now]:
        loop = cnt - d[now] + 1
        break
    else:
        cnt += 1
        d[now] = cnt
ans = 0
for i in range(N):
    if d[i] == d[now] + (K - cnt) % loop:
        ans = i + 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from math import factorial
from collections import Counter
from fractions import Fraction
import heapq, bisect, fractions
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
MOD = 10**9 + 7

n, k = MAP()
a = LIST()
tugi = [0]+a
cyclestart = []
town  =1
temp = [town, ]

while True:
    town = tugi[town]
    if town in temp:
        i = temp.index(town)
        notcycle = temp[:i]
        cycle = temp[i:]
        cyclestart.append(town)
        break
    temp.append(town)
k -= len(notcycle)
print(cycle[k%len(cycle)])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
i = 0
ans = 0
root = [-1] * n
flag = [False] * n
j = 0
while flag[a[i] - 1] == False:
    flag[a[i] - 1] = True
    root[j] = a[i]
    i = a[i] - 1
    j += 1
s_i = root.index(a[i])
x = 0
for i in range(k % (j - s_i)):
    ans = root[x]
    x = root[x] - 1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import itertools as iter
import math as m

def main():
    N, K = list(map(int, input().split()))
    A = [-1]
    A += [int(i) for i in input().split()]
    L = [0] * (N + 1)

    L[1] = 1
    x = A[1]
    L[x] = 1
    for i in range(K-1):
        x = A[x]
        if L[x] == 2:
            #print(L)
            lc = (K-i) % L.count(2)
            for _ in range(lc):
                x = A[x]
            print(A[x])
            sys.exit()
        else:
            L[x] += 1
    print(x)
    

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]
c=A[A[0]]
 
s=set()
s.add(0)
s.add(A[0])
I=[]#訪れた島を記憶
I.append(0)
I.append(A[0])

while True:
  if c in s:
    break
  s.add(c)
  I.append(c)
  c=A[c]

a=I.index(c)
K-=a

ans=0
for i in range(a):
  ans=A[ans]
#ansが今いる島
mod=len(s)-a
K%=mod

if K==0:
  print(ans+1)
  exit()
#ans=A[0]
#for i in range(a):
#  ans=A[ans]

for i in range(K):
  ans=A[ans]
print(ans+1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
d = [a[0]]
pos = a[0] - 1
while a[pos] not in d:
    d.append(a[pos])
    pos = a[pos] - 1
st = d.index(a[pos])
mae = len(d[:st])
ptt = len(d[st:])
cur = (k-mae)%ptt
print(d[cur-1+mae])

Submission status: Wrong Answer

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

# print(a_list)

NN = N
if N > K:
    NN = N
else:
    NN = N + K % N

next = 1
for _ in range(NN):
    next = a_list[next-1]

print(next)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
index = 0
ans = [1]
value = 1
for i in range(K):
    value = A[value-1]
    if value in ans:
        index = ans.index(value)
        break
    ans.append(value)
    
K -= index
h = K%(i-index+1)
print(A[index+h])

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=[]
i=1
count=0
while i not in place:
    place.append(i)
    i=A[i]
    count+=1
loop=[i]
start=i
j=A[i]
count+=1
while j!=start:
    loop.append(j)
    j=A[j]
    count+=1
num=(K-count)%len(loop)
print(loop[num])

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

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] 
    break
  flag[now] = i+2

  
#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=map(int,input().split())
a_list=list(map(int,input().split()))

flag=[0]*n
place=[0]*n
p=0
for i in range(len(a_list)+1):
    if flag[p]>0:
        rep=i
        replis=place[flag[p]-1:i]
        st=p+1
        break
        
    else:
        place[i]=p+1
        flag[p]=i+1
        p=a_list[p]-1

print(replis[(k-i-len(replis))%len(replis)])