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())
l=list(map(int,input().split()))
lcount=[0]*(n+1)
lnum=[0]*(n+1)
count=1
place=1
while lcount[place]==0:    
    lnum[place]=count
    lcount[place]=1
    place=l[place-1]
    count+=1 

dif=count-lnum[place]
k=(k+1-lnum[place])%dif+lnum[place]
print(lnum.index(k))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
lcount=[0]*(n+1)
lnum=[0]*(n+1)
count=0
place=1
while lcount[place]==0:
    lnum[place]=count
    lcount[place]=1
    place=l[place-1]
    count+=1
    
dif=count-lnum[place]
k=k%dif+lnum[place]
print(lnum.index(k))

Submission status: Wrong Answer

Solution code:
from numba import njit
import numpy as np


def main(N, K, A):
    d = np.zeros(N,np.int32)
    d=d-1
    p = 0
    for i in range(K):
        if d[p-1] == 0:
            d[p-1] = i
            p = A[p-1]
        else:
            p = A[p-1]
            loop = i-d[p-1]
            nokori = (K-d[p-1]) % loop
            for _ in range(nokori):
                p = A[p-1]
            return p
    return p


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

Submission status: Wrong Answer

Solution code:
N, K =map(int, input().split())
A = [int(a) - 1 for a in input().split()]
idx = 0
if K<=N:
    for i in range(K):
        idx = A[idx]
    print(A[idx] + 1)
    exit() #全ての実行をここで終了

t = [-1] * N
t[0] = 0
for i in range(1, N):
    idx = A[idx]
    last = i
    if t[idx] != -1:
        break
    t[idx] = i

d = i - t[idx]
K = K - i
amari = K%d

for i in range(amari):
    idx = A[idx]
print(idx + 1)

Submission status: Wrong Answer

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

def next_town(graph, now):
    return graph[now-1]

def find_roop(graph):
    n=len(graph)
    visited=[1]
    p = 1
    for _ in range(n):
        p = next_town(graph,p)
        if p not in visited:
            visited.append(p)
        else:
            count = 1
            q = next_town(graph,p)
            while q != p:
                q = next_town(graph,q)
                count+=1
            to_p=0
            q=1
            while q!=p:
                q = next_town(graph,q)
                to_p+=1
            return to_p, p, count
            

st, roop_st, roop = find_roop(A)

