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:
#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #ここにコード
    #input
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    #output
    #ループしたらそのループの周期でKを割る。
    loop = []
    position = 0
    loop.append(position)
    for i in range(N):
        position = A[position] - 1
        if position in loop:
            cycle = i+1 - loop.index(position)
            ini = loop.index(position)
            break
        else:
            loop.append(position)

    M = (K-ini) % cycle

    print(loop[(ini+M)] + 1)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np
def read_int():
    return int(input())

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

def read_lines(N):
    return [input() for i in range(N)]

def main():
    N, K = read_ints()
    towns = np.array(list(read_ints())) - 1
    #portals = {i: towns[i] for i in towns}
    portals = towns

    loop = []
    next_town = 0
    while next_town not in loop:
        loop.append(next_town)
        next_town = portals[next_town]
    loop.append(next_town)

    i = loop.index(next_town)
    all_loop = loop
    pre_loop, loop = loop[:i], loop[i:]

    if K < len(pre_loop):
        print(pre_loop[K])
    elif len(pre_loop) <= K < len(all_loop):
        print(loop[K - len(pre_loop)])
    else:
        print(loop[(K - len(pre_loop)) % len(loop)])
        



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [0] + A
# Dに何回目の移動で到達したか記録してみるか
D = [[] for _ in range(N+1)]

now = 1
i = 0
j = True
while i < K:
    now = A[now]
    D[now].append(i)
    if j and len(D[now]) > 2:
        length = D[now][2] - D[now][1]
        i = K - ((K - i) % length) - 1
        j = False
    i += 1
   
print(now)

Submission status: Wrong Answer

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

takahashi = [1, ]

for i in range(K):
    to = A[takahashi[i] - 1]
    if to in takahashi:
        l = (K - i)%(i - takahashi.index(to) + 1)
        for j in range(l):
            takahashi.append(to)
        break
    takahashi.append(to)

print(takahashi[-1])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [0] + A
# Dに何回目の移動で到達したか記録してみるか
D = [[] for _ in range(N+1)]

now = 1
i = 0
j = True
while i < K:
    now = A[now]
    D[now].append(i)
    if j and len(D[now]) > 2:
        length = D[now][2] - D[now][1]
        i = K - ((K - i) % length)
        j = False
    i += 1
   
print(now)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
A = list(map(int,input().split()))
roop = 0
port = [A[0]]
for _ in range(len(A)):
    port.append(A[port[-1]-1])
    if port.count(A[port[-2]-1]) == 2:#A in port
        port.pop()
        roop = len(port)-port.index(A[port[-1]-1])
        break
K = K-len(port)
print(port[len(port)-roop+(K%roop)-1])

Submission status: Wrong Answer

Solution code:
from sys import stdin,stdout
n,k = map(int,stdin.readline().split())
a = [int(x) for x in stdin.readline().split()]
visited = [1]
i = 1
j = 0
loop_len = 0
while j < k:
  i = a[i-1]
  if i in visited:
    loop_len = (len(visited)-visited.index(i))
    break
  visited.append(i)
  j += 1
if loop_len!=0:
  stdout.write(str(visited[k%loop_len]))
else:
  stdout.write(str(visited[-1]))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
def f(x,dic,i):
    p=dic1[x]
    if p in dic:
        return [dic,p]
    else:
        dic[p]=i
        return f(p,dic,i+1)

def inv(x,dic):
    for k,v in dic.items():
        if v==x:
            return k

n,k=map(int,input().split())
m=[i for i in range(n)]
l=list(map(int,input().split()))
for i in range(n):
    l[i]-=1
dic1=dict(zip(m,l))
d={0:0}
trace1=f(0,d,1)
u=trace1[0]
i=len(u)
if k<i:
    print(inv(i-k,u)+1)
