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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n, k = LI()
a = LI()

path = [-1] * n
i = 1
cnt = 1
while 1:
    i = a[i-1]
    if path[i-1] != -1:
        cnt -= 1
        break
    path[i-1] = cnt
    cnt += 1
s = path[i-1] - 1
if cnt >= k:
    for i in range(n):
        if path[i] == k:
            print(i+1)
            break
else:
    m = (k - s) % (cnt - s)
    for i in range(n):
        if path[i] - s == m:
            print(i+1)
            break

Submission status: Wrong Answer

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n, k = LI()
a = LI()

path = [-1] * n
i = 1
cnt = 1
while 1:
    i = a[i-1]
    if path[i-1] != -1:
        cnt -= path[i-1]
        s = path[i-1] - 1
        break
    path[i-1] = cnt
    cnt += 1

if s >= k:
    for i in range(n):
        if path[i] == k:
            print(i+1)
            break
else:
    m = (k - s) % cnt
    if m == 0:
        m = k
    for i in range(n):
        if path[i] == s + m:
            print(i+1)
            break

Submission status: Wrong Answer

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n, k = LI()
a = LI()

path = [-1] * n
i = 1
cnt = 1
while 1:
    i = a[i-1]
    if path[i-1] != -1:
        cnt -= path[i-1]
        s = path[i-1] - 1
        break
    path[i-1] = cnt
    cnt += 1
if s >= k:
    for i in range(n):
        if path[i] == k:
            print(i+1)
            break
else:
    m = (k - s) % cnt
    for i in range(n):
        if path[i] == s + m:
            print(i+1)
            break

Submission status: Wrong Answer

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heappop, heappush
from math import sqrt
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