ans = roop_st
for _ in range((k-st)%roop):
    ans = next_town(A,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

    A=list(map(lambda x:int(x)-1,input().split()))

    loop=[]

    slack_que=deque([])
    flag=0
    t=0
    start=0
    counter=0
    while flag==0:
        slack_que.append(t)
        t=A[t]
        A[t]=-1
        if t==-1:
            start=counter
            flag=1
        counter+=1
    slack=list(slack_que)
    slack_num=start
    loop_num=len(slack)-slack_num

    # print(slack)
    # print(start)
    # print(slack_num)
    # print(loop_num)
    #
    loop=slack[start:]
    # print(loop)
    if (K-slack_num)<0:
        res=slack[K]+1
    else:
        tele_num=((K-slack_num)%loop_num)
        town=loop[tele_num]
        res=town+1

    print(res)

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

N, K = LI()
A = LI()

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

next_city = 0
visited = []
for n in range(N):
    next_city = A[next_city]
    # print(n)
    if next_city in set(visited):
        roop_s = list.index(visited, next_city)
        roop_cnt = n - roop_s
        break
    visited.append(next_city)
ans = -1

if K - roop_s >= 0:
    amari = (K - roop_s) % roop_cnt
    next_city = 0
    for k in range(amari + roop_s):
        next_city = A[next_city]
    ans = next_city

else:
    next_city = 0
    for k in range(K):
        next_city = A[next_city]
    ans = next_city
    
print(ans + 1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = input().split()
A = [int(a) for a in A]
S = [1]
a = 1
number = 0
numbers=[-1 for _ in range(N)]
for _ in range(N + 1):
    b = A[a - 1]
    if numbers[b-1] != -1:
      c=numbers[b-1]
      break
    numbers[b-1]=number
    number += 1
    S.append(b)
    a = b
T = S[c:number]
if K <= number:
  print(S[K - 1])
else:
  z = K - c
  y = z % (number - c)
  print(T[y])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = input().split()
A = [int(a) for a in A]
S = []
a = 1
number = 0
numbers=[-1 for _ in range(N)]
for _ in range(N + 1):
    b = A[a - 1]
    if numbers[b-1] != -1:
      c=numbers[b-1]
      break
    numbers[b-1]=number
    number += 1
    S.append(b)
    a = b
T = S[c:number]
if K <= number:
  S.insert(0,1)
  print(S[K - 1])
else:
  z = K - c - 1
  y = z % (number - c)
  print(T[y])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = input().split()
A = [int(a) for a in A]
S = []
a = 1
number = 0
numbers=[-1 for _ in range(N)]
for _ in range(N + 1):
    b = A[a - 1]
    if numbers[b-1] != -1:
      c=numbers[b-1]
      break
    numbers[b-1]=number
    number += 1
    S.append(b)
    a = b
T = S[c:number]
if K <= N:
  S.insert(0,1)
  print(S[K - 1])
else:
  z = K - c - 1
  y = z % (number - c)
  print(T[y])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)

def teleport(city, end, teleportmap, count=1):

    next_city = teleportmap[city]

    if count == end:
        return next_city

    else:
        return teleport(next_city, end, teleportmap, count+1)

n, k = map(int,input().split())
a = list(map(int, input().split()))
teleportmap = dict(zip(range(1, len(a)+1), a))

if k <= n:
    print(teleport(1, n, teleportmap))

else:
    countmap = [0]*n
    countmap[0] = 1

    next_city =1
    for _ in range(1, n+1):
        next_city = a[next_city-1]
        countmap[next_city -1] += 1
        if countmap[next_city -1] == 2:
            start = next_city
            break

    roop = {}
    for j in range(1, n+1):
        roop[start] = teleportmap[start]
        start = roop[start]
    
    num_r = len(roop)

    num_until_r= 0
    for m in teleportmap.keys():
        if m not in roop and countmap[m-1] != 0:
            num_until_r += 1
    
    k_r = k - num_until_r
    num_end = k_r%num_r

    if num_end == 0:
        print(next_city)
        
    else:
        print(teleport(next_city, num_end, roop))

Submission status: Wrong Answer

Solution code:

def parse():
  N, K = map(int, input().split(" "))
  A = []
  for a in input().split(" "):
    A.append(int(a) - 1)
  return N, K, A

def fast_pow(x, n):
    """
    O(log n)
    """
    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2

    return K * x

def fast_mod(x, N):
  # return x % N
  if N % 2 == 0:
    return x & (N - 1)
  else:
    return x % N

def main():
  N, K, A = parse()
  # print(N, K, A)

  # 移動
  town = 0
  route = []
  loop_start_index = None
  i = 0
  for i in range(K):
    if town in route:
      # ループ検出
      loop_start_index = route.index(town)
      break

    route.append(town)

    # 次の町に移動
    town = A[town]
  
  if loop_start_index == None:
    print(town)
  else:
    loop_route = route[loop_start_index:]
    print(loop_route[(K - i) % len(loop_route)] + 1)
    # print(loop_route[])
  # print(town)
  exit(0)

  # 経路の計算
  route = []
  next_town = A[0]
  town = 0
  while not (next_town in route):  # 既出の町に行く場合
    route.append(town)
    next_town = A[town]
    town = next_town
  once_route = route[:route.index(next_town)]
  loop_route = route[route.index(next_town):]
  # print("once_route:", once_route)
  # print("loop_route:", loop_route)
  
  # Kが経路の長さより小さい場合
  if K > len(route):
    print(route[-1])
    exit(0)

  # print([town + 1 for town in route])
  # print("K %% len(loop_route):", K % len(loop_route))
  # print(route[K % len(loop_route) + len(once_route)] + 1)
  if K >= len(route) and len(once_route) > 0:
    # print("loop")
    loop_K = K - len(route)
    print(loop_route[fast_mod(loop_K, len(loop_route))] + 1)
    # print(loop_route[loop_K % len(loop_route)] + 1)
  else:
    # print("normal")
    # print(route[fast_mod(K, len(loop_route))] + 1)
    # print("K:", K)
    # print("K %% len(route):", K % len(route))
    print(route[fast_mod(K, len(route))] + 1)
    # print(route[K % len(route)] + 1)

if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:

def parse():
  N, K = map(int, input().split(" "))
  A = []
  for a in input().split(" "):
    A.append(int(a) - 1)
  return N, K, A

def fast_pow(x, n):
    """
    O(log n)
    """
    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2

    return K * x

def fast_mod(x, N):
  # return x % N
  if N % 2 == 0:
    return x & (N - 1)
  else:
    return x % N

def main():
  N, K, A = parse()
  # print(N, K, A)

  # 経路の計算
  route = []
  next_town = A[0]
  town = 0
  while not (next_town in route):  # 既出の町に行く場合
    route.append(town)
    next_town = A[town]
    town = next_town
  once_route = route[:route.index(next_town)]
  loop_route = route[route.index(next_town):]
  # print("once_route:", once_route)
  # print("loop_route:", loop_route)
  
  # print([town + 1 for town in route])
  # print("K %% len(loop_route):", K % len(loop_route))
  # print(route[K % len(loop_route) + len(once_route)] + 1)
  if K >= len(route) and len(once_route) > 0:
    # print("loop")
    loop_K = K - len(route)
    print(loop_route[fast_mod(loop_K, len(loop_route))] + 1)
    # print(loop_route[loop_K % len(loop_route)] + 1)
  else:
    # print("normal")
    # print(route[fast_mod(K, len(loop_route))] + 1)
    # print("K:", K)
    # print("K %% len(route):", K % len(route))
    print(route[fast_mod(K, len(route))] + 1)
    # print(route[K % len(route)] + 1)

if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:

def parse():
  N, K = map(int, input().split(" "))
  A = []
  for a in input().split(" "):
    A.append(int(a) - 1)
  return N, K, A

def fast_pow(x, n):
    """
    O(log n)
    """
    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2

    return K * x

def fast_mod(x, N):
  if N % 2 == 0:
    return x & (N - 1)
  else:
    return x % N
  return x % N

def main():
  N, K, A = parse()
  # print(N, K, A)

  # 経路の計算
  route = []
  next_town = A[0]
  town = 0
  while not (next_town in route):  # 既出の町に行く場合
    route.append(town)
    next_town = A[town]
    town = next_town
  once_route = route[:route.index(next_town)]
  loop_route = route[route.index(next_town):]
  # print("once_route:", once_route)
  # print("loop_route:", loop_route)
  
  # print([town + 1 for town in route])
  # print("K %% len(loop_route):", K % len(loop_route))
  # print(route[K % len(loop_route) + len(once_route)] + 1)
  if K > len(route) and len(once_route) > 0:
    # print("loop")
    loop_K = K - len(route)
    print(loop_route[fast_mod(loop_K, len(loop_route))] + 1)
    # print(loop_route[loop_K % len(loop_route)] + 1)
  else:
    # print("normal")
    # print(route[fast_mod(K, len(loop_route))] + 1)
    # print("K:", K)
    # print("K %% len(route):", K % len(route))
    print(route[fast_mod(K, len(route))] + 1)
    # print(route[K % len(route)] + 1)

if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:

from collections import deque
N, K = map(int, input().rstrip().split())
route = [0] + list(map(int, input().rstrip().split()))

# 町1から町nまでの距離
dis = [0] * (N+1)

# 訪問済み
checked = [0] * (N+1)

queue = deque([1])
hop_count = 0
offset = 0

current = 1
while queue:
    i = queue.popleft()
    hop_count += 1

    if hop_count == K:
        print(i)
        break

    # ループ判明
    if checked[i] == 1:
        roop_node_count = abs(hop_count - dis[i])
        offset = (K - hop_count) % roop_node_count

        for _ in range(offset):
            i = route[i]
        print(i)
        break

    A_i = route[i]
    queue.append(A_i)
    hop_count += 1
    dis[A_i] += hop_count
    checked[i] = 1

Submission status: Wrong Answer

Solution code:

from collections import deque
N, K = map(int, input().rstrip().split())
route = [0] + list(map(int, input().rstrip().split()))

# 町1から町nまでの距離
dis = [0] * (N+1)

# 訪問済み
checked = [0] * (N+1)

queue = deque([1])
hop_count = 0
offset = 0
while queue:
    i = queue.popleft()
    hop_count += 1

    if hop_count == K:
        print(i)
        break

    # ループ判明
    if checked[i] == 1:
        roop_node_count = hop_count - dis[i]
        offset = (K - hop_count) % roop_node_count

        for _ in range(offset):
            i = route[i]
        print(i)
        break

    A_i = route[i]
    queue.append(A_i)
    hop_count += 1
    dis[A_i] += hop_count
    checked[i] = 1

Submission status: Wrong Answer

Solution code:

def parse():
  N, K = map(int, input().split(" "))
  A = []
  for a in input().split(" "):
    A.append(int(a) - 1)
  return N, K, A

def fast_mod(x, N):
  return (x * N) >> 32

def main():
  N, K, A = parse()
  # print(N, K, A)

  # 経路の計算
  route = []
  next_town = A[0]
  town = 0
  while not (next_town in route):  # 既出の町に行く場合
    route.append(town)
    next_town = A[town]
    town = next_town
  once_route = route[:route.index(next_town)]
  loop_route = route[route.index(next_town):]
  # print("once_route:", once_route)
  # print("loop_route:", loop_route)
  
  # print([town + 1 for town in route])
  # print("K %% len(loop_route):", K % len(loop_route))
  # print(route[K % len(loop_route) + len(once_route)] + 1)
  if K > len(route) and len(once_route) > 0:
    # print("loop")
    loop_K = K - len(route)
    print(route[(loop_K - 1) % len(loop_route)] + 1)
  else:
    # print("normal")
    # print(route[fast_mod(K, len(loop_route))] + 1)
    print(route[K % len(route)] + 1)

if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:

def parse():
  N, K = map(int, input().split(" "))
  A = []
  for a in input().split(" "):
    A.append(int(a) - 1)
  return N, K, A

def main():
  N, K, A = parse()
  # print(N, K, A)

  # 経路の計算
  route = []
  next_town = A[0]
  town = 0
  while not (next_town in route):  # 既出の町に行く場合
    route.append(town)
    next_town = A[town]
    town = next_town
  once_route = route[:route.index(next_town)]
  loop_route = route[route.index(next_town):]
  # print("once_route:", once_route)
  # print("loop_route:", loop_route)
  
  # print([town + 1 for town in route])
  # print("K %% len(loop_route):", K % len(loop_route))
  # print(route[K % len(loop_route) + len(once_route)] + 1)
  print(route[K % len(loop_route)] + 1)

if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
import math
import copy

def Reach(A, T, K):
    for i in range(K):
        T = A[T]-1
    return T + 1


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

    digit_len = len(str(K))
    A_digit = [[0 for j in range(N+1)] for i in range(digit_len+1)]
    
    A_digit[0] = copy.copy(A)
    
    for d in range(digit_len):
        A_digit[d+1] = copy.copy(A_digit[d])
        for i in range(N):
            A_digit[d+1][i] = Reach(A_digit[d], i, 10)
        #print(A_digit)

    T = 0
    for d in range(digit_len):
        #print(int(str(K)[d]))
        for i in range(int(str(K)[d])):
            T = A_digit[d][T]-1

    print(T+1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

N,K = map(int,input().split())
A = list(map(int,input().split()))
route_history = defaultdict(lambda : -1)
position = 1
b_pos = -1
for i in range(1,N+1):
    if(route_history[position] != -1):
        b_pos = route_history[position]
        break
    route_history[position] = i-1
    position = A[position-1]

new_route_history = defaultdict(lambda : -1)
count = 0
for key,value in dict(route_history).items():
    if(key >= b_pos):
        new_route_history[count] = key
        count+=1

if(b_pos != -1):
    print(new_route_history[(K-b_pos)%len(new_route_history)])
else:
    print(position)

Submission status: Wrong Answer

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

ex = [1]
lo = 1
while A[lo - 1] not in ex:
    lo = A[lo - 1]
    ex.append(lo)
a = len(ex)
b = ex.index(A[ex[-1] - 1])
loop = a - b
c = (k - b) % loop
print(A[b + c])

Submission status: Wrong Answer

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

ex = [1]
lo = 1
for i in range(k):
    lo = A[lo - 1]
    if lo in ex:
        a = ex.index(lo)
        x = len(ex) - a
        b = (k - len(ex)) % x
        print(ex[a + b])
        break
    ex.append(lo)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def main():
    n,k = map(int,input().split())
    l = list(map(int,input().split()))
    position = 1

    town = ''
    stayed = [1]+[0]*n
    stayed_c = [0]*n
    for i in range(n):
        position = l[position-1]
        if not position in stayed:
            stayed[position-1] = position
            stayed_c[position-1] = i+1
        else:
            c = i+1 - stayed_c[position-1]
            g = stayed_c[position-1]
            town = position
            break
    m = (k -g) % c 
    position = town
    for i in range(m):
        position = l[position-1]
    print(position) 
    # print(m,g,k,c)
    # print(stayed_c)

main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def main():
    n,k = map(int,input().split())
    l = list(map(int,input().split()))
    position = 1

    town = ''
    stayed = [1]+[0]*n
    stayed_c = [0]*n
    for i in range(n):
        position = l[position-1]
        if not position in stayed:
            stayed[position-1] = position
            stayed_c[position-1] = i+1
        else:
            c = i+1 - stayed_c[position-1]
            g = stayed_c[position-1]
            town = position
            break
    m = (k -g) % c 
    position = town
    for i in range(m):
        position = l[position-1]
    print(position)

Submission status: Wrong Answer

Solution code:

n,k = map(int,input().split())
l = list(map(int,input().split()))
position = 1

town = ''
stayed = [1]
stayed_c = [0]*n
for i in range(n):
    position = l[position-1]
    if not position in stayed:
        stayed.append(position)
        stayed_c[position-1] = i+1
    else:
        c = i+1 - stayed_c[position-1]
        g = stayed_c[position-1]
        town = position
        break
m = (k -g) % c 
position = town
for i in range(m):
    position = l[position-1]
print(position)

Submission status: Wrong Answer

Solution code:


n,k = map(int,input().split())
l = list(map(int,input().split()))
position = 1
if k < n:
    for i in range(k):
        position = l[position-1]
    print(position)
    exit()

town = ''
stayed = [1]
stayed_c = [0]*n
for i in range(n):
    position = l[position-1]
    if not position in stayed:
        stayed.append(position)
        stayed_c[i] = i+1
    else:
        c = i+1 - stayed_c[position-1]
        g = stayed_c[position-1]
        town = position
        break
m = (k % c) + g
position = 1
for i in range(m):
    position = l[position-1]
print(position) 
# print(m,g,k)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
l = list(map(int,input().split()))
position = 1
town = ''
stayed = [1]
stayed_c = [0]*n
for i in range(n):
    position = l[position-1]
    if not position in stayed:
        stayed.append(position)
        stayed_c[i] = i+1
    else:
        c = i+1 - stayed_c[position-1]
        g = stayed_c[position-1]
        town = position
        break
m = (k % c) + g
position = 1
for i in range(m):
    position = l[position-1]
print(position)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
l = list(map(int,input().split()))
position = 1
town = ''
stayed = [1]
stayed_c = [0]*n
for i in range(n):
    position = l[position-1]
    if not position in stayed:
        stayed.append(position)
        stayed_c[i] = i+1
    else:
        c = i+1 - stayed_c[position-1]
        g = stayed_c[position-1]
        town = position
        break
m = abs((k % c) - g)
position = town
for i in range(m):
    position = l[position-1]
print(position) 
# print(m,g,k)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
l = list(map(int,input().split()))
position = 1
town = ''
stayed = [1]
stayed_c = [0]*n
for i in range(n):
    position = l[position-1]
    if not position in stayed:
        stayed.append(position)
        stayed_c[i] = i+1
    else:
        c = i+1 - stayed_c[position-1]
        k = stayed_c[position-1]
        town = position
        break
m = (k % c) - k
position = town
for i in range(m):
    position = l[position-1]
print(position)

Submission status: Wrong Answer

Solution code:


n,k = map(int,input().split())
l = list(map(int,input().split()))
position = 1
town = ''
stayed = [1]
stayed_c = [0]*n
for i in range(n):
    position = l[position-1]
    if not position in stayed:
        stayed.append(position)
        stayed_c[i] = i+1
    else:
        c = i+1 - stayed_c[position-1]
        town = position
        break
m = k % c
position = 1
for i in range(m):
    position = l[position-1]

print(position)

Submission status: Wrong Answer

Solution code:


n,k = map(int,input().split())
l = list(map(int,input().split()))
position = 1
town = ''
stayed = [1]
c = 0
for i in range(n):
    position = l[position-1]
    if not position in stayed:
        stayed.append(position)
    else:
        c = i+1
        town = position
        break
count = 0
if town:
    for i in range(n):
        position = l[position-1]
        if town == position:
            count = i+1
            break
    m = k % count 
    position = 1
    for i in range(m):
        position = l[position-1]

print(position)

Submission status: Wrong Answer

Solution code:


n,k = map(int,input().split())
l = list(map(int,input().split()))
position = 1
town = ''
stayed = [1]
for i in range(n):
    position = l[position-1]
    if not position in stayed:
        stayed.append(position)
    else:
        town = position
        break
count = 0
if town:
    for i in range(n):
        position = l[position-1]
        if town == position:
            count = i+1
            break
    m = k % n
    for i in range(m+1):
        position = l[position-1]

print(position)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split()) 
X = list(map(int, input().split()))
for i in range(k+1):
    if i == 0:
        aa = 1
    else:
        aa = X[bb-1]
    bb = X[aa-1]
print(bb)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10000000)
s = 0 
def main():
    n, k = map(int, readline().split())
    a = [int(i)-1 for i in readline().split()]
    done = [0 for _ in range(n)] 
    cycle = []
    def dfs(x):
        global s
        if done[x] == 1:
            s = x
            return
        nx = a[x]
        done[x] = 1
        cycle.append(x)
        dfs(nx)

    dfs(0)
    start_idx = cycle.index(s)
    ln = len(cycle)
    k -= start_idx
    if k < ln: 
        print(cycle[k-1]+1)
    else:
        idx = start_idx+(k%(ln-start_idx))
        print(cycle[idx]+1)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