else:
    d={trace1[1]:0}
    trace2=f(trace1[1],d,1)
    v=trace2[0]
    j=len(v)
    k=k-i
    k=k%j
    print(inv(k,v)+1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
n,k=map(int,input().split())
l=list(map(int,input().split()))
d=defaultdict(int)
for i in range(n):
    d[i+1]=l[i]
visited=set()
l=[]
i=1
while True:
    l.append(i)
    if i not in visited:
        visited.add(i)
    else:
        break
    i=d[i]
first=l.index(l[-1])
if k<first:
    print(l[k])
else:
    k=(len(l)-first)%((len(l)-1)-first)
    k+=first
    print(l[k])

Submission status: Wrong Answer

Solution code:
from sys import stdin,stdout
n,k = map(int,stdin.readline().split())
a = [int(x) for x in stdin.readline().split()]
visited = [1]
i = 1
while True:
  i = a[i-1]
  if i in visited:
    loop_len = (len(visited)-visited.index(i))
    break
  visited.append(i)
stdout.write(str(visited[k%loop_len]))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
n,k=map(int,input().split())
l=list(map(int,input().split()))
d=defaultdict(int)
for i in range(n):
    d[i+1]=l[i]
visited=set()
l=[]
i=1
while True:
    l.append(i)
    if i not in visited:
        visited.add(i)
    else:
        break
    i=d[i]
first=l.index(l[-1])
if k<=len(l):
    print(l[k-1])
else:
    k-=(first+1)
    k=(len(l)-1)-first
    print(l[k-1])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = [int(x) for x in input().split()]
visited = []
visited.append(1)
i = 1
index = 1
while True:
  i = a[i-1]
  if i in visited:
    loop_len = (index-visited.index(i))
    break
  visited.append(i)
  index+=1
print(visited[k%loop_len])

Submission status: Wrong Answer

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

G=defaultdict(list)
prev=1

visited=set()
visited.add(1)

next=A[1]
while(not next in visited):
    G[prev].append(next)
    prev=next
    visited.add(next)
    next=A[next]
G[prev].append(next)
#loop starts from next
#calculate length = number of nodes in loop from next to prev

origin=next
next=G[origin][0]
cnt=0 ##length of loop
while(next!=origin):
    cnt+=1
    next=G[next][0]
##get Distance require to travel from town 1 to origin.
cnt+=1
origin1=1
next=G[1][0]
dist=0
while(next!=origin):
    dist+=1
    next=G[next][0]
dist+=1
K=K-dist
N=K%cnt
#travel N nodes from origin i.e start of the loop

cnt=0
next=origin
while(cnt!=N):
    cnt+=1
    next=G[next][0]
print(next)

Submission status: Wrong Answer

Solution code:

#import numpy as np
from functools import *
import sys
sys.setrecursionlimit(500000)
input = sys.stdin.readline


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




def search(x,step,vnode):
    global L,visited,K
    
    #print(visited,x)
    #print(step)
    visited[x]=step
    vnode.append(x)
    
                
    if L[x]==x:
        print(x)
        sys.exit(0)
        
    if visited[L[x]]>=0:
        C=step+1-visited[L[x]]
        k=(K-visited[L[x]])%C
        #if k<0:
        #    k=(k+C)%C
            
        #print(vnode)
        #print(k,L[x],visited[L[x]])
        vnode.append(L[x])
        print(vnode[visited[L[x]]+k])
               
        #print(step,x,visited)
        sys.exit(0)
    
    if step==K+1:
        #print(step)
        
        #print(visited)
        sys.exit(0)            
    
    search(L[x],step+1,vnode)
    
    
    

N,K=acinput()
L=[0]
L.extend(acinput())

visited=[-1]*(N+1)
#visited[1]=1
search(1,0,[])

#4 500000000000000000000000001
#2 3 4 4

Submission status: Wrong Answer

Solution code:

#import numpy as np
from functools import *
import sys
sys.setrecursionlimit(500000)
input = sys.stdin.readline


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




def search(x,step,vnode):
    global L,visited,K
    
    #print(visited,x)
    #print(step)
    visited[x]=step
    vnode.append(x)
    
                
    if L[x]==x:
        print(x)
        sys.exit(0)
        
    if visited[L[x]]>=0:
        C=step+1-visited[L[x]]
        k=(K%(2*C)-visited[L[x]])%C
        if k<0:
            k=(k+C)%C
            
        #print(vnode)
        #print(k,L[x],visited[L[x]])
        vnode.append(L[x])
        print(vnode[visited[L[x]]+k])
               
        #print(step,x,visited)
        sys.exit(0)
    
    if step==K+1:
        #print(step)
        
        #print(visited)
        sys.exit(0)            
    
    search(L[x],step+1,vnode)
    
    
    

N,K=acinput()
L=[0]
L.extend(acinput())

visited=[-1]*(N+1)
#visited[1]=1
search(1,0,[])

#4 500000000000000000000000001
#2 3 4 4

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
l=list(map(int,input().split()))
x=y=t=0
c=0
x=min(l)
s=[0]*n
while 1:
    if s[x-1]==0:
        s[x-1]=1
    else:
        break
    y = l[x - 1]
    c+=1
    x=y
z=m%c
x=min(l)
if c>m:
    while m:
        y = l[x - 1]
        m -= 1
        x = y
    print(y)
else:
    while z:
        y = l[x - 1]
        z -= 1
        x = y
    print(y)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
a=list(map(int,input().split()))
s=[-1]*N
i=1
m=a[0]

for _ in range(K):
    if(s[a[m-1]-1]>=0):
        break
    s[a[m-1]-1]=i
    m=a[m-1]
    i+=1

j=a[0]
for _ in range(s[a[m-1]-1]-1+(K-s[a[m-1]-1])%(i-s[a[m-1]-1])):
    j=a[j-1]
print(j)

Submission status: Wrong Answer

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

Ns = list(map(int, input().split()))
  
now = 1
loop = []
num = 0
for j in range(len(Ns)) :
  if not now in loop :
    loop.append(now)
    now = Ns[now-1]
  else :
    n_loop = len(loop) - loop.index(now)
    ans = loop.index(now) + (K - loop.index(now)) % n_loop
    print(Ns[ans])
    sys.exit()
    
print(now)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
l=list(map(int,input().split()))
x=y=t=0
c=0
x=min(l)
s=[0]*n
while 1:
    if s[x-1]==0:
        s[x-1]=1
    else:
        break
    y = l[x - 1]
    c+=1
    x=y
z=m%c
x=min(l)
while z:
    y = l[x-1]
    z-=1
    x=y
print(y)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
l=list(map(int,input().split()))
x=y=t=0
c=1
x=min(l)
s=[0]*n
while 1:
    if s[x-1]==0:
        s[x-1]=1
    else:
        break
    y = l[x - 1]
    c+=1
    x=y
z=m%c
x=min(l)
while z:
    y = l[x-1]
    z-=1
    x=y
print(y)

Submission status: Wrong Answer

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

iti = 1
ex_A = []
basyo=0

for k in range(K):
    ex_A.append(iti)
    iti = A[iti - 1]
    if iti in ex_A:
        basyo = ex_A.index(iti)
        maru = len(ex_A[:basyo])
        nokori = (K - maru) % (len(ex_A[basyo:]))
        break


if basyo:
    iti = ex_A[basyo+nokori]
else:
    iti = ex_A[-1]
#print(ex_A)
print(iti)

Submission status: Wrong Answer

Solution code:
# @oj: atcoder
# @id: hitwanyang
# @email: 296866643@qq.com
# @date: 2020-05-10 21:37
# @url:https://atcoder.jp/contests/abc167/tasks/abc167_d

import sys,os
from io import BytesIO, IOBase
import collections,itertools,bisect,heapq,math,string
# region fastio

BUFSIZE = 8192

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------------------
def main():
    nk=list(map(int,input().split()))
    n,k=nk[0],nk[1]
    A=list(map(int,input().split()))
    pos=[1]
    cnt=0
    s=set()
    s.add(1)
    while cnt<len(A):
        temp=A[pos[-1]-1]
        pos.append(temp)
        # 注意要使用set来判断集合元素是否存在,
        # 不能使用 in pos的操作，数组的in存在超时的风险
        if temp in s:
            # pos.append(A[pos[-1]-1])
            break
        s.add(temp)
        cnt+=1
    # print (pos)
    index=pos.index(pos[-1])
    mod=(k-index)%(len(pos[index:])-1)
    # print (mod)
    print (pos[index+mod])
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# @oj: atcoder
# @id: hitwanyang
# @email: 296866643@qq.com
# @date: 2020-05-10 21:37
# @url:https://atcoder.jp/contests/abc167/tasks/abc167_d

import sys,os
from io import BytesIO, IOBase
import collections,itertools,bisect,heapq,math,string
# region fastio

BUFSIZE = 8192

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------------------
def main():
    nk=list(map(int,input().split()))
    n,k=nk[0],nk[1]
    A=list(map(int,input().split()))
    pos=[1]
    cnt=0
    s=set()
    while cnt<len(A):
        s.add(A[pos[-1]-1])
        pos.append(A[pos[-1]-1])
        if A[pos[-1]-1] in s:
            pos.append(A[pos[-1]-1])
            break
        cnt+=1
    # print (pos)
    index=pos.index(pos[-1])
    mod=(k-index)%(len(pos[index:])-1)
    # print (mod)
    print (pos[index+mod])
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# @oj: atcoder
# @id: hitwanyang
# @email: 296866643@qq.com
# @date: 2020-05-10 21:37
# @url:https://atcoder.jp/contests/abc167/tasks/abc167_d

import sys,os
from io import BytesIO, IOBase
import collections,itertools,bisect,heapq,math,string
# region fastio

BUFSIZE = 8192

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------------------
def main():
    nk=list(map(int,input().split()))
    n,k=nk[0],nk[1]
    A=list(map(int,input().split()))
    pos=[1]
    cnt=0
    while cnt<len(A):
        pos.append(A[pos[-1]-1])
        if A[pos[-1]-1] in pos:
            pos.append(A[pos[-1]-1])
            break
        cnt+=1
    # print (pos)
    index=pos.index(pos[-1])
    mod=(k-index)%(len(pos[index:])-1)
    # print (mod)
    print (pos[index+mod])
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
A = [-1] + A
memo = [1]
loop = []
visit = [1] + [0]*(n-1)
town = 1
while visit[A[town]-1] < 3:
    town = A[town]
    if visit[town-1] == 0:
        memo.append(town)
    else:
        loop.append(town)
    visit[town-1] += 1
outlier = len(memo) - len(loop) 
T = len(loop)
if k < outlier:
    print(memo[k])
else:
    print(loop[(k-outlier) % T])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
a=list(map(int,input().split()))
s=[-1]*N
i=1
m=a[0]

for _ in range(K):
    if(s[a[m-1]-1]>=0):
        break
    s[a[m-1]-1]=i
    m=a[m-1]
    i+=1

j=a[0]
for _ in range((K-s[a[m-1]-1])%(i-s[a[m-1]-1])):
    j=a[j-1]
print(j)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
done=[0 for _ in range(N)]
done[0]=1
W=[0]
init=0
loop=N
city=0
for i in range(N):
    if done[A[i]-1]==2:
        init=W.index(A[i]-1)
        loop=i-init
        break
    elif city==A[city]-1:
        init=i
        loop=1
        break
    city=A[city]-1
    W.append(city)
    done[city]+=1
if K>=init:
    print(W[(K-init)%loop+init]+1)
else:
    print(W[K]+1)

Submission status: Wrong Answer

Solution code:
n,k=list(map(int,input().split()))
A=list(map(int,input().split()))
start,steps=1,0
root=dict()

while start not in root and steps < k:
  root[start]=steps
  steps+=1
  start=A[start-1]

if steps == k:
  print(start+1)
else:
  pa = steps-root[start]
  s=root[start]
  ans=(k-steps)%pa
  root={v: k for k, v in root.items()}
  print(root[s+ans])

Submission status: Wrong Answer

Solution code:
def search_loop(list_route, now):
    list_route.append(list_a[now-1])
    for start in range(len(list_route)):
        for end in range(start+1, len(list_route)):
            if list_route[start] == list_route[end]:
                return start, end - start
    return search_loop(list_route, list_a[now-1])


N, K = map(int, input().split())
list_a = list(map(int, input().split()))
if list_a[0] == 1:
    print(1)
    quit()
list_route = [1]
loop_start, loop_length = search_loop(list_route, 1)
t = (K-loop_start+1) % (loop_length+1)
print(list_route[t])
print(list_a)

Submission status: Wrong Answer

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

vlist = [1]
vcnt = [0]*(n+1)

v = 1
for i in range(1,n+1):
    v = a[v]
    vlist.append(v)
    if i == k:
        print(vlist[-1])
        exit()
    if vcnt[v] == 0 and v != 1:
        vcnt[v] += i
    else:
        syuuki = i - vcnt[v]
        hajime = i - syuuki
        m_start = (hajime + 1)%syuuki
        break
"""
k %= syuuki
p = k - m_start
print(vlist[k + p])
"""
print(vlist[k%syuuki])

Submission status: Wrong Answer

Solution code:
import collections
n,k = map(int,input().split())
a = list(map(int,input().split()))
list = [1]
i = 0
num = a[0]
for i in range(3*n):
    list.append(num)
    num = a[num-1]

cnt = collections.Counter(list)
onevalue = [k for k,v in cnt.items() if v == 1]
lstart = len(onevalue)
lroop = len(set(list))-lstart

if k <= lstart:
    num = a[0]
    for i in range(k-1):
        num = a[num-1]
    print(num)
    exit()
else:
    k = (k-lstart)%lroop
    num = a[0]
    if lstart > 0:
        for i in range(lstart-1):
            num = a[num-1]
    for i in range(k):
        num = a[num-1]
    print(num)
    exit()

Submission status: Wrong Answer

Solution code:
import copy

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

for _ in range(N+K%N-1):
    i=A[i-1]
print(i)

Submission status: Wrong Answer

Solution code:
#4問目
import sys
N, K = map(int, input().split())
A = list(map(int, input().split()))

#すでに移動したことのある場所
already = [False] * N
step = 0
now = 1
count = 0
while(already[now-1] != True):
    already[now - 1] = True
    now = A[now - 1]
    count += 1
    if count == K:
      print(now)
      exit(0)
# count はstartから2度現れる点までの移動数

#ループに入るまで何回ステップがあったか調べる
# loop := ループに含まれる辺の個数
loop=0
cycle = 0
if(now-1 == 0):
    step = 0
    loop = count
else:
    step = 0
    temp = 1
    while(temp != now):
        temp = A[temp - 1]
        step += 1
    cycle = count + 1 - step
    loop= (K - step) % cycle

temp = 1
for i in range(step + loop - 1):
    temp = A[temp - 1]
print(temp)

Submission status: Wrong Answer

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

stack=[]
done=[0 for i in range(2*(10**5))]
done[0]=0
now=0
dou=[]

for i in range(2*(10**5)):
  
  if done[now]==2:
    break
  if done[now]==0:
    stack.append(now)
  else:
    dou.append(now)
  done[now]+=1
  now=A[now]-1

if K<len(stack):
  print(stack[K]+1)
else:
  print(dou[(K-len(stack)-len(dou))%len(dou)]+1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
l1=[0]*n
l1[0]+=1
c=0 #ループに入る前までの移動回数
now=0#現在位置
ne=a[now]-1 #次の移動位置
while True:
    if c==k:
        print(now+1)
        exit()
    if l1[ne]==0:
        l1[ne]+=1
        now=ne
        ne=a[now]-1
        c+=1
    else:
        now=ne
        ne=a[now]-1
        c+=1
        break

roop_c=0
while True:
    if c+roop_c==k:
        print(now+1)
        exit()
    if l1[ne]==1:
        l1[ne]+=1
        now=ne
        ne=a[now]-1
        roop_c+=1
    else:break

re=(k-c)%roop_c
if re==0:print(now+1)
else:
    for i in range(re):
        if i==re-1:print(now+1)
        ne+=-ne+a[ne]-1

Submission status: Wrong Answer

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

stack=[]
done=[0 for i in range(2*(10**5))]
done[0]=0
now=1
dou=[]

for i in range(2*(10**5)):
  
  if done[now-1]==2:
    break
  if done[now-1]==0:
    stack.append(now)
  else:
    dou.append(now)
  done[now-1]+=1
  now=A[now-1]


  
if K<len(stack):
  print(stack[K])
else:
  print(dou[(K-len(stack)-len(dou))%len(dou)])

Submission status: Wrong Answer

Solution code:
#4問目
import sys
N, K = map(int, input().split())
A = list(map(int, input().split()))

#すでに移動したことのある場所
already = [False] * N
step = 0
now = 1
count = 1
while(already[now-1] != True):
    already[now - 1] = True
    now = A[now - 1]
    count += 1
    if count - 1 == K:
      print(now)
      exit(0)
# count はループに含まれる頂点の数

#ループに入るまで何回ステップがあったか調べる
if(now-1 == 0):
    step = 0
    loop = count
else:
    step = 0
    temp = 1
    while(temp != now):
        temp = A[temp - 1]
        step += 1
    cycle = count - step
    loop= (K-step) % cycle
    
temp = 1
for i in range(step + loop -1):
    temp = A[temp - 1]
print(temp)

Submission status: Wrong Answer

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

stack=[]
done=[0 for i in range(N)]
done[0]=0
now=1
cnt=0
dou=[]

for i in range(2*(10**5)):
  
  if done[now-1]==2:
    break
  if done[now-1]==0:
    stack.append(now)
  else:
    dou.append(now)
  done[now-1]+=1
  now=A[now-1]


  
if K<len(stack):
  print(stack[K])
else:
  print(dou[(K-len(stack)-len(dou))%len(dou)])

Submission status: Wrong Answer

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

stack=[]
done=[0 for i in range(N)]
done[0]=0
now=1
cnt=0
dou=[]

for i in range(2*(10**5)):
  
  if done[now-1]==2:
    break
  if done[now-1]==0:
    stack.append(now)
  else:
    dou.append(now)
  done[now-1]+=1
  now=A[now-1]


  
if K<len(stack):
  print(stack[K])
else:
  print(dou[(K-len(stack))%len(dou)])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
path = []
x = 1
path.append(x)
for i in range(n):
    x = a[x-1]
    path.append(x)
#print(path)
count = [0]*n
roop_st = 0
for town in path:
    count[town-1] += 1
    if count[town-1] == 2:
        roop_st = town
        break
roop_st_idx = path.index(roop_st)
roop_fin_idx = path.index(roop_st, roop_st_idx+1)
#print(roop_st_idx, roop_fin_idx)
k_mod = (k - roop_st_idx) % (roop_fin_idx - roop_st_idx)

ans = 1
for _ in range(roop_st_idx+k_mod):
    ans = a[ans-1]
print(ans)

Submission status: Wrong Answer

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

when = [-1 for _ in range(N+1)]
new = 1
i = 0

while when[new] == -1:
    when[new] = i
    new = A[new - 1]
    i += 1

line = when[new]
roop = i - when[new]

K = (K - line) % roop
for j in range(N+1):
    if when[j] - line == K:
        print(j)
        break

Submission status: Wrong Answer

Solution code:
from collections import Counter
n, k = map(int, input().split())
a = list(map(int, input().split()))
path = []
x = 1
path.append(x)
for i in range(n):
    x = a[x-1]
    path.append(x)
#print(path)
c = Counter(path)
#print(c)
roop_st = 0
for key, v in c.items():
    if v >= 2:
        roop_st = key
        break
roop_st_idx = path.index(roop_st)
roop_fin_idx = path.index(roop_st, roop_st_idx+1)
#print(roop_st_idx, roop_fin_idx)
k_mod = (k - roop_st_idx) % (roop_fin_idx - roop_st_idx)

ans = 1
for _ in range(roop_st_idx+k_mod):
    ans = a[ans-1]
print(ans)

Submission status: Wrong Answer

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

iti = 1
ex_A = []
basyo=0

for k in range(N):
    ex_A.append(iti)
    iti = A[iti-1]
    if iti in ex_A:
        basyo = ex_A.index(iti)
        maru = len(ex_A[:basyo])
        nokori = (K - maru) % len(ex_A[basyo:])
        break

if basyo:
    iti = ex_A[basyo+nokori]
#print(ex_A)
print(iti)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n, k = map(int, input().split())
a = list(map(int, input().split()))
path = []
path.append(1)
x = a[0]
path.append(x)
for i in range(n-2):
    x = a[x-1]
    path.append(x)
#print(path)
c = Counter(path)
#print(c)
roop_st = 0
for key, v in c.items():
    if v >= 2:
        roop_st = key
        break
roop_st_idx = 0
roop_fin_idx = n
if roop_st != 0:
    roop_st_idx = path.index(roop_st)
    roop_fin_idx = path.index(roop_st, roop_st_idx+1)
#print(roopidx,roopidx2)
k_mod = (k - roop_st_idx) % (roop_fin_idx - roop_st_idx)

ans = 1
for _ in range(roop_st_idx+k_mod):
    ans = a[ans-1]
print(ans)

Submission status: Wrong Answer

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

stack=[]
done=[0 for i in range(N)]
done[0]=0
now=1
cnt=0

for i in range(2*(10**5)):
  done[now-1]+=1
  if done[now-1]==2:
    cnt+=1

  if done[now-1]==3:
    break
  
  stack.append(now)
  now=A[now-1]

amari=len(stack)%cnt
double=stack[len(stack)-cnt:]
if K<len(stack):
  print(stack[K])
else:
  K=K-amari
  K=K%cnt
  print(double[K])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
#N=6
#K=1
#A=[6,5,2,5,3,2]
iti = 1
ex_A = []
basyo=0

for k in range(K):
    ex_A.append(iti)
    iti = A[iti-1]
    if iti in ex_A:
        basyo = ex_A.index(iti)
        maru = len(ex_A[:basyo])
        nokori = (K - maru) % len(ex_A[basyo:])
        break

if basyo:
    iti = ex_A[basyo+nokori]
#print(ex_A)
print(iti)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[int(i)-1 for i in input().split()]
p=0
visited=[0]*n
first=[]
roop=[]
while visited[p]!=2:#二回通っていたらやめる
    if visited[p]==0:#初めて通った
        first.append(p)
    else:#一回通った
        roop.append(p)
    visited[p]+=1#通った跡をつける
    p=a[p]#テレポート
print(first,roop)
if len(first)>k:#一回もループしていないのなら
    print(first[k]+1)#最初のk番目
else:
    print(roop[(k-(len(first)-len(roop)))%len(roop)]+1)#そうでなければkから一回目のループと二回目のループを引いて二回目のループの回数の余剰

Submission status: Wrong Answer

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

s = [-1] * n
c = [1]
t = 0
now = 0
while s[now] == -1:
    s[now] = t
    now = a[now] - 1
    c.append(now+1)
    t += 1

start_cycle = c.index(c[-1])
loop = c[start_cycle:-1]
cycle = len(loop)

if k < cycle:
    print(c[k])
else:
    k -= start_cycle
    k %= cycle
    print(loop[k])