class Roop:
    def __init__(self, array):
        self.n = len(array)
        self.array = array
        # ループ検出
        self.roops = []
        # iはどのループのものか
        self.roop_dict = [-1] * self.n
        # ループ内の何番目にあるか
        self.opt_dic = {}
        ignore = [-1] * self.n
        cnt = 0
        for i in range(self.n):
            if ignore[i] >= 0:
                continue
            opt = [i]
            # opt内の何番目にあるか
            self.opt_dic[i] = 0
            c = 1
            # 探索したらフラグを立てる
            ignore[i] = 1
            # i → array[i]
            to = array[i]
            # ループが詰まるまで回す
            while True:
                if ignore[to] >= 0:
                    # 既にループ作成済なら
                    if self.roop_dict[to] >= 0:
                        break
                    # 作成してないならループ作成
                    for j in range(self.opt_dic[to], len(opt)):
                        self.roop_dict[opt[j]] = cnt
                    self.roops.append(opt[self.opt_dic[to]:])
                    # 次のループはcnt + 1番
                    cnt += 1
                    break
                opt.append(to)
                ignore[to] = 1
                self.opt_dic[to] = c
                c += 1
                to = array[to]

    # xがどの番号のループにあるか
    def roop_n(self, x):
        return self.roop_dict[x]

    # xが入っているループは何か
    # ループ内になければFalse
    def inspect(self, x):
        if self.roop_n(x) == -1:
            return False
        return self.roops[self.roop_n(x)]

    # ループの大きさ
    def roop_len(self, x):
        return len(self.roops[self.roop_n(x)])

    def move(self, x, k):
        cnt = k
        to = x
        head = []
        tail = []
        time = 0
        res = 0
        while cnt > 0:
            # 踏んだ場所がループ内にない時
            if self.roop_n(to) == -1:
                cnt -= 1
                to = self.array[to]
                head.append(to)
                res = head[-1]
            # ループ内にある時
            else:
                tail.append(to)
                r = self.roops[self.roop_n(to)]
                cnt -= (cnt // len(r)) * len(r)
                index = self.opt_dic[to] + 1
                while cnt > 0:
                    cnt -= 1
                    tail.append(r[index % len(r)])
                    res = tail[-1]
                    index += 1
        return res

N, K = getNM()
N -= 1
A = [i - 1 for i in getList()]
roop = Roop(A)
print(roop.move(0, K) + 1)

Submission status: Wrong Answer

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heappop, heappush
from math import sqrt
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

class Roop:
    def __init__(self, array):
        self.n = len(array)
        self.array = array
        # ループ検出
        self.roops = []
        # iはどのループのものか
        self.roop_dict = [-1] * self.n
        # ループ内の何番目にあるか
        self.opt_dic = {}
        ignore = [-1] * self.n
        cnt = 0
        for i in range(self.n):
            if ignore[i] >= 0:
                continue
            opt = [i]
            # opt内の何番目にあるか
            self.opt_dic[i] = 0
            c = 1
            # 探索したらフラグを立てる
            ignore[i] = 1
            # i → array[i]
            to = array[i]
            # ループが詰まるまで回す
            while True:
                if ignore[to] >= 0:
                    # 既にループ作成済なら
                    if self.roop_dict[to] >= 0:
                        break
                    # 作成してないならループ作成
                    for j in range(self.opt_dic[to], len(opt)):
                        self.roop_dict[opt[j]] = cnt
                    self.roops.append(opt[self.opt_dic[to]:])
                    # 次のループはcnt + 1番
                    cnt += 1
                    break
                opt.append(to)
                ignore[to] = 1
                self.opt_dic[to] = c
                c += 1
                to = array[to]

    # xがどの番号のループにあるか
    def roop_n(self, x):
        return self.roop_dict[x]

    # xが入っているループは何か
    # ループ内になければFalse
    def inspect(self, x):
        if self.roop_n(x) == -1:
            return False
        return self.roops[self.roop_n(x)]

    # ループの大きさ
    def roop_len(self, x):
        return len(self.roops[self.roop_n(x)])

    def move(self, x, k):
        cnt = k
        to = x
        head = []
        tail = []
        time = 0
        res = 0
        while cnt > 0:
            # 踏んだ場所がループ内にない時
            if self.roop_n(to) == -1:
                cnt -= 1
                to = self.array[to]
                head.append(to)
                res = head[-1]
            # ループ内にある時
            else:
                tail.append(to)
                r = self.roops[self.roop_n(to)]
                cnt -= (cnt // len(r)) * len(r)
                index = self.opt_dic[to] + 1
                while cnt > 0:
                    print(res,  cnt)
                    cnt -= 1
                    tail.append(r[index % len(r)])
                    res = tail[-1]
                    index += 1
        return res

N, K = getNM()
N -= 1
A = [i - 1 for i in getList()]
roop = Roop(A)
print(roop.move(0, K) + 1)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n, k = map(int, input().split())
A = list(map(int, input().split()))
C = Counter([])
X, Y = [], []
m = 0
for _ in range(n):
    a = A[m]-1
    C[m] += 1
    X.append(m)
    if C[a] != 0:
        Y = X[X.index(a):]
        X = X[:X.index(a)]
        break
    m = a
# print(X,Y)
if k <= len(X):
    print(X[k-1]+1)
else:
    k -= len(X)
    print(Y[k % len(Y)]+1)

Submission status: Wrong Answer

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

    visited = set([])
    next_pos = 1
    while not next_pos in visited:
        visited.add(next_pos)
        next_pos = A[next_pos]
    loop_begin = next_pos

    visited = set([])
    while not next_pos in visited:
        visited.add(next_pos)
        next_pos = A[next_pos]
    loop_len = len(visited)

    teleport_cnt = K
    next_pos = 1
    while next_pos != loop_begin:
        next_pos = A[next_pos]
        teleport_cnt -= 1

    teleport_cnt %= loop_len
    for i in range(teleport_cnt):
        next_pos = A[next_pos]
    print(next_pos)

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
num=[0]*n
num[0]=1
arrived=[0]
be=a[0]-1

while (1):
  num[be]+=1
  if num[be]==2:
    b=be
    break
    
  else:
    arrived.append(be)
    be=a[be]-1
  
    
ls1=[]
ls2=[]

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

else:
  c=arrived.index(b)
  
  ls1=arrived[:c]
  ls2=arrived[c:]
  
  k-=len(ls1)
  
  print(ls2[k%len(ls2)]+1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
for i in range(n):
    a[i]-=1
#print(a)
    
b=0
c=[-1]*n
c[0]=0
for i in range(n):
#   print(b,a[b])
   b=a[b]
   if c[b]>=0:
#       print("i:",i)
       break
   else:
       c[b]=i+1

ki=c[b]+1
kc=(i+1)-c[b]
k2=(k-ki)%kc+ki
#print("ki:",ki,"kc:",kc,"k2:",k2)
b=0
for i in range(k2):
    b=a[b]
print(b+1)

Submission status: Wrong Answer

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

visited = [0] * (N + 1)
route = [0] * (N + 1)
next_ = 1
count = 0

for i in range(K + 1):
    if visited[next_] != 0:
        x = visited[next_]
        count = (K - (x - 1)) % (i + 1 - x) + x
        print(route[count])
        exit()
    visited[next_] = i + 1
    route[i + 1] = next_
    next_ = A[next_]

Submission status: Wrong Answer

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

visited = [0] * (N + 1)
route = [0] * (N + 1)
next_ = 1
count = 0

for i in range(N + 1):
    if visited[next_] != 0:
        x = visited[next_]
        count = (K - (x - 1)) % (i + 1 - x) + x
        print(route[count])
        exit()
    visited[next_] = i + 1
    route[i + 1] = next_
    next_ = A[next_]

print(route[K + 1])

Submission status: Wrong Answer

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

visited = [0] * (N + 1)
route = [0] * (N + 1)
next_ = 1
count = 0

for i in range(N + 1):
    if visited[next_] != 0:
        x = visited[next_]
        count = (K - (x - 1)) % (i + 1 - x)
        loop = route[x:i + 1]
        print(loop[count])
        exit()
    visited[next_] = i + 1
    route[i + 1] = next_
    next_ = A[next_]

print(route[K + 1])

Submission status: Wrong Answer

Solution code:
from collections import Counter
N, K = map(int,input().split())
A = list(map(int,input().split()))
i = k = 0
B = [1]
for i in range(10 ** 6):
    B.append(A[B[i]-1])

S = Counter(B)
idx = S.most_common(1)[0][0]
i = k = cnt = 0
k_s = None

while k < 10 ** 6:
    if B[k] == idx and cnt == 1:
        k_s = k
        break
    elif B[k] == idx:
        k_f = k
        cnt += 1
    k += 1
if k_s and k_s < 10 ** 6:
    k_max = (K - B.index(idx)) % (k_s - k_f) + B.index(idx)
else:
    k_max = K
i = k = 0
while k < k_max:
    i = A[i] - 1
    k += 1
print(i + 1)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N, K = map(int,input().split())
A = list(map(int,input().split()))
i = k = 0
B = [1]
for i in range(N):
    B.append(A[B[i]-1])

S = Counter(B)
idx = S.most_common(1)[0][0]
i = k = cnt = 0
k_s = None

while k < 10 ** 6:
    if B[k] == idx and cnt == 1:
        k_s = k
        break
    elif B[k] == idx:
        k_f = k
        cnt += 1
    k += 1
if k_s and k_s < 10 ** 6:
    k_max = (K - B.index(idx)) % (k_s - k_f) + B.index(idx)
else:
    k_max = K
i = k = 0
while k < k_max:
    i = A[i] - 1
    k += 1
print(i + 1)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N, K = map(int,input().split())
A = list(map(int,input().split()))
i = k = 0
B = [1]
for i in range(N):
    B.append(A[B[i]-1])

S = Counter(B)
idx = S.most_common(1)[0][0]
i = k = cnt = 0
k_s = None

while k <= N:
    if B[k] == idx and cnt == 1:
        k_s = k
        break
    elif B[k] == idx:
        k_f = k
        cnt += 1
    k += 1
if k_s and k_s <= N:
    k_max = (K - B.index(idx)) % (k_s - k_f) + B.index(idx)
else:
    k_max = K
i = k = 0
while k < k_max:
    i = A[i] - 1
    k += 1
print(i + 1)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N, K = map(int,input().split())
A = list(map(int,input().split()))
i = k = 0
B = [1]
for i in range(N):
    B.append(A[B[i]-1])

S = Counter(B)
idx = S.most_common(1)[0][0]
i = k = cnt = 0
k_s = None
while k < N:
    if B[k] == idx and cnt == 1:
        k_s = k
        break
    elif B[k] == idx:
        k_f = k
        cnt += 1
    k += 1
if k_s and k_s < N:
    k_max = (K - B.index(idx)) % (k_s - k_f) + B.index(idx)
else:
    k_max = K
i = k = 0
while k < k_max:
    i = A[i] - 1
    k += 1
print(i + 1)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N, K = map(int,input().split())
A = list(map(int,input().split()))
i = k = 0
B = [1]
while k <= 10 ** 7:
    i = A[i] - 1
    B.append(i + 1)
    k += 1
S = Counter(B)
idx = S.most_common(1)[0][0]
i = k = cnt = 0
k_s = None
while k < 10 ** 7:
    if B[k] == idx and cnt == 1:
        k_s = k
        break
    elif B[k] == idx:
        k_f = k
        cnt += 1
    k += 1
if k_s:
    k_max = (K - B.index(idx)) % (k_s - k_f) + B.index(idx)
else:
    k_max = K
i = k = 0
while k < k_max:
    i = A[i] - 1
    k += 1
print(i + 1)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N, K = map(int,input().split())
A = list(map(int,input().split()))
i = k = 0
B = [1]
while k <= 10 ** 6:
    i = A[i] - 1
    B.append(i + 1)
    k += 1
S = Counter(B)
idx = S.most_common(1)[0][0]
i = k = cnt = 0
k_s = None
while k < 10 ** 6:
    if B[k] == idx and cnt == 1:
        k_s = k
        break
    elif B[k] == idx:
        k_f = k
        cnt += 1
    k += 1
if k_s:
    k_max = (K - B.index(idx)) % (k_s - k_f) + B.index(idx)
else:
    k_max = K
i = k = 0
while k < k_max:
    i = A[i] - 1
    k += 1
print(i + 1)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N, K = map(int,input().split())
A = list(map(int,input().split()))
i = k = 0
B = [1]
while k <= 10 ** 6:
    i = A[i] - 1
    B.append(i + 1)
    k += 1
S = Counter(B)
idx = S.most_common(1)[0][0]
cnts = S.most_common(1)[0][1]
i = k = cnt = 0
k_s = None
while k < 10 ** 6:
    if B[k] == idx and cnt == 1:
        k_s = k
        break
    elif B[k] == idx:
        k_f = k
        cnt += 1
    k += 1
if k_s:
    k_max = K - (k_s - k_f) * (cnts - 1)
else:
    k_max = K
i = k = 0
while k < k_max:
    i = A[i] - 1
    k += 1
print(i + 1)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N, K = map(int,input().split())
A = list(map(int,input().split()))
i = k = 0
B = [1]
while k <= 10 ** 6:
    i = A[i] - 1
    B.append(i + 1)
    k += 1
S = Counter(B)
idx = S.most_common(1)[0][0]
i = k = cnt = 0
k_s = None
while k < 10 ** 6:
    if B[k] == idx and cnt == 1:
        k_s = k
        break
    elif B[k] == idx:
        k_f = k
        cnt += 1
    k += 1
if k_s:
    k_max = K % (k_s - k_f) + (k_s - k_f) * 100
else:
    k_max = K
i = k = 0
while k < k_max:
    i = A[i] - 1
    k += 1
print(i + 1)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N, K = map(int,input().split())
A = list(map(int,input().split()))
i = k = 0
B = [1]
while k <= 10 ** 6:
    i = A[i] - 1
    B.append(i + 1)
    k += 1
S = Counter(B)
idx = S.most_common(1)[0][0]
i = k = cnt = 0
k_s = None
while k < 10 ** 6:
    if B[k] == idx and cnt == 1:
        k_s = k
        break
    elif B[k] == idx:
        k_f = k
        cnt += 1
    k += 1
if k_s:
    k_max = K % (k_s - k_f) + k_s - k_f
else:
    k_max = K
i = k = 0
while k < k_max:
    i = A[i] - 1
    k += 1
print(i + 1)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N, K = map(int,input().split())
A = list(map(int,input().split()))
i = k = 0
B = [1]
while k <= 10 ** 6:
    i = A[i] - 1
    B.append(i + 1)
    k += 1
S = Counter(B)
idx = S.most_common(1)[0][0]
i = k = cnt = 0
k_s = None
while k < 10 ** 6:
    if B[k] == idx and cnt == 1:
        k_s = k
        break
    elif B[k] == idx:
        k_f = k
        cnt += 1
    k += 1
if k_s:
    k_max = K % (k_s - k_f)
else:
    k_max = K
i = k = 0
while k < k_max:
    i = A[i] - 1
    k += 1
print(i + 1)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N, K = map(int,input().split())
A = list(map(int,input().split()))
i = k = 0
B = [1]
while k < 10 ** 6:
    i = A[i] - 1
    B.append(i + 1)
    k += 1
S = Counter(B)
idx = S.most_common(1)[0][0]
i = k = k_pr =  0
cnt = 0
while k < 10 ** 6:
    if B[k] == idx and cnt == 1:
        break
    elif B[k] == idx:
        cnt += 1
        k_pr = k
    k += 1
if k_pr != 0:
    k_max = K % (k - k_pr)
else:
    k_max = K
i = k = 0
while k < k_max:
    i = A[i] - 1
    k += 1
print(i + 1)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N, K = map(int,input().split())
A = list(map(int,input().split()))
i = k = 0
B = [1]
while k < 10 ** 6:
    i = A[i] - 1
    B.append(i + 1)
    k += 1
S = Counter(B)
idx = S.most_common(1)[0][0]
i = k = 0
cnt = 0
while k < 10 ** 6:
    if B[k] == idx and cnt == 1:
        break
    elif B[k] == idx:
        cnt += 1
        k_pr = k
    k += 1
k_max = K % (k - k_pr)
i = k = 0
while k < k_max:
    i = A[i] - 1
    k += 1
print(i + 1)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
ls = list(map(int,input().split()))
fi = [-1]*(n+1)
fi[1] = 0
a = 0
for i in range(k):
    if fi[ls[a]] == -1:
        fi[ls[a]] = i + 1
        a = ls[a] - 1
    else:
        ind = fi[ls[a]]
        t = (k - ind) % (max(fi) + 1 - ind)
        t += ind
        print(fi.index(t))
        break
else:
    print(ls[a])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
ls = list(map(int,input().split()))
fi = [-1]*(n+1)
fi[1] = 0
a = 0
for i in range(k):
    if fi[ls[a]] == -1:
        fi[ls[a]] = i + 1
        a = ls[a] - 1
    else:
        ind = fi[ls[a]]
        t = (k - ind) % (max(fi) + 1 - ind)
        t += ind
        print(fi.index(t))
        break
else:
    print(a)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
ls = list(map(int,input().split()))
di = [1]
a = 0
for i in range(k):
    if ls[a] not in di:
        di.append(ls[a])
        a = ls[a] - 1
    else:
        ind = di.index(ls[a])
        t = (k - ind) % (len(di) - ind)
        t += ind
        print(di[t])
        break
else:
    print(a)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
ls = list(map(int,input().split()))
di = [1] 
i = 0
cnt = 0
while True:
    cnt += 1
    if ls[i] not in di:
        di.append(ls[i])
        i = ls[i]-1
    else:
        a = di.index(ls[i])
        t = (k-i) % (len(di)-a) + a
        print(ls[t])
        break
    if cnt == k:
        print(i)
        break

Submission status: Wrong Answer

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

i = 1
l = [0] * n

for j in range(1, k + 1):
    if l[i - 1]: break
    l[i - 1] = j
    i = a[i - 1]

print(a[l.index((k-l[i]+1)%(j-l[i]) + l[i])])

Submission status: Wrong Answer

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

check = [0] * N
time = [-1] * N
check[0] = 1
time[0] = 0
now = 1
t = 1
ex = 0
roop = 0
for i in range(N + 1):
  now = A[now - 1]
  if check[now - 1] == 0:
    check[now - 1] = 1
    time[now - 1] = t
  else:
    ex = time[now - 1]
    roop = t - time[now - 1]
    break
  t += 1
  
if K < t:
  ans = time.index(K) + 1
  print(ans)
else:
  K -= (t - 1)
  K %= roop
  #print(K)
  ans = time.index(ex + K - 1) + 1
  print(ans)
#print(check, time, roop, t, ex)

Submission status: Wrong Answer

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

roop = [-1] * (N + 1)
time = 0
l = 0
T = True
while T:
  #next_town 
  l = A[l] - 1
  if l in roop:
    T = False
    index = roop.index(l)
    roop[time] = l
  else:
    roop[time] = l
    time += 1
#print(roop, time, index)
  
K -= index  
K %= (time - index)
print(roop[index + K - 1] + 1)

Submission status: Wrong Answer

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

C=[0]*n
C[0]=1
c=0
loopc=0
i=0
while i<n :
    newi = A[i]-1
    C[newi]+=1
    if C[newi]>=3 :
        rs=newi
        break
    elif C[newi]==2 :
        loopc+=1
    i=newi
    c+=1

if (k-c)<0 :
    print(i+1)
    exit()

mod = (k-c)%(loopc)
# print( k, c, loopc, i, mod)

# i=rs
while mod>0 :
    newi = A[i]-1
    i=newi
    mod-=1

print( i+1 )

Submission status: Wrong Answer

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

C=[0]*n
C[0]=1
c=0
loopc=0
i=0
while i<n :
    newi = A[i]-1
    C[newi]+=1
    if C[newi]>=3 :
        rs=newi
        break
    elif C[newi]==2 :
        loopc+=1
    i=newi
    c+=1

mod = (k-c)%(loopc)
# print( k, c, loopc, i, mod)

# i=rs
while mod>0 :
    newi = A[i]-1
    i=newi
    mod-=1

print( i+1 )

Submission status: Wrong Answer

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

C=[0]*n
C[0]=1
c=0
loopc=0
i=0
while i<n :
    newi = A[i]-1
    C[newi]+=1
    if C[newi]>=3 :
        rs=newi
        break
    elif C[newi]==2 :
        loopc+=1
    i=newi
    c+=1

mod = (k-(c+1)-loopc)%(loopc)
# print( C, c, loopc, mod)

i=rs
while mod>0 :
    newi = A[i]-1
    i=newi
    mod-=1

print( i+1 )

Submission status: Wrong Answer

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

C=[0]*n
C[0]=1
c=0
loopc=0
i=0
while i<n :
    newi = A[i]-1
    C[newi]+=1
    if C[newi]>=3 :
        rs=newi
        break
    elif C[newi]==2 :
        loopc+=1
    i=newi
    c+=1

mod = (k-(c+1)%loopc)%(loopc)
# print( C, c, loopc, mod)

i=rs
while mod>0 :
    newi = A[i]-1
    i=newi
    mod-=1

print( i+1 )

Submission status: Wrong Answer

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

C=[0]*n
C[0]=1
c=0
loopc=0
i=0
while i<n :
    newi = A[i]-1
    C[newi]+=1
    if C[newi]>=3 :
        rs=newi
        break
    elif C[newi]==2 :
        loopc+=1
    i=newi
    c+=1

mod = (k-(c+1)%loopc)%(loopc)
print( C, c, loopc, mod)

i=rs
while mod>0 :
    newi = A[i]-1
    i=newi
    mod-=1

print( i+1 )

Submission status: Wrong Answer

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

C=[0]*n
C[0]=1
c=0
loopc=0
i=0
while i<n :
    newi = A[i]-1
    C[newi]+=1
    if C[newi]>=3 :
        rs=newi
        break
    elif C[newi]==2 :
        loopc+=1
    i=newi
    c+=1

mod = (k-(c-loopc))%(loopc+1)
# print( C, c, loopc, mod)

i=rs
while mod>0 :
    newi = A[i]-1
    i=newi
    mod-=1

print( i+1 )

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
ls = []
ls.append(1)
x = 1
cnt = 0
while 1:
    next = A[x-1]
    if next in ls:
        l1 = ls.index(next)
        l2 = len(ls)
        ls.append(next)
        break
    else:
        ls.append(next)
        x = next
#print(ls,l1,l2)
location = (K-l1)%(l2-l1)
#print(ls[l1],location)

print(ls[l1+location])

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from collections import defaultdict
def main():
    n, k, *a = map(int, read().split())

    cur_town = 0
    transits = set()
    tran_time = defaultdict(int)
    cnt = 0
    while True:
        if k == 0:
            print(cur_town)
            sys.exit()
        if cur_town in transits:
            break
        else:
            transits.add(cur_town)
            tran_time[cur_town] = cnt
            cur_town = a[cur_town] - 1
            cnt += 1
            k -= 1
    k2 = k % (cnt - tran_time[cur_town])
    while k2 > 0:
        k2 -= 1
        cur_town = a[cur_town] - 1
    print(cur_town + 1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from collections import defaultdict
def main():
    n, k, *a = map(int, read().split())

    cur_town = 0
    transits = set()
    tran_time = defaultdict(int)
    cnt = 0
    while True:
        if cur_town in transits:
            break
        else:
            transits.add(cur_town)
            tran_time[cur_town] = cnt
            cur_town = a[cur_town] - 1
            cnt += 1
            k -= 1
    k2 = k % (cnt - tran_time[cur_town])
    while k2 > 0:
        k2 -= 1
        cur_town = a[cur_town] - 1
    print(cur_town + 1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

visit_town_li = []

now_town = 1
while not (li[now_town - 1] in visit_town_li):
    visit_town_li.append(now_town)
    now_town = li[now_town - 1]
visit_town_li.append(now_town)

loop_start_index = visit_town_li.index(li[now_town - 1])

offset = (K - loop_start_index) % (len(visit_town_li) - loop_start_index)

# print(loop_start_index)
# print(offset)
# print(len(visit_town_li))
print(visit_town_li[loop_start_index + offset])

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    N, K = map(int, input().split())
    A = [0] + list(map(int, input().split())) 

    prev = 1
    memo = []
    loop_start_index = 0
    while True:
        nxt = A[prev]
        if nxt not in memo:
            memo.append(nxt)
        else:
            end = nxt
            break
        prev = nxt
        loop_start_index += 1 
    start = 0
    for i, m in enumerate(memo):
        if m==end:
            start = i 
    d = loop_start_index-start
    if K<=start:
        print(memo[K])
    else:
        K -= start
        print(memo[K%d])

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    N, K = map(int, input().split())
    A = [0] + list(map(int, input().split())) 

    prev = 1
    memo = []
    loop_start_index = 0
    while True:
        nxt = A[prev]
        if nxt not in memo:
            memo.append(nxt)
        else:
            end = nxt
            break
        prev = nxt
        loop_start_index += 1 
    start = 0
    for i, m in enumerate(memo):
        if m==end:
            start = i 
    d = loop_start_index-start
    if K<start:
        print(memo[K])
    else:
        K -= 1
        print(memo[K%d])