## coding: UTF-8

#ダブリングを利用した実装

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

#2**59 < 10**18< 2**60より2**60まで格納する
l = [0] * (N*61)

for j in range(N):
    l[j] = A[j]


for i in range(1,61):
#for i in range(1, 4):
    for j in range(N):
        p = l[(i-1)*N+j] #一段上を参照(2**(i-1)回移動した時の移動先)
        q = l[(i-1)*N+(p-1)] #pから2**(i-1)回移動した時の移動先を参照　＝　2**i 回移動した時の移動先
        l[i*N+(j)] = q
    #print(l)
print(l)

bin_str = format(K, 'b')
print(bin_str)
s = bin_str[::-1]
print(s)
now = 1 #start地点
for i in range(len(s)):
    if(s[i] == '1'):
        now = l[(i)*N + (now-1)]
        print(now)

        '''
        next_index = i*N + l[i*N + (now-1)]-1
        nex = l[next_index]
        now = nex
        print(now)
        '''
print(now)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
l = [0]+list(map(int, input().split()))
#tp = {key: l[key-1] for key in range(1, n+1)}
path = []
to = 1
if n > k:
    for i in range(n):
        to = l[to]
    print(to, end="")
    exit()

for i in range(n):
    if to in path:
        path.append(to)
        break
    path.append(to)
    to = l[to]

