problem_id
stringlengths
6
6
user_id
stringlengths
10
10
time_limit
float64
1k
8k
memory_limit
float64
262k
1.05M
problem_description
stringlengths
48
1.55k
codes
stringlengths
35
98.9k
status
stringlengths
28
1.7k
submission_ids
stringlengths
28
1.41k
memories
stringlengths
13
808
cpu_times
stringlengths
11
610
code_sizes
stringlengths
7
505
p02684
u271469978
2,000
1,048,576
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.
['import numpy as np\nN, M, X = map(int, input().split())\nA = []\nC = []\nfor _ in range(N):\n c, *a = map(int, input().split())\n C.append(c)\n A.append(a)\nA = np.array(A,dtype=int)\nans = 10 ** 7\nfor bit in range(2 ** N):\n B = np.zeros(M, dtype=int)\n cost = 0\n for i in range(N):\n if bit & (1<<i):\n B += A[i]\n cost += C[i]\n if np.all(B >= X):\n ans = min(ans, cost)\nprint(ans if ans < 10**7 else -1)\n', 'N, K = map(int, input().split())\nG = [[] for _ in range(N)]\nfor i, a in enumerate(map(int, input().split())):\n G[i] = a - 1\nnow = 0\nseen = set()\n\nfor i in range(1, K+1):\n seen.add(now)\n now = G[now]\n if now in seen:\n break\nK -= i\nif K > 0:\n \n seen = set()\n for j in range(1,K+1):\n seen.add(now)\n now = G[now]\n if now in seen:\n break\n \n r = K % j\n for _ in range(r):\n now = G[now]\nprint(now + 1)\n']
['Runtime Error', 'Accepted']
['s054148308', 's054861567']
[27064.0, 38916.0]
[119.0, 211.0]
[449, 635]
p02684
u279266699
2,000
1,048,576
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.
['n, k = map(int, input().split())\na = list(map(lambda x: int(x) - 1, input().split()))\nvisited = [-1 for _ in range(n)]\nvisited[0] = 0\nl = [0]\nfor i in range(k):\n nex = a[l[i]]\n if visited[nex] != -1:\n res = visited[nex]\n cyc = i + 1 - visited[nex]\n break\n else:\n l += [nex]s\n visited[nex] = i + 1\nelse:\n res = k + 1\nprint(l[res + (k - res) % cyc] + 1 if k > res else l[k] + 1)\n', 'n, k = map(int, input().split())\na = list(map(lambda x: int(x) - 1, input().split()))\nvisited = [-1 for _ in range(n)]\nvisited[0] = 0\nl = [0]\nfor i in range(k):\n nex = a[l[i]]\n if visited[nex] != -1:\n res = visited[nex]\n cyc = i + 1 - visited[nex]\n break\n else:\n l += [nex]s\n visited[nex] = i + 1\nprint(l[res + (k - res) % cyc] + 1 if k > res else l[k] + 1)\n', 'n, k = map(int, input().split())\na = list(map(lambda x: int(x) - 1, input().split()))\nvisited = [-1 for _ in range(n)]\nvisited[0] = 0\nl = [0]\nfor i in range(k):\n nex = a[l[i]]\n if visited[nex] != -1:\n res = visited[nex]\n cyc = i + 1 - visited[nex]\n break\n else:\n l += [nex]s\n visited[nex] = i + 1\nelse:\n res = k + 1\nprint(l[res + (k - res) % cyc] + 1 if k > res else l[k] + 1)\n', 'n, k = map(int, input().split())\na = list(map(lambda x: int(x) - 1, input().split()))\nvisited = [-1 for _ in range(n)]\nvisited[0] = 0\nl = [0]\nfor i in range(k):\n nex = a[l[i]]\n if visited[nex] != -1:\n res = visited[nex]\n cyc = i + 1 - visited[nex]\n break\n else:\n l += [nex]\n visited[nex] = i + 1\nelse:\n res = k + 1\nprint(l[res + (k - res) % cyc] + 1 if k > res else l[k] + 1)\n']
['Runtime Error', 'Runtime Error', 'Runtime Error', 'Accepted']
['s320513985', 's618678321', 's641836118', 's141780643']
[8988.0, 8996.0, 9048.0, 32388.0]
[22.0, 25.0, 23.0, 178.0]
[424, 402, 424, 423]
p02684
u288430479
2,000
1,048,576
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.
['n,k = map(int,input().split())\nk += 1\nl1 = list(map(int,input().split()))\nl = list(i-1 for i in l1)\nbit = list(0 for i in range(n))\nbit[0] =1\nmi = [0]\nm = l[0]\ner = 0\nwhile er ==0:\n if bit[m]==1:\n a = mi.index(m)\n r = mi[a:]\n er =1\n else:\n bit[m] = 1\n mi.append(m)\n m = l[m]\n # print(bit)\nbe = mi[:a]+r\n#print(be,r)\nif k<= len(be):\n print(be[k])\nelse:\n w = k-len(be)-1\n u = (w%len(r))-1\n print(r[u])', 'n,k = map(int,input().split())\nl1 = list(map(int,input().split()))\nl = list(i-1 for i in l1)\nbit = list(0 for i in range(n))\nbit[0] =1\nmi = [0]\nm = l[0]\ner = 0\nwhile er ==0:\n if bit[m]==1:\n a = mi.index(m)\n r = mi[a:]\n er =1\n else:\n bit[m] = 1\n mi.append(m)\n m = l[m]\n # print(bit)\nbe = mi[:a]+r\nprint(be,r)\nif k< len(be):\n print(be[k]+1)\nelse:\n w = k-len(be)+1\n u = (w%len(r))-1\n print(r[u]+1)', 'n,k = map(int,input().split())\nl1 = list(map(int,input().split()))\nl = list(i-1 for i in l1)\nbit = list(0 for i in range(n))\nbit[0] =1\nmi = [0]\nm = l[0]\ner = 0\nwhile er ==0:\n if bit[m]==1:\n a = mi.index(m)\n r = mi[a:]\n er =1\n else:\n bit[m] = 1\n mi.append(m)\n m = l[m]\n # print(bit)\nbe = mi[:a]+r\n#print(be,r)\nif k< len(be):\n print(be[k]+1)\nelse:\n w = k-len(be)+1\n u = (w%len(r))-1\n print(r[u]+1)']
['Wrong Answer', 'Wrong Answer', 'Accepted']
['s021126730', 's790996306', 's172277828']
[32388.0, 34676.0, 32388.0]
[178.0, 215.0, 174.0]
[424, 419, 420]
p02684
u291028869
2,000
1,048,576
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.
['n,k = map(int,input().split())\n\nA = list(map(int,input().split()))\nt = 0\nc = 0\nans = [0] * n\nL = []\nfor _ in range(k):\n if ans[t] == 1:\n t += k%c\n t = L[t]\n break\n else:\n ans[t] = 1\n L.append(t)\n print(L)\n t = A[t]-1\n c += 1\n\nprint(t+1)', 'n,k = map(int,input().split())\n\nA = list(map(int,input().split()))\nt = 0\nc = 0\nans = [0] * n\nL = []\ns = 0\nfor _ in range(k):\n if ans[t] == 1:\n n = len(L) - L.index(t)\n temp = (k-c)%n\n t = L[L.index(t):][temp]\n break\n else:\n ans[t] = 1\n L.append(t)\n t = A[t]-1\n c += 1\nprint(t+1)']
['Runtime Error', 'Accepted']
['s776444833', 's750062418']
[147524.0, 32376.0]
[2678.0, 152.0]
[298, 340]
p02684
u291766461
2,000
1,048,576
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.
['N, K = map(int, input().split())\nA = list(map(int, input().split()))\nX = [1]\npos = 0\nfor i in range(2 * N):\n X.append(A[pos])\n pos = A[pos] - 1\nprint(X)\n\ns = set()\nloop = len(X)\nfor i, x in enumerate(X):\n if x in s:\n loop = i\n break\n else:\n s.add(x)\n#print(loop)\nx, y = X[loop], X[loop + 1]\nfor i in range(N-1):\n if X[i] == x and X[i + 1] == y:\n base = i\n break\n# print(base)\ndiff = loop - base\nif K < base:\n print(X[K])\nelse:\n mod = K % diff\n # print(mod)\n print(A[mod])', 'N, K = map(int, input().split())\nA = list(map(int, input().split()))\ns = []\nd = [-1] * (N + 1)\nv = 1\nc, l = 1, 0\nwhile d[v] == -1:\n d[v] = len(s)\n s.append(v)\n v = A[v - 1]\nc = len(s) - d[v]\nl = d[v]\nif K < l:\n print(s[K])\nelse:\n K -= l\n K %= c\n print(s[l + K])']
['Wrong Answer', 'Accepted']
['s106675875', 's942776650']
[36988.0, 32356.0]
[278.0, 164.0]
[533, 282]
p02684
u294385082
2,000
1,048,576
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.
['n, k = map(int, input().split())\na = list(map(int, input().split()))\n\nd = {}\nfor i in range(1, n + 1):\n d[i] = a[i - 1]\n\ndid = set(1)\nroute = [1]\n\nnow = 1\nfor i in range(k):\n now = d[now]\n route.append(now)\n if now in did:\n break\n did.add(now)\n\nif k < 10 ** 6:\n now = 1\n for i in range(k):\n now = d[now]\n\n print(now)\n\nelse:\n for i in range(len(route)):\n if route[i] == route[-1]:\n roop = route[i:-1]\n print(roop[(k - i) % len(roop)])\n exit()\n\n \n', 'n, k = map(int, input().split())\na = list(map(int, input().split()))\n\nd = {}\nfor i in range(1, n + 1):\n d[i] = a[i - 1]\n\ndid = set([1])\nroute = [1]\n\nnow = 1\nfor i in range(k):\n now = d[now]\n route.append(now)\n if now in did:\n break\n did.add(now)\n\nif k < 10 ** 6:\n now = 1\n for i in range(k):\n now = d[now]\n\n print(now)\n\nelse:\n for i in range(len(route)):\n if route[i] == route[-1]:\n roop = route[i:-1]\n print(roop[(k - i) % len(roop)])\n exit()\n\n \n']
['Runtime Error', 'Accepted']
['s541582728', 's869537738']
[40096.0, 49240.0]
[108.0, 270.0]
[571, 573]
p02684
u297651868
2,000
1,048,576
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.
['n,k= list(map(int, input().split()))\nli= list(map(int, input().split()))\nd = [0 for i in range(n)]\nc=[1]\nnex=1\nfirst=0\nflag=0\nfor i in range(k):\n nex=li[nex-1]\n if d[nex]:\n first=c.index(nex)\n flag=1\n break\n d[nex-1]=1\n c.append(nex)\nif flag:\n k-=first\n k%=len(c)-first\n print(c[first+k])\nelse:\n print(c[-1])', 'n,k= list(map(int, input().split()))\nli= list(map(int, input().split()))\nd = [0 for i in range(n)]\nc=[1]\nnex=1\nfirst=0\nflag=0\nfor i in range(k):\n nex=li[nex-1]\n if d[nex-1]:\n first=c.index(nex)\n flag=1\n break\n d[nex-1]=1\n c.append(nex)\nif flag:\n k-=first\n k%=len(c)-first\n print(c[first+k])\nelse:\n print(c[-1])']
['Runtime Error', 'Accepted']
['s507977256', 's887012685']
[92796.0, 32220.0]
[2209.0, 137.0]
[353, 355]
p02684
u300579805
2,000
1,048,576
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.
['n,k = map(int, input().split())\na=list(map(int,list(input().split())))\n\nexist = [a[0]]\nex = {}\n\ncnt = 1\nwhile(True):\n if a[exist[-1]-1] in ex:\n idx = exist.index(a[exist[-1]-1])\n exist.append(a[exist[-1]-1])\n break\n exist.append(a[exist[-1]-1])\n ex[a[exist[-1]-1]] = 0\n \n cnt += 1\n if cnt >= k-1:\n print(exist[-1])\n exit()\n\n\n#k = k-idx\n#print(idx,exist,cnt)\nd = (k-idx) % (cnt-idx)\n\nprint(exist[d+idx-1])\n', 'n,k = map(int, input().split())\na=list(map(int,list(input().split())))\n\nexist = [1]\n#exist.append(a[0])\nex = {}\nex[1] = 0\n#ex[a[0]] = 0\nif k == 1:\n print(a[0])\n exit()\n\ncnt = 0\nwhile(True):\n if a[exist[-1]-1] in ex: \n idx = exist.index(a[exist[-1]-1])\n exist.append(a[exist[-1]-1])\n break\n ex[a[exist[-1]-1]] = 0\n exist.append(a[exist[-1]-1])\n \n cnt += 1\n if cnt-1 >= k-1:\n print(exist[-1])\n exit()\n\n\n#k = k-idx\nd = (k-idx) % (cnt-idx+1)\n\nprint(exist[d+idx])\n']
['Runtime Error', 'Accepted']
['s584157928', 's782070807']
[31208.0, 31508.0]
[64.0, 178.0]
[458, 525]
p02684
u305349402
2,000
1,048,576
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.
['N, K = list(map(int,input().split()))\nmove_list = list(map(int,input().split()))\n \n \ntest = 1\nfind_f = {}\nfor i in range(N):\n find_f[i+1]=[]\nt = K\n \nfind_f[1].append(0)\nfor i in range(K):\n test = move_list[test-1]\n find_f[test].append(i+1)\n if len(find_f[test]) == 2 and find_f[test][1]-find_f[test][0]<=100000:\n state ,f = test, find_f[test]\n K = K-f[0]\n f = f[1]-f[0]\n remain = K%f\n break\n\nif t == K:\n print(test)\nelse:\n for i in range(remain):\n \tstate = move_list[state-1]\n \n print(state)\n#state = 1', 'import sys\nN, K = list(map(int,input().split()))\nmove_list = list(map(int,input().split()))\n \n \ntest = 1\nfind_f = {}\nfor i in range(N):\n find_f[i+1]=[]\n \nfind_f[1].append(0)\nfor i in range(K):\n test = move_list[test-1]\n find_f[test].append(i+1)\n if len(find_f[test]) == 2 and find_f[test][1]-find_f[test][0]<=100000:\n state ,f = test, find_f[test]\n break\ntry:\n K = K-f[0]\n f = f[1]-f[0]\n remain = K%f\nexcept:\n print(test)\n sys.exit()\n \nfor i in range(remain):\n state = move_list[state-1]\n \n print(state)\n\n#state = 1', 'N, K = list(map(int,input().split()))\nmove_list = list(map(int,input().split()))\n \n \ntest = 1\nfind_f = {}\nfor i in range(N):\n find_f[i+1]=[]\n \nfind_f[1].append(0)\nfor i in range(K):\n test = move_list[test-1]\n find_f[test].append(i+1)\n if len(find_f[test]) == 2 and find_f[test][1]-find_f[test][0]<=1000000:\n state ,f = test, find_f[test]\n break\ntry:\n K = K-f[0]\n f = f[1]-f[0]\n remain = K%f\n for i in range(remain):\n state = move_list[state-1]\n \n print(state)\nexcept:\n print(test)\n#state = 1']
['Wrong Answer', 'Wrong Answer', 'Accepted']
['s594628442', 's950297695', 's081096926']
[156140.0, 151888.0, 57468.0]
[2210.0, 2209.0, 332.0]
[527, 533, 511]
p02684
u307516601
2,000
1,048,576
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.
['n,k = list(map(int, input().split()))\na = list(map(int, input().split()))\n\ninit = [1]\nwhile True:\n nxt = a[init[-1]-1]\n if nxt in init:\n idx_nxt = init[nxt]\n loop = init[idx_nxt:]\n init = init[:idx_nxt]\n break\n else:\n init.append(nxt)\n\nidx = (k-len(init))%len(loop)\nprint(loop[idx])', 'n, k = map(int, input().split())\na = tuple(map(lambda n: int(n) - 1, input().split()))\n\nvisited = [0] * n\nnext_town = 0\norder = []\n\nwhile True:\n if visited[next_town]:\n break\n order.append(next_town)\n visited[next_town] = 1\n next_town = a[next_town]\n\nfirst_loop = len(order)\noffset = order.index(next_town)\nsecond_loop = first_loop - offset\n\nif k < first_loop:\n print(order[k] + 1)\nelse:\n print(order[offset + (k - first_loop) % second_loop] + 1)\n']
['Runtime Error', 'Accepted']
['s792495779', 's162179502']
[32380.0, 32364.0]
[2206.0, 160.0]
[326, 472]
p02684
u307592354
2,000
1,048,576
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.
['def resolve():\n N,K = map(int,input().split())\n A = [int(i)-1 for i in input().split()]\n visited =[False]*N\n steps = [K for i in range(N)]\n\n current = 0\n while True:\n\n if K == 0:\n\t\t\tprint(current+1)\n return\n if not visited[current]:\n visited[current]=True\n steps[current]=K\n current=A[current]\n K-=1\n else:\n K= K % (steps[current]-K)\n break\n \n while K != 0:\n\n current= A[current]\n K-=1\n print(current+1)\n\n\n\n\nif __name__ == "__main__":\n resolve()\n', 'def resolve():\n N,K = map(int,input().split())\n A = [int(i)-1 for i in input().split()]\n visited =[False]*N\n steps = [K for i in range(N)]\n\n current = 0\n while True:\n\n if K == 0:\n print(current+1)\n return\n if not visited[current]:\n visited[current]=True\n steps[current]=K\n current=A[current]\n K-=1\n else:\n K= K % (steps[current]-K)\n break\n \n while K != 0:\n\n current= A[current]\n K-=1\n print(current+1)\n\n\n\n\nif __name__ == "__main__":\n resolve()\n']
['Runtime Error', 'Accepted']
['s683994776', 's688759288']
[9044.0, 32328.0]
[24.0, 138.0]
[588, 597]
p02684
u307622233
2,000
1,048,576
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.
['n, k = map(int, input().split())\na = [int(i) for i in input().split()]\n\nlst = []\nvisit_now = 1\n\nwhile True:\n if visit_list[visit_now - 1] == 1:\n break\n visit_list[visit_now - 1] = 1\n lst.append(visit_now)\n visit_now = a[visit_now - 1]\n\n\nstart = lst.index(visit_now)\nloop_num = n - start - 1\nlst = lst[start:]\n\nk -= start\nk %= loop_num\n\nprint(lst[k])\n# print(lst, loop_num, loop_list, k)\n', 'map(int, input().split())\na = [int(i) for i in input().split()]\n\n\nlst = []\nvisit_now = 1\nvisit_list = [0] * n\n\nwhile True:\n if visit_list[visit_now - 1] == 1:\n break\n visit_list[visit_now - 1] = 1\n lst.append(visit_now)\n visit_now = a[visit_now - 1]\n\n\nstart = lst.index(visit_now)\nloop_num = n - start - 1\n\nif k <= start:\n ans = lst[k]\nelse:\n k -= start\n k %= loop_num\n ans = lst[start + k]\n\nprint(ans)', 'n, k = map(int, input().split())\na = [int(i) for i in input().split()]\n\n\nlst = []\nvisit_now = 1\nvisit_list = [-1] * (n + 1)\ncnt = 0\n\nwhile visit_list[visit_now - 1] == -1:\n visit_list[visit_now - 1] = cnt\n lst.append(visit_now)\n visit_now = a[visit_now - 1]\n cnt += 1\n\nstart = visit_list[visit_now - 1]\nloop_num = cnt - start\n\nif k <= start:\n ans = lst[k]\nelse:\n k -= start\n k %= loop_num\n ans = lst[start + k]\n\nprint(ans)']
['Runtime Error', 'Runtime Error', 'Accepted']
['s029201121', 's417218492', 's733774240']
[32352.0, 32256.0, 32276.0]
[74.0, 67.0, 179.0]
[406, 433, 446]
p02684
u312158169
2,000
1,048,576
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.
['n,m = map(int,input().split())\na = [int(x) for x in input().split()]\n\nans1 = [1]\ntemp = 0\ncount = 0\nwhile(True):\n ans1 += [a[temp]]\n temp = a[temp]-1\n if len(ans1) != len(set(ans1)):\n break\n count += 1\n if count == m:\n print(ans1[len(ans1)-1])\n exit()\n\n\nnum1 = ans1.index(temp+1)\nans2 = ans1[num1:len(ans1)-1]\nprint(ans2)\nnum = len(ans2)\n\nprint(ans2[(m-num1)%num])\n', 'n,m = map(int,input().split())\na = [int(x) for x in input().split()]\n\nans1 = [1]\ntemp = 0\nvisit = [0]*(n+1)\nvisit[1] = 1\nwhile(True):\n ans1 += [a[temp]]\n\n if visit[a[temp]] == 1:\n break\n\n visit[a[temp]] = 1\n temp = a[temp]-1\n\n\n if len(ans1) == m+1:\n print(ans1[len(ans1)-1])\n exit()\n\n\nnum1 = ans1.index(a[temp])\nans2 = ans1[num1:len(ans1)-1]\n\nnum = len(ans2)\n\nprint(ans2[(m-num1)%num])\n']
['Wrong Answer', 'Accepted']
['s976101998', 's652503583']
[32280.0, 32332.0]
[2206.0, 179.0]
[403, 423]
p02684
u314089899
2,000
1,048,576
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.
['N,K = map(int, input().split())\nA_list = [0] + [int(e) for e in input().split()]\n\nused_tele = 0\nrepeating_flag = False\nmoving_dict = dict() \nmoving_dict[1]= 0\nnow_town = 1\n\nwhile used_tele<K:\n next_town = A_list[now_town]\n used_tele += 1\n print(used_tele,":",now_town,"→",next_town)\n \n if next_town in moving_dict:\n repeating_flag = True\n break\n \n moving_dict[next_town] = used_tele\n now_town = next_town\n \nif repeating_flag == False:\n print(next_town)\nelse:\n moving_list = list(moving_dict.keys())\n repeating_start_town_index = moving_list.index(next_town)\n \n #print(moving_list)\n #print(moving_list[0:repeating_start_town_index])\n #print(moving_list[repeating_start_town_index:])\n \n repeating_moving_list = moving_list[repeating_start_town_index:]\n K -= len(moving_list[0:repeating_start_town_index])\n \n print(repeating_moving_list[K%len(repeating_moving_list)])', 'N,K = map(int, input().split())\nA_list = [0] + [int(e) for e in input().split()]\n\nused_tele = 0\nrepeating_flag = False\nmoving_dict = dict() \nmoving_dict[1]= 0\nnow_town = 1\n\nwhile used_tele<K:\n next_town = A_list[now_town]\n used_tele += 1\n \n \n if next_town in moving_dict:\n repeating_flag = True\n break\n \n moving_dict[next_town] = used_tele\n now_town = next_town\n \nif repeating_flag == False:\n print(next_town)\nelse:\n moving_list = list(moving_dict.keys())\n repeating_start_town_index = moving_list.index(next_town)\n \n #print(moving_list)\n #print(moving_list[0:repeating_start_town_index])\n #print(moving_list[repeating_start_town_index:])\n \n repeating_moving_list = moving_list[repeating_start_town_index:]\n K -= len(moving_list[0:repeating_start_town_index])\n \n print(repeating_moving_list[K%len(repeating_moving_list)])']
['Wrong Answer', 'Accepted']
['s282993024', 's376806162']
[31988.0, 33372.0]
[349.0, 149.0]
[993, 994]
p02684
u316231243
2,000
1,048,576
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.
['from collections import defaultdict\nn,k=map(int,input().split())\nl=list(map(int,input().split()))\nd=defaultdict(int)\nfor i in range(n):\n d[i+1]=l[i]\nvisited=set()\nl=[]\ni=1\nwhile True:\n l.append(i)\n if i not in visited:\n visited.add(i)\n else:\n break\n i=d[i]\nfirst=l.index(l[-1])\nif k<first:\n print(l[k])\nelse:\n k=(len(l)-first)%((len(l)-1)-first)\n k+=first\n print(l[k])', 'from collections import defaultdict\nn,k=map(int,input().split())\nl=list(map(int,input().split()))\nd=defaultdict(int)\nfor i in range(n):\n d[i+1]=l[i]\nvisited=set()\nl=[]\ni=1\nwhile True:\n l.append(i)\n if i not in visited:\n visited.add(i)\n else:\n break\n i=d[i]\nfirst=l.index(l[-1])\nif k<first:\n print(l[k])\nelse:\n k=(k-first)%((len(l)-1)-first)\n k+=first\n print(l[k])']
['Wrong Answer', 'Accepted']
['s873410861', 's430418196']
[47520.0, 47652.0]
[223.0, 225.0]
[409, 404]
p02684
u319589470
2,000
1,048,576
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.
['#!/usr/bin/env python3\nimport bisect\nimport heapq\nimport itertools\nimport math\nimport numpy as np\nfrom collections import Counter, defaultdict, deque\nfrom copy import deepcopy\nfrom decimal import Decimal\nfrom math import gcd\nfrom operator import add, itemgetter, mul, xor\ndef cmb(n,r,mod):\n bunshi=1\n bunbo=1\n for i in range(r):\n bunbo = bunbo*(i+1)%mod\n bunshi = bunshi*(n-i)%mod\n return (bunshi*pow(bunbo,mod-2,mod))%mod\nmod = 10**9+7\ndef I(): return int(input())\ndef LI(): return list(map(int,input().split()))\ndef MI(): return map(int,input().split())\ndef LLI(n): return [list(map(int, input().split())) for _ in range(n)]\n\n\n\n\n\ndef now_place(li,n):\n now = 1\n for i in range(n):\n now = li[now-1]\n return now\n\n\n\ndef root(li,p):\n ans = []\n now = 1\n for j in range(p):\n ans.append(now_place(li,j+1))\n return ans\n\n\n\nn,k = MI()\na = LI()\n\n\n\ncnt = 0\nplace=[1]\ns = a[1-1]\nfor i in range(n):\n if a[s-1] in place:\n town = a[s-1]\n cnt = i - 1\n break\n place.append(a[s-1])\n s = a[s-1]\n\nt = 0\nnow = town\nfor i in range(n):\n t += 1\n now = a[now-1]\n if now == town:\n break\nanswer = root(a,cnt+t)\nprint(answer)\nif k <= cnt:\n print(answer[k-1])\nelse:\n print(answer[cnt+(k-cnt)%t-1])', '#!/usr/bin/env python3\nimport bisect\nimport heapq\nimport itertools\nimport math\nimport numpy as np\nfrom collections import Counter, defaultdict, deque\nfrom copy import deepcopy\nfrom decimal import Decimal\nfrom math import gcd\nfrom operator import add, itemgetter, mul, xor\ndef cmb(n,r,mod):\n bunshi=1\n bunbo=1\n for i in range(r):\n bunbo = bunbo*(i+1)%mod\n bunshi = bunshi*(n-i)%mod\n return (bunshi*pow(bunbo,mod-2,mod))%mod\nmod = 10**9+7\ndef I(): return int(input())\ndef LI(): return list(map(int,input().split()))\ndef MI(): return map(int,input().split())\ndef LLI(n): return [list(map(int, input().split())) for _ in range(n)]\n\n\n\n\n\ndef now_place(li,n):\n now = 1\n for i in range(n):\n now = li[now-1]\n return now\n\n\n\ndef root(li,p):\n ans = []\n now = 1\n for j in range(p):\n ans.append(now_place(li,j+1))\n return ans\n\n\n\nn,k = MI()\na = LI()\n\ntown\u3000= []\n\nvisit=[0]*n\np = 1\nwhile visit[p-1] == 0:\n town.append(p)\n visit[p-1] = 1\n p = a[p-1]\n\n\nl = town.index(p)\nif k <len(town):\n print(town[k])\nelse:\n print(town[l + (k-l)%(len(town[l:])))', '#!/usr/bin/env python3\nimport bisect\nimport heapq\nimport itertools\nimport math\nimport numpy as np\nfrom collections import Counter, defaultdict, deque\nfrom copy import deepcopy\nfrom decimal import Decimal\nfrom math import gcd\nfrom operator import add, itemgetter, mul, xor\ndef cmb(n,r,mod):\n bunshi=1\n bunbo=1\n for i in range(r):\n bunbo = bunbo*(i+1)%mod\n bunshi = bunshi*(n-i)%mod\n return (bunshi*pow(bunbo,mod-2,mod))%mod\nmod = 10**9+7\ndef I(): return int(input())\ndef LI(): return list(map(int,input().split()))\ndef MI(): return map(int,input().split())\ndef LLI(n): return [list(map(int, input().split())) for _ in range(n)]\n\n\n\n\n\ndef now_place(li,n):\n now = 1\n for i in range(n):\n now = li[now-1]\n return now\n\n\n\ndef root(li,p):\n ans = []\n now = 1\n for j in range(p):\n ans.append(now_place(li,j+1))\n return ans\n\n\n\nn,k = MI()\na = LI()\n\ntown = []\n\nvisit=[0]*n\np = 1\nwhile visit[p-1] == 0:\n town.append(p)\n visit[p-1] = 1\n p = a[p-1]\n\n\nl = town.index(p)\nif k <len(town):\n print(town[k])\nelse:\n print(town[l + (k-l)%(len(town[l:]))])\n']
['Runtime Error', 'Runtime Error', 'Accepted']
['s623107782', 's730310528', 's039206868']
[50756.0, 8928.0, 50548.0]
[2207.0, 27.0, 239.0]
[1616, 1464, 1464]
p02684
u320511454
2,000
1,048,576
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.
['n,k=list(map(int,input().split()))\nA=list(map(int,input().split()))\nstart,i,j=1,0,0\nroot=[1]\n\nfor j in range(k):\n start=A[start-1]\n if start in root:\n i = start\n break\n root.append(start)\n \nif k <= len(root):\n print(root[k-1])\nelse:\n newroot=root[i-1:]\n print(newroot)\n ans=(k-len(root))%len(newroot)\n print(newroot[ans])', 'n,k=list(map(int,input().split()))\nA=list(map(int,input().split()))\nstart,i=1,0\nroot=[1]\n\nfor j in range(k):\n start=A[start-1]\n if start in root:\n i = start\n break\n root.append(start)\n \nif k <= len(root):\n print(root[k-1])\nelse:\n newroot=root[i-1:]\n print(newroot)\n ans=(k-len(root))%len(newroot)\n print(newroot[ans])', 'n,k=list(map(int,input().split()))\nA=list(map(int,input().split()))\nstart,steps=1,0\nroot=dict()\n\nwhile start not in root and steps < k:\n steps+=1\n root[start]=steps\n start=A[start-1]\n\nif steps == k:\n print(start+1)\nelse:\n pa = steps-root[start]\n s=root[start]\n ans=(k-steps)%pa\n root={v: k for k, v in root.items()}\n print(root[s+ans])', 'n,k=list(map(int,input().split()))\nA=list(map(int,input().split()))\nstart,steps=1,0\nroot=dict()\n\nwhile start not in root and steps < k:\n root[start]=steps\n steps+=1\n start=A[start-1]\n\nif steps == k:\n print(start)\nelse:\n pa = steps-root[start]\n ans= root[start] + (k-steps)%pa\n root={v: k for k, v in root.items()}\n print(root[ans])']
['Runtime Error', 'Runtime Error', 'Runtime Error', 'Accepted']
['s020405202', 's521688952', 's818683760', 's909293992']
[32376.0, 32380.0, 36896.0, 37044.0]
[2206.0, 2206.0, 158.0, 165.0]
[336, 332, 344, 339]
p02684
u321035578
2,000
1,048,576
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.
["def main():\n n,k = map(int,input().split())\n a = list(map(int,input().split()))\n cnt = [-1] * n\n cnt[0] = 0\n ary = []\n now = 1\n ary.append(now)\n if k <= n:\n for i in range(1,k+1):\n now = a[now-1]\n if i == k:\n print(now)\n return\n else:\n for i in range(1,k+1):\n now = a[now-1]\n if cnt[now-1] != -1:\n break\n else:\n cnt[now-1] = i\n ary.append(a[now-1])\n\n\n print(ary[cnt[now-1]+((k-cnt[now-1]) % (i - cnt[now-1]))+1])\n \nif __name__=='__main__':\n main()\n", "def main():\n n,k = map(int,input().split())\n a = list(map(int,input().split()))\n cnt = [-1] * n\n cnt[0] = 0\n ary = []\n now = 0\n for i in range(n):\n a[i] -= 1\n\n ary.append(now)\n if k <= n:\n for i in range(1,k+1):\n now = a[now]\n if i == k:\n print(now+1)\n return\n else:\n for i in range(1,k+1):\n now = a[now]\n if cnt[now] != -1:\n break\n else:\n cnt[now] = i\n ary.append(now)\n loop_s = cnt[now]\n\n print(ary[cnt[now] +((k-cnt[now]) % (i - cnt[now]))] + 1)\n # print(cnt)\n # print(now)\n # print(ary)\n\nif __name__=='__main__':\n main()\n"]
['Runtime Error', 'Accepted']
['s794558281', 's758015210']
[32360.0, 33400.0]
[119.0, 123.0]
[636, 744]
p02684
u323045245
2,000
1,048,576
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.
['n,k = map(int, input().split())\ntele = list(map(int, input().split()))\nmove = [-1]*n\nmove[0] = 0\nnow = 0\ncnt = 0\n\nwhile True:\n cnt += 1\n now = tele[now] - 1 \n\n if move[now] != -1:\n cycle = cnt - move[now]\n break\n move[now] = cnt\n\n\nif cnt-1 >= k:\n ans = 0\n for i in range(k):\n ans = tele[ans] - 1\n\nelse:\n loop = (k-(cnt-1))%cycle\n ans = cnt - 1\n if loop == 0:\n ans = tele[ans] - 1\n for i in range(loop+1):\n ans = tele[ans] - 1\nprint(ans+1)\n \n\n\n', 'n,k = map(int, input().split())\ntele = [0] + list(map(int, input().split()))\nmove = [-1]*(n+1)\nmove[1] = 0\nnow = 1\ncnt = 0\n\nwhile True:\n cnt += 1\n now = tele[now] \n\n if move[now] != -1:\n cycle = cnt - move[now]\n break\n else:\n move[now] = cnt\n\nnotCycle = move[now]\n\nif notCycle > k:\n now = 1\n for i in range(k):\n now = tele[now]\n\nelse:\n loop = (k-notCycle)%cycle\n for i in range(loop):\n now = tele[now]\nprint(now)']
['Wrong Answer', 'Accepted']
['s746397298', 's270915892']
[32188.0, 32300.0]
[145.0, 129.0]
[712, 767]
p02684
u324090406
2,000
1,048,576
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.
['N, K = list(map(int, input().split()))\na_list = list(map(int, input().split()))\n\ndone_list = [1]\ncur_place = 1\nflag = True\nfor i in range(K):\n if a_list[cur_place-1] not in done_list:\n cur_place = a_list[cur_place-1]\n done_list.append(cur_place)\n else:\n period = len(done_list) - done_list.index(a_list[cur_place-1])\n print(done_list[done_list.index(a_list[cur_place-1]) + (K - i + 1) % period])\n flag = False\n break\n\nif flag: \n print(cur_place)\n ', 'N, K = list(map(int, input().split()))\na_list = list(map(int, input().split()))\n\ndone_list = [1]\ncur_place = 1\nflag = True\nfor i in range(K):\n if a_list[cur_place-1] not in done_list:\n cur_place = a_list[cur_place-1]\n done_list.append(cur_place)\n else:\n period = len(done_list) - done_list.index(a_list[cur_place-1])\n print(done_list[done_list.index(a_list[cur_place-1]) + (K - i) % period + 1])\n flag = False\n break\n\nif flag: \n print(cur_place)\n ', 'N, K = list(map(int, input().split()))\na = [0] + list(map(int, input().split()))\n\nbefore = 1\nstart = 0\nend = 0\n\nif K <= N:\n for i in range(K):\n before = a[before]\n print(before)\n\n\n \nelse:\n market_town = 0\n for i in range(N):\n before = a[before]\n marked_town = before\n K -= N\n counter = 1\n while marked_town != a[before]:\n before = a[before]\n counter += 1\n K = K % counter\n for i in range(K+1):\n before = a[before]\n print(before)']
['Wrong Answer', 'Runtime Error', 'Accepted']
['s052832524', 's754816503', 's156009834']
[32268.0, 32236.0, 32200.0]
[2206.0, 2206.0, 155.0]
[508, 508, 691]
p02684
u324197506
2,000
1,048,576
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.
['n, k = map(int, input().split())\n\nroads = list(map(int, input().split()))\n\ncity = 1\nroute = []\ncnt = [0] * n\ncnt[0] += 1\nroute.append(0)\n\n\nwhile True:\n city = roads[city-1] \n cnt[city-1] += 1\n if cnt[city-1] == 2:\n st = route.index(city)\n length = len(route) - st\n break\n route.append(city)\n\nif k <= len(route):\n print(route[k])\n\nelif length >= 1:\n amari = (k+1-st) % length\n if amari == 0:\n print(route[-1])\n else:\n print(route[amari+st-1])', 'n, k = map(int, input().split())\n\nroads = list(map(int, input().split()))\n\ncity = 1\nroute = []\ncnt = [0] * n\ncnt[0] += 1\nroute.append(1)\n\n\nwhile True:\n city = roads[city-1] \n cnt[city-1] += 1\n if cnt[city-1] == 2:\n st = route.index(city)\n length = len(route) - st\n break\n route.append(city)\n\nprint(cnt)\nprint(route)\nprint(st)\nprint(length)\n\nif k < len(route):\n print(route[k])\n\nelif length >= 1:\n amari = (k+1-st) % length\n if amari == 0:\n print(route[-1])\n else:\n print(route[amari+st-1])', 'n, k = map(int, input().split())\n\nroads = list(map(int, input().split()))\n\ncity = 1\nroute = []\ncnt = [0] * n\ncnt[0] += 1\nroute.append(1)\n\n\nwhile True:\n city = roads[city-1] \n cnt[city-1] += 1\n if cnt[city-1] == 2:\n st = route.index(city)\n length = len(route) - st\n break\n route.append(city)\n\n\nif k < len(route):\n print(route[k])\n\nelse:\n amari = (k+1-st) % length\n if amari == 0:\n print(route[-1])\n else:\n print(route[amari+st-1])']
['Runtime Error', 'Wrong Answer', 'Accepted']
['s184586048', 's512410369', 's873592630']
[32380.0, 32380.0, 32396.0]
[175.0, 202.0, 202.0]
[501, 549, 489]
p02684
u328179275
2,000
1,048,576
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.
['n,k = map(int,input().split())\na = input().split()\nb = [0]*n\nd = {}\nind = 1\nnum = 0\nloop = 1\nwhile num == k:\n if b[ind-1] == 0:\n b[ind-1] = -1\n d[ind] = num\n num += 1\n ind = int(a[ind-1])\n else:\n loop = num-d[ind]\n sup = d[ind]\n break\n\nif k-sup >=0:\n for i in range((k-sup)%loop):\n ind = int(a[ind-1])\n\nprint(ind)', 'n,k = map(int,input().split())\na = input().split()\nb = [0]*n\nd = {}\nind = 1\nnum = 0\nwhile True:\n if b[ind-1] == 0:\n b[ind-1] = -1\n d[ind] = num\n num += 1\n ind = int(a[ind-1])\n else:\n loop = num-d[ind]\n sup = d[ind]\n break\n\n\nfor i in range((k-sup)%loop):\n ind = int(a[ind-1])\nprint(int(a[ind]))', 'n,k = map(int,input().split())\na = input().split()\nb = [0]*n\nd = {}\nind = 1\nnum = 0\nloop = 1\nsup =0\n\nwhile num != k:\n if b[ind-1] == 0:\n b[ind-1] = -1\n d[ind] = num\n num += 1\n ind = int(a[ind-1])\n else:\n loop = num-d[ind]\n sup = d[ind]\n break\n\nif k-sup >=0:\n for i in range((k-sup)%loop):\n ind = int(a[ind-1])\n\nprint(ind)']
['Runtime Error', 'Runtime Error', 'Accepted']
['s748115035', 's952589312', 's632119820']
[24584.0, 51972.0, 40908.0]
[43.0, 255.0, 180.0]
[378, 351, 348]
p02684
u331226975
2,000
1,048,576
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.
['N, K = map(int, input().split())\nA = list(map(int, input().split()))\n\nr = [0]*N\n\nnow = 0\nr[now]=1\nfor i in range(N):\n now = A[now]-1\n if r[now] == 0:\n r[now] = i+2\n else:\n x1 = r[now]\n x2 = i+1\n break\nprint(x1, x2)\nprint(r)\nroop_num = x2 - x1 + 1\nx = (K-x1+1) % roop_num\nprint(x)\nif K < x1:\n print(r.index(K+1)+1)\nelse:\n print(r.index(x1 + x)+1)\n', 'N, K = map(int, input().split())\nA = list(map(int, input().split()))\n\nnow = 1\nr = [0]*N\nr[0]=now\nfor i in range(N):\n if A[now-1] in r:\n x = r.index(A[now-1])\n break\n else:\n now = A[now-1]\n r[i+1]=now\nr_x = r[x:]\nprint(r_x[(K-x)%len(r[x:])])\n', 'N, K = map(int, input().split())\nA = list(map(int, input().split()))\n\nr = [0]*N\n\nnow = 0\nr[now]=1\nfor i in range(N):\n now = A[now]-1\n if r[now] == 0:\n r[now] = i+2\n else:\n x1 = r[now]\n x2 = i+1\n break\n\nroop_num = x2 - x1 + 1\nx = (K-x1+1) % roop_num\n\nif K < x1:\n print(r.index(K+1)+1)\nelse:\n print(r.index(x1 + x)+1)\n']
['Wrong Answer', 'Wrong Answer', 'Accepted']
['s582535976', 's949206793', 's073508197']
[32300.0, 32376.0, 32296.0]
[143.0, 2206.0, 155.0]
[363, 255, 333]
p02684
u337626942
2,000
1,048,576
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.
['n, k=map(int, input().split())\na=list(map(int, input().split()))\na.insert(0, 0)\n\nfor i in range(1, n+1):\n if a.count(i)>=1:\n s_index=i\n break\n\ncnt=0\nn_index=a[s_index]\nfor j in range(1, n+1):\n if n_index==s_index:\n cnt+=1\n break\n else:\n n_index=a[n_index]\n cnt+=1\n\nprint(a)\nprint(s_index)\nprint(cnt)', 'n, k=map(int, input().split())\na=[0]+list(map(int, input().split()))\n\nt=[1]\n\nfor i in range(2*n):\n nxt=a[t[-1]]\n t.append(nxt)\n\nif k<n+1:\n print(t[k])\n exit()\n\nloop_end=n\nloop_start=n-1\n\nwhile t[loop_start]!=t[loop_end]:\n loop_start-=1\n\ncycle=loop_end-loop_start\nk%=cycle\n\nwhile k<n+1:\n k+=cycle\n\nprint(t[k])']
['Wrong Answer', 'Accepted']
['s198839327', 's068063984']
[32376.0, 32380.0]
[138.0, 213.0]
[350, 326]
p02684
u340781749
2,000
1,048,576
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.
['import sys\n\nn, k, *aaa = map(int, sys.stdin.buffer.read().split())\nshortest = [-1] * n\nv = 0\nd = 0\nwhile shortest[v] == -1:\n shortest[v] = d\n v = aaa[v] - 1\n d += 1\n if d == k:\n print(v + 1)\n exit()\n\nfirst = shortest[v]\nloop = d - first\nremaining = (k - first) % loop\nprint(v, first, loop, remaining + first, shortest)\nans = shortest.index(remaining + first) + 1\nprint(ans)\n', 'import sys\n\nn, k, *aaa = map(int, sys.stdin.buffer.read().split())\naaa = [0] + aaa\np = 1\nwhile k:\n if k & 1:\n p = aaa[p]\n aaa = [aaa[a] for a in aaa]\n k >>= 1\nprint(p)\n']
['Wrong Answer', 'Accepted']
['s869357582', 's967430804']
[27932.0, 27908.0]
[166.0, 1111.0]
[400, 184]
p02684
u342563578
2,000
1,048,576
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.
['n,k = map(int,input().split())\na = list(map(int,input().split()))\np = a[0]\ns = a[0]\ng = []\nq = []\nfor i in range(n+1):\n g.append(p)\n p = a[p-1]\nfor i in range(len(g)):\n if g[i] == g[i] + mod\ng.reverse()\nfor i in range(n):\n if g[0] == g[i+1]:\n mod = i+1\n break\ng.reverse()\nfor i in range(len(g)):\n if g[i] == g[i] + mod:\n d = i\n break\nif k <= mod:\n for i in range(k%mod):\n q.append(s)\n s = a[s-1]\n q.reverse()\n q.append(1)\n print(q[0])\nelse:\n print(g[d+(k-d)%mod])', 'n,k = map(int,input().split())\na = list(map(int,input().split()))\np = a[0]\ns = a[0]\ng = [1]\nq = []\nfor i in range(n+1):\n g.append(p)\n p = a[p-1]\ng.reverse()\nfor i in range(n):\n if g[0] == g[i+1]:\n mod = i+1\n break\ng.reverse()\nfor i in range(len(g)):\n if g[i] == g[i + mod]:\n d = i\n break\nif k <= n:\n for i in range(k):\n q.append(s)\n s = a[s-1]\n q.reverse()\n print(q[0])\nelse:\n print(g[d+(k-d)%mod])']
['Runtime Error', 'Accepted']
['s590727477', 's632774113']
[9052.0, 32272.0]
[23.0, 180.0]
[535, 464]
p02684
u344030307
2,000
1,048,576
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.
['n,k = map(int, input().split())\na = list(map(int, input().split()))\n\ntele = [1]\nwent = set(tele)\nnow = 1\ncnt = 0\nflag = True\n\nfor i in range(k):\n now = a[now-1]\n if now in went:\n tele = tele[tele.index(now):]\n cnt = len(tele)\n flag = False\n break\n tele.append(now)\n went.add(now)\n\nif flag == True:\n print(tele[-1])\n\nelse:\n cnt = k % cnt\n print(tele[cnt])\n\nprint(tele)', 'n,k = map(int, input().split())\na = list(map(int, input().split()))\n\ntele = [1]\nwent = set(tele)\nnow = 1\ncnt = 0\nflag = True\n\nfor i in range(k):\n now = a[now-1]\n if i != k-1 and now in went:\n tele = tele[tele.index(now):]\n cnt = i+1\n flag = False\n break\n tele.append(now)\n went.add(now)\n\nif flag == True:\n print(tele[-1])\n\nelse:\n k -= cnt\n cnt = k % len(tele)\n print(tele[cnt])']
['Wrong Answer', 'Accepted']
['s003785869', 's899866661']
[32264.0, 32356.0]
[167.0, 161.0]
[386, 397]
p02684
u347600233
2,000
1,048,576
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.
['N,K,*A=map(int,open(0).read().split())\na=1\nwhile K>0:\n if K&1:\n a=A[a-1]\n A=[A[A[i]-1] for i in range(N)]\n K>>=1\nprint(a)', 'n, k = map(int, input().split())\na = [int(i) for i in input().split()]\nordi = [0, ]\nfor i in range(n):\n if a[ordi[-1]] - 1 not in ordi:\n ordi.append(a[ordi[-1]] - 1)\n else:\n ordi.append(a[ordi[-1]] - 1)\n index = ordi.index(a[ordi[-1]] - 1)\n len_ = len(ordi) - index\n break\nprint(ordi[index + (k - index) % len_])', 'n, k = map(int, input().split())\na = [int(i) - 1 for i in input().split()]\nans = 0\nwhile k:\n if k & 1:\n ans = a[ans]\n a = [a[ai] for ai in a]\n k >>= 1\nprint(ans + 1)']
['Wrong Answer', 'Wrong Answer', 'Accepted']
['s211169260', 's971337676', 's039832395']
[31768.0, 32196.0, 32332.0]
[1260.0, 2206.0, 1162.0]
[141, 353, 181]
p02684
u349444371
2,000
1,048,576
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.
['n,k=map(int,input().split())\nA=list(map(int,input().split()))\np=1\nl=[0]*n\n#l[0]+=1\nif k<=100000:\n for i in range(k):\n p=A[p-1]\n l[p-1]+=1\n print(p)\n exit()\nelse:\n for i in range(100001):\n p=A[p-1]\n l[p-1]+=1\n #print(p)\n if l[p-1]==2:\n x=i\n break\n #print("p=",str(p))\n #print(l,x)\n\n d=1\n for j in range(100001):\n d=A[d-1]\n if d==p:\n y=j\n break\n #print("y=",str(y))\n z=(k-x-1)%(x-y)\n for i in range(z):', 'n,k=map(int,input().split())\nA=list(map(int,input().split()))\np=1\nl=[0]*n\n#l[0]+=1\nif k<=200000:\n for i in range(k):\n p=A[p-1]\n l[p-1]+=1\n print(p)\n exit()\nelse:\n for i in range(200001):\n p=A[p-1]\n l[p-1]+=1\n #print(p)\n if l[p-1]==2:\n x=i\n break\n #print("p=",str(p))\n #print(l,x)\n\n d=1\n for j in range(200001):\n d=A[d-1]\n if d==p:\n y=j\n break\n #print("y=",str(y))\n z=(k-x-1)%(x-y)\n #print(z)\n for i in range(z):\n p=A[p-1]\n #print(p)\n print(p)']
['Runtime Error', 'Accepted']
['s228617965', 's964430709']
[9100.0, 32380.0]
[25.0, 150.0]
[626, 688]
p02684
u362255558
2,000
1,048,576
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.
["N, K = [ int(i) for i in input().strip().split(' ') ]\nA = [ int(i) for i in input().strip().split(' ') ]\n\ncurrent = 1\nN_counter = [0] * N \nN_counter_idx = [0] * N \nN_counter[current-1] += 1\n\nk_reserved = -1\nfor i in range(1, N+1):\n current = A[current-1]\n N_counter[current-1] += 1\n if i == K:\n k_reserved = current\n if N_counter[current-1] == 2:\n span = i\n break\n N_counter_idx[current-1] = i\n\nif K < i:\n print(k_reserved)\nelse:\n #print(N_counter)\n #print(N_counter_idx)\n pre_loop_length = N_counter_idx[current-1] - 1\n loop_length = max(N_counter_idx) - pre_loop_length\n K_new = (K - pre_loop_length) % loop_length + pre_loop_length\n \n \n current = 1\n for k in range(K_new):\n current = A[current-1]\n print(current)", "N, K = [ int(i) for i in input().strip().split(' ') ]\nA = [ int(i) for i in input().strip().split(' ') ]\n\ncurrent = 1\nN_counter = [0] * N \nN_counter_idx = [0] * N \nN_counter[current-1] += 1\nN_counter_idx[current-1] = 1\n\nk_reserved = -1\nfor i in range(1, N+1):\n current = A[current-1]\n N_counter[current-1] += 1\n if i == K:\n k_reserved = current\n if N_counter[current-1] == 2:\n break\n N_counter_idx[current-1] = i+1\n\nif K < i:\n print(k_reserved)\nelse:\n #print(N_counter)\n #print(N_counter_idx)\n pre_loop_length = N_counter_idx[current-1] - 1\n loop_length = max(N_counter_idx) - pre_loop_length\n #print(pre_loop_length)\n #print(loop_length)\n K_new = (K - pre_loop_length) % loop_length + pre_loop_length\n \n \n current = 1\n for k in range(K_new):\n current = A[current-1]\n print(current)"]
['Wrong Answer', 'Accepted']
['s500078997', 's302075047']
[32332.0, 32340.0]
[191.0, 185.0]
[818, 884]
p02684
u364027015
2,000
1,048,576
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.
['import numpy as np\nN,K=map(int,input().split())\nA=list(map(int,input().split()))\ndef B(i):\n if i==0:\n return 1\n return A[B(i-1)-1]\nC=[1]\nfor i in range(1,N+1):\n if C.count(B(i))==0:\n C.append(B(i))\n else:\n l=C.index(B(i))+1\n break\nif K+1<=l:\n print(C[K])\nelse:\n print(C[l-1])\nprint(C)', 'import numpy as np\nN,K=map(int,input().split())\nA=list(map(int,input().split()))\nC=[1]\nfor i in range(1,N+1):\n if C.count(A[C[i]-1])==0:\n C.append(A[C[i]-1])\n else:\n l=C.index(A[C[i]-1])+1\n break\nif K+1<=l:\n print(C[K])\nelse:\n print(C[l-1+(K+1-l)%(len(C)-l+1)])\nprint(C)', 'import numpy as np\nimport sys\nsys.setrecursionlimit(200000)\nN,K=map(int,input().split())\nA=list(map(int,input().split()))\ndef B(i):\n if i==0:\n return 1\n return A[B(i-1)-1]\nC=[1]\nfor i in range(1,N+1):\n if C.count(B(i))==0:\n C.append(B(i))\n else:\n l=C.index(B(i))+1\n break\nif K+1<=l:\n print(C[K])\nelse:\n print(C[l-2+(K+2-l)%(len(C)-l+1)])\nprint(C)', 'import numpy as np\nN,K=map(int,input().split())\nA=list(map(int,input().split()))\nC=[1]\ns=set([1])\nfor i in range(N):\n if A[C[i]-1] in s:\n l=C.index(A[C[i]-1])+1\n break\n else:\n C.append(A[C[i]-1])\n s.add(A[C[i]-1])\nif K+1<=l:\n print(C[K])\nelse:\n print(C[l-1+(K+1-l)%(len(C)-l+1)])']
['Runtime Error', 'Runtime Error', 'Wrong Answer', 'Accepted']
['s238523207', 's461486924', 's537613504', 's958107455']
[51044.0, 50936.0, 51104.0, 50940.0]
[305.0, 154.0, 2207.0, 284.0]
[302, 283, 364, 295]
p02684
u367965715
2,000
1,048,576
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.
['n, k = map(int, input().split())\na = list(map(lambda x: int(x)-1, input().split()))\n\nvisited = [1] + [0]*(n-1)\nidx = [0] + [-1]*(n-1)\nroute = [0]\nwhile True:\n now = a[route[-1]]\n if visited[now]:\n break\n visited[now] = 1\n idx[now] = len(route)\n route.append(now)\n\nborder = idx[a[route[-1]]]\nnon_loop = route[:border]\nloop = route[border:]\n\nif a[route[-1]] == 0:\n print(route[k%len(route)])\nelse:\n print(loop[(k-len(non_loop))%len(loop)]+1)\n', 'n, k = map(int, input().split())\na = list(map(lambda x: int(x)-1, input().split()))\n\nvisited = [1] + [0]*(n-1)\nidx = [0] + [-1]*(n-1)\nroute = [0]\nwhile True:\n now = a[route[-1]]\n if visited[now]:\n break\n visited[now] = 1\n idx[now] = len(route)\n route.append(now)\n\nborder = idx[a[route[-1]]]\nnon_loop = route[:border]\nloop = route[border:]\n\nprint(non_loop[k]+1) if k < len(non_loop) else print(loop[(k-len(non_loop))%len(loop)]+1)\n']
['Wrong Answer', 'Accepted']
['s995029106', 's562312759']
[32384.0, 32308.0]
[214.0, 191.0]
[468, 452]
p02684
u369133448
2,000
1,048,576
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.
['n,k=map(int,input().split())\na=list(map(int,input().split()))\ndic={}\nwk=1\nstart=0\nfor i in range(1,n+1):\n wk=a[wk-1]\n if wk in dic:\n start=dic[wk]\n break\n dic[wk]=i\nend=len(dic)\nloopcnt=end-start+1\nkeys=list(dic.keys())\nif k<start:\n print(keys[k-1])\nelse:\n chk=(k-start+1)%loopcntac\n if chk==0:\n chk=loopcnt\n print(keys[(start-1)+chk-1])\n', 'n,k=map(int,input().split())\na=list(map(int,input().split()))\ndic={}\nwk=1\nstart=0\nfor i in range(1,n+1):\n wk=a[wk-1]\n if wk in dic:\n start=dic[wk]\n break\n dic[wk]=i\nend=len(dic)\nloopcnt=end-start+1\nkeys=list(dic.keys())\nif k<start:\n print(keys[k-1])\nelse:\n chk=(k-start+1)%loopcnt\n if chk==0:\n chk=loopcnt\n print(keys[(start-1)+chk-1])\n']
['Runtime Error', 'Accepted']
['s027324205', 's124669717']
[38704.0, 38696.0]
[147.0, 147.0]
[354, 352]
p02684
u370721525
2,000
1,048,576
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.
['N, K = map(int, input().split())\nA = list(map(int, input().split()))\n\nnum = 1\nl = []\nl.append(num)\ni = 0\nwhile i < K-1:\n num = A[num-1]\n if num not in l:\n l.append(num)\n i += 1\n else:\n i = K - (K-1-i) % (i+1-(l.index(num)))\n break;\n \nwhile i < K-1:\n num = A[num-1]\n i += 1\n \nprint(num)', 'N, K = map(int, input().split())\nA = list(map(int, input().split()))\nwas = []\ntf = [False] * N\nplace = 1\ncnt = 0\n\nwhile tf[place-1] == False:\n if cnt < K:\n tf[place-1] = True\n was.append(place)\n place = A[place-1]\n cnt += 1\n else:\n print(place)\n break;\nelse:\n loop = cnt - was.index(place)\n num = (K-cnt) % loop + was.index(place)\n print(was[num])']
['Wrong Answer', 'Accepted']
['s285821724', 's145692762']
[32364.0, 32376.0]
[2206.0, 166.0]
[306, 370]
p02684
u372320597
2,000
1,048,576
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.
['N,K = map(int, input().split())\nA = list(map(int, input().split()))\np = 1\np_hist = []\nwhile p not in p_hist:\n p_hist.append(p)\n p = A[p-1]\nend_loop = p_hist[-1]', 'N,K = map(int, input().split())\nA = list(map(int, input().split()))', 'N,K = map(int, input().split())\nA = list(map(int, input().split()))\np = 1\np_hist = []\np_hist_set = set()\np_hist_index = [-1] * (N+1)\nwhile p not in p_hist_set:\n p_hist_index[p] = len(p_hist)\n p_hist.append(p)\n p_hist_set.add(p)\n p = A[p-1]\nend_loop = p_hist[-1]\nend_loop_idx = p_hist_index[end_loop]\nstart_loop = A[end_loop - 1]\nstart_loop_idx = p_hist_index[start_loop]\nif K < start_loop_idx:\n print(p_hist[K])\nelse:\n print(p_hist[(K-start_loop_idx)%(end_loop_idx-start_loop_idx+1)+start_loop_idx])\n']
['Wrong Answer', 'Wrong Answer', 'Accepted']
['s610134459', 's695086999', 's348863543']
[32172.0, 32376.0, 36884.0]
[2206.0, 66.0, 204.0]
[166, 67, 518]
p02684
u373047809
2,000
1,048,576
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.
['n,k,*a=map(int,open(0).read().split());a=[0]+a;p=1\nwhile k:p=k%2*p and a[p];a=[a[b]for b in a];k//=2\nprint(p)', '_,k,*a=map(int,open(0).read().split());a=[0]+a;p=1\nwhile k:p=~-k%2*p or a[p];a=[a[b]for b in a];k//=2\nprint(p)']
['Wrong Answer', 'Accepted']
['s117698838', 's056778374']
[31812.0, 31804.0]
[1374.0, 1169.0]
[109, 110]
p02684
u374935093
2,000
1,048,576
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.
['max = int(data[1])\nlimit = max\ntimes = 0\npos = 1\nexperience = [1]\nwhile max > 0:\n pos = telepo[pos - 1]\n max -= 1\n times += 1\n #print(pos)\n if pos in experience:\n length = len(experience) - experience.index(pos)\n max = max % length\n if max == 0:\n break\n while max > 0:\n pos = telepo[pos - 1]\n max -= 1\n times += 1\n #print(pos)\n break\n experience.append(pos)\nprint(pos)', 'data = input().rstrip().split(" ")\ntele = input().rstrip().split(" ")\ntelepo = [int(i) for i in tele]\n\nmax = int(data[1])\nlimit = max\ntimes = 0\npos = 1\nexperience = [1]\ncheck = {1}\nbefore = 0\nwhile max > 0:\n pos = telepo[pos - 1]\n max -= 1\n times += 1\n before = len(check)\n check.add(pos)\n if before == len(check):\n length = len(experience) - experience.index(pos)\n max = max % length\n if max == 0:\n break\n while max > 0:\n pos = telepo[pos - 1]\n max -= 1\n times += 1\n #print(pos)\n break\n experience.append(pos)\nprint(pos)']
['Runtime Error', 'Accepted']
['s908561332', 's883784758']
[9084.0, 44412.0]
[22.0, 213.0]
[476, 632]
p02684
u375172966
2,000
1,048,576
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.
['n, k = map(int, input().split())\na = list(map(int, input().split()))\nvisited = [0]\nfor _ in range(n):\n now = a[visited[-1]]-1\n visited.append(now)\n\nif k <= n:\n print(visited[k]+1)\nelse:\n p = visited.index(visited[-1])\n loop = visited[::-1][1:].index(visited[-1])+1\n x = (k-p-1)%loop\n print(p, loop, x)\n print(visited[p+x+1]+1)\n', 'n, k = map(int, input().split())\na = list(map(int, input().split()))\nvisited = [0]\nfor _ in range(n):\n now = a[visited[-1]]-1\n visited.append(now)\n\nif k < n:\n print(visited[k]+1)\nelse:\n p = visited.index(visited[-1])\n loop = visited[::-1][1:].index(visited[-1])+1\n x = (k-p-1)%loop\n print(visited[p+x+1]+1)\n']
['Wrong Answer', 'Accepted']
['s873045537', 's708735685']
[32328.0, 32332.0]
[133.0, 130.0]
[351, 328]
p02684
u379601169
2,000
1,048,576
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.
["from collections import deque\n\nN, M = map(int, input().split())\nAB = [map(int, input().split()) for _ in range(M)]\n\nlinks = [[] for _ in range(N + 1)]\nfor a, b in AB:\n links[a].append(b)\n links[b].append(a)\n\nresult = [-1] * (N + 1)\nq = deque([1])\nwhile q:\n i = q.popleft()\n for j in links[i]:\n if result[j] == -1:\n result[j] = i\n q.append(j)\nprint('Yes')\nprint('\\n'.join(str(i) for i in result[2:]))", 'num_town, num_move = map(int, input().split())\nteleports = list(map(lambda x: int(x) - 1, input().split()))\n\ncurrent = 0\nmemo = {}\nfor i in range(num_move):\n if memo.get(current) == 2:\n break\n if not memo.get(current):\n memo[current] = 1\n else:\n memo[current] += 1\n destination = teleports[current]\n current = destination\n\nloop = {k:v for k,v in memo.items() if v==2}\nminus = {k:v for k,v in memo.items() if v==1}\n\nnew_move_count = 0\nif len(loop) == 0:\n current = 0\n new_move_count = num_move\nelse:\n new_move_count = (num_move - len(minus)) % len(loop)\n\nfor i in range(new_move_count):\n destination = teleports[current]\n current = destination\n\nprint(current + 1)\n']
['Runtime Error', 'Accepted']
['s329432774', 's914266343']
[25912.0, 34248.0]
[42.0, 230.0]
[440, 714]
p02684
u379716238
2,000
1,048,576
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.
['N, K = map(int, input().split())\nA = list(map(int, input().split()))\n\nc = [0]*N\ncc = [0]\nc[0] = 1\ni = 0\nwhile True:\n c[A[i]-1] += 1 \n cc.append(A[i]-1)\n if c[A[i]-1] > 1:\n memo = A[i]-1\n break\n i = A[i] - 1 \n \nused = [i for i in range(N) if c[i] > 0]\n\npos = [i for i in range(len(cc)) if cc[i] == memo]\n\nres = pos[1] - pos[0]\n\nif K < pos[0]+1:\n for i in range(K):\n c[A[i]-1] += 1 \n i = A[i] - 1\n ans = i+1\nelse:\n i = (K - (pos[0]+1)) % res\n ans = cc[i]\n \nprint(ans)', 'N, K = map(int, input().split())\nA = list(map(int, input().split()))\n\nc = [0]*N\ncc = [0]\nc[0] = 1\ni = 0\nwhile True:\n c[A[i]-1] += 1 \n cc.append(A[i]-1)\n if c[A[i]-1] > 1:\n memo = A[i]-1\n break\n i = A[i] - 1 \n \nused = [i for i in range(N) if c[i] > 0]\n\npos = [i for i in range(len(cc)) if cc[i] == memo]\n\nres = pos[1] - pos[0]\nloop = [i for i in cc[pos[0]:pos[1]]]\n\nif K < N:\n ans = cc[K]+1\nelse:\n i = (K - (pos[0])) % res\n ans = loop[i]+1\n \nprint(ans)']
['Wrong Answer', 'Accepted']
['s526469457', 's520432912']
[33964.0, 35624.0]
[238.0, 229.0]
[560, 511]
p02684
u394950523
2,000
1,048,576
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.
['N, K = list(map(int, input().split()))\nA = list(map(int, input().split()))\n\nl = [1, A[0]]\na = A[0] - 1\nfor _ in range(0, N):\n if A[a] not in l:\n l.append(A[a])\n a = A[a] - 1\n else:\n a = A[a]\n break\nn = len(l)\nprint(l)\nb = 0\nfor i in range(n):\n if l[i] == a:\n del l[0:i]\n b = i\n break\nT = len(l)\nprint(l)\nS = (K - b) % T\nans = l[S]\nprint(S)\nprint(b)\n\nprint(ans)', 'N, K = list(map(int, input().split()))\nA = list(map(int, input().split()))\n\nl = [A[0]]\na = A[0] - 1\nfor _ in range(0, N):\n if A[a] not in l:\n l.append(A[a])\n a = A[a] - 1\n else:\n break\n a = A[a]\nn = len(l)\nprint(l)\nb = 0\nfor i in range(n):\n if l[i] == a:\n del l[0:i]\n b = i - 1\n break\nN = len(A)\nS = (K - b) % N\nans = A[S - 1]\n\nprint(ans)', 'N, K = list(map(int, input().split()))\nA = list(map(int, input().split()))\n\nl = [A[0]]\ns = {A[0]}\na = A[0] - 1\nfor _ in range(K):\n if A[a] not in s:\n l.append(A[a])\n s.add(A[a])\n a = A[a] - 1\n else:\n a = A[a]\n break\nn = len(l)\nb = 0\nfor i in range(n):\n if l[i] == a:\n del l[0:i]\n b = i\n break\nT = len(l)\nS = (K - b) % T\nans = l[S - 1]\n\nprint(ans)\n']
['Wrong Answer', 'Wrong Answer', 'Accepted']
['s688074543', 's711876245', 's076530863']
[32344.0, 32384.0, 32456.0]
[2206.0, 2206.0, 173.0]
[418, 396, 412]
p02684
u396210538
2,000
1,048,576
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.
['from sys import stdin\nimport sys\n\nN, K = [int(x) for x in stdin.readline().rstrip().split()]\nA = list(map(int, input().split()))\nB = [i for i in range(1, N+1)]\nd = dict(zip(B, A))\nt = [0 for i in range(N)]\nd1 = dict(zip(B, t))\ntemp = 1\ncnt = 0\nans = []\nfor i in range(K+1):\n d1[d[temp]] = d1.get(d[temp], 0)+1\n if d1.get(d[temp], 0) == 2:\n cnt += 1\n ans.append(d[temp])\n # print(i, d[temp])\n if d1.get(d[temp], 0) == 3:\n break\n temp = d[temp]\n if i == K:\n print(temp)\n sys.exit()\ncnt1 = 0\nfor i, v in enumerate(d1):\n if v == 1:\n cnt1 += 1\n# print(cnt, cnt1, d1, temp, ans)\nk = K-1-cnt1\nprint(int(ans[k % cnt]))\n', 'from sys import stdin\nimport sys\n\nN, K = [int(x) for x in stdin.readline().rstrip().split()]\nA = list(map(int, input().split()))\nB = [i for i in range(1, N+1)]\nd = dict(zip(B, A))\nt = [0 for i in range(N)]\nd1 = dict(zip(B, t))\ntemp = 1\ncnt = 0\nans = []\nfor i in range(K):\n d1[d[temp]] = d1.get(d[temp], 0)+1\n if d1.get(d[temp], 0) == 2:\n cnt += 1\n ans.append(d[temp])\n # print(i, d[temp])\n if d1.get(d[temp], 0) == 3:\n break\n temp = d[temp]\n if i == K-1:\n print(d[temp])\n sys.exit()\ncnt1 = 0\nfor i, v in enumerate(d1):\n if v == 1:\n cnt1 += 1\n# print(cnt, cnt1, d1, temp, ans)\nk = K-1-cnt1\nprint(ans[k % cnt])\n', 'from sys import stdin\nimport sys\n\nN, K = [int(x) for x in stdin.readline().rstrip().split()]\nA = list(map(int, input().split()))\nB = [i for i in range(1, N+1)]\nd = dict(zip(B, A))\nt = [0 for i in range(N)]\nd1 = dict(zip(B, t))\ntemp = 1\ncnt = 0\nans = []\nfor i in range(K+1):\n d1[d[temp]] = d1.get(d[temp], 0)+1\n if d1.get(d[temp], 0) == 2:\n cnt += 1\n ans.append(d[temp])\n # print(i, d[temp])\n if d1.get(d[temp], 0) == 3:\n break\n temp = d[temp]\n if i == K:\n print(temp)\n sys.exit()\ncnt1 = 0\nfor i, v in enumerate(d1):\n if v == 1:\n cnt1 += 1\n# print(cnt, cnt1, d1, temp, ans)\nk = K-1-cnt1\nprint(ans[k % cnt])\n', 'from sys import stdin\nimport sys\n\nN, K = [int(x) for x in stdin.readline().rstrip().split()]\nA = list(map(int, input().split()))\nB = [i for i in range(1, N+1)]\nd = dict(zip(B, A))\nt = [0 for i in range(N)]\nd1 = dict(zip(B, t))\ntemp = 1\ncnt = 0\nans = []\nfor i in range(K):\n d1[d[temp]] = d1.get(d[temp], 0)+1\n if d1.get(d[temp], 0) == 2:\n cnt += 1\n ans.append(d[temp])\n # print(i, d[temp])\n if d1.get(d[temp], 0) == 3:\n break\n temp = d[temp]\n if i == K-1:\n print(temp)\n sys.exit()\ncnt1 = 0\nfor i, v in enumerate(d1):\n if d1[v] == 1:\n cnt1 += 1\n# print(cnt, cnt1, d1, temp, ans)\nk = K-1-cnt1\nprint(ans[k % cnt])\n']
['Wrong Answer', 'Wrong Answer', 'Wrong Answer', 'Accepted']
['s258825974', 's307840550', 's860855732', 's122463617']
[56796.0, 56900.0, 56888.0, 56816.0]
[470.0, 493.0, 470.0, 449.0]
[678, 676, 673, 677]
p02684
u396858476
2,000
1,048,576
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.
["N, K = map(int, input().split())\nA = input().split()\nA_MAP = {}\nfor i in range(N):\n A_MAP[str(i+1)] = A[i]\n\nprint(A_MAP)\nroute_hash = {'1': 0}\ncurrent_pos = '1'\nnext_pos = ''\ncounter = 0\nwhile counter <= K:\n next_pos = A_MAP[current_pos]\n counter+=1\n if next_pos in route_hash:\n break\n route_hash[next_pos] = counter \n current_pos = next_pos\n\nif counter >= K:\n print(current_pos)\n exit()\n\nprint(route_hash, current_pos, counter)\nremain = K - counter\ncounter -= route_hash[next_pos]\nremain = remain % counter\nprint(K, counter, remain, next_pos)\n\ncurrent_pos = next_pos\nfor i in range(remain):\n next_pos = A_MAP[current_pos]\n route_hash[next_pos] = counter \n current_pos = next_pos\nprint(current_pos)\n", "N, K = map(int, input().split())\nA = input().split()\nA_MAP = {}\nfor i in range(N):\n A_MAP[str(i+1)] = A[i]\n\nroute_hash = {'1': 0}\ncurrent_pos = '1'\nnext_pos = ''\ncounter = 0\nbreaked = False\nwhile counter < K:\n next_pos = A_MAP[current_pos]\n counter+=1\n if next_pos in route_hash:\n breaked = True\n break\n route_hash[next_pos] = counter \n current_pos = next_pos\n\nif not breaked:\n print(current_pos)\n exit()\n\nremain = K - counter\ncounter -= route_hash[next_pos]\nremain = remain % counter\n\ncurrent_pos = next_pos\nfor i in range(remain):\n next_pos = A_MAP[current_pos]\n route_hash[next_pos] = counter \n current_pos = next_pos\nprint(current_pos)\n"]
['Wrong Answer', 'Accepted']
['s351518094', 's556083035']
[64296.0, 61492.0]
[333.0, 265.0]
[827, 775]
p02684
u404629709
2,000
1,048,576
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.
['n,k=map(int,input().split())\nnum=[0 for i in range(n)]\nfirst=[0 for i in range(n)]\nfirst[0]=1\na=list(map(int,input().split()))\nfor i in range(n):\n a[i]-=1\nnum[0]=1\nloop=0\ns=0\nfor i in range(n):\n if num[a[s]]==0:\n num[a[s]]+=1\n first[a[s]]=i\n if num[a[s]]==2:\n loop1=loop\n break\n s=a[s]\n\nkai=first[a[s]]+(k-first[a[s]])%(loop1-first[a[s]])\ns=0\nfor i in range(kai):\n s=a[s]\nprint(s+1)', 'n,k=map(int,input().split())\na=list(map(int,input().split()))\nfor i in range(n):\n a[i]-=1\nkai=[-1 for i in range(n)]\nnx=0\nnow=0\nkai[0]=0\n\nif k<=n:\n for i in range(k):\n nx=a[now]\n now=nx\n #print(nx)\n ans=nx+1\nelse:\n for i in range(n):\n nx=a[now]\n if kai[nx]==-1:\n kai[nx]=i+1\n now=nx\n else:\n T=i+1-kai[nx]\n break\n times=kai[nx]+(k-kai[nx])%T\n now=0\n nx=0\n for i in range(times):\n nx=a[now]\n now=nx\n #print(nx)\n ans=nx+1\nprint(ans)']
['Runtime Error', 'Accepted']
['s784943994', 's103083244']
[35488.0, 32364.0]
[203.0, 197.0]
[399, 485]
p02684
u405660020
2,000
1,048,576
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.
['n, k_in=map(int, input().split())\na_l=list(map(int, input().split()))\n\ndef f(s):\n a = s\n b = s\n i=1\n while True:\n a = a_l[a-1]\n a = a_l[a-1]\n b = a_l[b-1]\n if a == b:\n break\n i+=1\n\n j=1\n a = a_l[0]\n while True:\n a = a_l[a-1]\n b = a_l[b-1]\n if a == b:\n break\n j+=1\n\n k=1\n while True:\n a = a_l[a-1]\n a = a_l[a-1]\n b = a_l[b-1]\n if a == b:\n break\n k+=1\n\n return j, k\n\nlam, myu = f(a_l[0])\n\npos=[]\npos.append(a_l[0])\nfor i in range(n):\n pos.append(a_l[pos[i]])\n\nif k_in<=lam:\n print(pos[k_in-1])\nelse:\n k_in=(k_in-lam)%myu\n print(pos[lam+k_in-1])\n', 'n, k=map(int, input().split())\na=list(map(int, input().split()))\n\nfor i in range(n):\n a[i]-=1\n\nrec=[-1]*n\nturn=0\ncurrent=0\n\nwhile True:\n if k<=0:\n print(current+1)\n break\n if rec[current]==-1:\n rec[current]=turn\n turn+=1\n current=a[current]\n k-=1\n else:\n loop=turn-rec[current]\n k%=loop\n rec=[-1]*n\n']
['Runtime Error', 'Accepted']
['s422841931', 's455341622']
[33364.0, 32296.0]
[327.0, 164.0]
[716, 374]
p02684
u408071652
2,000
1,048,576
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.
['n,k = map(int,input().split())\na = list(map(int,input().split()))\na.insert(0,0)\nprint(a)\n\ntown =1\nvisited = [0] * (n+1)\n\nfor i in range(1,k+1):\n town = a[town]\n if visited[town] !=0:\n rest =(k - visited[town]) % (i -visited[town])\n goal =visited.index(visited[town]+rest)\n break\n visited[town] = i \n \n\nprint(goal)', '\n\n#input = sys.stdin.readline\n\ndef main():\n N, K = map(int,input().split())\n A = list(map(int,input().split()))\n A = [0] +A \n visited = [0] * (N+1)\n town = 1\n cycle = 0\n for i in range(1,K+1):\n town = A[town]\n if visited[town] ==0:\n visited[town] = i\n else:\n cycle = i - visited[town]\n place = (K - visited[town]) % cycle + visited[town]\n print(visited.index(place))\n exit()\n print(town)\n\n\n\n\nif __name__ == "__main__":\n main()']
['Runtime Error', 'Accepted']
['s316349217', 's668640732']
[32296.0, 32336.0]
[155.0, 98.0]
[347, 542]
p02684
u409542115
2,000
1,048,576
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.
['n,k=map(int,input().split())\nA=list(map(int,input().split()))\nA=[0]+A\nB=[]\nC=[0]+[-1]*n\nnow_city=1\nseen=set()\n\n\nfor i in range(1,len(A)):\n print("Baah")\n', 'n,k=map(int,input().split())\nA=list(map(int,input().split()))\nA=[0]+A\nB=[]\nC=[0]+[-1]*n\nnow_city=1\nseen=set()\n\n\nfor i in range(1,len(A)):\n print("Baah")\n if now_city in B:\n route=B[C[now_city]-1:i-1]\n break\n B.append(now_city)\n C[now_city]=i', 'n,k=map(int,input().split())\nA=list(map(int,input().split()))\nA=[0]+A\nB=[]\nC=[0]+[-1]*n\nnow_city=1\nseen=set()\n\n\nfor i in range(1,len(A)):\n now_city=A[now_city]\n \n #if now_city in B:\n #route=B[C[now_city]-1:i-1]\n # break\n B.append(now_city)\n C[now_city]=i\nif k <= len(B):\n print(B[k-1])\nelse:\n k = k- len(B)\n \n if x ==0:\n #print(route[-1])\n else:\n #print(route[x-1])', 'n,k=map(int,input().split())\nA=list(map(int,input().split()))\nA=[0]+A\nB=[]\nC=[0]+[-1]*n\nnow_city=1\nseen=set()\n\n\nfor i in range(1,len(A)):\n print("Baah")\n if now_city in B:\n route=B[C[now_city]-1:i-1]\n break', 'n,k=map(int,input().split())\nA=list(map(int,input().split()))\nA=[0]+A\nB=[]\nC=[0]+[-1]*n\nnow_city=1\nseen=set()\n\n\nfor i in range(1,len(A)):\n print("Baah")\nif k <= len(B):\n print("Baah")\nelse:\n k = k- len(B)\n \n if x ==0:\n print("Baah")\n else:\n print("Baah")', 'n,k=map(int,input().split())\nA=list(map(int,input().split()))\nA=[0]+A\nB=[]\nC=[0]+[-1]*n\nnow_city=1\nseen=set()\n \n\nfor i in range(1,len(A)):\n now_city=A[now_city]', 'n,k=map(int,input().split())\nA=list(map(int,input().split()))\nA=[0]+A\nB=[]\nC=[0]+[-1]*n\nnow_city=1\nseen=set()\n\n\nfor i in range(1,len(A)):\n now_city=A[now_city]\n \n #if now_city in B:\n # route=B[C[now_city]-1:i-1]\n # break\n B.append(now_city)\n C[now_city]=i\nif k <= len(B):\n print(B[k-1])\nelse:\n k = k- len(B)\n x = k % len(route)\n if x ==0:\n print(route[-1])\n else:\n print(route[x-1])\n', 'n,k=map(int,input().split())\nA=list(map(int,input().split()))\nA=[0]+A\nB=[]\nC=[0]+[-1]*n\nnow_city=1\nseen=set()\n \n\nfor i in range(1,len(A)):\n now_city=A[now_city]\n \n if now_city in B:\n route=B[C[now_city]-1:i-1]\n break\n B.append(now_city)', 'n,k=map(int,input().split())\nA=list(map(int,input().split()))\nA=[0]+A\nB=[]\nC=[0]+[-1]*n\nnow_city=1\nseen=set()\n \n\nfor i in range(1,len(A)):\n now_city=A[now_city]\n \n if now_city in B:\n route=B[C[now_city]-1:i-1]\n break\n B.append(now_city)\n C[now_city]=i', 'n,k=map(int,input().split())\nA=list(map(int,input().split()))\nA=[0]+A\nB=[]\nC=[0]+[-1]*n\nnow_city=1\nseen=set()\n\n\nfor i in range(1,len(A)):\n print("Baah")\n if now_city in B:\n print("Waah")', 'n,k = map(int,input().split())\ns = list(map(int,input().split()))\n# print(s)\nnow_city = 1\ns_l_city = 1\nt = [0] \nf = [0] \na=0\nb = 0\nf_l=0\n\n\nfor i in range(n):\n t.append(0)\n f.append(-1)\n# print(s)\nsycle = 0\n\nfor i in range(1,n+10):\n now_city = s[now_city-1]\n\n if t[now_city-1]==1:\n # print(i,f[j])\n sycle=i-f[now_city-1]\n f_l=f[now_city-1]\n # f_l=i\n # a = 1\n b = i\n # s_l_city=now_city\n \n break\n t[now_city-1]+=1\n f[now_city-1]=i\n# print(t)\n# print(f_l)\n# print(f_l,sycle,s_l_city)\n\nif k>b:\n # now_city=s_l_city\n for _ in range((k-f_l)%sycle):\n\n now_city = s[now_city-1]\n # print((k-f_l)%sycle)\n print(now_city)\nelse:\n \n now_city=1\n for _ in range(k):\n now_city=s[now_city-1]\n print(now_city)\n']
['Wrong Answer', 'Wrong Answer', 'Runtime Error', 'Wrong Answer', 'Runtime Error', 'Wrong Answer', 'Runtime Error', 'Wrong Answer', 'Wrong Answer', 'Wrong Answer', 'Accepted']
['s013765017', 's250367745', 's314422702', 's348930670', 's613071852', 's689337113', 's728627634', 's821845993', 's828004579', 's929722753', 's384005857']
[32232.0, 32368.0, 9048.0, 32364.0, 32380.0, 32348.0, 32296.0, 32376.0, 32368.0, 32280.0, 32344.0]
[125.0, 70.0, 24.0, 130.0, 123.0, 118.0, 144.0, 2206.0, 2206.0, 137.0, 226.0]
[184, 295, 530, 254, 333, 191, 528, 351, 369, 227, 1103]
p02684
u411478442
2,000
1,048,576
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.
['def check(pair,m,x):\n l = len(pair)\n sum = 0\n \n for i in range(1,m):\n p = 0\n for j in range(l):\n p += pair[j][i]\n \n if(p < x):\n return -1\n \n for e in pair:\n sum += e[0]\n return sum\n \nn,m,x = (int(x) for x in input().split())\na = []\nfor i in range(n):\n a.append(tuple([int(x) for x in input().split()]))\n \nmin = -1\n \nfor i in range(2 ** n):\n pair = []\n for j in range(n):\n if((i >> j) & 1):\n pair.append(a[j])\n \n sum = check(pair,m,x)\n if sum != -1:\n if min == -1:\n min = sum\n elif min > sum:\n min = sum\n \nprint(min)', 'n,k = (int(x) for x in input().split())\na = tuple([int(x) for x in input().split()])\n \nc = [0] + [-1]*(n)\nl = [1]\nnow = 1\ndupl = -1\n\nfor i in range(k):\n now = a[now - 1]\n c[now - 1] += 1\n \n if c[now - 1] == 1:\n dupl = now\n break\n else:\n l.append(now)\n \nif dupl == -1:\n print(now)\n exit()\n \nnotloop = l.index(dupl)\nlength = len(l)-notloop\nprint(l[(k-notloop)%length+notloop])']
['Runtime Error', 'Accepted']
['s644291207', 's858204171']
[9268.0, 32332.0]
[22.0, 163.0]
[593, 400]
p02684
u413632062
2,000
1,048,576
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.
['n,k=map(int,input().split())\na=[int(i)-1 for i in input().split()]\np=0\nvisited=[0]*n\nfirst=[]\nroop=[]\nwhile visited[p]!=2:\n if visited[p]==0:\n first.append(p)\n else:\n roop.append(p)\n visited[p]+=1\n p=a[p]\nprint(first,roop)\nif len(first)>k:\n print(first[k]+1)\nelse:\n print(roop[(k-(len(first)-len(roop)))%len(roop)]+1)', 'n,k=map(int,input().split())\na=[int(i)-1 for i in input().split()]\np=0\nvisited=[0]*n\nfirst=[]\nroop=[]\nwhile visited[0]!=2:\n if visited[p]==0:\n first.append(p)\n else:\n roop.append(p)\n visited[p]+=1\n p=a[p]\nif len(first)>k:\n print(first[k])\nelse:\n print(roop[(k-len(first)-len(roop))%len(roop)])', 'n,k=map(int,input().split())\na=[int(i)-1 for i in input().split()]\np=0\nvisited=[0]*n\nfirst=[]\nroop=[]\nwhile visited[0]!=2:\n if visited[p]==0:\n first.append(p)\n else:\n roop.append(p)\n visited[p]+=1\n p=a[p]\nif len(first)>k:\n print(first[k])\nelse:\n print(roop[(k-(len(first)-len(roop)))%len(roop)])', 'n,k=map(int,input().split())\na=[int(i)-1 for i in input().split()]\np=0\nvisited=[0]*n\nfirst=[]\nroop=[]\nwhile visited[p]!=2:\n if visited[p]==0:\n first.append(p)\n else:\n roop.append(p)\n visited[p]+=1\n p=a[p]\nif len(first)>k:\n print(first[k]+1)\nelse:\n print(roop[(k-(len(first)-len(roop)))%len(roop)]+1)']
['Wrong Answer', 'Wrong Answer', 'Wrong Answer', 'Accepted']
['s286760332', 's404816797', 's455510655', 's890258435']
[32204.0, 88536.0, 87792.0, 32332.0]
[306.0, 2208.0, 2208.0, 231.0]
[644, 620, 622, 626]
p02684
u416011173
2,000
1,048,576
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.
['# -*- coding: utf-8 -*-\n\nimport math\n\n\ndef get_input() -> tuple:\n \n N, K = list(map(int, input().split()))\n A = list(map(int, input().split()))\n\n return N, K, A\n\n\ndef main(N: int, K: int, A: list) -> None:\n \n \n to = [A]\n for i in range(math.ceil(math.log2(K))):\n to_i = to[i]\n to_next = []\n \n if ((K >> i) & 1):\n for j in range(N):\n to_next.append(to_i[to_i[j]-1])\n\n to.append(to_next)\n\n result = 1\n for i in range(math.ceil(math.log2(K))):\n if ((K >> i) & 1):\n result = to[i][result-1]\n\n print(result)\n\n\nif __name__ == "__main__":\n N, K, A = get_input()\n main(N, K, A)\n', '# -*- coding: utf-8 -*-\n\nimport math\n\n\ndef get_input() -> tuple:\n \n N, K = list(map(int, input().split()))\n A = list(map(int, input().split()))\n\n return N, K, A\n\n\ndef main(N: int, K: int, A: list) -> None:\n \n \n to = [A]\n for i in range(math.ceil(math.log2(K))):\n to_i = to[i]\n to_next = []\n for j in range(N):\n to_next.append(to_i[to_i[j]-1])\n\n to.append(to_next)\n\n result = 1\n for i in range(math.ceil(math.log2(K))):\n if ((K >> i) & 1):\n result = to[i][result-1]\n\n print(result)\n\n\nif __name__ == "__main__":\n N, K, A = get_input()\n main(N, K, A)\n']
['Runtime Error', 'Accepted']
['s770442350', 's698606243']
[31192.0, 113488.0]
[246.0, 1919.0]
[1053, 956]
p02684
u419354839
2,000
1,048,576
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.
['N,K = map(int,input().split())\nA = list(map(lambda x: int(x) - 1, input().split()))\nP = [-1] * N \ncur = 0\nP[cur] = 0\nroute = [1]\n\nfor step in range(K):\n cur = A[cur]\n route.append(cur + 1)\n if P[cur] >= 0:\n l_start = P[cur]\n l_end = step + 1\n l_len = l_end - l_start\n break\n else:\n P[cur] = step + 1\nelse:\n print(cur)\n quit()\n \n\nprint(route,l_start,l_end)\nprint(route[l_start + ((K - l_start) % l_len)])', 'N,K = map(int,input().split())\nA = list(map(lambda x: int(x) - 1, input().split()))\nP = [-1] * N \ncur = 0\nP[cur] = 0\nroute = [1]\n\nfor step in range(K):\n cur = A[cur]\n route.append(cur + 1)\n if P[cur] >= 0:\n l_start = P[cur]\n l_end = step + 1\n l_len = l_end - l_start\n break\n else:\n P[cur] = step + 1\nelse:\n print(cur + 1)\n quit()\n\nprint(route[l_start + ((K - l_start) % l_len)])']
['Wrong Answer', 'Accepted']
['s210007365', 's816781921']
[32380.0, 32268.0]
[212.0, 139.0]
[425, 399]
p02684
u423665486
2,000
1,048,576
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.
['import sys\nfrom io import StringIO\nimport unittest\n\nsys.setrecursionlimit(100**5)\n\ndef search(u, n, g, disc, k):\n if disc[u] != None:\n circles = n - disc[u]\n v = (k - disc[u]) % circles\n ans = u\n for i in range(v):\n ans = g[ans]\n return ans + 1\n disc[u] = n\n return search(g[u], n+1, g, disc, k)\n\ndef resolve():\n n, k = map(int, input().split())\n g = list(map(lambda x: int(x) - 1, input().split()))\n disc = [None]*n\n v = search(0, 0, g, disc, k)\n print(v)\nresolve()', 'import sys\nfrom io import StringIO\nimport unittest\n\nsys.setrecursionlimit(10**5)\n\ndef search(u, n, g, disc, k):\n if disc[u] != None:\n circles = n - disc[u]\n v = (k - disc[u]) % circles\n ans = u\n for i in range(v):\n ans = g[ans]\n return ans + 1\n disc[u] = n\n return search(g[u], n+1, g, disc, k)\nresolve()', 'def resolve():\n\tn, k = map(int, input().split())\n\ta = list(map(int, input().split()))\n\trec = [True]*(n+2)\n\tnow = 0\n\tstp = [1]\n\n\twhile rec[now]:\n\t\trec[now] = False\n\t\tnow = a[now] - 1\n\t\tstp.append(now+1)\n\tbefore_loop = stp.index(now+1)\n\tloop = stp[before_loop:-1]\n\tif k <= (before_loop):\n\t\tprint(stp[k])\n\telse:\n\t\tr = (k - before_loop) % len(loop)\n\t\tprint(loop[r])\nresolve()']
['Runtime Error', 'Runtime Error', 'Accepted']
['s311291510', 's773073450', 's305671479']
[16244.0, 16272.0, 32360.0]
[67.0, 68.0, 131.0]
[536, 359, 371]
p02684
u425762225
2,000
1,048,576
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.
['N, K = map(int,input().split())\na = [0] + list(map(int,input().split()))\n\njourney = [1]\n\nfor i in range(N):\n nxt = a[journey[-1]]\n journey.append(nxt)\n\nif K < N:\n print(journey[K])\n exit()\n\nloop_start = N-1\nloop_end = N\n\nfinished = False\nfor i in range(N):\n for j in range(i,N+1):\n if journey[i] == journey[j]:\n loop_start = i\n loop_end = j\n finished = True\n break\n if finished:\n break\n\nperiod = loop_end - loop_start\n\nans = (K - loop_start) % period\nprint(ans)', 'N, K = map(int,input().split())\na = [0] + list(map(int,input().split()))\n\njourney = [1]\n\nfor i in range(N):\n nxt = a[journey[-1]]\n journey.append(nxt)\n\nif K < N:\n print(journey[K])\n exit()\n\nloop_start = N-1\nloop_end = N\n\nfinished = False\nfor i in range(1,N):\n for j in range(i+1,N+1):\n if journey[i] == journey[j]:\n loop_start = i\n loop_end = j\n finished = True\n break\n if finished:\n break\n\nperiod = loop_end - loop_start\n\nK %= period\nwhile K < period:\n K += period\n\nprint(journey[K])\n', 'N, K = map(int,input().split())\na = [0] + list(map(int,input().split()))\n\njourney = [1]\n\nfor i in range(N):\n nxt = a[journey[-1]]\n journey.append(nxt)\n\nif K < N:\n print(journey[K])\n exit()\n\nloop_start = N-1\nloop_end = N\n\nfinished = False\nfor i in range(1,N):\n for j in range(i+1,N+1):\n if journey[i] == journey[j]:\n loop_start = i\n loop_end = j\n finished = True\n break\n if finished:\n break\n\nperiod = loop_end - loop_start\n\nans = K % period\nwhile ans < period:\n ans += period\n\nprint(journey[ans])\n', 'N,K = map(int,input().split())\na = list(map(int,input().split()))\n\nans = a[K % N]\nprint(ans)', 'N, K = map(int,input().split())\na = [0] + list(map(int,input().split()))\n\njourney = [1]\n\nfor i in range(2*N+1000):\n nxt = a[journey[-1]]\n journey.append(nxt)\n\nif K <= N:\n print(journey[K])\n exit()\n\nloop_start = N-1\nloop_end = N\n \nwhile(journey[loop_start] != journey[loop_end]):\n loop_start -= 1\n \nperiod = loop_end - loop_start\n\nK %= period\nwhile K < N:\n K += period\n\nprint(journey[K])\n']
['Runtime Error', 'Runtime Error', 'Runtime Error', 'Wrong Answer', 'Accepted']
['s201731122', 's401782966', 's494970321', 's560623440', 's487685760']
[32376.0, 32412.0, 32416.0, 32376.0, 32232.0]
[107.0, 2206.0, 2206.0, 70.0, 258.0]
[541, 566, 577, 92, 408]
p02684
u426175055
2,000
1,048,576
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.
['N,K = map(int,input().split())\nA = list(map(int,input().split()))\n\ntown = [0 for i in range(N)]\n\ndef next(A,now):\n return(A[now-1])\n\nnow = 1\ni = 0\nwhile True:\n if town[now-1] != 0:\n roop_start = town[now-1]\n roop_end = i\n break\n town[now-1] = i+1\n now = next(A,now)\n i += 1\n\nroop = roop_end - roop_start + 1\nteleport = roop_start + (K - roop_start)%roop\nprint(teleport)\nnow = 1\nfor i in range(teleport):\n now = next(A,now)\n\nprint(now)\n', 'N,K = map(int,input().split())\nA = list(map(int,input().split()))\n\ntown = [0 for i in range(N+1)]\n\ndef next(A,now):\n return(A[now-1])\n\nnow = 1\ni = 0\nwhile True:\n if town[now] != 0:\n roop_start = town[now]\n roop_end = i\n break\n town[now] = i+1\n now = next(A,now)\n i += 1\n\nroop = roop_end - roop_start + 1\nteleport = min(roop_start + (K - roop_start)%roop,K)\n\nnow = 1\nfor i in range(teleport):\n now = next(A,now)\n\nprint(now)\n']
['Wrong Answer', 'Accepted']
['s083754699', 's540457942']
[32380.0, 32368.0]
[227.0, 204.0]
[474, 462]
p02684
u426572476
2,000
1,048,576
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.
['import sys\nimport heapq\nimport re\nfrom itertools import permutations\nfrom bisect import bisect_left, bisect_right\nfrom collections import Counter, deque\nfrom math import factorial, sqrt, ceil, gcd\nfrom functools import lru_cache, reduce\nINF = 1 << 60\nMOD = 1000000007\nsys.setrecursionlimit(10 ** 7)\n\n# UnionFind\nclass UnionFind():\n def __init__(self, n):\n self.n = n\n self.parents = [-1] * n\n\n def find(self, x):\n if self.parents[x] < 0:\n return x\n else:\n self.parents[x] = self.find(self.parents[x])\n return self.parents[x]\n\n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n\n if x == y:\n return\n\n if self.parents[x] > self.parents[y]:\n x, y = y, x\n\n self.parents[x] += self.parents[y]\n self.parents[y] = x\n\n def size(self, x):\n return -self.parents[self.find(x)]\n\n def same(self, x, y):\n return self.find(x) == self.find(y)\n\n def members(self, x):\n root = self.find(x)\n return [i for i in range(self.n) if self.find(i) == root]\n\n def roots(self):\n return [i for i, x in enumerate(self.parents) if x < 0]\n\n def group_count(self):\n return len(self.roots())\n\n def all_group_members(self):\n return {r: self.members(r) for r in self.roots()}\n\n def __str__(self):\n return \'\\n\'.join(\'{}: {}\'.format(r, self.members(r)) for r in self.roots())\n\n\ndef dijkstra_heap(s, edge, n):\n \n d = [10**20] * n\n used = [True] * n \n d[s] = 0\n used[s] = False\n edgelist = []\n for a,b in edge[s]:\n heapq.heappush(edgelist,a*(10**6)+b)\n while len(edgelist):\n minedge = heapq.heappop(edgelist)\n \n if not used[minedge%(10**6)]:\n continue\n v = minedge%(10**6)\n d[v] = minedge//(10**6)\n used[v] = False\n for e in edge[v]:\n if used[e[1]]:\n heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])\n return d\n\n\ndef factorization(n):\n arr = []\n temp = n\n for i in range(2, int(-(-n**0.5//1))+1):\n if temp%i==0:\n cnt=0\n while temp%i==0:\n cnt+=1\n temp //= i\n arr.append([i, cnt])\n\n if temp!=1:\n arr.append([temp, 1])\n\n if arr==[]:\n arr.append([n, 1])\n\n return arr\n\n\ndef lcm(x, y):\n return (x * y) // gcd(x, y)\n\n\ndef lcm_list(numbers):\n return reduce(lcm, numbers, 1)\n\n\ndef gcd_list(numbers):\n return reduce(gcd, numbers)\n\n\ndef is_prime(n):\n if n <= 1:\n return False\n p = 2\n while True:\n if p ** 2 > n:\n break\n if n % p == 0:\n return False\n p += 1\n return True\n\n\n\ndef eratosthenes(limit):\n A = [i for i in range(2, limit+1)]\n P = []\n\n while True:\n prime = min(A)\n \n if prime > sqrt(limit):\n break\n \n P.append(prime)\n \n i = 0\n while i < len(A):\n if A[i] % prime == 0:\n A.pop(i)\n continue\n i += 1\n \n for a in A:\n P.append(a)\n \n return P\n\n\ndef permutation_with_duplicates(L):\n\n if L == []:\n return [[]]\n\n else:\n ret = []\n\n \n S = sorted(set(L))\n\n for i in S:\n\n data = L[:]\n data.remove(i)\n\n for j in permutation_with_duplicates(data):\n ret.append([i] + j)\n\n return ret\n\n\n\nn, k = map(int, input().split()) \na = [i - 1 for i in list(map(int, input().split()))]\n# print(a)\nb = []\ncurrent = 0\n# cnt = 0\nvisited = [0 for i in range(n)]\nprint("a =", a)\nwhile True:\n if visited[current] == 1:\n break \n visited[current] = 1\n b.append(current)\n current = a[current]\nprint("b =", b)\nm = len(b)\nans = b[k % m] + 1\nprint(ans)', 'import sys\nimport heapq\nimport re\nfrom itertools import permutations\nfrom bisect import bisect_left, bisect_right\nfrom collections import Counter, deque\nfrom math import factorial, sqrt, ceil, gcd\nfrom functools import lru_cache, reduce\nINF = 1 << 60\nMOD = 1000000007\nsys.setrecursionlimit(10 ** 7)\n\n# UnionFind\nclass UnionFind():\n def __init__(self, n):\n self.n = n\n self.parents = [-1] * n\n\n def find(self, x):\n if self.parents[x] < 0:\n return x\n else:\n self.parents[x] = self.find(self.parents[x])\n return self.parents[x]\n\n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n\n if x == y:\n return\n\n if self.parents[x] > self.parents[y]:\n x, y = y, x\n\n self.parents[x] += self.parents[y]\n self.parents[y] = x\n\n def size(self, x):\n return -self.parents[self.find(x)]\n\n def same(self, x, y):\n return self.find(x) == self.find(y)\n\n def members(self, x):\n root = self.find(x)\n return [i for i in range(self.n) if self.find(i) == root]\n\n def roots(self):\n return [i for i, x in enumerate(self.parents) if x < 0]\n\n def group_count(self):\n return len(self.roots())\n\n def all_group_members(self):\n return {r: self.members(r) for r in self.roots()}\n\n def __str__(self):\n return \'\\n\'.join(\'{}: {}\'.format(r, self.members(r)) for r in self.roots())\n\n\ndef dijkstra_heap(s, edge, n):\n \n d = [10**20] * n\n used = [True] * n \n d[s] = 0\n used[s] = False\n edgelist = []\n for a,b in edge[s]:\n heapq.heappush(edgelist,a*(10**6)+b)\n while len(edgelist):\n minedge = heapq.heappop(edgelist)\n \n if not used[minedge%(10**6)]:\n continue\n v = minedge%(10**6)\n d[v] = minedge//(10**6)\n used[v] = False\n for e in edge[v]:\n if used[e[1]]:\n heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])\n return d\n\n\ndef factorization(n):\n arr = []\n temp = n\n for i in range(2, int(-(-n**0.5//1))+1):\n if temp%i==0:\n cnt=0\n while temp%i==0:\n cnt+=1\n temp //= i\n arr.append([i, cnt])\n\n if temp!=1:\n arr.append([temp, 1])\n\n if arr==[]:\n arr.append([n, 1])\n\n return arr\n\n\ndef lcm(x, y):\n return (x * y) // gcd(x, y)\n\n\ndef lcm_list(numbers):\n return reduce(lcm, numbers, 1)\n\n\ndef gcd_list(numbers):\n return reduce(gcd, numbers)\n\n\ndef is_prime(n):\n if n <= 1:\n return False\n p = 2\n while True:\n if p ** 2 > n:\n break\n if n % p == 0:\n return False\n p += 1\n return True\n\n\n\ndef eratosthenes(limit):\n A = [i for i in range(2, limit+1)]\n P = []\n\n while True:\n prime = min(A)\n \n if prime > sqrt(limit):\n break\n \n P.append(prime)\n \n i = 0\n while i < len(A):\n if A[i] % prime == 0:\n A.pop(i)\n continue\n i += 1\n \n for a in A:\n P.append(a)\n \n return P\n\n\ndef permutation_with_duplicates(L):\n\n if L == []:\n return [[]]\n\n else:\n ret = []\n\n \n S = sorted(set(L))\n\n for i in S:\n\n data = L[:]\n data.remove(i)\n\n for j in permutation_with_duplicates(data):\n ret.append([i] + j)\n\n return ret\n\n\n\nn, k = map(int, input().split()) \na = [i - 1 for i in list(map(int, input().split()))]\n# print(a)\nb = []\ncurrent = 0\n# cnt = 0\nvisited = [0 for i in range(n)]\n# print("a =", a)\nfr = None\nwhile True:\n if visited[current] == 1:\n fr = current \n break \n visited[current] = 1\n b.append(current)\n current = a[current]\nb2 = [fr]\ncurrent2 = fr\nwhile True:\n \n nextp = a[current2]\n if nextp == fr:\n break\n b2.append(nextp)\n current2 = nextp\n# print("b2 =", b2)\n# print("b =", b)\nm = len(b)\nm2 = len(b2)\nif m > k:\n print(b[k] + 1)\nelse:\n k -= m\n k %= m2\n print(b2[k] + 1)']
['Wrong Answer', 'Accepted']
['s155828477', 's634545592']
[34020.0, 34036.0]
[205.0, 227.0]
[4291, 4589]
p02684
u426649993
2,000
1,048,576
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.
['root = list()\n\n\ndef dfs(A, num, N):\n if A[num] in root:\n return root.index(A[num])\n else:\n root.append(A[num])\n return dfs(A, A[num]-1, N)\n\n\ndef main():\n N, K = map(int, input().split())\n A = list(map(int, input().split()))\n\n start = dfs(A, 0, N)\n\n if K <= len(root):\n print(root[K-1])\n else:\n index = (K - start) % (len(root)-start)\n print(root[index])\n\n\nif __name__ == "__main__":\n main()\n', 'import sys\nsys.setrecursionlimit(10**6)\n\nroot = list()\n\n\ndef dfs(A, num, N, memo):\n if memo[A[num]-1] == 1:\n return root.index(A[num])\n else:\n root.append(A[num])\n memo[A[num]-1] = 1\n return dfs(A, A[num]-1, N, memo)\n\n\ndef main():\n N, K = map(int, input().split())\n A = list(map(int, input().split()))\n\n memo = [0] * N\n start = dfs(A, 0, N, memo)\n\n if K <= len(root):\n print(root[K-1])\n else:\n index = (K - start) % (len(root)-start)\n loop = root[start:]\n print(loop[index-1])\n\n\nif __name__ == "__main__":\n main()\n']
['Runtime Error', 'Accepted']
['s783303583', 's955626891']
[32372.0, 193324.0]
[78.0, 322.0]
[457, 597]
p02684
u428199834
2,000
1,048,576
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.
['N,K=map(int,input().split())\nA=list(map(int,input().split()))\ns=1\nB=[1]\nfor i in range(K):\n s=A[s-1]\n if s in B:\n u=B.index(s)\n u_=len(B)-u\n t=(K-u+1)%u_\n if t>=1 and u>=1:\n print(B[u+t-1])\n break\n elif u==0:\n t=(K+1)%u_\n print(B[t-1])\n break\n else:\n print(B[u])\n break\n else:\n B.append(s)\n \nprint(s) \n', 'n,k=map(int,input().split())\na=list(map(int,input().split()))\nvisit=[0]*n\nvisit[0]=1\nnow=0\nc=0\nfor i in range(k):\n now_=now\n now=a[now]-1\n if visit[now]==0:\n visit[now]=visit[now_]+1\n else:\n c=1\n break\nif c==1:\n t=(k+2-visit[now])%(visit[now_]-visit[now]+1)\n if t==0:\n print(now_+1)\n else:\n i=now\n for _ in range(t-1):\n i=a[i]-1\n print(i+1) \nelse:\n u=now+1\n print(u)\n ']
['Wrong Answer', 'Accepted']
['s157415658', 's746474662']
[32380.0, 32176.0]
[2206.0, 134.0]
[382, 404]
p02684
u430726059
2,000
1,048,576
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.
['n,k=map(int, input().split())\na=list(map(int, input().split()))\nalist=[]\ni=1\nwhile a[i-1] not in alist:\n i=a[i-1]\n alist.append(i)\nloop1=alist.index(a[i-1])\nif k<loop1+1:\n print(alist[k-1])\nelse:\n lenloop=len(alist)-loop1\n print(alist[((k-loop1-1)%lenroop)+loop1])', 'n,k=map(int, input().split())\na=list(map(int, input().split()))\nalist=[]\ncnt=0\ni=1\nblist=[0]*(n+1)\nwhile blist[a[i-1]]==0:\n i=a[i-1]\n alist.append(i)\n blist[i]=1\n cnt+=1\nloop1=alist.index(a[i-1])\nif k<loop1+1:\n print(alist[k-1])\nelse:\n print(alist[((k-loop1-1)%(cnt-loop1))+loop1])']
['Runtime Error', 'Accepted']
['s474784240', 's922383655']
[32216.0, 32372.0]
[2206.0, 167.0]
[269, 287]
p02684
u431658223
2,000
1,048,576
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.
['import sys\ninput = lambda : sys.stdin.readline().strip()\nn,k = map(int,input().split())\nk = k%n\na = list(map(int,input().split()))\ncur = 0\nwhile k:\n k-=1\n cur = a[cur]-1\nprint(cur+1)', 'import sys\ninput = lambda : sys.stdin.readline().strip()\nn,k = map(int,input().split())\na = list(map(int,input().split()))\nsala = set([0,a[0]-1])\nf = 0\ns = a[0]-1\npower = 1\nlength = 1\nwhile f!=s:\n if length==power:\n power*=2\n length=0\n f = s \n s = a[s]-1\n sala.add(s)\n length+=1\nstart = len(sala)-length\nif k<=start:\n cur = 0 \n while k:\n k-=1\n cur = a[cur]-1\n print(cur+1)\nelse:\n k-=start \n k%=length\n f = 0\n s = 0 \n while length:\n s = a[s]-1\n length-=1\n while s!=f:\n s = a[s]-1\n f = a[f]-1\n cur = f \n while k:\n cur = a[cur]-1\n k-=1\n print(cur+1)']
['Wrong Answer', 'Accepted']
['s731561278', 's438498104']
[32372.0, 35724.0]
[113.0, 251.0]
[188, 671]
p02684
u432251613
2,000
1,048,576
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.
['def main():\n \n N,K = map(int, input().split())\n A = list(map(int, input().split()))\n\n \n \n \n now = 0\n history = [0]\n i = 0\n while i<K:\n \n print(i)\n now = A[now]-1\n \n if now not in history:\n history.append(now)\n i+=1\n \n else:\n \n skip = len(history)-history.index(now)-1\n now = history[history.index(now)+skip]\n i += skip\n\n print(now+1)\n\nmain()\n', 'import sys\ndef main():\n \n\tinput = sys.stdin.readline\n N,K = map(int, input.split())\n input = sys.stdin.readline\n A = list(map(int, input.split()))\n\n \n \n \n now = 0\n history = [0]\n i = 0\n while i<K:\n \n now = A[now]-1\n \n if now not in history:\n history.append(now)\n i+=1\n \n else:\n start = history.index(now)\n r = start + (K-1-i)%(len(history)-start)\n now = history[r]\n break\n\n print(now+1)\n\nmain()\n', 'def main():\n \n N,K = map(int, input().split())\n A = list(map(int, input().split()))\n\n \n \n \n now = 0\n history = [0]\n isVisited = [False for i in range(N)]\n i = 0\n while i<K:\n \n now = A[now]-1\n \n #if now not in history:\n if isVisited[now] == False:\n history.append(now)\n isVisited[now] = True\n i+=1\n \n else:\n start = history.index(now)\n r = start + (K-1-i)%(len(history)-start)\n now = history[r]\n break\n\n print(now+1)\n\nmain()\n']
['Wrong Answer', 'Runtime Error', 'Accepted']
['s773907470', 's799745825', 's800363437']
[50108.0, 9012.0, 31888.0]
[2277.0, 24.0, 122.0]
[869, 904, 951]
p02684
u433371341
2,000
1,048,576
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.
['N, K = [int(n) for n in input().split()]\nA = [int(n) for n in input().split()]\n\ndest = []\n\ncurrent = 0\nnext_dest = A[current]\ncount = 0\nseen = set()\n\nwhile not next_dest in seen:\n dest.append(next_dest)\n current = next_dest-1\n next_dest = A[current]\n seen.add(current)\n\ndest.append(next_dest)\nmodulus = len(dest)\n\nprint(dest[(K - 1) % modulus])\n', 'N, K = [int(n) for n in input().split()]\n\nA = [1] + [int(n) for n in input().split()]\n\ndest = []\n\ncurrent = 0\ncount = 0\nnext_dest = A[current]\nseen = {}\n\nwhile not next_dest in seen:\n current = next_dest\n next_dest = A[current]\n dest.append(current)\n seen[current] = count\n count += 1\n\ncycle_start = seen[next_dest]\ncycle_length = seen[current] - cycle_start + 1\n\nif K <= cycle_start:\n print(dest[K])\nelse:\n print(dest[cycle_start + ((K - cycle_start) % cycle_length)])']
['Wrong Answer', 'Accepted']
['s559532551', 's634541973']
[96940.0, 40008.0]
[2208.0, 167.0]
[357, 490]
p02684
u434609232
2,000
1,048,576
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.
['import sys\n\nN, K = list(map(int, input().split()))\nA = list(map(int, input().split()))\nA = [0] + A\ntown = 1\nvisited = [0]*(N+1)\n\nfor i in range(1, K+1):\n town = A[town]\n if visited[town] == 0:\n visited[town] = i\n else:\n loop = i - visited[town]\n town = (K - visited[town]) % loop + visited[town]\nprint(town)', 'import sys\n\nN, K = list(map(int, input().split()))\nA = list(map(int, input().split()))\nA = [0] + A\ntown = 1\nvisited = [0]*(N+1)\n\nfor i in range(1, K+1):\n town = A[town]\n if visited[town] == 0:\n visited[town] = i\n else:\n loop = i - visited[town]\n town = (K - visited[town]) % loop + visited[town]\n break\nprint(town)', 'import sys\n\nN, K = list(map(int, input().split()))\nA = list(map(int, input().split()))\nA = [0] + A\ntown = 1\nvisited = [0]*(N+1)\n\nfor i in range(1, K+1):\n town = A[town]\n if visited[town] == 0:\n visited[town] = i\n else:\n loop = i - visited[town]\n place = (K - visited[town]) % loop + visited[town]\n print(visited.index(place))\n sys.exit()\nprint(town)']
['Runtime Error', 'Wrong Answer', 'Accepted']
['s474180109', 's741728173', 's078899674']
[32328.0, 32376.0, 32312.0]
[166.0, 137.0, 129.0]
[337, 351, 393]
p02684
u440129511
2,000
1,048,576
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.
['n,k=map(int,input().split())\na=list(map(int,input().split()))\nl=[]*n\nfor i in range(n):\n b=[0,0]\n b[0]+=i+1\n b[1]+=a[i]\n l.append(b)\nc=0\nne=0\nl1=[0]*n\nwhile True:\n if c==k:\n print(l[ne][1])\n exit()\n ne+=-ne+l[ne][1]-1\n c+=1\n if l1[ne]==0:l1[ne]+=1\n else:\n roop_act+=ne\n break\n\nroop_c=0\nwhile True:\n if c+roop_c==k:\n print(l[ne][1])\n exit()\n if l1[ne]==1:l1[ne]+=1\n else:break\n ne+=-ne+l[ne][1]-1\n roop_c+=1\n\nre=(k-c)%roop_c\nif re==0:print(l[ne][0])\nelse:\n for i in range(re):\n if i==re-1:print(l[ne][1])\n ne+=-ne+l[ne][1]-1', 'n,k=map(int,input().split())\na=list(map(int,input().split()))\nl=[]*n\nfor i in range(n):\n b=[0,0]\n b[0]+=i+1\n b[1]+=a[i]\n l.append(b)\nc=0\nne=0\nl1=[0]*n\nwhile True:\n if c==k:\n print(l[ne][1])\n exit()\n ne+=-ne+l[ne][1]-1\n c+=1\n if l1[ne]==0:l1[ne]+=1\n else:\n break\n\nroop_c=0\nwhile True:\n if c+roop_c==k:\n print(l[ne][1])\n exit()\n if l1[ne]==1:l1[ne]+=1\n else:break\n ne+=-ne+l[ne][1]-1\n roop_c+=1\n\nre=(k-c)%roop_c\nif re==0:print(l[ne][0])\nelse:\n for i in range(re):\n if i==re-1:print(l[ne][1])\n ne+=-ne+l[ne][1]-1', 'n,k=map(int,input().split())\na=list(map(int,input().split()))\nl1=[0]*n\nl1[0]+=1\nc=0 \nnow=0\nne=a[now]-1 \nwhile True:\n if c==k:\n print(now+1)\n exit()\n if l1[ne]==0:\n l1[ne]+=1\n now=ne\n ne=a[now]-1\n c+=1\n else:\n now=ne\n ne=a[now]-1\n c+=1\n break\n\nroop_c=0\nwhile True:\n if c+roop_c==k:\n print(now+1)\n exit()\n if l1[ne]==1:\n l1[ne]+=1\n now=ne\n ne=a[now]-1\n roop_c+=1\n else:break\n\nre=(k-c)%roop_c\nif re==0:print(now+1)\nelse:\n for i in range(re):\n if i==re-1:print(ne+1)\n ne+=-ne+a[ne]-1']
['Runtime Error', 'Wrong Answer', 'Accepted']
['s783097508', 's964142921', 's080996343']
[50072.0, 50088.0, 32308.0]
[329.0, 409.0, 180.0]
[624, 603, 701]
p02684
u443872523
2,000
1,048,576
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.
['n, k = map(int, input().split())\na = list(map(int, input().split()))\n\nl = [1]\nind = 0\nval = 0\nfor i in range(n):\n val = a[ind]\n l.append(val)\n ind = val - 1\n\nprint("l:", l)\ncnt = 1\nrepeat =[]\nrepeat.append(l.pop())\nz = 0\n\nfor j in range(n - 1, -1, -1):\n z = l.pop()\n if z == repeat[0]:\n print("yes")\n break\n else:\n cnt += 1\n repeat.append(z)\n\nrepeat = repeat[::-1]\n\njouyo = (k- n - 1) % cnt\n\nprint(repeat[jouyo])\n', 'n, k = map(int, input().split())\na = list(map(int, input().split()))\n\nl = [1]\nind = 0\nval = 0\nfor i in range(n):\n val = a[ind]\n l.append(val)\n ind = val - 1\n\nif n >=k:\n answer1 = l[k]\n\ncnt = 1\nrepeat =[]\nrepeat.append(l.pop())\nz = 0\n\nfor j in range(n - 1, -1, -1):\n z = l.pop()\n if z == repeat[0]:\n break\n else:\n cnt += 1\n repeat.append(z)\n\nrepeat = repeat[::-1]\n\njouyo = (k - n - 1) % cnt\n\nif n >= k:\n print(answer1)\nelse:\n print(repeat[jouyo])\n']
['Wrong Answer', 'Accepted']
['s973600241', 's594739941']
[32404.0, 32424.0]
[207.0, 175.0]
[459, 494]
p02684
u445624660
2,000
1,048,576
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.
['\nn, k = map(int, input().split())\na = list(map(lambda x: int(x) - 1, input().split()))\ncount_arr = [0] * n\ncount = 0\n\nnow = 0\nloop_length = -1\nwhile True:\n if count == k:\n print(a[now] + 1)\n exit()\n if count_arr[now] != 0:\n loop_length = count - count_arr[now] + 1\n break\n count += 1\n count_arr[now] = count\n now = a[now]\n\n\nrest = (k - count) % loop_length\n\n\nif rest == 0:\n print(now + 1)\n exit()\n\nwhile rest > 0:\n print(f"now = {now}(next = {a[now]})")\n rest -= 1\n if rest == 0:\n break\n now = a[now]\nprint(a[now] + 1)\n# 727202214173249352', '\n\n\n\n\nn, k = map(int, input().split())\na = list(map(lambda x: int(x) - 1, input().split()))\ncount_arr = [0] * n\ncount = 0\n\nnow = 0\nloop_length = -1\nwhile True:\n if count == k:\n print(now + 1)\n exit()\n if count_arr[now] != 0:\n loop_length = count - count_arr[now] + 1\n break\n count += 1\n count_arr[now] = count\n now = a[now]\n\n\nrest = (k - count) % loop_length\n\n\nif rest == 0:\n print(now + 1)\n exit()\n\n#\nwhile rest > 0:\n now = a[now]\n rest -= 1\nprint(a[now] + 1)', '\n\nn, k = map(int, input().split())\na = list(map(lambda x: int(x) - 1, input().split()))\ncount_arr = [0] * n\ncount = 0\n\nnow = 0\nloop_length = -1\nwhile True:\n if count == k:\n print(now + 1)\n exit()\n if count_arr[now] != 0:\n loop_length = count - count_arr[now] + 1\n break\n count += 1\n count_arr[now] = count\n now = a[now]\n\n\nrest = (k - count) % loop_length\n\n\nif rest == 0:\n print(now + 1)\n exit()\n\nwhile rest > 0:\n rest -= 1\n if rest == 0:\n break\n now = a[now]\nprint(a[now] + 1)\n# 727202214173249352']
['Wrong Answer', 'Wrong Answer', 'Accepted']
['s875588894', 's927951547', 's289726512']
[33336.0, 32264.0, 32308.0]
[167.0, 149.0, 147.0]
[778, 1038, 733]
p02684
u446808186
2,000
1,048,576
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.
['N,K=map(int,(input().split()))\nTO=list(map(int,input().split()))\n\nvisit_flag=[[0]*2 for i in [1]*N]\nvisit_count=0\nfirst_visit=0\nlast_visit=0\nnext_town=1\nvisit_flag[0][0]=1\nvisit_flag[0][1]=0\n\nfor i in range(K):\n visit_count+=1\n next_town = TO[next_town-1]\n if visit_flag[next_town-1][0]==0:\n visit_flag[next_town-1][0]=1\n visit_flag[next_town-1][1]=visit_count\n else:\n first_visit=visit_flag[next_town-1][1]\n last_visit=visit_count\n break\n\nroop=last_visit-first_visit\nspan=visit_flag[next_town-1][1]\n\nif K<=roop:\n next_town = 1\n for i in range(K): \n next_town = TO[next_town-1]\n ans=next_town\nelse:\n# if roop==0:\n# ans=next_town\n else:\n for i in range((K-span)%roop):\n next_town = TO[next_town-1]\n ans=next_town\n\nprint(ans)', 'N,K=map(int,(input().split()))\nTO=list(map(int,input().split()))\n\nvisit_flag=[[0]*2 for i in [1]*N]\nvisit_count=0\nfirst_visit=0\nlast_visit=0\nnext_town=1\nvisit_flag[0][0]=1\nvisit_flag[0][1]=1\n\nfor i in range(K):\n visit_count+=1\n next_town = TO[next_town-1]\n if visit_flag[next_town-1][0]==0:\n visit_flag[next_town-1][0]=1\n visit_flag[next_town-1][1]=visit_count\n else:\n first_visit=visit_flag[next_town-1][1]\n last_visit=visit_count\n break\n\nroop=last_visit-first_visit\n\nif K<=roop:\n next_town = 1\n for i in range(K): \n next_town = TO[next_town-1]\n ans=next_town\nelse:\n for i in range((K-roop)%roop):\n next_town = TO[next_town-1]\n ans=next_town\n\nprint(ans)', 'N,K=map(int,(input().split()))\nTO=list(map(int,input().split()))\n\nvisit_flag=[[0]*2 for i in [1]*N]\nvisit_count=0\nfirst_visit=0\nlast_visit=0\nnext_town=1\nvisit_flag[0][0]=1\nvisit_flag[0][1]=1\n\nfor i in range(K):\n visit_count+=1\n next_town = TO[next_town-1]\n if visit_flag[next_town-1][0]==0:\n visit_flag[next_town-1][0]=1\n visit_flag[next_town-1][1]=visit_count\n else:\n first_visit=visit_flag[next_town-1][1]\n last_visit=visit_count\n break\n\nroop=last_visit-first_visit\n\nif K<=roop:\n next_town = 1\n for i in range(K): \n next_town = TO[next_town-1]\n ans=next_town\nelse:\n for i in range((K-roop+1)%roop):\n next_town = TO[next_town-1]\n ans=next_town\n\nprint(ans)', 'N,K=map(int,(input().split()))\nTO=list(map(int,input().split()))\n\nvisit_flag=[[0]*2 for i in [1]*N]\nvisit_count=0\nfirst_visit=0\nlast_visit=0\nnext_town=1\nvisit_flag[0][0]=1\nvisit_flag[0][1]=0\n\nfor i in range(K):\n visit_count+=1\n next_town = TO[next_town-1]\n if visit_flag[next_town-1][0]==0:\n visit_flag[next_town-1][0]=1\n visit_flag[next_town-1][1]=visit_count\n else:\n first_visit=visit_flag[next_town-1][1]\n last_visit=visit_count\n break\n\nroop=last_visit-first_visit\n\nif K<=roop:\n next_town = 1\n for i in range(K): \n next_town = TO[next_town-1]\n ans=next_town\nelse:\n for i in range((K-roop+1)%roop):\n next_town = TO[next_town-1]\n ans=next_town\n\nprint(ans)', 'N,K=map(int,(input().split()))\nTO=list(map(int,input().split()))\n\nvisit_flag=[[0]*2 for i in [1]*N]\nvisit_count=0\nfirst_visit=0\nlast_visit=0\nnext_town=1\nvisit_flag[0][0]=1\nvisit_flag[0][1]=0\n\nfor i in range(K):\n visit_count+=1\n next_town = TO[next_town-1]\n if visit_flag[next_town-1][0]==0:\n visit_flag[next_town-1][0]=1\n visit_flag[next_town-1][1]=visit_count\n else:\n first_visit=visit_flag[next_town-1][1]\n last_visit=visit_count\n break\n\nroop=last_visit-first_visit\nspan=visit_flag[next_town-1][1]\n\nif K<=roop:\n next_town = 1\n for i in range(K): \n next_town = TO[next_town-1]\n ans=next_town\nelse:\n if roop==0:\n ans=next_town\n else:\n for i in range((K-span)%roop):\n next_town = TO[next_town-1]\n ans=next_town\n\nprint(ans)']
['Runtime Error', 'Runtime Error', 'Runtime Error', 'Runtime Error', 'Accepted']
['s158762166', 's225774514', 's608026707', 's787256985', 's110996754']
[9052.0, 40776.0, 40668.0, 40664.0, 40876.0]
[25.0, 298.0, 301.0, 283.0, 312.0]
[832, 734, 736, 736, 826]
p02684
u447899880
2,000
1,048,576
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.
['n,k=map(int,input().split())\na=list(map(int,input().split()))\n\nans=0\nnow=1\ni=1\nresult=[1]\nwhile i<=k:\n now=a[now]\n if now in result:\n x=result.index(now)\n length=i-1-x\n ans=result[(k-x)%length+x]\n result.append(now)\n i+=1\n \nif ans!=0:\n print(ans)\nelse:\n print(result[-1])', 'n,k=map(int,input().split())\na=list(map(int,input().split()))\n\nans=0\nnow=1\ni=1\nresult=[1]\nwhile i<=k:\n now=a[now-1]\n if now in result:\n x=result.index(now)\n length=i-1-x\n ans=result[(k-x)%length+x]\n result.append(now)\n i+=1\n \nif ans!=0:\n print(ans)\nelse:\n print(result[-1])', 'n,k=map(int,input().split())\na=list(map(lambda x:int(x)-1,input().split()))\n\nans=-1\nl=[-1]*n\nnow=0\ni=1\nresult=[0]\nwhile i<=k:\n now=a[now]\n if l[now]!=-1:\n \n length=i-l[now]\n d=(k-i)%length+l[now]\n ans=l.index(d)\n break\n l[now]=i\n i+=1\n \nif ans!=-1:\n print(ans+1)\nelse:\n print(now+1)']
['Runtime Error', 'Runtime Error', 'Accepted']
['s210961376', 's288347931', 's153942460']
[35420.0, 34508.0, 32300.0]
[2206.0, 2206.0, 141.0]
[313, 315, 338]
p02684
u449580152
2,000
1,048,576
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.
['n, k = map(int, input().split())\nA = list(map(int, input().split()))\ndef f(x):\n return A[x-1]\nc = 1, P = [1]\n\nfor i in range(k):\n c = f(c)\n P.append(c)\n if c in P:\n roop_start = P.find(c)\n roop_num = i + 1- roop_start\n break\na = (k - roop_start) % roop_num\nfor i in range(a):\n roop_start = f(roop_start)\nprint(roop_start)', 'n, k = map(int, input().split())\nA = list(map(int, input().split()))\nc = 1\nP = [1]\nfor i in range(1,k+1):\n c = A[c-1]\n if i == k:\n print(c)\n exit(0)\n if c in P:\n roop_start = P.index(c)\n roop_num = i- roop_start\n a = (k - roop_start) % roop_num\n b = P[a + roop_start]\n print(b)\n P.append(c)', 'n, k = map(int, input().split())\nA = list(map(int, input().split()))\ndef f(x):\n return A[x-1]\nc = 1\nP = [1]\n \nfor i in range(k):\n c = f(c)\n P.append(c)\n if c in P:\n roop_start = P.find(c)\n roop_num = i + 1- roop_start\n break\na = (k - roop_start) % roop_num\nfor i in range(a):\n roop_start = f(roop_start)\nprint(roop_start)', 'n, k = map(int, input().split())\nA = list(map(int, input().split()))\ndoubleA = {}\ndoubleA[1] = [A[i-1] for i in range(n)]\ndef f(x):\n return A[x-1]\nfor i in range(2, 73):\n doubleA[i] = [doubleA[i-1][doubleA[i-1][j]-1] for j in range(n)]\nc = k - 2**(len(bin(k))-3)\nstart = doubleA[len(bin(k))-3][0]\nfor i in range(c):\n start = f(start)\nprint(start)', 'n, k = map(int, input().split())\nA = list(map(int, input().split()))\ndoubleA = {}\ndoubleA[1] = [A[i-1] for i in range(n)]\n\ndef f(x):\n return A[x-1]\nfor i in range(2, 73):\n doubleA[i] = [doubleA[i-1][doubleA[i-1][j]-1] for j in range(n)]\nc = k - 2**(len(bin(k))-3)\nstart = doubleA[2**(len(bin(k))-3)][0]\nfor i in range(c):\n start = f(start)\nprint(start)', 'n, k = map(int, input().split())\nA = list(map(int, input().split()))\ndef f(x):\n return A[x-1]\nc = 1\nP = [1]\n \nfor i in range(k):\n c = f(c)\n P.append(c)\n if c in P:\n roop_start = P.index(c)\n roop_num = i + 1- roop_start\n break\na = (k - roop_start) % roop_num\nfor i in range(a):\n roop_start = f(roop_start)\nprint(roop_start)', 'n, k = map(int, input().split())\nA = list(map(int, input().split()))\ndoubleA = {}\ndoubleA[1] = A\nfor i in range(2, 73):\n doubleA[i] = [doubleA[i-1][doubleA[i-1][j]-1] for j in range(n)]\nnow = 1\nfor i in range(len(bin(k))-3):\n if (k>>i)&1:\n now = doubleA[i+1][now-1]\nprint(now)\n', 'n, k = map(int, input().split())\nA = list(map(int, input().split()))\ndef f(x):\n return A[x-1]\nc = 1\nP = [1]\n \nfor i in range(1,k+1):\n c = f(c)\n if c in P:\n roop_start = P.index(c)\n roop_num = i- roop_start\n break\n P.append(c)\na = (k - roop_start) % roop_num\nfor i in range(a):\n P[roop_start] = f(P[roop_start])\nprint(roop_start)', 'n, k = map(int, input().split())\nA = list(map(int, input().split()))\nnow = 1\n \nfor i in range(len(bin(k))-2):\n if (k>>i)&1:\n now = A[now-1]\n A = [A[a-1] for a in A]\nprint(now)']
['Runtime Error', 'Wrong Answer', 'Runtime Error', 'Wrong Answer', 'Runtime Error', 'Runtime Error', 'Wrong Answer', 'Runtime Error', 'Accepted']
['s165977471', 's192229849', 's415729900', 's603060250', 's607505989', 's682208192', 's731819277', 's866114848', 's231802504']
[9140.0, 46964.0, 32352.0, 113504.0, 117612.0, 32376.0, 116636.0, 32236.0, 32364.0]
[23.0, 2223.0, 69.0, 2210.0, 2208.0, 68.0, 2209.0, 2206.0, 1251.0]
[359, 352, 359, 355, 361, 360, 290, 365, 188]
p02684
u455957433
2,000
1,048,576
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.
['def main():\n \n IN = [int(_) for _ in input().split()]\n A = [int(_) for _ in input().split()]\n N = IN[0]\n K = IN[1]\n\n loop = []\n target = 1\n mark = [False] * N\n print(mark)\n mark[0] = True\n while True:\n target = A[target -1]\n loop.append(target)\n if mark[target-1]:\n break\n mark[target-1] = True \n\n looper = loop\n si = len(loop)-1\n if loop[si] != 1:\n K = K - loop[si] -1\n looper = loop[loop[si]-1:si]\n \n ind = (len(looper) % K) - 1\n print(A[ind])\n \n\n\n\nif __name__ == "__main__":\n main()\n', 'def main():\n \n IN = [int(_) for _ in input().split()]\n A = [int(_) for _ in input().split()]\n N = IN[0]\n K = IN[1]\n\n \n l = []\n l.append(1)\n \n nextInd = 1\n \n mark = [False] * N\n mark[0] = True\n \n startLoopCount = 0\n \n while True:\n nextInd = A[nextInd-1]\n if mark[nextInd-1]:\n startLoopCount = l.index(nextInd)\n break\n l.append(nextInd)\n mark[nextInd-1] = True\n \n move = l[0:startLoopCount]\n loop = l[startLoopCount:len(l)]\n\n # print(startLoopCount)\n # print(move)\n # print(loop)\n \n if K < startLoopCount:\n print(move[K])\n return\n index = (K - startLoopCount) % len(loop)\n print(loop[index])\n return\n\nif __name__ == "__main__":\n main()\n']
['Wrong Answer', 'Accepted']
['s658052158', 's319955837']
[32256.0, 32352.0]
[140.0, 125.0]
[604, 1098]
p02684
u459798349
2,000
1,048,576
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.
['n,k=map(int,input().split())\nA=[int(i) for i in input().split()]\n\nwent={i:0 for i in range(1,n+1)}\n\nnow=1\ncnt=1\nfor i in range(n+1):\n if went[now]>0:\n t=cnt\n s=went[now]\n break\n else:\n went[now]=cnt\n now=A[now-1]\n cnt+=1\n\nval=((k-t)%(t-s+1)-1)%(t-s+1)+s\nfor ele in went:\n if went[ele]==val:\n print(ele)\n break', 'n,k=map(int,input().split())\nA=[int(i) for i in input().split()]\n\nwent={i:0 for i in range(1,n+1)}\n\nnow=1\ncnt=1\nfor i in range(n+1):\n if went[now]>0:\n t=cnt-1\n s=went[now]\n break\n else:\n went[now]=cnt\n now=A[now-1]\n cnt+=1\n\n\nif k <= t-1:\n val=k+1\nelse:\n val=((k+1-t)%(t-s+1)-1)%(t-s+1)+s\nfor ele in went:\n if went[ele]==val:\n print(ele)\n break']
['Wrong Answer', 'Accepted']
['s677450652', 's227140895']
[40460.0, 40592.0]
[262.0, 290.0]
[374, 414]
p02684
u460229551
2,000
1,048,576
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.
['n,k=list(map(int,input().split()))\nAs=list(map(int,input().split()))\nroot=[1]\ncount=[0]*n\ncount[0]+=1\n\nfor i in range(k):\n if count[root[-1]-1]!=0:\n break\n else:\n root.append(As[root[-1]-1])\n count[As[root[-1]-1]-1]+=1\nprint(root[k%len(root)])', 'n,k=list(map(int,input().split()))\nAs=list(map(int,input().split()))\nroot=[1]\ncount=[0]*n\ncount[0]+=1\n\ncnt=0\nfor _ in range(k):\n dis=As[root[-1]-1]-1\n if count[dis]!=0:\n cnt+=1\n break\n else:\n root.append(dis+1)\n count[dis]+=1\nif cnt==1:\n first=root.index(As[root[-1]-1])\n print(root[(k-first)%(len(root)-first)+first])\nelse:\n print(root[-1])']
['Wrong Answer', 'Accepted']
['s319794088', 's257375954']
[32376.0, 32376.0]
[68.0, 163.0]
[270, 387]
p02684
u464205401
2,000
1,048,576
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.
['n,k=list(map(int,input().split()))\na=[0]+ list(map(int,input().split()))\n\nvisited=[1]\n\nfor i in range(n):\n next=a[visited[-1]]\n visited.append(next)\n \nloop_end=n\nloop_start=n-1\nwhile visited[loop_start]!=visited[loop_end]:\n loop_start-=1\nperiod=loop_end-loop_start\n#print(visited) \n#print("p:",period)\n#print("k%period:",k%period)\n\nif k<n+1:\n print(visited[k])\n exit()\nelse:\n print(visited[n-(k-n)%period+1])', 'n,k=list(map(int,input().split()))\na=[0]+ list(map(int,input().split()))\n\nvisited=[1]\n\nfor i in range(n):\n next=a[visited[-1]]\n visited.append(next)\n \nloop_end=n\nloop_start=n-1\nwhile visited[loop_start]!=visited[loop_end]:\n loop_start-=1\nperiod=loop_end-loop_start\n#print(visited) \n#print("p:",period)\n#print("(k-n)%period:",(k-n)%period)\n\nif k<n+1:\n print(visited[k])\n exit()\nelse:\n print(visited[n-period+(k-n)%period])']
['Runtime Error', 'Accepted']
['s932768421', 's227737584']
[32376.0, 32364.0]
[162.0, 179.0]
[415, 428]
p02684
u465900169
2,000
1,048,576
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.
['N, K = map(int, input().split())\nA = list(map(int, input().split()))\nB = []\ni = 0\nB.append(1)\nfor n in range(N):\n B.append(A[i])\n i = A[i]-1\nif K>N:\n start = 0\n num = 0\n for j in range(len(B)):\n if B[len(B)-1] == B[j]:\n start = j\n break\n for j in range(1,len(B)-start):\n if B[start+j] == B[start]:\n num = j\n break\n print(B[((K-start)%num)+start],((K-start)%num)+start)\nelse:\n print(B[K])', 'N, K = map(int, input().split())\nA = list(map(int, input().split()))\nB = []\ni = 0\nB.append(1)\nfor n in range(N):\n B.append(A[i])\n i = A[i]-1\nif K>N:\n start = 0\n num = 0\n for j in range(len(B)):\n if B[len(B)-1] == B[j]:\n start = j\n break\n for j in range(1,len(B)-start):\n if B[start+j] == B[start]:\n num = j\n break\n print(B[((K-start)%num)+start])\nelse:\n print(B[K])']
['Wrong Answer', 'Accepted']
['s282364812', 's663270287']
[32260.0, 32380.0]
[176.0, 179.0]
[421, 399]
p02684
u466143662
2,000
1,048,576
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.
['n, k = map(int, input().split())\na = list(map(int, input().split()))\n\nidx = a[0]\nhist = [1]\nflag = 0\nfor i, _ in enumerate(range(k), start=1):\n if idx in hist:\n hist.append(idx)\n flag = 1\n break\n hist.append(idx)\n idx = a[idx-1]\n \nif flag:\n l_start2, l_end2 = hist.index(idx), i\n surplus = (k-l_start) % (l_end - l_start)\n print(hist[l_start+surplus])\nelse:\n print(idx)', 'n, k = map(int, input().split())\na = list(map(int, input().split()))\n\nidx = a[0]\nhist = [1]\npassed = [0] * n\nflag = 0\nfor i, _ in enumerate(range(k), start=1):\n if passed[idx-1] == 1:\n hist.append(idx)\n passed[idx-1] = 1\n flag = 1\n break\n hist.append(idx)\n passed[idx-1] = 1\n idx = a[idx-1]\n \n \nif flag:\n l_start, l_end = hist.index(idx), i\n surplus = (k - l_start) % (l_end - l_start)\n print(hist[l_start + surplus])\nelse:\n print(hist[-1])']
['Runtime Error', 'Accepted']
['s073002843', 's779279566']
[32312.0, 32328.0]
[2206.0, 153.0]
[414, 498]
p02684
u469254913
2,000
1,048,576
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.
['import numpy as np\n# import math\n# import copy\n# from collections import deque\nimport sys\ninput = sys.stdin.readline\n\nfrom numba import njit\n\n\n@njit\ndef make_my_route(route,A,N):\n pre = 1\n for i in range(N):\n now = A[pre-1]\n temp = np.sum(route == now)\n if temp == 1:\n break\n route = np.append(route,now)\n pre = now\n return (route,now)\n\n\ndef main():\n N,K = map(int,input().split())\n A = list(map(int,input().split()))\n A = np.array(A)\n\n route = np.array([1])\n\n route,now = make_my_route(route,A,N)\n start = int(np.where(route == now)[0])\n\n if K < start:\n res = route[K]\n elif start+1 != N:\n K -= start\n loop = route[start:]\n \n M = len(loop)\n K %= M\n res = loop[K]\n else:\n res = route[K]\n\n print(res)\n\n\n\nmain()\n', 'import numpy as np\n# import math\n# import copy\n# from collections import deque\nimport sys\ninput = sys.stdin.readline\n\nfrom numba import njit\n\n\n@njit\ndef make_my_route(route,A,N):\n pre = 1\n for i in range(N):\n now = A[pre-1]\n route = np.append(route,now)\n temp = np.sum(route == now)\n if temp == 2:\n break\n pre = now\n return route\n\n\ndef main():\n N,K = map(int,input().split())\n A = list(map(int,input().split()))\n A = np.array(A)\n\n route = np.array([1])\n\n route = make_my_route(route,A,N)\n now = route[-1]\n start = int(np.where(route == now)[0][0])\n\n if K < start:\n res = route[K]\n elif start+1 != N:\n K -= start\n loop = route[start:-1]\n \n M = len(loop)\n K %= M\n res = loop[K]\n else:\n res = route[K]\n\n print(res)\n\n\n\nmain()\n', 'import numpy as np\n# import math\n# import copy\n# from collections import deque\nimport sys\ninput = sys.stdin.readline\n\nfrom numba import njit\n\n\n@njit\ndef make_my_route(route,A,N):\n pre = 1\n for i in range(N):\n now = A[pre-1]\n temp = np.sum(route == now)\n if temp == 1:\n break\n route = np.append(route,now)\n pre = now\n return (route,now)\n\n\ndef main():\n N,K = map(int,input().split())\n A = list(map(int,input().split()))\n A = np.array(A)\n\n route = np.array([1])\n\n route,now = make_my_route(route,A,N)\n start = int(np.where(route == now)[0])\n\n if start+1 != N:\n K -= start\n loop = route[start:]\n \n M = len(loop)\n K %= M\n res = loop[K]\n else:\n res = route[K]\n\n print(res)\n\n\n\nmain()\n', '# import numpy as np\n# import math\n# import copy\n# from collections import deque\nimport sys\ninput = sys.stdin.readline\n\n# from numba import njit\n\n\ndef main():\n N,K = map(int,input().split())\n A = list(map(int,input().split()))\n\n s = []\n ord = [-1] * (N+1)\n v = 1\n\n while ord[v] == -1:\n ord[v] = len(s)\n s.append(v)\n v = A[v-1]\n\n c = len(s) - ord[v]\n l = ord[v]\n\n if K < l:\n res = s[K]\n else:\n K -= l\n K %= c\n res = s[l+K]\n\n print(res)\n\n\n\nmain()\n']
['Time Limit Exceeded', 'Time Limit Exceeded', 'Wrong Answer', 'Accepted']
['s436778737', 's719649963', 's949790191', 's040319753']
[119696.0, 119764.0, 119636.0, 30852.0]
[2210.0, 2210.0, 2209.0, 120.0]
[896, 911, 853, 557]
p02684
u471503862
2,000
1,048,576
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.
['n, k = map(int, input().split())\na = list(map(int,input().split()))\ntimestamp = [0]*n\nposition = 1\nperiod = 0\nstart=0\nfor i in range(n):\n if timestamp[position-1] != 0:\n start = timestamp[position-1]\n period = i-timestamp[position-1]\n break\n timestamp[position-1] =i\n position = a[position-1]\nprint(period,start)\n\n\nif k <=start:\n position = 1\n for i in range(k-1):\n position = a[position-1]\n print(position)\n \nelse:\n cycle = (k-start)%period\n for i in range(cycle):\n position = a[position-1]\n print(position) \n ', 'n, k = map(int, input().split())\na = list(map(int,input().split()))\ntimestamp = [0]*n\nposition = 1\nperiod = 0\nstart=0\nfor i in range(n*2):\n if timestamp[position-1] != 0:\n start = timestamp[position-1]\n period = i-timestamp[position-1]\n break\n timestamp[position-1] =i\n position = a[position-1]\n \n\nif k <=start:\n position = 1\n for i in range(k):\n position = a[position-1]\n print(position)\n \nelse:\n cycle = (k-start)%period\n for i in range(cycle):\n position = a[position-1]\n print(position) \n ']
['Runtime Error', 'Accepted']
['s952190476', 's921348355']
[32376.0, 32384.0]
[174.0, 165.0]
[537, 519]
p02684
u475402977
2,000
1,048,576
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.
['N,K=(int(x) for x in input().split())\nA=list(map(int, input().split()))\nif K<=N:\n s=1\n for i in range(K):\n s=A[s-1]\n print(s)\nelse:\n s=1\n S={1}\n iloop=1\n while A[s-1] not in S:\n S.add(A[s-1])\n s=A[s-1]\n iloop=iloop+1\n p=s\n loop=1\n while A[s-1] != p:\n loop=loop+1\n s=A[s-1]\n g=(K-iloop)%loop+iloop\n s=1\n for i in range(g):\n s=A[s-1]\n print(s)\n print(s)', 'N,K=(int(x) for x in input().split())\nA=list(map(int, input().split()))\nif K<=N:\n s=1\n for i in range(K):\n s=A[s-1]\n print(s)\nelse:\n s=1\n S={1}\n iloop=1\n while A[s-1] not in S:\n S.add(A[s-1])\n s=A[s-1]\n iloop=iloop+1\n p=s\n loop=0\n while A[s-1] != p:\n loop=loop+1\n s=A[s-1]\n g=(K-iloop)%loop+iloop\n s=1\n for i in range(g):\n s=A[s-1]\n print(s)\n', 'N,K=(int(x) for x in input().split())\nA=list(map(int, input().split()))\nif K<=N:\n s=1\n for i in range(K):\n s=A[s-1]\n print(s)\nelse:\n s=1\n S={1}\n iloop=1\n while A[s-1] not in S:\n S.add(A[s-1])\n s=A[s-1]\n iloop=iloop+1\n p=s\n loop=1\n while A[s-1] != p:\n loop=loop+1\n s=A[s-1]\n g=(K-iloop)%loop+iloop\n s=1\n for i in range(g):\n s=A[s-1]\n print(s)']
['Wrong Answer', 'Runtime Error', 'Accepted']
['s221233739', 's860111431', 's443971689']
[32448.0, 32408.0, 32640.0]
[242.0, 212.0, 247.0]
[449, 433, 432]
p02684
u475503988
2,000
1,048,576
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.
['N, K = map(int, input().split())\nA = list(map(int, (input().split())))\n\ndone = [0]*N\nnow = 0\ncnt = 1\nwhile True:\n if done[now] != 0:\n start = done[now] - 1\n cycle = cnt - start - 1\n break\n else:\n done[now] = cnt\n now = A[now]-1\n cnt += 1\nif K>=start:\n K -= start\n K %= cycle\nfor _ in range(K):\n now = A[now]-1\nprint(now+1)\nprint(start, cycle, now, done)', 'N, K = map(int, input().split())\nA = list(map(int, (input().split())))\n\ndone = [0]*N\nnow = 0\ncnt = 1\nwhile True:\n if done[now] != 0:\n start = done[now] - 1\n cycle = cnt - start - 1\n break\n else:\n done[now] = cnt\n now = A[now]-1\n cnt += 1\nif K>=start:\n K -= start\n K %= cycle\nelse:\n now = 0\nfor _ in range(K):\n now = A[now]-1\nprint(now+1)']
['Wrong Answer', 'Accepted']
['s627333073', 's672868677']
[32396.0, 32372.0]
[210.0, 182.0]
[410, 397]
p02684
u475675023
2,000
1,048,576
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.
['n,k,*a=map(int,open(0).read().split())\nb=[1]\ns_b={1}\nfor i in range(n):\n tmp=a[b[-1]-1]\n if tmp in s_b:\n idx=b.index(tmp)\n break\n else:\n b.append(tmp)\n s_b|={tmp}\nmod=len(b[idx:])\nd=len(b[:idx])\nprint(d,(k-d)%mod)\nprint(b[d+(k-d)%mod])', 'n,k,*a=map(int,open(0).read().split())\na=[0]+a\nans=1\nwhile k:\n if k&1:\n ans=a[ans]\n a=[a[i] for i in a]\n k>>=1\nprint(ans)']
['Wrong Answer', 'Accepted']
['s401390064', 's208002444']
[33592.0, 31792.0]
[171.0, 1325.0]
[250, 127]
p02684
u476674874
2,000
1,048,576
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.
["import numpy as np\ndef read_int():\n return int(input())\n\ndef read_ints():\n return map(int, input().split())\n\ndef read_lines(N):\n return [input() for i in range(N)]\n\ndef main():\n N, K = read_ints()\n towns = np.array(list(read_ints())) - 1\n portals = {i: towns[i] for i in towns}\n\n loop = []\n next_town = 0\n while next_town not in loop:\n loop.append(next_town)\n next_town = portals[next_town]\n loop.append(next_town)\n\n i = loop.index(next_town)\n all_loop = loop\n pre_loop, loop = loop[:i], loop[i:]\n\n if K < len(pre_loop):\n print(pre_loop[K] + 1)\n elif len(pre_loop) <= K < len(all_loop):\n print(loop[K - len(pre_loop)] + 1)\n else:\n print(loop[(K - len(pre_loop)) % len(loop)] + 1)\n \n\n\n\nif __name__ == '__main__':\n main()", "\ndef read_int():\n return int(input())\n\ndef read_ints():\n return map(int, input().split())\n\ndef read_lines(N):\n return [input() for i in range(N)]\n\ndef main():\n N, K = read_ints()\n towns = [int(input()) - 1 for i in range(N)]\n portals = {i: towns[i] for i in towns}\n\n loop = []\n next_town = 0\n while next_town not in loop:\n loop.append(next_town)\n next_town = portals[next_town]\n loop.append(next_town)\n\n i = loop.index(next_town)\n all_loop = loop\n pre_loop, loop = loop[:i], loop[i:]\n\n if K < len(pre_loop):\n print(pre_loop[K] + 1)\n elif len(pre_loop) <= K < len(all_loop):\n print(loop[K - len(pre_loop)] + 1)\n else:\n print(loop[(K - len(pre_loop)) % len(loop)] + 1)\n \n\n\n\nif __name__ == '__main__':\n main()", "import numpy as np\ndef read_int():\n return int(input())\n\ndef read_ints():\n return map(int, input().split())\n\ndef read_lines(N):\n return [input() for i in range(N)]\n\ndef main():\n N, K = read_ints()\n towns = np.array(list(read_ints())) - 1\n #portals = {i: towns[i] for i in towns}\n portals = towns\n\n loop = []\n next_town = 0\n while next_town not in loop:\n loop.append(next_town)\n next_town = portals[next_town]\n loop.append(next_town)\n\n i = loop.index(next_town)\n all_loop = loop\n pre_loop, loop = loop[:i], loop[i:]\n\n if K < len(pre_loop):\n print(pre_loop[K])\n elif len(pre_loop) <= K < len(all_loop):\n print(loop[K - len(pre_loop)])\n else:\n print(loop[(K - len(pre_loop)) % len(loop)])\n \n\n\n\nif __name__ == '__main__':\n main()", 'def read_int():\n return int(input())\n\ndef read_ints():\n return map(int, input().split())\n\ndef read_lines(N):\n return [input() for i in range(N)]\n\ndef main():\n N, K = read_ints()\n towns = [int(t) -1 for t in input().split() ]\n #print(N, K , towns)\n\n loop = []\n loop_set = set()\n next_town = towns[0]\n while next_town not in loop_set:\n #print(f"next_t={next_town}")\n loop.append(next_town)\n loop_set.add(next_town)\n next_town = towns[next_town]\n\n i = loop.index(next_town)\n all_loop = loop\n pre_loop = loop[:i]\n loop = loop[i:]\n pre_length = len(pre_loop)\n loop_length = len(loop)\n\n """\n for k in range(1, 10):\n if k - 1 < len(all_loop):\n print(f"k={k}", all_loop[k-1] + 1)\n else:\n i = ((k -1 - len(pre_loop)) % loop_length)\n print(i, pre_loop, loop)\n print(f"k={k}", loop[i]+1)\n """\n\n #print(pre_loop, loop)\n if K - 1 < len(all_loop):\n print(all_loop[K-1] + 1)\n else:\n i = ((K -1 - pre_length) % loop_length)\n #print(i, pre_loop, loop)\n print(loop[i]+1)\n\nif __name__ == \'__main__\':\n main()']
['Runtime Error', 'Runtime Error', 'Wrong Answer', 'Accepted']
['s003676130', 's315427903', 's407254503', 's585218955']
[57668.0, 12428.0, 51096.0, 32484.0]
[2207.0, 30.0, 2206.0, 146.0]
[812, 799, 821, 1174]
p02684
u478378139
2,000
1,048,576
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.
['tem1=input().split()\nN = int(tem1[0])\nK = int(tem1[1])\ntem2 = input().split()\nnow =1\nfact = K%N\nfor i in range(0,fact):\n now = int(tem2[now-1])\nprint(now)', 'tem1=input().split()\nN = int(tem1[0])\nK = int(tem1[1])\ntem2 = input().split()\njud = [0 for i in range(len(tem2))]\nnow =1\ncircle=0\nfor i in range(0,K):\n if jud[now-1]==0:\n jud[now-1]=i\n now = int(tem2[now - 1])\n else:\n circle=i-jud[now-1]\n fact=(K-i-1)%circle\n for j in range(0,fact+1):\n now = int(tem2[now - 1])\n break\n\nprint(now)']
['Wrong Answer', 'Accepted']
['s957997166', 's394393427']
[24460.0, 30804.0]
[94.0, 194.0]
[157, 389]
p02684
u478719560
2,000
1,048,576
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.
['import sys\n#from collections import defaultdict, deque, Counter\n\n#import heapq\n#import math\n#from itertools import accumulate\n#from itertools import permutations as perm\n#from itertools import combinations as comb\n#from itertools import combinations_with_replacement as combr\n#from fractions import gcd\n#import numpy as np\n\nstdin = sys.stdin\nsys.setrecursionlimit(10 ** 7)\nMIN = -10 ** 9\nMOD = 10 ** 9 + 7\nINF = float("inf")\nIINF = 10 ** 18\n\n#n = int(stdin.readline().rstrip())\nn,k = map(int, stdin.readline().rstrip().split())\nA = list(map(int, stdin.readline().rstrip().split()))\n#numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]\n#word = [stdin.readline().rstrip() for _ in range(n)]\n#number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]\n\n\n# find loop\nvisited = []\nvisited.append(0)\nnext = A[0] - 1\nloop = 0\nfor i in range(n-1):\n if next in visited:\n loop = i+1-visited.index(next)\n break\n else:\n visited.append(next)\n next = A[next] - 1\n\nroad = len(visited) - loop\nprint(visited, road, loop)\n\nk = k - road\nk = k%loop\nprint(visited[road + k] + 1)\n', 'import sys\n#from collections import defaultdict, deque, Counter\n\n#import heapq\n#import math\n#from itertools import accumulate\n#from itertools import permutations as perm\n#from itertools import combinations as comb\n#from itertools import combinations_with_replacement as combr\n#from fractions import gcd\n#import numpy as np\n\nstdin = sys.stdin\nsys.setrecursionlimit(10 ** 7)\nMIN = -10 ** 9\nMOD = 10 ** 9 + 7\nINF = float("inf")\nIINF = 10 ** 18\n\n#n = int(stdin.readline().rstrip())\nn,k = map(int, stdin.readline().rstrip().split())\nA = list(map(int, stdin.readline().rstrip().split()))\n#numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]\n#word = [stdin.readline().rstrip() for _ in range(n)]\n#number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]\n\n\n# find loop\nvisited = [0]*n\nvisited[0] = 1\nnext = A[0] - 1\nloop = 0\nv = 0\nfor i in range(n):\n if visited[next] != 0:\n loop = i-visited[next]+2\n v = i\n break\n else:\n visited[next]= i+2\n next = A[next] - 1\n\nroad = v+1 - loop\n\nif k <= road:\n print(visited.index(k+1) + 1)\n exit()\nk = k - road\nk = k%loop\nprint(visited.index(k+1+road) +1)\n']
['Wrong Answer', 'Accepted']
['s709314575', 's528248406']
[32272.0, 32372.0]
[2207.0, 151.0]
[1179, 1221]
p02684
u480847874
2,000
1,048,576
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.
['def m():\n n, k = map(int, input().split())\n a = [0] + list(map(int, input().split()))\n visited = [[] for _ in range(n + 1)]\n visited[1].append(0)\n cur = 1\n i = 1\n while True:\n cur = a[cur]\n visited[cur].append(i)\n i += 1\n if len(visited[cur]) >= 2:\n break\n\n if k <= visited[cur][0]:\n now = 1\n for p in range(1, k+1):\n now = a[now]\n return now\n else:\n tmp = k - visited[cur][0]\n MOD = visited[cur][1] - visited[cur][0]\n count = tmp % MOD\n now = 1\n for p in range(1, count+1+tmp):\n now = a[now]\n return now\n\n\nprint(m())\n', 'def m():\n n, k = map(int, input().split())\n a = [0] + list(map(int, input().split()))\n visited = [[] for _ in range(n + 1)]\n visited[1].append(0)\n cur = 1\n i = 1\n while True:\n cur = a[cur]\n visited[cur].append(i)\n i += 1\n if len(visited[cur]) >= 2:\n break\n\n if k <= visited[cur][0]:\n now = 1\n for p in range(1, k+1):\n now = a[now]\n return now\n else:\n tmp = k - visited[cur][0]\n MOD = visited[cur][1] - visited[cur][0]\n count = tmp % MOD\n now = 1\n for p in range(1, count+1+visited[cur][0]):\n now = a[now]\n return now\n\n\nprint(m())\n']
['Wrong Answer', 'Accepted']
['s810622195', 's152662838']
[43448.0, 43516.0]
[2207.0, 213.0]
[669, 681]
p02684
u485979475
2,000
1,048,576
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.
['import sys\nn,k=map(int,input().split())\nalist=list(map(int,input().split()))\n\nvislist=[1]\nvisset={1}\nloopflag=True\nendflag=False\ndonek=0\nima=1\n\nwhile loopflag:\n ima=alist[ima-1]\n if ima in visset:\n loopflag=False\n if ima == alist[ima-1]:\n endflag=True\n else:\n vislist.append(ima)\n visset.add(ima)\n\n donek+=1\n\n if donek == k:\n loopflag=False\n endflag=True\n\nif endflag:\n print(ima)\n sys.exit()\n\nloopstartindex=vislist.index(ima)\nlooplength=len(visset)-loopstartindex\nif (k+1-loopstartindex)%looplength == 0:\n print(vislist[loopstartindex])\nelse:\n print(vislist[loopstartindex+(k+1-loopstartindex)%looplength-1])\n\n', 'import sys\nn,k=map(int,input().split())\nalist=list(map(int,input().split()))\n\nvislist=[1]\nvisset={1}\nloopflag=True\nendflag=False\ndonek=0\nima=1\n\nwhile loopflag:\n ima=alist[ima-1]\n if ima in visset:\n loopflag=False\n if ima == alist[ima-1]:\n endflag=True\n else:\n vislist.append(ima)\n visset.add(ima)\n\n donek+=1\n\n if donek == k:\n loopflag=False\n endflag=True\n\nif endflag:\n print(ima)\n sys.exit()\n\nloopstartindex=vislist.index(ima)\nlooplength=len(visset)-loopstartindex\nif (k+1-loopstartindex)%looplength == 0:\n print(vislist[-1])\nelse:\n print(vislist[loopstartindex+(k+1-loopstartindex)%looplength-1])\n\n']
['Wrong Answer', 'Accepted']
['s459426820', 's954529397']
[32640.0, 32556.0]
[167.0, 157.0]
[707, 695]
p02684
u486074261
2,000
1,048,576
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.
['from sys import stdin\n\n\ndef main():\n N, K = [int(x) for x in stdin.readline().rstrip().split()]\n As = [int(x) for x in stdin.readline().rstrip().split()]\n\n done = [0] * N\n step = -1\n start = 0\n current = 1\n between = 0\n\n while True:\n if done[current - 1] > 0:\n start = done[current - 1] - 1\n between = step - done[current - 1] + 1\n break\n step += 1\n done[current - 1] = step\n current = As[current - 1]\n print(done)\n\n if K <= current:\n print(done.index(K) + 1)\n else:\n res = (K - start) % between\n print(done.index(res) + 1)\n\n\nif __name__ == "__main__":\n main()\n', 'from sys import stdin\n\n\ndef main():\n N, K = [int(x) for x in stdin.readline().rstrip().split()]\n As = [int(x) for x in stdin.readline().rstrip().split()]\n\n done = [0] * N\n step = -1\n start = 0\n current = 1\n between = 0\n\n while True:\n if done[current - 1] > 0:\n start = done[current - 1] - 1\n between = step - done[current - 1] + 1\n break\n step += 1\n done[current - 1] = step\n current = As[current - 1]\n\n if K <= step:\n print(done.index(K) + 1)\n else:\n res = (K - start) % between\n if res == 0:\n res = between\n print(done.index(res + start) + 1)\n\n\nif __name__ == "__main__":\n main()\n']
['Wrong Answer', 'Accepted']
['s611976863', 's806359754']
[32168.0, 33572.0]
[148.0, 162.0]
[679, 715]
p02684
u487594898
2,000
1,048,576
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.
['#!/usr/bin/env python3\n\nimport sys\n\n\ninput = sys.stdin.readline\n#n = int(input())\n#n,k = map(int,input().split())\n#A = list(map(int,input().split()))\n#s=input()\n#a = input()\nn,k= map(int,input().split())\nA = [0]\nA += list(map(int,input().split()))\nB = [-1 for _ in range(n+1)]\nB[1] = 0\nnow =1\ncnt = 0\ncic = 0\nfor i in range(1,n+1):\n now = A[now]\n cnt +=1\n if B[now] ==-1:\n B[now] = cnt\n else:\n cic = cnt - B[now]\n road = B[now] \n break\nprint(B,road,cic,cnt)\nif k < road:\n print(B.index(k)+1) \nelse:\n print(B.index(((k-road)%cic+road)))', 'n,k= map(int,input().split())\nA = [0]\nA += list(map(int,input().split()))\nB = [-1 for _ in range(n+1)]\nB[1] = 0\nnow =1\ncnt = 0\ncic = 0\nfor i in range(1,n+1):\n now = A[now]\n cnt +=1\n if B[now] ==-1:\n B[now] = cnt\n else:\n cic = cnt - B[now]\n road = B[now] \n break\n\nif k <= road:\n print(B.index(k)) \nelse:\n print(B.index(((k-road)%cic+road)))']
['Wrong Answer', 'Accepted']
['s011583777', 's748974618']
[31260.0, 32248.0]
[196.0, 159.0]
[582, 386]
p02684
u490195279
2,000
1,048,576
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.
['from numba import njit\nimport numpy as np\n\n\ndef main(N, K, A):\n d = np.zeros(N,np.int32)\n d=d-1\n p = 0\n for i in range(K):\n if d[p-1] == 0:\n d[p-1] = i\n p = A[p-1]\n else:\n p = A[p-1]\n loop = i-d[p-1]\n nokori = (K-d[p-1]) % loop\n for _ in range(nokori):\n p = A[p-1]\n return p\n return p\n\n\nN, K = map(int, input().split())\nA = list(map(int, input().split()))\nans=main(N, K, A)\nprint(ans)\n', 'from numba import njit\nimport numpy as np\n\n\ndef main(N, K, A):\n d = np.zeros(N, np.int32)\n d = d-1\n p = 1\n for i in range(K):\n if d[p-1] == -1:\n d[p-1] = i\n p = A[p-1]\n else:\n p = A[p-1]\n loop = i-d[p-1]+1\n nokori = (K-d[p-1]) % loop\n for _ in range(nokori):\n p = A[p-1]\n return p\n return p\n\n\nN, K = map(int, input().split())\nA = list(map(int, input().split()))\nans = main(N, K, A)\nprint(ans)\n']
['Wrong Answer', 'Accepted']
['s935201542', 's675874452']
[115268.0, 115320.0]
[435.0, 701.0]
[506, 514]
p02684
u493318999
2,000
1,048,576
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.
['n,k = map(int,input().split())\na = list(map(int,input().split()))\nnum = [a[0]]\nwhile True:\n for i in range(len(num)-1):\n if num[i] == num[-1]:\n break\n num.append(a[num[-1]])\nindex = index(a[-1])\nlstart = len(a[:index-1])\nlroop = len(a[index])\nnumber = (k-lstart)%lroop\nfor i in range(lroop-1):\n number = a[number-1]\n print(number)\nprint(number)', 'import collections\nn,k = map(int,input().split())\na = list(map(int,input().split()))\nlist = [1]\ni = 0\nnum = a[0]\nfor i in range(3*n):\n list.append(num)\n num = a[num-1]\n\ncnt = collections.Counter(list)\nonevalue = [k for k,v in cnt.items() if v == 1]\nlstart = len(onevalue)\nlroop = len(set(list))-lstart\n\nif k <= lstart:\n num = a[0]\n for i in range(k-1):\n num = a[num-1]\n print(num)\n exit()\nelse:\n k = (k-lstart)%lroop\n num = a[0]\n if lstart > 0:\n for i in range(lstart-1):\n num = a[num-1]\n for i in range(k):\n num = a[num-1]\n print(num)\n exit()', 'import collections\nn,k = map(int,input().split())\na = list(map(int,input().split()))\nlist = [1]\ni = 0\nnum = a[0]\nfor i in range(3*n):\n list.append(num)\n num = a[num-1]\n\ncnt = collections.Counter(list)\nonevalue = [k for k,v in cnt.items() if v == 1]\nlstart = len(onevalue)\nlroop = len(set(list))-lstart\n\nif k <= lstart:\n num = a[0]\n for i in range(k-1):\n num = a[num-1]\n print(num)\n exit()\nelif lstart == 0:\n k = (k-lstart)%lroop\n num = 1\n for i in range(k):\n num = a[num-1]\n print(num)\n exit()\nelse:\n k = (k-lstart)%lroop\n num = a[0]\n if lstart > 0:\n for i in range(lstart-1):\n num = a[num-1]\n for i in range(k):\n num = a[num-1]\n print(num)\n exit()']
['Runtime Error', 'Wrong Answer', 'Accepted']
['s063570011', 's213846390', 's350664943']
[32296.0, 50880.0, 50948.0]
[2206.0, 334.0, 340.0]
[374, 611, 738]
p02684
u502126017
2,000
1,048,576
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.
['n,k=map(int,input().split())\na=list(map(int,input().split()))\ntown=[1]\ncnt=0\nloop=0\nindex=0\nwhile(len(town) <= k):\n town.append(a[town[-1]-1])\n cnt+=1\n index=len(town)-1\n if cnt != index:\n loop = cnt - index\n town.pop(-1)\n break\n\nif k<index:\n print(town[k])\nelse:\n k-=index\n k%=loop\n print(town[index+k])\n', 'n,k=map(int,input().split())\na=list(map(int,input().split()))\n\ns=[]\nordn=[]\nfor i in range(n+1):\n ordn.append(-1)\n\nv=1\nwhile(ordn[v] == -1):\n ordn[v]=len(s)\n s.append(v)\n v=a[v-1]\n\nc=len(s)-ordn[v]\nl=ordn[v]\n\nif k<l:\n print(s[k])\nelse:\n k -= l\n k%=c\n print(s[l+k])']
['Runtime Error', 'Accepted']
['s097569271', 's611531424']
[63616.0, 32380.0]
[2207.0, 189.0]
[350, 288]
p02684
u509739538
2,000
1,048,576
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.
['import math\nimport queue\nfrom collections import defaultdict\n \ndef readInt():\n\treturn int(input())\ndef readInts():\n\treturn list(map(int, input().split()))\ndef readChar():\n\treturn input()\ndef readChars():\n\treturn input().split()\ndef factorization(n):\n\tres = []\n\tif n%2==0:\n\t\tres.append(2)\n\tfor i in range(3,math.floor(n//2)+1,2):\n\t\tif n%i==0:\n\t\t\tc = 0\n\t\t\tfor j in res:\n\t\t\t\tif i%j==0:\n\t\t\t\t\tc=1\n\t\t\tif c==0:\n\t\t\t\tres.append(i)\n\treturn res\ndef fact2(n):\n\tp = factorization(n)\n\tres = []\n\tfor i in p:\n\t\tc=0\n\t\tz=n\n\t\twhile 1:\n\t\t\tif z%i==0:\n\t\t\t\tc+=1\n\t\t\t\tz/=i\n\t\t\telse:\n\t\t\t\tbreak\n\t\tres.append([i,c])\n\treturn res\ndef fact(n):\n\tans = 1\n\tm=n\n\tfor _i in range(n-1):\n\t\tans*=m\n\t\tm-=1\n\treturn ans\ndef comb(n,r):\n\tif n<r:\n\t\treturn 0\n\tl = min(r,n-r)\n\tm=n\n\tu=1\n\tfor _i in range(l):\n\t\tu*=m\n\t\tm-=1\n\treturn u//fact(l)\ndef printQueue(q):\n\tr=qb\n\tans=[0]*r.qsize()\n\tfor i in range(r.qsize()-1,-1,-1):\n\t\tans[i] = r.get()\n\tprint(ans)\ndef dq():\n\treturn queue.deque()\nclass UnionFind():\n\tdef __init__(self, n):\n\t\tself.n = n\n\t\tself.parents = [-1]*n\n\n\tdef find(self, x): # root\n\t\tif self.parents[x]<0:\n\t\t\treturn x\n\t\telse:\n\t\t\tself.parents[x] = self.find(self.parents[x])\n\t\t\treturn self.parents[x]\n\n\tdef union(self,x,y):\n\t\tx = self.find(x)\n\t\ty = self.find(y)\n\n\t\tif x==y:\n\t\t\treturn\n\n\t\tif self.parents[x]>self.parents[y]:\n\t\t\tx,y = y,x\n\n\t\tself.parents[x]+=self.parents[y]\n\t\tself.parents[y]=x\n\n\tdef size(self,x):\n\t\treturn -1*self.parents[self.find(x)]\n\n\tdef same(self,x,y):\n\t\treturn self.find(x)==self.find(y)\n\n\tdef members(self,x): # much time\n\t\troot = self.find(x)\n\t\treturn [i for i in range(self.n) if self.find(i)==root]\n\n\tdef roots(self):\n\t\treturn [i for i,x in enumerate(self.parents) if x<0]\n\n\tdef group_count(self):\n\t\treturn len(self.roots())\n\n\tdef all_group_members(self):\n\t\treturn {r: self.members(r) for r in self.roots()} # 1~n\n\nn,k = readInts()\na = readInts()\nex = [0]*n\nex[0] = 0\nex_r = []\nf = 1\nl = 1\ncount = 0\nif a[0]==1:\n\tprint(1)\n\texit()\nwhile 1:\n\tl = a[l-1]\n\tex_r.append(l)\n\tif ex[l-1]==1:\n\t\tbreak\n\tex[l-1]=1\n\nfirst = ex_r.index(ex_r[-1])\n#print(ex_r)\np2 = ex_r[:-1]\n#print(p2)\nrepeat = len(p2)-first\n#print(first,repeat)\n\nif k<=len(p2):\n\tprint(ex_r[k-1])\nelse:\n\ty = (k-first)%repeat+repeat\n\tprint(ex_r[first+y-1])', 'import math\nimport queue\nfrom collections import defaultdict\n \ndef readInt():\n\treturn int(input())\ndef readInts():\n\treturn list(map(int, input().split()))\ndef readChar():\n\treturn input()\ndef readChars():\n\treturn input().split()\ndef factorization(n):\n\tres = []\n\tif n%2==0:\n\t\tres.append(2)\n\tfor i in range(3,math.floor(n//2)+1,2):\n\t\tif n%i==0:\n\t\t\tc = 0\n\t\t\tfor j in res:\n\t\t\t\tif i%j==0:\n\t\t\t\t\tc=1\n\t\t\tif c==0:\n\t\t\t\tres.append(i)\n\treturn res\ndef fact2(n):\n\tp = factorization(n)\n\tres = []\n\tfor i in p:\n\t\tc=0\n\t\tz=n\n\t\twhile 1:\n\t\t\tif z%i==0:\n\t\t\t\tc+=1\n\t\t\t\tz/=i\n\t\t\telse:\n\t\t\t\tbreak\n\t\tres.append([i,c])\n\treturn res\ndef fact(n):\n\tans = 1\n\tm=n\n\tfor _i in range(n-1):\n\t\tans*=m\n\t\tm-=1\n\treturn ans\ndef comb(n,r):\n\tif n<r:\n\t\treturn 0\n\tl = min(r,n-r)\n\tm=n\n\tu=1\n\tfor _i in range(l):\n\t\tu*=m\n\t\tm-=1\n\treturn u//fact(l)\ndef printQueue(q):\n\tr=qb\n\tans=[0]*r.qsize()\n\tfor i in range(r.qsize()-1,-1,-1):\n\t\tans[i] = r.get()\n\tprint(ans)\ndef dq():\n\treturn queue.deque()\nclass UnionFind():\n\tdef __init__(self, n):\n\t\tself.n = n\n\t\tself.parents = [-1]*n\n\n\tdef find(self, x): # root\n\t\tif self.parents[x]<0:\n\t\t\treturn x\n\t\telse:\n\t\t\tself.parents[x] = self.find(self.parents[x])\n\t\t\treturn self.parents[x]\n\n\tdef union(self,x,y):\n\t\tx = self.find(x)\n\t\ty = self.find(y)\n\n\t\tif x==y:\n\t\t\treturn\n\n\t\tif self.parents[x]>self.parents[y]:\n\t\t\tx,y = y,x\n\n\t\tself.parents[x]+=self.parents[y]\n\t\tself.parents[y]=x\n\n\tdef size(self,x):\n\t\treturn -1*self.parents[self.find(x)]\n\n\tdef same(self,x,y):\n\t\treturn self.find(x)==self.find(y)\n\n\tdef members(self,x): # much time\n\t\troot = self.find(x)\n\t\treturn [i for i in range(self.n) if self.find(i)==root]\n\n\tdef roots(self):\n\t\treturn [i for i,x in enumerate(self.parents) if x<0]\n\n\tdef group_count(self):\n\t\treturn len(self.roots())\n\n\tdef all_group_members(self):\n\t\treturn {r: self.members(r) for r in self.roots()} # 1~n\ndef bitArr(n):\n\tx = 1\n\tzero = "0"*n\n\tans = []\n\tans.append([0]*n)\n\tfor i in range(2**n-1):\n\t\tans.append(list(map(lambda x:int(x),list((zero+bin(x)[2:])[-1*n:]))))\n\t\tx+=1\n\treturn ans;\ndef arrsSum(a1,a2):\n\tfor i in range(len(a1)):\n\t\ta1[i]+=a2[i]\n\treturn a1\n\n\nn,k = readInts()\na = readInts()\n\nnow = [1]\ncheck = [0]*n\ncheck[0] = 1\nl = 1\n\nwhile check[l-1]<2:\n\tl = a[l-1]\n\tcheck[l-1]+=1\n\tnow.append(l)\n\nzen = now.index(now[-1])\nkou = len(now)-zen\n\nif k<zen:\n\tprint(now[k])\nelse:\n\tx = (k-zen)%(kou-1)\n\tprint(now[zen:][x])']
['Runtime Error', 'Accepted']
['s460674707', 's287554482']
[32228.0, 32160.0]
[164.0, 160.0]
[2224, 2363]
p02684
u511449169
2,000
1,048,576
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.
['while True:\n k -= 1\n cnt += 1\n if k == 0:\n break\n dir = a[dir - 1]\n if hash_a[dir - 1] == 0 or hash_a[dir - 1] == 1 or hash_a[dir - 1] == 2:\n hash_a[dir - 1] += 1\n if hash_a[dir - 1] == 2:\n two += 1\n elif hash_a[dir - 1] == 3:\n k = (k + cnt) - (cnt - (two * 2))\n k %= two\n hash_a = [-k for _ in range(n)]\nprint(dir)', 'n, k = map(int, input().split())\na = list(map(int, input().split()))\ndir = a[0]\nhash_a = [0 for _ in range(n)]\nhash_a[0] = 1\ncnt = 0\ntwo = 0\nwhile True:\n k -= 1\n cnt += 1\n if k == 0:\n break\n dir = a[dir - 1]\n if hash_a[dir - 1] == 0 or hash_a[dir - 1] == 1 or hash_a[dir - 1] == 2:\n hash_a[dir - 1] += 1\n if hash_a[dir - 1] == 2:\n two += 1\n elif hash_a[dir - 1] == 3:\n k = (k + cnt) - (cnt - (two * 2))\n k %= two\n hash_a = [-k for _ in range(n)]\nprint(dir)']
['Runtime Error', 'Accepted']
['s607320384', 's549066938']
[9140.0, 32404.0]
[24.0, 362.0]
[386, 527]
p02684
u511457539
2,000
1,048,576
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.
['# = int(input())\nN, K = map(int, input().split())\nA = list(map(int, input().split()))\n\n\nnext = 1\nList = [-1]*N\n\nfor i in range(K):\n if List[next-1] == -1:\n List[next-1] = i\n next = A[next-1]\n\n else:\n loop = i - List[next-1]\n afterloop = (K-(i-loop+1))%loop\n print(List.index(i - loop + afterloop)+1)\n exit()\n\nprint(next)\n', '# = int(input())\nN, K = map(int, input().split())\nA = list(map(int, input().split()))\n\n\nnext = 1\nList = [-1]*N\n\nfor i in range(K):\n if List[next-1] == -1:\n List[next-1] = i\n next = A[next-1]\n\n else:\n loop = i - List[next-1]\n afterloop = (K-(i-loop+1))%loop\n print(List.index(i - loop + afterloop))\n exit()\n\nprint(next)\n', '# = int(input())\nN, K = map(int, input().split())\nA = list(map(int, input().split()))\n\n\nnext = 1\nList = [-1]*N\n\nfor i in range(K):\n if List[next-1] == -1:\n List[next-1] = i\n next = A[next-1]\n\n else:\n loop = i - List[next-1]\n afterloop = (K-(i-loop))%loop\n print(List.index((i-loop) + afterloop)+1)\n exit()\n\nprint(next)\n']
['Wrong Answer', 'Wrong Answer', 'Accepted']
['s823940021', 's851500420', 's807071584']
[32376.0, 32384.0, 32300.0]
[168.0, 162.0, 128.0]
[460, 458, 458]
p02684
u511501183
2,000
1,048,576
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.
['\ndef solve(N,K,A):\n idx = [-1 for _ in range(len(A))]\n now = 0\n period = 0\n bias = 0\n tmp = []\n for i in range(K+1):\n tmp.append(now+1)\n if idx[now] == -1:\n idx[now] = i\n else:\n period = i - idx[now]\n cycle = tmp[-period-1:-1]\n bias = i - period\n break\n\n if K <= bias:\n now = 0\n for _ in range(K):\n now = A[now] - 1\n print(now + 1)\n else:\n K -= bias\n K = K % period\n print(cycle[K])\n\nif __name__=="__main__":\n # import random\n # import numpy as np\n # for N in range(2, 10):\n # A = list(range(1, N+1))\n \n # for K in range(1, 10**18):\n # solve(N, K, A)\n N,K = [int(a) for a in input().split()]\n A = [int(a) for a in input().split()]\n solve(N, K, A)', 'def solve(N, K, A):\n idx = [-1 for _ in range(len(A))]\n now = 0\n period = 0\n bias = 0\n tmp = []\n found = False\n i = 0\n while not found:\n tmp.append(now+1)\n if idx[now] == -1:\n idx[now] = i\n else:\n period = i - idx[now]\n cycle = tmp[-period-1:-1]\n bias = i - period\n break\n now = A[now]-1\n i += 1\n\n if K <= bias:\n now = 0\n for _ in range(K):\n now = A[now] - 1\n print(now + 1)\n else:\n K -= bias\n K = K % period\n print(cycle[K])\n\nif __name__=="__main__":\n N,K = [int(a) for a in input().split()]\n A = [int(a) for a in input().split()]\n solve(N, K, A)']
['Wrong Answer', 'Accepted']
['s806610634', 's510091732']
[32356.0, 33964.0]
[77.0, 171.0]
[868, 729]
p02684
u516566941
2,000
1,048,576
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.
['N,K = map(int,input().split())\nsys.setrecursionlimit(20000000)\nA = list(map(int,input().split()))\nadj_list=[0]*N\nfor i in range(N):\n adj_list[i]=(A[i])\nk=0\nnode = 1\nc=[0]*N\ncount=1\nflag=0\nROOP=[]\nroopstart=0\nwhile k<K:\n k+=1\n c[node-1]+=1\n node = adj_list[node-1]\n if c[node-1] ==1:\n if flag ==0:\n roopstart = k\n flag=1\n ROOP.append(node)\n if c[node-1] ==2:\n break\nnum = (K-len(ROOP)-roopstart) % (len(ROOP))\nprint(ROOP[num])', 'N,K = map(int,input().split())\nsys.setrecursionlimit(20000000)\nA = list(map(int,input().split()))\nadj_list=[0]*N\nfor i in range(N):\n adj_list[i]=(A[i])\nk=0\nnode = 1\nc=[0]*N\ncount=1\nflag=0\nROOP=[]\nroopstart=0\nwhile k<K:\n k+=1\n c[node-1]+=1\n node = adj_list[node-1]\n if c[node-1] ==1:\n if flag ==0:\n roopstart = k\n flag=1\n ROOP.append(node)\n if c[node-1] ==2:\n break\nnum = (K-len(ROOP)-roopstart) % (len(ROOP))\nprint(ROOP[num])', 'N,K = map(int,input().split())\nA = list(map(int,input().split()))\nadj_list=[0]*N\nfor i in range(N):\n adj_list[i]=(A[i])\nk=0\nnode = 1\nc=[0]*N\ncount=1\nflag=0\nROOP=[]\nroopstart=0\nwhile k<K:\n k+=1\n c[node-1]+=1\n node = adj_list[node-1]\n if c[node-1] ==1:\n if flag ==0:\n roopstart = k\n flag=1\n ROOP.append(node)\n if c[node-1] ==2:\n break\nl = len(ROOP)\nif l != 0:\n num = (K-l-roopstart) % (l)\n print(ROOP[num])\nelse:\n print(node)']
['Runtime Error', 'Runtime Error', 'Accepted']
['s250512529', 's524242464', 's599926538']
[9272.0, 9252.0, 32328.0]
[22.0, 26.0, 212.0]
[486, 486, 493]
p02684
u517447467
2,000
1,048,576
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.
['N, K = map(int, input().split())\nM = list(map(lambda x: int(x)-1, input().split()))\ns = 0\norder = [s]\nsets = set(order)\nwhile True:\n if M[s] in sets:\n #print(order, M[s])\n K -= order.index(M[s])\n order = order[order.index(M[s]):]\n print(order[K%len(order)-1]+1)\n break\n order.append(M[s])\n sets.add(M[s])\n s = M[s]\n\n', 'N, K = map(int, input().split())\nM = list(map(lambda x: int(x)-1, input().split()))\ns = 0\norder = [s]\nsets = set(order)\nwhile True:\n if M[s] in sets:\n #print(order, M[s])\n if len(order) >= K:\n print(order[K-1]+1)\n else:\n K -= order.index(M[s])\n order = order[order.index(M[s]):]\n print(K, order, order[K%len(order)]+1)\n break\n order.append(M[s])\n sets.add(M[s])\n s = M[s]\n\n', 'N, K = map(int, input().split())\nM = list(map(lambda x: int(x)-1, input().split()))\ns = 0\norder = [s]\nsets = set(order)\nwhile True:\n if M[s] in sets:\n #print(order, M[s])\n if len(order) > K:\n print(order[K]+1)\n else:\n K -= order.index(M[s])\n order = order[order.index(M[s]):]\n print(order[(K+1)%len(order)-1]+1)\n break\n order.append(M[s])\n sets.add(M[s])\n s = M[s]\n\n']
['Wrong Answer', 'Wrong Answer', 'Accepted']
['s768406431', 's852346533', 's446041122']
[32320.0, 32256.0, 32300.0]
[213.0, 183.0, 177.0]
[335, 409, 402]
p02684
u517910772
2,000
1,048,576
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.
['def d():\n N, K = map(int, input().split())\n # A = list(map(int, input().split()))\n print(\'A = {}\'.format(A))\n warps = [1]\n\n for i in range(K):\n city = A[warps[-1] - 1]\n # print(\'city, warps = {}, {}\'.format(city, warps))\n if city in warps:\n loop_start_index = warps.index(city)\n loop = warps[loop_start_index:]\n K -= loop_start_index\n print(loop[K % len(loop)])\n return\n else:\n warps.append(city)\n print(warps[-1])\n\n\n##########\nif __name__ == "__main__":\n d()\n', 'def d():\n N, K = map(int, input().split())\n A = list(map(int, input().split()))\n print(\'A = {}\'.format(A))\n warps = [1]\n\n for i in range(K):\n city = A[i]\n print(\'i, city, warps = {}, {}, {}\'.format(i, city, warps))\n if city in warps:\n loop_start_index = warps.index(city)\n loop = warps[loop_start_index:]\n K -= loop_start_index\n print(loop[K % len(loop)])\n return\n else:\n warps.append(city)\n print(warps[-1])\n\n\n##########\nif __name__ == "__main__":\n d()\n', 'def d():\n N, K = map(int, input().split())\n A = list(map(int, input().split()))\n print(\'A = {}\'.format(A))\n warps = [1]\n\n for i in range(K):\n city = A[warps[-1] - 1]\n # print(\'city, warps = {}, {}\'.format(city, warps))\n if city in warps:\n loop_start_index = warps.index(city)\n loop = warps[loop_start_index:]\n K -= loop_start_index\n print(loop[K % len(loop)])\n return\n else:\n warps.append(city)\n print(warps[-1])\n\n\n##########\nif __name__ == "__main__":\n d()\n', 'def d():\n N, K = map(int, input().split())\n A = list(map(int, input().split()))\n print(\'A = {}\'.format(A))\n warps = [1]\n\n for i in range(K):\n city = A[warps[-1] - 1]\n print(\'city, warps = {}, {}\'.format(city, warps))\n if city in warps:\n loop_start_index = warps.index(city)\n loop = warps[loop_start_index:]\n K -= loop_start_index\n print(loop[K % len(loop)])\n return\n else:\n warps.append(city)\n print(warps[-1])\n\n\n##########\nif __name__ == "__main__":\n d()\n', 'def d():\n N, K = map(int, input().split())\n A = list(map(int, input().split()))\n # print(\'A = {}\'.format(A))\n warps = [1]\n w = {}\n\n for i in range(K):\n city = A[warps[-1] - 1]\n # print(\'city, warps = {}, {}\'.format(city, warps))\n if city in w:\n lsi = warps.index(city)\n Kn = (K - lsi) % (len(warps) - lsi)\n print(warps[lsi + Kn])\n return\n else:\n warps.append(city)\n w[city] = city\n print(warps[-1])\n\n\ndef dd():\n N, K = map(int, input().split())\n A = list(map(int, input().split()))\n v = []\n w = {}\n now = 0\n for i in range(K):\n print(\'i, v, w, now = {}, {}, {}, {}\'.format(i, v, w, now))\n p = A[now]\n now = p-1\n if p in w:\n j = v.index(p)\n df = i-j\n Kn = (K-i-1) % df\n p = v[j+Kn]\n break\n else:\n v.append(p)\n w[p] = p\n print(p)\n\n\n##########\nif __name__ == "__main__":\n d()\n']
['Runtime Error', 'Wrong Answer', 'Wrong Answer', 'Runtime Error', 'Accepted']
['s118300557', 's170040648', 's664262069', 's949394974', 's667339213']
[9212.0, 58356.0, 32296.0, 146936.0, 32256.0]
[24.0, 658.0, 2206.0, 3677.0, 129.0]
[575, 569, 573, 571, 1024]
p02684
u518064858
2,000
1,048,576
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.
['n,k=map(int,input().split())\np=list(map(int,input().split()))\na=[0]+p\nflag=0\nbef=1\ntown=[1]\ntown_set=set(town)\ncnt=0\nwhile flag==0 and cnt<k:\n if not a[bef] in town_set:\n town.append(a[bef])\n town_set.add(a[bef])\n bef=a[bef]\n else:\n flag=1\n memo=a[bef]\n print(cnt)\n cnt+=1\nif flag==0:\n print(bef)\n exit()\nfor j in range(len(town)):\n if town[j]==memo:\n num=j\nx=len(town)-num\ny=(k-num)%x\nprint(town[num+y])\n', 'n,k=map(int,input().split())\np=list(map(int,input().split()))\na=[0]+p\nflag=0\nbef=1\ntown=[1]\ntown_set=set(town)\ncnt=0\nwhile flag==0 and cnt<k:\n if not a[bef] in town_set:\n town.append(a[bef])\n town_set.add(a[bef])\n bef=a[bef]\n else:\n flag=1\n memo=a[bef]\n break\n cnt+=1\nif cnt==k:\n print(bef)\n exit()\nfor j in range(len(town)):\n if town[j]==memo:\n num=j\n break\nx=len(town)-num\ny=(k-num)%x\nprint(town[num+y])\n']
['Wrong Answer', 'Accepted']
['s627244018', 's890580504']
[33868.0, 33852.0]
[221.0, 211.0]
[472, 480]
p02684
u518386853
2,000
1,048,576
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.
['n, k = map(int, input().split())\na = list(map(int, input().split()))\n\nl = [0]*n\ns = set()\nx = 0\n\nfor i in range(n):\n machi = a[x]\n if machi in s:\n hajime = l.index(machi)\n owari = i-1\n repeat = l[hajime:owari+1]\n print(l)\n break\n x = machi-1\n l[i] = machi\n s.add(machi)\nelse:\n amari = k%n\n print(l[amari-1])\n exit()\n\nif k < owari:\n print(l[k-1])\nelse:\n amari = (k-hajime)%len(repeat)\n if hajime + amari == 0:\n print(repeat[-1])\n else:\n print(l[hajime+amari-1])\n', 'n, k = map(int, input().split())\na = list(map(int, input().split()))\n\nl = [0]*n\ns = set()\nx = 0\n\nfor i in range(n):\n machi = a[x]\n if machi in s:\n hajime = l.index(machi)\n repeat = l[hajime:i]\n break\n x = machi-1\n l[i] = machi\n s.add(machi)\nelse:\n amari = k%n\n print(l[amari-1])\n exit()\n\nif k < hajime:\n print(l[k-1])\nelse:\n amari = (k-hajime)%len(repeat)\n print(repeat[amari-1])\n']
['Wrong Answer', 'Accepted']
['s771060367', 's095735089']
[33532.0, 33552.0]
[216.0, 202.0]
[546, 434]
p02684
u522293645
2,000
1,048,576
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.
['from collections import deque\n\nn,k = map(int,input().split())\nli = list(map(int,input().split()))\n\nfor i,l in enumerate(li):\n li[i]-=1\n\ndone = [0]*n\nroot = []\nque = deque()\n\ndone[0] = 1\nroot.append(0)\nque.append(li[0])\n\n\nwhile(len(que)!=0 and len(root)<k+1):\n v = que.pop()\n \n if done[v]!=0:\n root.append(v)\n break\n\n done[v] = 1\n root.append(v)\n que.append(li[v])\n\nprint(root)\n\nif len(root)==k+1:\n print(root[-1]+1)\nelse:\n fidx = root.index(root[-1])\n lidx = len(root)-1\n lp = lidx-fidx\n\n cnt = (k-fidx)%lp\n\n if cnt==0:\n print(root[-1]+1)\n else:\n que.append(root[-1])\n for i in range(cnt):\n v = que.pop()\n que.append(li[v])\n print(que.pop()+1)', 'from collections import deque\n\nn,k = map(int,input().split())\nli = list(map(int,input().split()))\n\nfor i,l in enumerate(li):\n li[i]-=1\n\ndone = [0]*n\nroot = []\nque = deque()\n\ndone[0] = 1\nroot.append(0)\nque.append(li[0])\n\n\nwhile(len(que)!=0 and len(root)<k+1):\n v = que.pop()\n \n if done[v]!=0:\n root.append(v)\n break\n\n done[v] = 1\n root.append(v)\n que.append(li[v])\n\nif len(root)==k+1:\n print(root[-1]+1)\nelse:\n fidx = root.index(root[-1])\n lidx = len(root)-1\n lp = lidx-fidx\n\n cnt = (k-fidx)%lp\n\n if cnt==0:\n print(root[-1]+1)\n else:\n que.append(root[-1])\n for i in range(cnt):\n v = que.pop()\n que.append(li[v])\n print(que.pop()+1)']
['Wrong Answer', 'Accepted']
['s742930725', 's939103564']
[32716.0, 33800.0]
[246.0, 217.0]
[688, 675]
p02684
u525796732
2,000
1,048,576
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.
[' \nimport sys\nstdin=sys.stdin\n\nns = lambda:stdin.readline().rstrip()\nni = lambda:int(stdin.readline().rstrip())\nnm = lambda:map(int,stdin.readline().split())\nnl = lambda:list(map(int,stdin.readline().split()))\n\nN,K=nm()\nA=nl()\n\nD=60\nMAX_N=2000005\nto=[[0]*MAX_N for i in range(D)]\n\nto[0][1:N+1]=A\n\nfor i in range(D-1):\n for j in range(1,N+1):\n to[i+1][j]=to[i][to[i][j]]\n\nv=1\nfor i in range(D-1,-1,-1):\n l = 1<<i\n if(l<=K):\n v=to[i][v]\n K-=l\n\nprint(v)\n\n', ' \nimport sys\nstdin=sys.stdin\n\nns = lambda:stdin.readline().rstrip()\nni = lambda:int(stdin.readline().rstrip())\nnm = lambda:map(int,stdin.readline().split())\nnl = lambda:list(map(int,stdin.readline().split()))\n\nN,K=nm()\nA=nl()\n\ncity_l=[0]\ncity_l.append(1)\ncity_c=[0]*(N+1)\ncity_c[1]+=1\nn_city=A[0]\nflag=True\nr_start=-1\nr_end=-1\nwhile(flag):\n city_c[n_city]+=1\n city_l.append(n_city)\n if(city_c[n_city]>1):\n r_start=city_l.index(n_city)\n r_end=len(city_l)-1\n city_l.append(A[n_city-1])\n flag=False\n n_city=A[n_city-1]\n\nif(K<=r_end):\n print(A[city_l[K]-1])\n\n\nelse:\n K2=K-r_end+1\n K2=K2%(r_end-r_start)\n if(K2==0):\n print(city_l[r_end])\n else:\n print(city_l[r_start+K2])\n']
['Time Limit Exceeded', 'Accepted']
['s784817248', 's483024597']
[958436.0, 32256.0]
[2232.0, 151.0]
[492, 745]