input
stringlengths
20
127k
target
stringlengths
20
119k
problem_id
stringlengths
6
6
from itertools import accumulate N,K=list(map(int,input().split())) N_s=int(N**0.5) list_a=[1]*N_s list_b=[N//j-N_s for j in range(1,N_s+1)] list_p=[N//j-N_s for j in range(1,N_s+1)] for p in range(1,len(list_p)): list_p[p-1]=list_p[p-1]-list_p[p] for p in range(1,len(list_b)): list_b[p-1...
from itertools import accumulate as ac N,K=list(map(int,input().split())) n=int(N**0.5) mod=10**9+7 list_a=[1]*n list_b=[N//j-N//(j+1) for j in range(1,n)]+[(N//n-n)] list_p=[p for p in list_b] list_aa=list(ac(list_a)) list_bb=list(reversed(list(ac(reversed(list_b))))) for i in range(K-1): temp=sum(list...
p02992
N,K = list(map(int, input().split())) # As = [N//i for i in range(1,int(N**0.5)+1)] # Bs = [As[i]-As[i+1] for i in range(len(As)-1)] # Bs[0] -= 1 # Bs = sorted(Bs) + [1] As = [] for i in range(1,int(N**0.5)+1): As.append(i) if N//i != i: As.append(N//i) As = sorted(As,reverse=False) nums = [...
from itertools import accumulate N,K = list(map(int, input().split())) As = [] for i in range(1,int(N**0.5)+1): As.append(i) if N//i != i: As.append(N//i) As = sorted(As,reverse=False) nums = [] for i in range(len(As)-1): nums.append(As[i+1]-As[i]) nums = [1] + nums nums.reverse() mo...
p02992
import sys input = sys.stdin.readline from itertools import accumulate MOD = 10**9+7 n, k = list(map(int, input().split())) S = set() i = 1 while i * i <= n: S.add(i) S.add(n//i) i += 1 L = [0] + sorted(S) l = len(L) - 1 num = [0]*l for i in range(l): num[i] = L[i+1] - L[i] dp = [0]*l dp[0]...
import sys input = sys.stdin.readline from itertools import accumulate MOD = 10**9+7 def main(): n, k = list(map(int, input().split())) S = set() i = 1 while i * i <= n: S.add(i) S.add(n//i) i += 1 L = [0] + sorted(S) l = len(L) - 1 num = [0]*l for i in range(l): num[i]...
p02992
import math import copy N, K = list(map(int, (input().split()))) mod = 10**9 + 7 ans_array = [] divide_max = N for i in range(2, math.floor(math.sqrt(N)) + 2): divide = math.floor(N / i) ans_array.append(divide_max - divide) divide_max = divide for i in range(divide): ans_array.append...
import math N, K = list(map(int, (input().split()))) mod = 10**9 + 7 ans_array = [] divide_max = N divide_count = [] for i in range(2, math.floor(math.sqrt(N)) + 2): divide = math.floor(N / i) ans_array.append(divide_max - divide) divide_count.append(divide_max - divide) divide_max = div...
p02992
n, k = [int(x) for x in input().strip().split(" ")] mod = pow(10, 9) + 7 ll = [] ks = [] cc = 0 last = 0 for i in range(n): i += 1 if n//i < i: last = n//i break ll.append(n//i) ks.append(1) last = n//i cc += 1 while True: if last == 0: break ...
from itertools import accumulate as r;n,k=list(map(int,input().split()));m=10**9+7;j=int(n**0.5);c=[n//(i+1)-n//(i+2) for i in range(n//(j+1))]+[1]*j;x=c;exec("x=[(y*d)%m for y,d in zip(r(x[::-1]),c)];"*k);print((x[-1]))
p02992
a,b,c=list(map(int,input().split())) ans=0 poison=False while(True): if(poison): if(b>0): b-=1 ans+=1 elif(a>0): a-=1 else: break poison=False else: if(c>0): c-=1 ans+=1 p...
a,b,c=list(map(int,input().split())) if(c<=a+b): print((c+b)) else: print(((a+b)+b+1))
p03186
import sys input=sys.stdin.readline a,b,c = list(map(int, input().split())) hp = 2 ans = 0 while True: if a == 0 and b == 0 and c == 0: break if hp == 1 and a == 0 and b == 0: break if hp > 1 and c > 0: ans += 1 c -= 1 hp -= 1 if hp < 2 or c == 0: ...
import sys input=sys.stdin.readline a,b,c = list(map(int, input().split())) if a >= c-1 or b >= c-1 or a+b >= c-1: print((b+c)) else: print((b+(a+b)+1))
p03186
a,b,c=list(map(int, input().split())) count=0 while(True): if c>0: if b>0: c-=1 b-=1 count+=2 else: if a>0: c-=1 a-=1 count+=1 else: c-=1 count...
a,b,c=list(map(int, input().split())) count=0 if c>=b: count+=2*b c=c-b if c>a: count+=a c=c-a if c>0: count+=1 else: count+=c else: count+=c+b print(count)
p03186
A, B, C = list(map(int, input().split())) ans = 0 c = C for i in range(C) : if c > 0 and B > 0 : ans += 2 c -= 1 B -= 1 elif c > 0 and A > 0 : ans += 1 c -= 1 A -= 1 if c > 0 : ans += 1 print((ans + B))
A, B, C = list(map(int, input().split())) ans = 0 if B >= C : print((B + C)) exit() ans = 2 * B C -= B if A >= C : print((ans + C)) exit() print((ans + A + 1))
p03186
A,B,C=(int(i) for i in input().split()) counter=0 while C>0: C-=1 counter+=1 if B>0: B-=1 counter+=1 elif A>0: A-=1 else: break if B>0: counter+=B print(counter)
A,B,C=(int(i) for i in input().split()) counter=0 #BとCから if C>=B: counter+=B*2 C=C-B B=0 if C>A: counter+=A+1 else: counter+=C else: counter+=C*2 B-=C C=0 counter+=B print(counter)
p03186
A,B,C = list(map(int,input().split())) can = min(A+B,C) eat = 0 for i in range(can): C -= 1 eat += 1 if A > 0: A -= 1 else: eat += 1 B -= 1 if C > 0: eat += 1 print((eat+B))
A,B,C = list(map(int,input().split())) can = min(A+B,C) eat = 0 C -= can eat += can*2-A B = A+B -can if C > 0: eat += 1 print((eat+B))
p03186
a,b,c = (int(i) for i in input().split()) if a + b >= c: print((b + c)) else: print((a + b + b + 1))
a, b, c = (int(i) for i in input().split()) if a + b >= c: print((b + c)) else: print((b + a + b + 1))
p03186
A, B, C = list(map(int, input().split())) jyotai = 0 ans = 0 while True: if A == 0 and B == 0 and jyotai == 1: break if B == 0 and C == 0: break if jyotai == 0: if C != 0: ans += 1 C -= 1 jyotai += 1 else: ans +=...
A, B, C = list(map(int, input().split())) ans = 2 * B C -= B if A >= C: ans += C else: ans += A + 1 print(ans)
p03186
# https://atcoder.jp/contests/agc030/tasks/agc030_a def main(): A, B, C = list(map(int, input().split())) deli = 0 while A > 0 and C > 0: deli += 1 A -= 1 C -= 1 # 美味しくないクッキーがなくなった while B > 0 and C > 0: deli += 2 B -= 1 C -= 1 if ...
# https://atcoder.jp/contests/agc030/tasks/agc030_a def main(): A, B, C = list(map(int, input().split())) deli = 0 if A > 0 and C > 0: eat = min(A, C) deli += eat A -= eat C -= eat # 美味しくないクッキーがなくなった, お腹壊してない if B > 0 and C > 0: eat = min(B, C) ...
p03186
from collections import defaultdict as dd from collections import Counter from pprint import pprint as pp YN = {True: 'Yes', False: 'No'} ri = lambda: int(eval(input())) ria = lambda: list(map(int, input().split())) rian = lambda n: [ria() for _ in range(n)] rs = lambda: eval(input()) rsa = lambda: input().sp...
from collections import defaultdict as dd from collections import Counter from pprint import pprint as pp YN = {True: 'Yes', False: 'No'} ri = lambda: int(eval(input())) ria = lambda: list(map(int, input().split())) rian = lambda n: [ria() for _ in range(n)] rs = lambda: eval(input()) rsa = lambda: input().sp...
p03186
a, b, c = list(map(int, input().split())) if c > (a + b): print((a + b + 1 + b)) else: print((c + b))
a, b, c = list(map(int, input().split())) print((b + min(a + b + 1, c)))
p03186
A, B, C = list(map(int, input().split())) print((min(C, A + B + 1) + B))
A, B, C = list(map(int, input().split())) print((B + min(C, A + B + 1)))
p03186
# -*- coding: utf-8 -*- # 整数の入力 a, b, c = list(map(int, input().split())) sum = 0 while (1 == 1): #Cがある時 if c > 0: c = c - 1 sum = sum + 1 if a > 0: a = a - 1 elif b > 0: b = b -1 sum = sum + 1 else : break ...
# -*- coding: utf-8 -*- # 整数の入力 a, b, c = list(map(int, input().split())) result = 0 if a + b + 1 >= c: result = b + c else: result = b + (a + b + 1) # 出力 print(("{}" .format(result)))
p03186
''' AtCoder Grand Contest 030 A - Poisonous Cookies ''' def main(): a, b, c = list(map(int, input().split())) oishii = b + c # クッキーを食べる while a + b > 0: c = c - 1 if c > 0 else 0 if b > 0: b -= 1 elif a > 0: a -= 1 # まだcが残ってるな...
''' AtCoder Grand Contest 030 A - Poisonous Cookies ''' def main(): a, b, c = list(map(int, input().split())) ''' A + B + 1 ≥ C ならすべての毒入りの クッキーを食べることができるので B + C。 そうでないなら答えはB + (A + B + 1) 。 ※「+1」は最後にCを食べるので ''' print((b + min(c, a + b + 1))) if __name__ == "__main...
p03186
A, B, C = [int(s) for s in input().split()] poisonous = False eaten_cookies = 0 while True: if not poisonous and C > 0: eaten_cookies += 1 C -= 1 poisonous = True elif poisonous and A > 0: A -= 1 poisonous = False elif B > 0: eaten_cookies += 1 B -= 1 poisonous = ...
A, B, C = [int(s) for s in input().split()] print((min([C, A + B + 1]) + B))
p03186
a, b, c = list(map(int, input().split())) cnt = 0 while c > 0 or b > 0: if c > 0: c -= 1 cnt += 1 elif b > 0: b -= 1 cnt += 1 continue if b > 0: b -= 1 cnt += 1 continue elif a > 0: a -= 1 continue e...
a, b, c = list(map(int, input().split())) cnt = 0 if a + b + 1>= c: ans = b + c else: ans = b + a + b + 1 print(ans)
p03186
A,B,C=list(map(int,input().split())) poisoned=False ans=0 while True: if C>0 and not poisoned: ans+=1 C-=1 poisoned=True if B>0: ans+=1 poisoned=False B-=1 if A>0 and poisoned: A-=1 poisoned=False if (B==0 and A==0 and poisoned) or (B==0 and C==0): break prin...
A,B,C=list(map(int,input().split())) if A+B>=C: print((B+C)) else: print((B*2+A+1))
p03186
a,b,c = list(map(int, input().split())) x = 0 if a<=c: x += a c -= a a = 0 elif a>c: x += c c = 0 a -= c if c<=b: x += (c+b) c = 0 b = 0 elif c>b: x += (2*b) b = 0 c -= b if c>0: x += 1 print(x)
a,b,c = list(map(int, input().split())) print((b+min(c,a+b+1)))
p03186
a,b,c=[int(i) for i in input().split()] count=0 while c > 0: c -= 1 count += 1 if b > 0: b -= 1 count += 1 elif a>0: a -= 1 else: break if b>0: count+=b print(count)
a,b,c=[int(i) for i in input().split()] if c<b or c<=a+b: print((b+c)) else: print((a+b*2+1))
p03186
from collections import defaultdict import sys sys.setrecursionlimit(10 ** 6) input = sys.stdin.readline int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def main(): def dfs(level=0, u=0): if len(to[(level, u)]) == 1: cnt_tree[l - level] += 1 for kl, ku in to...
from collections import defaultdict import sys sys.setrecursionlimit(10 ** 6) input = sys.stdin.readline int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def main(): n, l = map(int, input().split()) ss = [input()[:-1] for _ in range(n)] + ["$"] ss.sort() # print(ss) #...
p03491
def main(): import sys from collections import defaultdict input = sys.stdin.readline N, L = list(map(int, input().split())) dic = defaultdict(int) for _ in range(N): s = input().rstrip('\n') for i in range(len(s)): dic[s[:i+1]] = 1 g = 0 key = ...
def main(): import sys from collections import defaultdict input = sys.stdin.readline N, L = list(map(int, input().split())) dic = defaultdict(int) for _ in range(N): s = input().rstrip('\n') s = [si for si in s] for i in range(len(s)): dic[''.join...
p03491
def main(): import sys from collections import defaultdict input = sys.stdin.readline mod = 10**10+7 mod2 = 10**10+9 mod3 = 998244353 N, L = list(map(int, input().split())) dic = defaultdict(int) dic2 = defaultdict(int) dic3 = defaultdict(int) h_list = [] h2...
def main(): import sys input = sys.stdin.readline class TreiNode: def __init__(self, char_num, depth): self.end = False self.child = [None] * char_num self.depth = depth def __setitem__(self, i, x): self.child[i] = x def...
p03491
s = input().split() n = int(s[0]) k = int(s[1]) a = input().split() siyouzumi = [1] t = 1 for i in range(1,k+1): t = int(a[t-1]) if t in siyouzumi: haba = i - siyouzumi.index(t) kaisu = int((k - i) / haba) l = k - (i + (haba * kaisu)) for j in range(l): ...
s = input().split() n = int(s[0]) k = int(s[1]) a = input().split() siyouzumi = {1} y = [1] t = 1 for i in range(1,k+1): t = int(a[t-1]) if t in siyouzumi: haba = i - y.index(t) kaisu = int((k - i) / haba) l = k - (i + (haba * kaisu)) for j in range(l):...
p02684
N,K = list(map(int,input().split())) A = list(map(int,input().split())) town = 1 record = [1] for i in range(N): town = A[town-1] if town in record: break record.append(town) newrecord = record[record.index(town):] roop = len(newrecord) newK = K-(len(record)-roop) if newK>=0: ...
N,K = list(map(int,input().split())) A = list(map(int,input().split())) town = 1 record = [1] record2 = [0]*N for i in range(N): town = A[town-1] if record2[town-1]==1: break record.append(town) record2[town-1] += 1 newrecord = record[record.index(town):] roop = len(newrecord) ...
p02684
n, k = list(map(int, input().split())) a = list(map(int, input().split())) c = 0 loc = 0 locs = [0] ini = 0 while True: c += 1 loc = a[loc] - 1 if c == k: print((loc + 1)) break if loc in locs: ini = locs.index(loc) break locs.append(loc) i...
n, k = list(map(int, input().split())) a = list(map(int, input().split())) c = 0 loc = 0 locs = {0: 0} ini = 0 while True: c += 1 loc = a[loc] - 1 if c == k: print((loc + 1)) break if loc in locs: ini = locs.get(loc) break locs[loc] = c if ...
p02684
N, K = list(map(int, input().split())) A = [1] + list(map(int, input().split())) p = 1 points = [p] for _ in range(N): p = A[p] if p in points: break points.append(p) idx = points.index(p) if idx > K: print((points[K])) exit() cycle = points[idx:] print((cycle[(K - id...
N, K = list(map(int, input().split())) A = [1] + list(map(int, input().split())) p = 1 points = [p] visited = set([p]) for _ in range(N): p = A[p] if p in visited: break points.append(p) visited.add(p) idx = points.index(p) if idx > K: print((points[K])) exit() c...
p02684
n,k = list(map(int,input().split())) alist = list(map(int,input().split())) town_loop = [1] for i in range(k): next_town = alist[town_loop[i]-1] try: loop_start = town_loop.index(next_town) break except ValueError: town_loop.append(next_town) if len(town_loop) == ...
n,k = list(map(int,input().split())) alist = list(map(int,input().split())) town_loop = [1] used = [-1] * n for i in range(k): next_town = alist[town_loop[i]-1] if used[next_town-1] == -1: town_loop.append(next_town) used[next_town-1] = 1 else: break loop_start =...
p02684
import sys n, k = list(map(int, input().split())) a = list(map(int, input().split())) b = [1] cnt = 0 next_town = 0 l = len(a) for j in range(l): cnt += 1 if j==0: b.append(a[0]) next_town = a[0] continue b.append(a[next_town - 1]) next_town = a[next_town -1...
N, K = list(map(int, input().split())) A = [int(a) - 1 for a in input().split()] if K <= N: p = 0 for i in range(K): p = A[p] print((p + 1)) exit() p = 0 t = [-1] * N t[0] = 0 for i in range(1, N): p = A[p] if t[p] != -1: break t[p] = i d = i - t[p] K -...
p02684
def main(): N_machi, K_teleport = list(map(int, input().split())) to_teleport = list(map(int, input().split())) def find_double_town(): visited_city = [0] first_twice = N_machi next_city = 0 for i in range(N_machi): next_city = to_t...
def main(): N_machi, K_teleport = list(map(int, input().split())) to_teleport = list(map(int, input().split())) visited_city = [1] order = [-1 for i in range(N_machi + 1)] order[1] = 0 next_city = 1 for i in range(N_machi): next_city = to_teleport[next_city - 1] ...
p02684
import sys n, k = list(map(int, input().split())) A = list(map(int, input().split())) L = [1] tmp = 1 ct = 0 check = 0 while True: x = A[tmp - 1] ct += 1 if x in L or ct > k: check = x break else: L.append(x) tmp = x if ct > k: print((L[k])) ...
n, k = list(map(int, input().split())) A = list(map(int, input().split())) visit = [-1] * n #到達しているかどうかを管理 D = [1] # 循環するもののindexを格納 tmp = A[0] #初期位置 ct = 1 while visit[tmp - 1] == -1: D.append(tmp) visit[tmp - 1] = ct #到達済に変更 if k == ct: print(tmp) exit() ct += ...
p02684
n,k=list(map(int,input().split())) l=list(map(int,input().split())) kiseki=[] u=1 kiseki.append(1) for i in l: u=l[u-1] if kiseki.count(u)==0: kiseki.append(u) else: kiseki.append(u) break kuri=kiseki[kiseki.index(u):len(kiseki)-1] if k<=len(kiseki)-(len(kuri)+1): print((kiseki[k])) e...
n,k=list(map(int,input().split())) l=list(map(int,input().split())) kiseki=[-1]*n i=0 v=1 while kiseki[v-1]==-1: kiseki[v-1]=i v=l[v-1] i+=1 saisyo=kiseki[v-1] kuri=i-saisyo if saisyo>k: print((kiseki.index(k)+1)) else: amari=(k-saisyo)%kuri print((kiseki.index(amari+saisyo)+1))
p02684
def main(): N, K = list(map(int, input().split())) A_list = input().split() root = [1] break_flag = 0 for n in range(K): new = A_list[ int(root[n])-1 ] if new not in root: root.append(new) else: ind = root.index(new) break else: print(new) break_flag =...
def main(): N, K = list(map(int, input().split())) A_list = input().split() root = [1] record = [-1]*N break_flag = 0 for n in range(K): new = int(A_list[ int(root[n])-1 ]) if record[new-1] <0: record[new-1]=1 root.append(new) else: ind = root.index(new) ...
p02684
n,k=list(map(int,input().split())) a = list(map(int,input().split())) rep = [] rep.append(1) i = 0 cnt = 0 if n>k: end =k else: end =n nof = 0 while True: cnt+=1 if cnt>end: nof=1 break go = a[i]-1 if a[i] not in rep: rep.append(a[i]) i = go ...
n,k=list(map(int,input().split())) a = list(map(int,input().split())) visit = [] visit = [0]*n pos=0 move=[] roop=[] while visit[pos]!=2: if visit[pos]==0: move.append(pos) else: roop.append(pos) visit[pos]+=1 pos = a[pos]-1 if len(move)>k: print((move[k]+1)) else: ...
p02684
N, K = list(map(int, input().split())) As = list(map(int, input().split())) ts = [] t = 0 for i in range(N): ts.append(t) t = As[t]-1 if t in ts: break d = ts.index(t) p = i - d + 1 if K > d: idx = (K - d) % p + d else: idx = K print((ts[idx]+1))
N, K = list(map(int, input().split())) As = list(map(int, input().split())) tl = [0] ts = {0} t = 0 for i in range(N): t = As[t]-1 if t in ts: break else: tl.append(t) ts.add(t) d = tl.index(t) p = i - d + 1 if K > d: idx = (K - d) % p + d else: idx = K...
p02684
n,k = list(map(int, input().split())) a = list(map(int, input().split())) idx=1 li=[] idx_li=[1] flag=False for i in range(k): idx=a[idx-1] if idx in idx_li: flag=True break idx_li.append(idx) if flag: #non_loop=idx_li[:idx_li.index(idx)] loop = idx_li[idx_li.index(idx)...
n,k = list(map(int, input().split())) a = list(map(int, input().split())) idx=1 idx_li=[] ord_=[-1]*n flag=False for i in range(k): idx_li.append(idx) ord_[idx-1]=1 idx=a[idx-1] if ord_[idx-1]==1: flag=True break if flag: #non_loop=idx_li[:idx_li.index(idx)] loop =...
p02684
N, K = list(map(int, input().split())) As = list(map(int, input().split())) path = [1] for _ in range(N+10): next_A = As[path[-1] - 1] if next_A in path: break else: path.append(next_A) before_loop = path.index(next_A) loop_len = len(path) - before_loop if K <= before_loop: ...
N, K = list(map(int, input().split())) As = list(map(int, input().split())) appeared = [False] * N appeared[0] = True path = [1] for _ in range(N+10): next_A = As[path[-1] - 1] if appeared[next_A-1]: break else: path.append(next_A) appeared[next_A-1] = True before_loo...
p02684
n, k = list(map(int, input().split())) a = list(map(int, input().split())) now=1 flg = 0 path = [1] pathcheck ={1} for i in range(k): now = a[now-1] if now in pathcheck: flg = -1 path.append(now) break path.append(now) pathcheck.add(now) if flg == 0: prin...
import sys readline = sys.stdin.buffer.readline def main(): n, k = list(map(int, input().split())) a = list(map(int, input().split())) now=1 flg = 0 path = [1] pathcheck ={1} for i in range(k): now = a[now-1] if now in pathcheck: flg = -1 ...
p02684
n, k = list(map(int, input().split())) an = list(map(int, input().split())) for i in range(n): an[i] -= 1 import math dv = [] dv.append(an) K = k N = n for _ in range(1, int(math.log2(K)) + 1): l = [0] * N dv.append(l) for k in range(1, int(math.log2(K)) + 1): for n in range(N): ...
N, K = list(map(int, input().split())) ai = list(map(int, input().split())) for i in range(N): ai[i] -= 1 import math dv = [] dv.append(ai) for k in range(1, int(math.log2(K)) + 1): l = [0] * N dv.append(l) for n in range(N): dv[k][n] = dv[k - 1][dv[k - 1][n]] a = [] ...
p02684
N, K = list(map(int, input().split())) ai = list(map(int, input().split())) for i in range(N): ai[i] -= 1 import math dv = [] dv.append(ai) for k in range(1, int(math.log2(K)) + 1): l = [0] * N dv.append(l) for n in range(N): dv[k][n] = dv[k - 1][dv[k - 1][n]] a = [] ...
N, K = list(map(int, input().split())) ai = list(map(int, input().split())) for i in range(N): ai[i] -= 1 import math dv = [] dv.append(ai) for k in range(1, int(math.log2(K)) + 1): l = [0] * N dv.append(l) for n in range(N): dv[k][n] = dv[k - 1][dv[k - 1][n]] #a = []...
p02684
import sys input = sys.stdin.readline from collections import * N, K = list(map(int, input().split())) A = list(map(int, input().split())) log_size = 100 dp = [[0]*N for _ in range(log_size)] for i in range(N): dp[0][i] = A[i]-1 for i in range(1, log_size): for j in range(N): dp[i][...
import sys input = sys.stdin.readline N, K = list(map(int, input().split())) A = list(map(int, input().split())) l = [0] s = {0} for i in range(K): nex = A[l[-1]]-1 if nex in s: for i in range(len(l)): if l[i]==nex: mark = i break ...
p02684
n,k = list(map(int,input().split())) a = [0]+list(map(int,input().split())) def bin(arr,val): left,right = 0,len(arr)-1 while left<=right: mid = (left+right)//2 if arr[mid]==val: return 1 elif arr[mid]<val: left = mid + 1 else: right = mid - 1 return 0 know = []...
n,k = list(map(int,input().split())) a = [0]+list(map(int,input().split())) def bin(arr,val): left,right = 0,len(arr)-1 while left<=right: mid = (left+right)//2 if arr[mid]==val: return 1 elif arr[mid]<val: left = mid + 1 else: right = mid - 1 return 0 know = []...
p02684
N, K = list(map(int, input().split())) towns = list(map(int, input().split())) towns.insert(0, -1) p = 1 tmp_move, first_move, roop_move = [1], [], [] res = -1 while True: if towns[p] in tmp_move: roop_move = tmp_move[tmp_move.index(towns[p]):] first_move = tmp_move[:tmp_move.index(towns[...
N, K = list(map(int, input().split())) towns = list(map(int, input().split())) towns.insert(0, -1) p = 1 tmp_move, first_move, roop_move = [1], [], [] visited = [False for _ in range(N + 1)] res = -1 while not visited[p]: visited[p] = True tmp_move.append(towns[p]) p = towns[p] index = tmp_mo...
p02684
import sys input = sys.stdin.readline from collections import Counter def main(): n, k = list(map(int, input().split())) a = list(map(int, input().split())) a = [i - 1 for i in a] ac = Counter(a) kaiten = [0] * n for i1 in range(n): if ac[i1] >= 1: kikan = n ...
import sys input = sys.stdin.readline def main(): n, k = list(map(int, input().split())) a = [0] ta = list(map(int, input().split())) a += ta kaiten = [0] * (n + 1) count = 0 koko = 1 kaitenkikan = 0 while k > 0: koko = a[koko] count += 1 k -= ...
p02684
import sys read = sys.stdin.read readlines = sys.stdin.readlines from collections import defaultdict def main(): n, k, *a = list(map(int, read().split())) cur_town = 0 transits = set() tran_time = defaultdict(int) cnt = 0 while True: if k == 0: print((cur_town +...
import sys read = sys.stdin.read readlines = sys.stdin.readlines from collections import defaultdict def main(): n, k, *a = list(map(int, read().split())) cur_town = 0 tran_time = defaultdict(int) cnt = 0 while True: if k == 0: print((cur_town + 1)) sys....
p02684
n,k = [int(i) for i in input().split()] data = [int(i) for i in input().split()] d = 1 count = 0 loop = [] route = [-1] * n data_len = len(data) for i in range(data_len): route[i] = d d = data[d-1] if d in route: loop = route[route.index(d):route.index(-1)] break loop_len = ...
n, k = list(map(int, input().split())) a = list(map(int, input().split())) visited = [0] for _ in range(n): now = a[visited[-1]]-1 visited.append(now) if k < n: print((visited[k]+1)) else: p = visited.index(visited[-1]) loop = visited[::-1][1:].index(visited[-1])+1 x = (k-p-1)%loop ...
p02684
def main(): N, K, *A = list(map(int, open(0).read().split())) A = [0] + A cur = 1 visited = [-1] * (N + 1) visited[cur] = 0 for i in range(1, K + 1): next = A[cur] if cur == next: print(next) return elif (j := visited[next]) != -1: ...
def main(): N, K, *A = list(map(int, open(0).read().split())) A = [0] + A cur_pos = 1 last_visit = [-1] * (N + 1) last_visit[cur_pos] = 0 for i in range(1, K + 1): nxt_pos = A[cur_pos] if cur_pos == nxt_pos: print(nxt_pos) return elif (...
p02684
n, k = list(map(int, input().split())) to = [0] to += list(map(int, input().split())) circle = [1] st = -1 while True: point = to[circle[-1]] if point in circle: st = point break circle.append(point) l1 = circle.index(st) l2 = len(circle) - l1 if k < l1: print((circle[k])) elif ...
n, k = list(map(int, input().split())) to = [0] to += list(map(int, input().split())) passed = [0] * (n + 1) circle = [1] st = -1 while True: nx = to[circle[-1]] if passed[nx] == 1: st = nx break circle.append(nx) passed[nx] = 1 l1 = circle.index(st) l2 = len(circle) - l1 if...
p02684
def calc(i, k): ans_list = [i] count = 1 i = a[i] while (i not in ans_list) & (count < n): ans_list.append(i) count += 1 i = a[i] index = ans_list.index(i) ans_list.append(i) return count, ans_list n, k = list(map(int, input().split())) a = [int(i) - 1 f...
n, k = list(map(int, input().split())) a = [int(i) - 1 for i in input().split()] visit = {0} tmp = 0 a_list = [0] ans = True for i in range(k): tmp = a[tmp] if tmp in visit: left = a_list.index(tmp) ans = False print((a_list[left + (k - left) % (i + 1 - left)] + 1)) b...
p02684
n,k = list(map(int,input().split())) mp = list(map(int,input().split())) save = [] s_queue = [[1,0]] while(len(s_queue)>0): i,cnt = s_queue.pop() if(cnt==k): break if(mp[i-1] in save): last=mp[i-1] break save.append(mp[i-1]) s_queue.append([mp[i-1],cnt+1]) ...
n,k = list(map(int,input().split())) mp = list(map(int,input().split())) save = [] isSaved = [False]*(n+1) s_queue = [[1,0]] while(len(s_queue)>0): i,cnt = s_queue.pop() if(cnt==k): break if(isSaved[i]): last=mp[i-1] break nexti = mp[i-1] isSaved[i] = True s...
p02684
n,k = list(map(int,input().split())) mp = list(map(int,input().split())) save = [] isSaved = [False]*(n+1) s_queue = [[1,0]] while(len(s_queue)>0): i,cnt = s_queue.pop() if(cnt==k): break if(isSaved[i]): last=mp[i-1] break nexti = mp[i-1] isSaved[i] = True s...
n,k = list(map(int,input().split())) A = [int(x)-1 for x in input().split()] isSearched = [False]*n isSearched[0] = True ans = [] def f(): global k now = 0 nex = A[0] ans.append(now) for i in range(k): now = nex ans.append(now) isSearched[now] = True n...
p02684
n,k=list(map(int,input().split())) a=list(map(int,input().split())) start=1 list=[1] kenti=[0 for i in range(n)] kenti[0]=1 zan=-1 for i in range(k): start-=1 next=a[start] start=next if kenti[start-1]!=0: zan=k-(i+1) inx=list.index(start) list=list[inx:] break else: ...
n,k=list(map(int,input().split())) a=list(map(int,input().split())) start=1 list=[1] kenti=[0 for i in range(n)] kenti[0]=1 zan=-1 for i in range(k): start-=1 next=a[start] start=next if kenti[start-1]!=0: zan=k-(i+1) inx=list.index(start) list=list[inx:] break else: ...
p02684
from sys import stdin nii=lambda:list(map(int,stdin.readline().split())) lnii=lambda:list(map(int,stdin.readline().split())) n,k=nii() a=lnii() x=0 x_list=[1] for i in range(k): nx=a[x] if nx in x_list: inx=x_list.index(nx) loop=x_list[inx:] zan=k-i q=zan%len(loop) nx=l...
from sys import stdin nii=lambda:list(map(int,stdin.readline().split())) lnii=lambda:list(map(int,stdin.readline().split())) n,k=nii() a=lnii() x=0 x_list=[1] x_table=[0 for i in range(n+1)] x_table[1]=1 for i in range(k): nx=a[x] if x_table[nx]!=0: inx=x_list.index(nx) loop=x_list[inx:...
p02684
import sys n,k=list(map(int,input().split())) a=[int(x) for x in input().split()] vis=[0] now=0 for i in range(1,k+1): now=a[now]-1 if now in vis: bgn=vis.index(now) print((vis[bgn+((k-bgn)%(len(vis)-bgn))]+1)) sys.exit() else: vis.append(now) print((vis[-1]+1))
import sys n,k=list(map(int,input().split())) a=[int(x) for x in input().split()] vis_ti=[-1]*n vis_at=[0] now=0 for i in range(1,k+1): now=a[now]-1 if vis_ti[now]>=0: bgn=vis_ti[now] print((vis_at[bgn+(k-bgn)%(i-bgn)]+1)) sys.exit() vis_ti[now]=i vis_at.append(now) print((vis_at[-1]+1...
p02684
n, k = list(map(int, input().split())) port = list(map(int, input().split())) visited = [1] current = 1 t = k while port[current-1] not in visited and t > 0: visited.append(port[current-1]) current = port[current-1] t -= 1 if t > 0: s = visited.index(port[current-1]) bubun = visited...
n, k = list(map(int, input().split())) port = list(map(int, input().split())) visited = [1] flag = [False for i in range(n)] current = 1 t = k while flag[port[current-1]-1] == False and t > 0: visited.append(port[current-1]) flag[port[current-1]-1] = True current = port[current-1] t -= 1 ...
p02684
N,K=list(map(int,input().split())) A=list(map(int,input().split())) genzaichi=1 rireki=[] houmon=[0]*(N+1) flag=0 for i in range(1,K+1): genzaichi=A[genzaichi-1] if genzaichi not in rireki: houmon[genzaichi]=i rireki.append(genzaichi) else: roop_from=genzaichi ...
N,K=list(map(int,input().split())) A=list(map(int,input().split())) genzaichi=1 rireki=set() houmon=[0]*(N+1) flag=0 for i in range(1,K+1): genzaichi=A[genzaichi-1] if genzaichi not in rireki: houmon[genzaichi]=i rireki.add(genzaichi) else: roop_from=genzaichi ...
p02684
def ma(): return list(map(int,input().split())) y="Yes" t="No" n,k=ma() A=list(ma()) ans=[] now=1 while True: ans.append(now) now=A[now-1] if now in ans: use=ans.index(now) tool=len(ans)-use break if use>=k: print((ans[k])) exit() else: p=(k-use)...
def ma(): return list(map(int,input().split())) def main(): n,k=ma() A=list(ma()) ans=[] exp=[0]*(n+1) now=1 while True: ans.append(now) exp[now]=1 now=A[now-1] if exp[now]==1: use=ans.index(now) tool=len(ans)-use ...
p02684
N, K = list(map(int, input().split())) A = [int(i) for i in input().split()] from_ = 1 way = [] way.append(1) for i in range(K): if(A[from_ - 1] in way): way.append(A[from_ - 1]) break else: way.append(A[from_ - 1]) from_ = A[from_ - 1] key_from = way.index(wa...
N, K = list(map(int, input().split())) A = [int(i) for i in input().split()] from_ = 1 way = [] way.append(1) way_set = set([1]) for i in range(K): s = A[from_ - 1] if(s in way_set): way.append(s) break else: way.append(s) way_set.add(s) from_ = ...
p02684
N, K = list(map(int,input().split())) ls_pass = [0]+list(map(int,input().split())) a=1 b=0 c=0 d=0 ls_past=[1] ls_cicle=[] for i in range(1,N*3): b=a a=ls_pass[a] if b==a: print(a) break elif a in ls_past: if c==0: c=i d=a ls_cicle.append(d) continue e...
N, K = list(map(int,input().split())) ls_pass = [0]+list(map(int,input().split())) a=1 b=0 ls=["ンゴ"]+[0]+["ンゴ"]*(N-1) ls_past=[1] for i in range(1,N*3): b=a a=ls_pass[a] if ls[a]!="ンゴ": ls_cicle=ls_past[ls[a]:i] print((ls_cicle[(K-i)%len(ls_cicle)])) break elif i==K: print(a) ...
p02684
#!/usr/bin python3 # -*- coding: utf-8 -*- import sys def main(): N, K = list(map(int, input().split())) A = list(map(int, input().split())) nw = 1 lp0 = [1] for i in range(N): nw = A[nw-1] if nw in lp0: break else: lp0.append(nw) ...
#!/usr/bin python3 # -*- coding: utf-8 -*- import sys def main(): N, K = list(map(int, input().split())) A = list(map(int, input().split())) nw = 1 lp0 = [1] lpl = {1} for i in range(N): nw = A[nw-1] if nw in lpl: break else: lp...
p02684
def parse(): N, K = list(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...
def parse(): N, K = list(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...
p02684
n, k = list(map(int, input().split())) a = list(map(int, input().split())) h = [] i = 0 s = 1 ans = 0 while i < k: g = a[s-1] if g not in h: h.append(g) s = g i+=1 else: c = h.index(g) l = (k-1-c) % (i-c) ans = h[c+l] break if ans == 0: ans = h[-1] ...
n, k = list(map(int, input().split())) a = list(map(int, input().split())) s = 1 g = 0 i = 1 history = [s] flag = [False] * n flag[0] = True while True: g = a[s-1] if flag[g-1] == True: break else: flag[g-1] = True history.append(g) s = g i += 1 ...
p02684
def main(): n, k, *a = list(map(int, open(0).read().split())) a = [0] + a root = [1] visited = [False for _ in range(n+1)] nx = 1 visited[1] = True step = 0 while True: step += 1 now = nx nx = a[now] if step == k: print(nx) ...
def main(): n, k, *a = list(map(int, open(0).read().split())) a = [0] + a check = [0] * (n + 1) check[1] = 1 i = 0 nw = 1 path = [1] while 1: nx = a[nw] i += 1 if i == k: print(nx) break if check[nx]: j ...
p02684
def main(): n, k, *a = list(map(int, open(0).read().split())) a = [0] + a check = [0] * (n + 1) check[1] = 1 i = 0 nw = 1 path = [1] while 1: nx = a[nw] i += 1 if i == k: print(nx) break if check[nx]: j ...
def main(): import sys n, k, *a = list(map(int, sys.stdin.read().split())) a = [0] + a check = [0] * (n + 1) check[1] = 1 i = 0 nw = 1 path = [1] while 1: nx = a[nw] i += 1 if i == k: print(nx) break if check[nx...
p02684
def main(): import sys n, k, *a = list(map(int, sys.stdin.read().split())) a = [0] + a check = [0] * (n + 1) check[1] = 1 i = 0 nw = 1 path = [1] while 1: nx = a[nw] i += 1 if i == k: print(nx) break if check[nx...
def main(): import sys n, k, *a = list(map(int, sys.stdin.read().split())) a = [0] + a check = [0] * (n + 1) check[1] = 1 i = 0 nw = 1 path = [1] while True: nx = a[nw] i += 1 if i == k: print(nx) break if check...
p02684
n, k = list(map(int, input().split())) A = list(map(int, input().split())) route = [1] dest = A[0] while dest not in route: route.append(dest) dest = A[dest - 1] ind = route.index(dest) cycle = route[ind:] if k >= ind: ans = (k - ind) % len(cycle) print((cycle[ans])) else: print((route[...
n, k = list(map(int, input().split())) A = list(map(int, input().split())) route = [1] dest = A[0] check = {i: False for i in range(n)} while not check[dest - 1]: check[dest - 1] = True route.append(dest) dest = A[dest - 1] ind = route.index(dest) cycle = route[ind:] if k >= ind: ans = (k - ...
p02684
n,k=list(map(int,input().split())) a=[0]+list(map(int,input().split())) MAX_N=2*10**5 b=[1] visited=[-1]*(n+10) for i in range(1,2*MAX_N+1): nxt=a[b[-1]] b.append(nxt) if k<=n: print((b[k])) exit() # print(b) for i,bi in enumerate(b): if visited[bi]==-1: visited[b...
n,k,*a=list(map(int,open(0).read().split())) now=1 if k<=n: for i in range(k): now=a[now-1] print(now) exit() # 絶対ループする visited=[False]*n visited[now-1]=-1 for i in range(4*10**5): now=a[now-1] if visited[now-1]==False: visited[now-1]=i else: c,d=visited[now-1],i break k...
p02684
#!/usr/bin/env python3 import itertools n, k = list(map(int, input().split())) a = list(map(int, input().split())) ans = [1] if k > len(list(itertools.permutations(a))): for i in range(k % n): ans.append(a[ans[i] - 1]) print((a[-1])) else: for i in range(k): ans.append(a[ans[i] - ...
#!/usr/bin/env python3 n, k = list(map(int, input().split())) a = list(map(int, input().split())) ans = [1] loop_start = 0 loop = 0 for i in range(n): ans.append(a[ans[i] - 1]) for j in ans: if j in ans[:loop_start] or j in ans[loop_start+1:]: break loop_start += 1 loop = ans[loop_start+...
p02684
#!/usr/bin/env python3 n, k = list(map(int, input().split())) a = list(map(int, input().split())) ans = [1] loop_start = 0 loop = 0 for i in range(n): ans.append(a[ans[i] - 1]) for j in ans: if j in ans[:loop_start] or j in ans[loop_start+1:]: break loop_start += 1 loop = ans[loop_start+...
#!/usr/bin/env python3 n, k = list(map(int, input().split())) a = list(map(int, input().split())) ans = [1] c = 1 for i in range(n): ans.append(a[ans[i] - 1]) b = ans.index(ans[n]) if k < b: for l in range(k): c = a[c - 1] else: for l in range((k-b)%(n-b)+b): c = a[c - 1] print...
p02684
n, k = list(map(int, input().split())) a = list(map(int, input().split())) memo = [] memo_cir = [] index = 0 flag = True while flag: if index in memo: flag = False else: memo.append(index) index = a[index] - 1 flag = False for i_ in memo: if i_ == index: flag =...
n, k = list(map(int, input().split())) a = list(map(int, input().split())) memo = [] memo_cir = [] visited = [False] * n index = 0 flag = True while flag: if visited[index]: flag = False else: memo.append(index) visited[index] = True index = a[index] - 1 flag = Fals...
p02684
N, K = list(map(int, input().split())) a = [i - 1 for i in list(map(int, input().split()))] logK = 1 while ((1 << logK) < K): logK += 1 doubling = [[-1 for _ in range(N)] for _ in range(logK)] for i in range(N): doubling[0][i] = a[i] for i in range(logK - 1): for j in range(N): doubling[i ...
N, K = list(map(int, input().split())) a = [i - 1 for i in list(map(int, input().split()))] logK = K.bit_length() doubling = [[-1 for _ in range(N)] for _ in range(logK)] for i in range(N): doubling[0][i] = a[i] for i in range(logK - 1): for j in range(N): doubling[i + 1][j] = doubling[i][doubli...
p02684
n, k = list(map(int, input().split())) a = [0] + list(map(int, input().split())) cheak = [0] * (2*(10**5) + 1) point = 1 dev = [1] flag = 0 for i in range(1, k + 1): if cheak[point]: loop = i -cheak[point] pre_loop = cheak[point] flag = 1 break else: ...
n, k = list(map(int, input().split())) a = [0] + list(map(int, input().split())) cheak = [0] * (2*(10**5) + 1) point = 1 dev = [1] flag = 0 for i in range(1, k + 1): if cheak[point]: loop = i -cheak[point] pre_loop = cheak[point] flag = 1 break else: ...
p02684
N, K = list(map(int, input().split())) A_list = list(map(int, input().split())) place = [0] while True: next_place = A_list[place[-1]] - 1 if next_place in place: idx = place.index(next_place) loop = place[idx:] break place.append(next_place) if K < len(place): p...
N, K = list(map(int, input().split())) A_list = list(map(int, input().split())) place = [0] table = [0] * N table[0] = 1 while True: next_place = A_list[place[-1]] - 1 if table[next_place] == 1: idx = place.index(next_place) loop = place[idx:] break table[next_place] =...
p02684
n,k = list(map(int, input().split())) A = tuple(map(int,input().split())) import sys sys.setrecursionlimit(1000000000) from collections import deque seen = [0 for i in range(n)] hist = deque([]) # 頂点の訪問履歴 pos = -1 # サイクルの1成分 -1はサイクル未発見 def dfs(x, p): # pはxの親 global pos # global!!!!!!!!!!!...
n,k = list(map(int ,input().split())) A = tuple(map(int, input().split())) seen = [-1]*n tmp = 0 count = 0 while True: tmp = A[tmp]-1 count +=1 if seen[tmp] != -1: seennum = seen.count(1) tm = tmp se = [-1]*n roopmem = [] while True: if...
p02684
N,K = list(map(int,input().split())) A = list(map(int, input().split())) l = [1] now_town = 1 loop_town = 0 for i in range(N): now_town = A[now_town-1] if now_town in l: loop_town = l.index(now_town) break else: l.append(now_town) loop_num = len(l)-loop_town if K >...
N,K = list(map(int,input().split())) A = list(map(int, input().split())) l = [1] kanryou_town = [0]*(N+1) kanryou_town[1] = 1 now_town = 1 loop_town = 0 for i in range(N): now_town = A[now_town-1] if kanryou_town[now_town] == 1: loop_town = l.index(now_town) break else: ...
p02684
data = input().split() N, k = int(data[0]), int(data[1]) tab = [] data = input().split() for i in range(N): tab.append(int(data[i]) - 1) parcour = [] position = 0 while (not (position in parcour)): parcour.append(position) position = tab[position] i = 0 while parcour[i] != ...
data = input().split() N, k = int(data[0]), int(data[1]) tab = [] data = input().split() for i in range(N): tab.append(int(data[i]) - 1) parcour = [] tmp = [0] * N position = 0 while (tmp[position] == 0): parcour.append(position) tmp[position] = 1 position = tab[positio...
p02684
n,k = list(map(int,input().split())) L = list(map(int,input().split())) X = [0]*(n+1) X[0] = 1 for i in range(n): if L[X[i]-1] in X: X[i+1] = L[X[i]-1] break X[i+1] = L[X[i]-1] def f(lst,value): return [i for i,x in enumerate(lst) if x == value] for x in X: if X.count(x) == 2: a...
n,k = list(map(int,input().split())) lst = list(map(int,input().split())) loop = [] mark = ['t'] *n i = 0 loop.append(1) mark[0] = 0 while True: if mark[lst[i]-1] != 't': start = mark[lst[i]-1] last = mark[i] break else: mark[lst[i]-1] = mark[i] + 1 loop.append(lst[i]) i = lst...
p02684
def roop(now, a_list): return a_list[now-1] if __name__ == '__main__': n, k = list(map(int, input().split())) a_list = list(map(int, input().split())) now = 1 check = [1] for i in range(k): now = roop(now, a_list) if now not in check: check.append(no...
if __name__ == '__main__': n, k = list(map(int, input().split())) a_list = list(map(int, input().split())) now = 1 check = [0] * n check[0] = 1 move = [1] for i in range(k): now = a_list[now-1] move.append(now) if check[now-1] == 0: check[...
p02684
import sys input = lambda: sys.stdin.readline() def cin_int_list(): return [int(x) for x in input().split()] def cin_int_iter(): return (int(x) for x in input().split()) def cin_int(): return int(eval(input())) def cout_int_iter(a): print((' '.join(map(str, a)))) def iota...
import sys input = lambda: sys.stdin.readline() def cin_int_list(): return [int(x) for x in input().split()] def cin_int_iter(): return (int(x) for x in input().split()) def cin_int(): return int(eval(input())) def cout_int_iter(a): print((' '.join(map(str, a)))) def iota...
p02684
n, k = list(map(int, input().split())) a = list(map(int, input().split())) towns = [0] * n i = 0 cnt = 1 towns[0] = cnt nums = [1] while True: cnt += 1 i = a[i] - 1 if towns[i] == 0: towns[i] = cnt nums.append(i + 1) else: s_loop = nums.index(i + 1) m = l...
n, k = list(map(int, input().split())) a = list(map(int, input().split())) flag = [0] * n i = 0 loop = [1] while True: i = a[i] - 1 if flag[i] == 0: flag[i] = 1 loop.append(i + 1) else: start = loop.index(i + 1) m = len(loop) length = m - start ...
p02684
n,k=list(map(int,input().split())) arr=list(map(int,input().split())) for i in range(n): arr[i]-=1 flag=format(k,'b') flag=flag[::-1] pos=0 for i in range(len(flag)): if flag[i]=='1': pos=arr[pos] tmp=[] for j in range(n): tmp.append(arr[arr[j]]) arr=tmp print((pos+1))
n,k=list(map(int,input().split())) arr=list(map(int,input().split())) l=[-1]*(n+1) tmp=[] pos=1 cnt=0 while 1: if l[pos]!=-1: t=cnt-l[pos] if k<cnt: print((tmp[k])) else: print((tmp[l[pos]+(k-cnt)%t])) break l[pos]=cnt cnt+=1 tmp.append(pos) pos=arr[pos-1]
p02684
# -*- coding: utf-8 -*- import sys (N, K) = (int(i) for i in input().split()) A = [int(i) for i in input().split()] # 一周する数を求める have_been = [1] now_place = 1 while True: next_place = A[now_place - 1] if next_place not in have_been: have_been.append(next_place) now_place = next_p...
# -*- coding: utf-8 -*- import sys (N, K) = (int(i) for i in input().split()) A = [int(i) for i in input().split()] # 一周する数を求める have_been = set() have_been_list = [] have_been.add(1) have_been_list.append(1) now_place = 1 while True: next_place = A[now_place - 1] if next_place not in have_been...
p02684
N, K = list(map(int, input().split())) A = list(map(int, input().split())) index = 1 route = [index] node = [index] for i in range(1, K + 1): index = A[index - 1] if index in node: pre = route.index(index) loop = len(route) route_idx = (K - pre) % (loop - pre) + pre ...
N, K = list(map(int, input().split())) A = list(map(int, input().split())) index = 1 route = [index] node = {index} for i in range(1, K + 1): index = A[index - 1] if index in node: pre = route.index(index) loop = len(route) route_idx = (K - pre) % (loop - pre) + pre ...
p02684
import array n, k = list(map(int, input().split())) tel = array.array('i', list(map(int, input().split()))) log = array.array('i', [1]) end = True while(end): log.append(tel[log[-1] - 1]) for i in range(len(log) - 1): if(log[i] == log[-1]): star = i mod = len(log) - sta...
n, k = list(map(int, input().split())) tel = list(map(int, input().split())) log = [1] record = [0] * n while(True): log.append(tel[log[-1] - 1]) if(record[log[-1] - 1] == 1): break record[log[-1] - 1] += 1 for i in range(len(log) - 1): if(log[i] == log[-1]): star = i ...
p02684
import bisect n, k = list(map(int, input().split())) a = list(map(int, input().split())) seen = [] seen_sort = [1] start = 1 while True: tmp = a[start - 1] seen.append(start) index = bisect.bisect_left(seen_sort, tmp) if index >= len(seen_sort) or seen_sort[index] != tmp: seen_sort....
n, k = list(map(int, input().split())) a = list(map(int, input().split())) visited = [] visited_index = [-1] * n pre = 0 while visited_index[pre] == -1: now = a[pre] visited.append(pre) visited_index[pre] = len(visited) pre = now - 1 loop = len(visited) - visited_index[pre] + 1 head = vis...
p02684
N, K = list(map(int, input().split())) A = list(map(int, input().split())) zumi = [-1]*(2*(10**5)) ima = 1 for i in range(K): # 行ったことある町を通る=ループする if ima in zumi: zumi[i] = ima break # 同じ町へテレポートしている if ima == A[ima-1]: print(ima) exit(0) zumi[i] = ima ...
N, K = list(map(int, input().split())) A = list(map(int, input().split())) zumi_hash = {} zumi = [-1]*(2*(10**5)) ima = 1 for i in range(K): # 行ったことある町を通る=ループする if ima in zumi_hash: zumi[i] = ima break # 同じ町へテレポートしている if ima == A[ima-1]: print(ima) exit(0) ...
p02684
N,K = list(map(int, input().split())) A = list(map(int, input().split())) loc = 1 loclog=[1] flag = 0 for i in range(K): loc = A[loc-1] if loc in loclog: roopstart = loclog.index(loc) roopend = len(loclog) flag = 1 break loclog.append(loc) if flag == 0: ...
N,K = list(map(int, input().split())) A = list(map(int, input().split())) loc = 1 loclog=[1] flag = 0 hist = [0]*N for i in range(K): hist[loc-1] = 1 loc = A[loc-1] if hist[loc-1] != 0: roopstart = loclog.index(loc) roopend = len(loclog) flag = 1 break l...
p02684
n,k=list(map(int,input().split())) a=list(map(int,input().split())) dict={} for i in range(n): dict[i]=a[i]-1 #i番目の町からどこにつくか tmp=0#現在知 num2city={} city2num={} city2num[0]=0#どのまちが何回目か num2city[0]=0#j回目にどこにいるか for j in range(1,k+1): if tmp==dict[tmp]: break tmp=dict[tmp]#現在地更新 if tmp...
n,k=list(map(int,input().split())) a=list(map(int,input().split())) dict={} for i in range(n): dict[i]=a[i]-1 #i番目の町からどこにつくか tmp=0#現在知 num2city={} city2num={} city2num[0]=0#どのまちが何回目か num2city[0]=0#j回目にどこにいるか alre=[-1]*n alre[0]=1 for j in range(1,k+1): if tmp==dict[tmp]: break tmp=dic...
p02684
n, k = list(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 < start_cycle: print((c...
n, k = list(map(int,input().split())) a = list(map(int,input().split())) s = [False] * n c = [1] now = 0 while not s[now]: s[now] = True now = a[now] - 1 c.append(now+1) start_cycle = c.index(c[-1]) loop = c[start_cycle:-1] cycle = len(loop) if k < start_cycle: print((c[k])) else: ...
p02684
N, K = list(map(int, input().split())) A = list(map(int, input().split())) current = 1 path = [1] while K > 0: if A[current - 1] in path: loop = path[path.index(A[current - 1]):] path += [loop[(K-1) % len(loop)]] break path += [A[current - 1]] current = A[current - 1]...
N, K = list(map(int, input().split())) A = list(map(int, input().split())) current = 1 path = [1] already = [False] * len(A) while K > 0: if already[current - 1]: loop = path[path.index(A[current - 1]):] current = loop[(K - 1) % len(loop)] break path += [A[current - 1]] ...
p02684
n,k=list(map(int,input().split())) a=list([int(x)-1 for x in input().split()]) town=[0] i=0 while True: idx=town[-1] if a[idx] in town: s=a[idx] i+=1 break else: i+=1 town.append(a[idx]) if i!=n: idp=town.index(s) chain=len(town[:idp]) cycle=len(town[idp:]) if k<=chain...
n,k=list(map(int,input().split())) a=list([int(x)-1 for x in input().split()]) town=[0] set={0} i=0 while True: idx=town[-1] if a[idx] in set: s=a[idx] i+=1 break else: i+=1 town.append(a[idx]) set.add(a[idx]) if i!=n: idp=town.index(s) chain=len(town[:idp]) cycle=l...
p02684
import sys;input=lambda:sys.stdin.readline().rstrip();aint=lambda:int(input());ints=lambda:list(map(int,input().split())) import math;floor,ceil=lambda x:int(math.floor(x)),lambda x:int(math.ceil(x)) from functools import reduce;gcd=lambda*x:reduce(math.gcd,x);lcm=lambda*x:reduce(lambda x,y:(x*y)//gcd(x,y),x,1) from...
import sys;input=lambda:sys.stdin.readline().rstrip();aint=lambda:int(input());ints=lambda:list(map(int,input().split())) import math;floor,ceil=lambda x:int(math.floor(x)),lambda x:int(math.ceil(x)) from functools import reduce;gcd=lambda*x:reduce(math.gcd,x);lcm=lambda*x:reduce(lambda x,y:(x*y)//gcd(x,y),x,1) from...
p02684
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines N,K=list(map(int,readline().split())) A=list([int(x)-1 for x in readline().split()]) visited=[True]*N visitednum=[] now=0 cnt=0 while True: if visited[now]==False: break e...
n, k = list(map(int, input().split())) a = list(map(int, input().split())) num = 1 li = [1] flag = [True] * n flag[0] = False for i in range(k): num = a[num-1] if flag[num-1]: li.append(num) flag[num-1] = False else: break d = li.index(num) ans = (k-d)%(len(li)-d)...
p02684
def ans(n, k, list_a): i=0 list_memo=[1] while i<k: next = list_a[list_memo[i]-1] if next not in list_memo: list_memo.append(next) else: num=list_memo.index(next) len_list=len(list_memo) i+=1 x = len_list-num ...
def ans(n, k, list_a): i=0 dic={} dic[1]=1 list_memo=[1] while i<k: next = list_a[list_memo[i]-1] if next not in dic: list_memo.append(next) dic[next]=1 else: num=list_memo.index(next) len_list=len(list_memo) ...
p02684
n, k = list(map(int, input().split())) A=list(map(int, input().split())) c=1 path=[] d={} l=0 while l<=k: if c in path: p=d[c] #print(p, path) pattern_l = l-p k-=((k-p)//pattern_l)*pattern_l path.append(c) l+=1 d[c]=l-1 c=A[c-1] print((path[k]))
n, k = list(map(int, input().split())) A=list(map(int, input().split())) c=1 path=[] d={} l=0 while l<=k: if c in d: p=d[c] #print(p, path) pattern_l = l-p k-=((k-p)//pattern_l)*pattern_l path.append(c) l+=1 d[c]=l-1 c=A[c-1] print((path[k]))
p02684
n,k=list(map(int,input().split())) A=list(map(int,input().split())) cnt=0 s=1 M=[0] B=[0]*(n+1) while cnt<n: s=A[s-1] if B[s-1]==0: B[s-1]+=1 else: imp=s-1 break M.append(s-1) cnt+=1 x=M.index(imp) y=len(M)-x if k<=x: print((M[k]+1)) else: N=M[x:...
n,k=list(map(int,input().split())) A=list(map(int,input().split())) cnt=0 s=1 M=[0] B=[False]*(n+1) while cnt<n: s=A[s-1] if not B[s-1]: B[s-1]=True else: break M.append(s-1) cnt+=1 x=M.index(s-1) y=len(M)-x if k<=x: print((M[k]+1)) else: N=M[x:] pri...
p02684