lp_st = (path.index(path[-1]))
loop = len(path) - lp_st - 1

k -= lp_st
k = k % loop
to = path[lp_st]
for i in range(k):
    to = l[to]
#print(path[k+lp_st], end="")
print(to, end="")

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
l = list(map(int, input().split()))
tp = {key: l[key-1] for key in range(1, n+1)}

path = []
to = 1
for i in range(n):
    if to in path:
        path.append(to)
        break
    path.append(to)
    to = tp[to]

lp_st = (path.index(path[-1]))
loop = len(path) - lp_st - 1

k -= lp_st
k = k % loop
print(path[k+lp_st], end="")

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
l = list(map(int, input().split()))
tp = {key: l[key-1] for key in range(1, n+1)}
loop = 0
path = []
to = 1
for i in range(n):
    if to in path:
        path.append(to)
        break
    path.append(to)
    to = tp[to]

lp_st = (path.index(path[-1]))
loop = len(path) - lp_st - 1

k -= lp_st
k = k % loop
to = path[lp_st]
for i in range(k):
    to = tp[to]
print(to, end="")

Submission status: Wrong Answer

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

s=1
ans=0
v={}
for j in range(len(a)):
	if(s-1 in v):
		break
	else:
		v.update({s-1:ans})
		s=a[s-1]
		ans+=1
lens=ans-v[s-1]
k=(k-ans)%lens
for j in range(k):
	s=a[s-1]
print(s)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
*A, = map(int, input().split())
dist = [-1]*(N+1)

d = {i:[] for i in range(1, N+1)}
for i, j in enumerate(A, start=1):
    d[i].append(j)

dist[1] = 0
q = [1]
cycle = 0
ls = 0
while q:
    a = q.pop()
    for b in d[a]:
        if dist[b]==-1:
            dist[b] = dist[a] + 1
            q.append(b)
        elif dist[b]==0 or dist[b]:
            cycle = dist[a] + 1 - dist[b]
            ls = b
            break

b = (K-dist[ls])%cycle
for i, j in enumerate(dist):
    if j==dist[ls]+b:
        print(i)
        break

Submission status: Wrong Answer

Solution code:
import sys
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = []
cnt = 0
next_town = 0

l = len(a) + 1
for j in range(l):
    if j==0:
        b.append(a[0])
        next_town = a[0]
        continue
    
    b.append(a[next_town - 1])
    next_town = a[next_town -1]
for i in range(l):
    if b.count(i+1)==2:
        k -= b.index(i+1)
        indexes = [p for p, x in enumerate(b) if x == i+1]
        roop_list = b[indexes[0]:indexes[1]]
        roop_num = indexes[1]-indexes[0]
        last = k %roop_num
        

        print(roop_list[last-1])
        sys.exit()

Submission status: Wrong Answer

Solution code:
import sys
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = []
cnt = 0
next_town = 0

l = len(a) + 1
for j in range(l):
    if j==0:
        b.append(a[0])
        next_town = a[0]
        continue
    
    b.append(a[next_town - 1])
    next_town = a[next_town -1]
print(b)
for i in range(l):
    if b.count(i+1)==2:
        cnt = b.index(i+1)
        k -= cnt
        indexes = [p for p, x in enumerate(b) if x == i+1]
        roop_list = b[indexes[0]:indexes[1]]
        roop_num = indexes[1]-indexes[0]
        last = k %roop_num

        print(roop_list[last-1])
        sys.exit()

Submission status: Wrong Answer

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

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

moved = {}
now_city = 1
next_city = A[0]
i=0

while i < N:
    moved[now_city] = i
    if next_city in moved:
        break
    now_city = next_city
    next_city = A[now_city-1]
    i += 1

# i 回目の試行で、now_city から next_city に移ろうとしている

mod = i - moved[next_city]

moved_list = [x for x in moved.keys()]

loop_list = moved_list[moved[next_city]:i+1]
loop = len(loop_list)

if K < moved[next_city]:
    print(moved_list[K-1])
else:
    print(loop_list[(K-moved[next_city])%loop])


# print(f"{next_city=} {i=}")
# print(f"{loop_list=}")
# print(f"{moved_list=}")
# print(f"{K-moved[next_city]=}")
# print(f"{loop=}")

# ans = 5
# 6 10
# 2 3 4 5 4 6

Submission status: Wrong Answer

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

if k < 10:
    for i in range(k):
        if i == 0:
            next_town = a[0]
            continue
        
        next_town = a[next_town - 1]
    print(next_town)


b = []
cnt = 0
next_town = 0

l = len(a)
for i in range(l):
    cnt += 1
    if i==0:
        b.append(a[0])
        next_town = a[0]
        continue
    
    b.append(a[next_town - 1])
    next_town = a[next_town -1]
    if a.count(i+1)==2:
        k -= cnt
        idx_f = a.index(i+1)
        roop_list = a[idx_f:-1]
        roop_num = len(a[idx_f:-1])
        last = k %roop_num
        
        print(roop_list[last])
        sys.exit()

Submission status: Wrong Answer

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

for i in range(1,N+1):
    dict[i] = A[i-1]
#print(dict)

point = [-1]*N
point[0] += 1
tmp = 1
cnt = 0

while True:
    tmp = dict[tmp]
    point[tmp-1] += 1
    cnt += 1
    if point[tmp-1] >= 2:
        break

#print(point)
#print(cnt)
#print(point.count(0)) # ループに遷移するまでの移動数
#print(point.count(1)+point.count(2)) # ループ１周の移動数

remain = (K - (point.count(0))) % (point.count(1)+point.count(2))
#print(remain)

tmp = dict[point.index(2)+1]
if remain > 0:
    while True:
        tmp = dict[tmp]
        remain -= 1
        if remain < -1:
            print(tmp)
            break
else:
    print(point.index(2)+1)