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
u797572808
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 = [0]*n\n\na = [int(s) for s in input().split()]\n\nnxt = a[0]\n\nseen = [0]\n#load = []\nres = 0\nflag = 0\nfor i in range(k-1):\n #print(nxt)\n if not(nxt in seen):\n seen.append(nxt)\n nxt = a[nxt-1]\n else:\n root = i - seen.index(nxt) + 1\n print(root)\n print(k-1-i)\n zansa = (k-1-i)%root\n res = seen[seen.index(nxt)+zansa]\n flag = 1\n break\n\n\nif(flag == 1):\n print(res)\nelse:\n print(nxt)', 'n,k = map(int,input().split())\n\na = [0]*n\n\na = [int(s) for s in input().split()]\n\nnxt = a[0]\n\nseen = [0]*n\nseen[0] = 1\n#load = []\nres = 0\nflag = 0\nfor i in range(k-1):\n \n #print(nxt)\n if not(nxt in seen and i > n):\n seen[i+1] = nxt\n nxt = a[nxt-1]\n else:\n root = i - seen.index(nxt) + 1\n #print(root)\n #print(k-1-i)\n zansa = (k-1-i)%root\n res = seen[seen.index(nxt)+zansa]\n flag = 1\n break\n\n\nif(flag == 1):\n print(res)\nelse:\n print(nxt)', 'n,k = map(int,input().split())\n\na = [0]*n\n\na = [int(s) for s in input().split()]\n\nnxt = a[0]\n\nseen = [1]\nload = set()\nload.add(1)\nres = 0\nflag = 0\nfor i in range(k-1):\n \n #print(nxt)\n if not(nxt in load):\n seen.append(nxt)\n load.add(nxt)\n nxt = a[nxt-1]\n else:\n root = i - seen.index(nxt) + 1\n #print(root)\n #print(k-1-i)\n zansa = (k-1-i)%root\n res = seen[seen.index(nxt)+zansa]\n flag = 1\n break\n\n\nif(flag == 1):\n print(res)\nelse:\n print(nxt)']
['Wrong Answer', 'Runtime Error', 'Accepted']
['s160925239', 's239806874', 's697071800']
[33752.0, 33756.0, 33832.0]
[2206.0, 2206.0, 160.0]
[488, 530, 543]
p02684
u801049006
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\ns = [0] * N\np = 1\nh = []\nwhile True:\n h.append(p)\n s[p-1] = 1\n p = A[p-1]\n\n if s[p-1] != 0:\n break\nprint(h, p)\nlast = h.index(p)\nleng = len(h) - last\nrem = (K - last) % leng\nprint(h[last:][rem])\n', 'n, k = map(int, input().split())\na = [0] + list(map(int, input().split()))\n\nseen = [False] * (n+1)\nvisited = []\nnxt = 1\ncnt = 0\nwhile not seen[nxt] and cnt != k:\n seen[nxt] = True\n visited.append(nxt)\n nxt = a[nxt]\n cnt += 1\n\nif cnt == k:\n print(nxt)\n exit()\n \nidx = visited.index(nxt)\nprint(visited[idx:][(k-idx)%(len(visited)-idx)])\n']
['Wrong Answer', 'Accepted']
['s624129243', 's602133000']
[32392.0, 32100.0]
[202.0, 133.0]
[284, 356]
p02684
u805552010
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\nt = 1\ntl = [1]\nts = {1}\nfor i in range(len(a)):\n\n t = a[t-1]\n if t in ts:\n break\n else:\n tl.append(t)\n ts.add(t)\n\ntl2 = tl[tl.index(t):len(tl)]\n\nprint(tl)\nprint(tl2)\n\nif k < len(tl):\n ans = tl[k]\nelse:\n k = k - (len(tl) - len(tl2))\n j = k % len(tl2)\n ans = tl2[j]\n\nprint(ans)', 'n,k = map(int,input().split())\na = list(map(int,input().split()))\n\nt = 1\ntl = [1]\nts = {1}\nfor i in range(len(a)):\n\n t = a[t-1]\n if t in ts:\n break\n else:\n tl.append(t)\n ts.add(t)\n\ntl2 = tl[tl.index(t):len(tl)]\n\n\nif k < len(tl):\n ans = tl[k]\nelse:\n k = k - (len(tl) - len(tl2))\n j = k % len(tl2)\n ans = tl2[j]\n\nprint(ans)']
['Wrong Answer', 'Accepted']
['s797830430', 's320410162']
[32652.0, 32272.0]
[200.0, 205.0]
[384, 363]
p02684
u807028974
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# import math #sqrt,gcd,pi\n\n\n\n# import heapq # priolity-queue\n\n# from itertools import product,permutations,\\\n# combinations,combinations_with_replacement\n\n\n# from operator import itemgetter,mul\n# from fractions import Fraction\n# from functools import reduce\n\nmod = int(1e9+7)\nINF = 1<<29\nlINF = 1<<35\n\ndef readInt():\n return list(map(int,input().split()))\n\ndef main():\n n,k = readInt()\n a = readInt()\n pos = 0\n visit = [0] * n\n move = []\n roop = []\n while visit[pos]!=2:\n if visit[pos]==0:\n move.append(pos)\n else:\n roop.append(pos)\n visit[pos] += 1\n pos = a[pos] - 1\n \n roop.reverse()\n num = 0\n if len(move)>=k:\n print(move[k]+1)\n else:\n num = (k - (len(move)-len(roop))) % len(roop)\n print(roop[num]+1)\n return\n\nif __name__=='__main__':\n main()\n", 'N,K = map(int,input().split())\nA = list(map(int,input().split()))\npos = 0\nvisit = [0] * N\nmove = []\nroop = []\nwhile visit[pos]!=2:\n if visit[pos]==0:\n move.append(pos)\n else:\n roop.append(pos)\n visit[pos] += 1\n pos = a[pos] - 1\nif len(move)>K:\n print(move[K]+1)\nelse:\n print(roop[(K-(len(move)-len(roop)))%len(roop)]+1)', 'N,K = map(int,input().split())\nA = list(map(int,input().split()))\npos = 0\nvisit = [0] * N\nmove = []\nroop = []\nwhile visit[pos]!=2:\n if visit[pos]==0:\n move.append(pos)\n else:\n roop.append(pos)\n visit[pos] += 1\n pos = A[pos] - 1\nif len(move)>K:\n print(move[K]+1)\nelse:\n print(roop[(K-(len(move)-len(roop)))%len(roop)]+1)']
['Runtime Error', 'Runtime Error', 'Accepted']
['s267430591', 's762157747', 's933195770']
[32716.0, 32380.0, 32644.0]
[198.0, 68.0, 234.0]
[1052, 351, 351]
p02684
u811202694
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\nsys.setrecursionlimit(10000000)\nN,K = [int(v) for v in input().split()]\nA = [int(v) for v in input().split()]\nprint(N,K)\n\n\nloop = []\n\ndef dfs(now):\n loop.append(now)\n if A[now-1] == 1:\n return\n else:\n dfs(A[now-1])\ndfs(1)\n\nprint(loop[K%len(loop)])\n', 'import sys\n\nsys.setrecursionlimit(10000000)\nN,K = [int(v) for v in input().split()]\nA = [int(v) for v in input().split()]\n\nloop = []\npre = []\nvisited = set()\n\ndef dfs(index,now):\n global pre\n global loop\n loop.append(now)\n if now in visited:\n for left in range(len(loop)):\n if now == loop[left]:\n break\n pre = loop[:left]\n loop = loop[left:index]\n return\n else:\n visited.add(now)\n dfs(index+1,A[now-1])\ndfs(0,1)\n\nif len(pre) > K:\n print(pre[K])\nelse:\n K-=len(pre)\n print(loop[K%len(loop)])\n']
['Runtime Error', 'Accepted']
['s573543403', 's701733923']
[612288.0, 191740.0]
[746.0, 341.0]
[283, 582]
p02684
u811436126
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\nvisited = [0] * (n + 1)\nnow = 1\ncheck = 2 * n\nloop_start = 1\nwhile check > 0:\n if visited[now - 1] == 1:\n loop_start = now\n break\n else:\n visited[now - 1] += 1\n now = a[now - 1]\n check -= 1\n\nvisited = [0] * (n + 1)\nnow = loop_start\ncheck = n\nloop = 0\nwhile check > 0:\n if visited[now - 1] == 1:\n break\n else:\n visited[now - 1] += 1\n now = a[now - 1]\n check -= 1\n loop += 1\n\nnow = 1\nwhile k > 0:\n if now == loop_start:\n k %= loop\n if k <= 0:\n break\n else:\n now = a[now - 1]\n k = min(k - 1, 0)\n else:\n now = a[now - 1]\n k -= 1\n\nans = now\nprint(ans)\n', 'n, k = map(int, input().split())\na = list(map(int, input().split()))\n\nvisited = [0] * (n + 1)\nnow = 1\ncheck = 2 * n\nloop_start = 1\nwhile check > 0:\n if visited[now - 1] == 1:\n loop_start = now\n break\n else:\n visited[now - 1] += 1\n now = a[now - 1]\n check -= 1\n\nvisited = [0] * (n + 1)\nnow = loop_start\ncheck = n\nloop = 0\nwhile check > 0:\n if visited[now - 1] == 1:\n break\n else:\n visited[now - 1] += 1\n now = a[now - 1]\n check -= 1\n loop += 1\n\nnow = 1\nwhile k > 0:\n if now == loop_start:\n k %= loop\n if k == 0:\n break\n now = a[now - 1]\n k -= 1\n\nprint(now)\n']
['Wrong Answer', 'Accepted']
['s537014510', 's077104351']
[32404.0, 32408.0]
[216.0, 241.0]
[773, 672]
p02684
u811817592
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 -*-\nN, K = map(int, input().split())\nA_list = list(map(int, input().split()))\n\nans_list = [1]\nhistory_list = [0 for _ in range(N)]\nbefore_town = 1\nloop_start_town = 10 ** 10\nfor i in range(N):\n now_town = A_list[before_town - 1]\n if history_list[now_town - 1] == 1\n break\n ans_list.append(now_town)\n before_town = now_town\n\nif len(ans_list) >= K:\n print(ans_list[K])\nelse:\n loop_start_idx = ans_list.index(A_list[ans_list[-1] - 1])\n loop_len = len(ans_list)\n ans_list = ans_list[loop_start_idx:]\n mod_num = (K - loop_len) % (loop_len - loop_start_idx)\n print(ans_list[mod_num])', '# -*- coding: utf-8 -*-\nN, K = map(int, input().split())\nA_list = list(map(int, input().split()))\n\nans_list = [1]\nhistory_list = [0 for _ in range(N)]\nbefore_town = 1\nloop_start_town = 10 ** 10\nfor i in range(N):\n now_town = A_list[before_town - 1]\n if history_list[now_town - 1] == 1:\n break\n ans_list.append(now_town)\n before_town = now_town\n\nif len(ans_list) >= K:\n print(ans_list[K])\nelse:\n loop_start_idx = ans_list.index(A_list[ans_list[-1] - 1])\n loop_len = len(ans_list)\n ans_list = ans_list[loop_start_idx:]\n mod_num = (K - loop_len) % (loop_len - loop_start_idx)\n print(ans_list[mod_num])', '# -*- coding: utf-8 -*-\nN, K = map(int, input().split())\nA_list = list(map(int, input().split()))\n\nans_list = [1]\nhistory_list = [0 for _ in range(N)]\nbefore_town = 1\nloop_start_town = 10 ** 10\nfor i in range(N):\n now_town = A_list[before_town - 1]\n if history_list[now_town - 1] == 1\n break\n ans_list.append(now_town)\n before_town = now_town\n\nif len(ans_list) >= K:\n print(ans_list[K])\nelse:\n loop_start_idx = ans_list.index(A_list[ans_list[-1] - 1])\n loop_len = len(ans_list)\n ans_list = ans_list[loop_start_idx:]\n mod_num = (K - loop_len) % (loop_len - loop_start_idx)\n print(ans_list[mod_num])', '# -*- coding: utf-8 -*-\nN, K = map(int, input().split())\nA_list = list(map(int, input().split()))\n\nans_list = [1]\nhistory_list = [0 for _ in range(N)]\nbefore_town = 1\nloop_start_town = 10 ** 10\nfor i in range(N):\n now_town = A_list[before_town - 1]\n if history_list[now_town - 1] == 1:\n break\n ans_list.append(now_town)\n before_town = now_town\n\nif len(ans_list) > K:\n print(ans_list[K])\nelse:\n loop_start_idx = ans_list.index(A_list[ans_list[-1] - 1])\n loop_len = len(ans_list)\n ans_list = ans_list[loop_start_idx:]\n mod_num = (K - loop_len) % (loop_len - loop_start_idx)\n print(ans_list[mod_num])']
['Runtime Error', 'Runtime Error', 'Runtime Error', 'Accepted']
['s240706618', 's685028204', 's821243815', 's962129992']
[9044.0, 32260.0, 9040.0, 32376.0]
[24.0, 139.0, 22.0, 190.0]
[634, 635, 634, 634]
p02684
u813993459
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()))\n# a = list(map(int,input().split()))\n\ntmp_city=[]\nc=0\ncount=0\ninit_city=0\n\nwhile (1):\n count+=1\n tmp=a[c]\n if k==count:\n tmp_city.append(tmp)\n tmp_city=[1]+tmp_city\n break\n if tmp == 1:\n init_city=0\n tmp_city=[1]+tmp_city\n break\n if tmp in tmp_city:\n init_city=tmp_city.index(tmp)+1\n tmp_city=[1]+tmp_city\n break\n tmp_city.append(tmp)\n c=tmp-1\nif k==count:\n print(tmp_city[-1])\nelse:\n print(tmp_city[init_city:][(k-(init_city))%len(tmp_city[init_city:])])', 'n, k = list(map(int,input().split()))\na = list(map(int,input().split()))\n\nnow=1\ncount=0\n\ncities = [0]\n\nfor i in range(1,k+1):\n tmp=a[cities[i-1]-1]\n if tmp in cities[:i]:\n init_city=cities.index(tmp)\n if init_city>k:\n print(cities[k])\n else:\n print(cities[init_city:i][(k-init_city)%(i-init_city)])\n break\n else:\n cities.append(tmp)\nelse:\n print(now)', 'n, k = list(map(int,input().split()))\na = list(map(int,input().split()))\n\nnow=1\ncount=0\n\ncities = [0]\n\nfor i in range(1,k+1):\n tmp=a[cities[i-1]-1]\n if tmp in cities[:i]:\n init_city=cities.index(tmp)\n if init_city>k:\n print(cities[k])\n else:\n print(cities[init_city:i][(k-init_city)%(i-init_city)])\n break\n else:\n cities.append(tmp)\nelse:\n print(cities[k])', 'n, k = map(int, input().split())\nA = list(map(int, input().split()))\nD = [0] * (n+1)\nnow = 1\nfor i in range(1, k+1):\n tmp = A[now-1]\n if not D[tmp]:\n now = tmp\n D[now] += i\n else:\n pre = D[tmp]\n loop = i - D[tmp]\n if loop>1:\n now=D.index((k - pre) % loop + pre)\n else:\n now=tmp\n break\nprint(now)']
['Runtime Error', 'Wrong Answer', 'Wrong Answer', 'Accepted']
['s192005535', 's339733584', 's513960485', 's269920248']
[9168.0, 32312.0, 32360.0, 32368.0]
[21.0, 2206.0, 2206.0, 127.0]
[603, 419, 425, 375]
p02684
u816265237
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.
['#167\n#d\nn,k = map(int, input().split())\nln = list(map(int, input().split()))\n\n# n,k = 10*4,10**18\n# ln = list(range(2,n+1))\n# ln.append(1)\n\nvisit = [0]*n \nnow= 1 \nbefore =[] \nroop =[] \nwhile visit[now -1] != 2:\n if visit[now -1] == 0:\n before.append(now)\n elif visit [now-1] == 1:\n roop.append(now)\n visit[now -1]+= 1\n now = ln[now-1]\nif k < len(before):\n print(before[k])\nelse :\n print(roop[((k-len(before))%len(roop)) -1])\n \n ', '#167\n#d\nn,k = map(int, input().split())\nln = list(map(int, input().split()))\n\n# n,k = 10*4,10**18\n# ln = list(range(2,n+1))\n# ln.append(1)\n\nvisit = [0]*n \nnow= 1 \nbefore =[] \nroop =[] \nwhile visit[now -1] != 2:\n if visit[now -1] == 0:\n before.append(now)\n elif visit [now-1] == 1:\n roop.append(now)\n \n visit[now -1]+= 1\n now = ln[now-1]\n\n\nif k < len(before):\n print(before[k])\nelse :\n print(roop[((k-len(before))%len(roop)) ])\n ']
['Wrong Answer', 'Accepted']
['s761641834', 's271699043']
[32000.0, 32368.0]
[261.0, 241.0]
[602, 602]
p02684
u822662438
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()))\n\nvisited = [1]\nis_visited = [0] * n\nnow = 1\nstep = 0\nfor i in range(k):\n now = a[now-1]\n step += 1\n if is_visited[now-1] == 0:\n is_visited[now-1] = 1\n else:\n ind = visited.index(now)\n loop = visited[ind:]\n tmp = (k-step) % len(loop)\n now = loop[tmp]\n break\n\nprint(now)\n', 'n, k = map(int, input().split())\na = list(map(int, input().split()))\n\nvisited = [1]\nis_visited = [0] * n\nnow = 1\nstep = 0\nfor i in range(k):\n now = a[now-1]\n step += 1\n\n if is_visited[now-1] == 0:\n is_visited[now-1] = 1\n else:\n ind = visited.index(now)\n loop = visited[ind:]\n tmp = (k-step) % len(loop)\n now = loop[tmp]\n break\n\nprint(now)\n', 'n, k = map(int, input().split())\n\na = list(map(int, input().split()))\n\nvisited = [1]\nis_visited = [0] * n\nnow = 1\nstep = 0\nfor i in range(k):\n now = a[now-1]\n step += 1\n if is_visited[now-1] == 0:\n is_visited[now-1] = 1\n visited.append(now)\n else:\n ind = visited.index(now)\n loop = visited[ind:]\n tmp = (k-step) % len(loop)\n now = loop[tmp]\n break\n\nprint(now)\n']
['Runtime Error', 'Runtime Error', 'Accepted']
['s013093541', 's570115519', 's824522642']
[32364.0, 32300.0, 32260.0]
[143.0, 127.0, 136.0]
[393, 393, 421]
p02684
u825186577
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\npos = 0\nvisited = [0]*N\nmove = []\nloop = []\n\nwhile visited[pos]!=2:\n if visited[pos] ==0:\n move.append(pos)\n else:\n loop.append(pos)\n visited[pos] += 1\n pos = A[pos]-1\n\nif len(move)>k:\n print(move[k]+1)\nelse:\n print(roop[(K-(len(move)-len(loop)))%len(loop)]+1)', 'N,K = map(int,input().split())\nA = list(map(int,input().split()))\npos = 0\nvisit = [0] * N\nmove = []\nroop = []\nwhile visit[pos]!=2:\n if visit[pos]==0:\n move.append(pos)\n else:\n roop.append(pos)\n visit[pos] += 1\n pos = A[pos] - 1\nif len(move)>K:\n print(move[K]+1)\nelse:\n print(roop[(K-(len(move)-len(roop)))%len(roop)]+1)\n']
['Runtime Error', 'Accepted']
['s271857748', 's231111522']
[32668.0, 32840.0]
[257.0, 257.0]
[339, 352]
p02684
u830054172
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()))\n\ncnt = [0 for _ in range(N)]\ncnt[0] += 1\n\nwarp_rireki= [A[0]]\nroop = []\n# c = 0\n# print(cnt)\nwarp = A[0]\nwhile True:\n # print(cnt[warp-1])\n if cnt[warp-1] == 2:\n break\n warp_rireki.append(A[warp-1])\n if cnt[warp-1] == 1:\n roop.append(A[warp-1])\n cnt[warp-1] += 1\n warp = A[warp-1]\n\n\none_root = sum(cnt)-len(roop)\n# print("root", roop)\n# print("one", one_root)\nif K < len(warp_rireki):\n print(warp_rireki[K])\nelse:\n print(roop[(K-one_root)%len(roop)-1])', 'N, K = map(int, input().split())\n\nA = list(map(int, input().split()))\n\ncnt = [0 for _ in range(N)]\ncnt[0] += 1\n\nwarp_rireki= [A[0]]\nroop = []\n# c = 0\n# print(cnt)\nwarp = A[0]\nwhile True:\n # print(cnt[warp-1])\n if cnt[warp-1] == 2:\n break\n warp_rireki.append(A[warp-1])\n if cnt[warp-1] == 1:\n roop.append(A[warp-1])\n cnt[warp-1] += 1\n warp = A[warp-1]\n\n\none_root = cnt.count(1)\n# print("root", roop)\n# print("one", one_root)\nif K < len(warp_rireki):\n print(warp_rireki[K])\nprint(roop[(K-one_root)%len(roop)-1])', 'N, K = map(int, input().split())\n\nA = list(map(int, input().split()))\n\ncnt = [0 for _ in range(N)]\ncnt[0] += 1\n\nwarp_rireki= [A[0]]\nroop = []\n# c = 0\n# print(cnt)\nwarp = A[0]\nwhile True:\n # print(cnt[warp-1])\n if cnt[warp-1] == 2:\n break\n warp_rireki.append(A[warp-1])\n if cnt[warp-1] == 1:\n roop.append(A[warp-1])\n cnt[warp-1] += 1\n warp = A[warp-1]\n\n\none_root = cnt.count(1)\n# print("root", roop)\n# print("one", one_root)\nif K < len(warp_rireki):\n print(warp_rireki[K])\nelse:\n print(roop[(K-one_root)%len(roop)-1])', 'N, K = map(int, input().split())\n\nA = list(map(int, input().split()))\n\nord = [-1 for _ in range(N)]\nord[0] = 0\nv = A[0]-1\nroot = [0]\n\nwhile ord[v] == -1:\n \n ord[v] = len(root)\n \n root.append(v)\n\n \n v = A[v]-1\n\n# print(ord)\n# print(root)\n\n\n\nl = ord[v]\n\nc = len(root) - l\n\nif K < l:\n \n ans = root[K]\nelse:\n \n \n K -= l\n \n K %= c\n \n ans = root[l+K]\nprint(ans+1)\n\n']
['Wrong Answer', 'Wrong Answer', 'Wrong Answer', 'Accepted']
['s053534298', 's460651000', 's597840443', 's927648820']
[32384.0, 32388.0, 32068.0, 33396.0]
[289.0, 336.0, 304.0, 176.0]
[560, 544, 554, 880]
p02684
u833492079
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\ndef p(*_a):\n _s=" ".join(map(str,_a))\n #print(_s)\n sys.stderr.write(_s+"\\n")\n#-------------------------------------------------------------------\nN,K = map(int, input().split())\t\t# 5 7 2\nA = [0] + list( map(int, input().split()) )\t# 1 2 3 4 5 ...\n\n\nM = [ [0,0] for _ in range(N+1) ]\t\t\t#=> [[], [], [], [], [], [], [], []]\n\np(M)\n\ni = 1\nx = y = 0\nfor k in range(1, K+1):\n\tx,y = M[i]\n\t\n\tif x==0:\n\t\tM[i][0] = A[i]\n\t\tM[i][1] = k\n\t\ti = A[i]\n\t\n\telse:\n\t\t\n\t\tbreak\n\nelse:\n\tans = i\n\tprint(ans)\n\texit(0)\n\n\np(x,y,k)\np("M[1:]", M[1:])\n\nk = k - y\n\nj = k % (k-y)\n\ni = \nfor a in range(j):\n\tx,y = M[i]\n\tM[i][0] = A[i]\n\tM[i][1] = k\n\ti = A[i]\n\nprint(i)\t', '#-------------------------------------------------------------------\nimport sys\ndef p(*_a):\n _s=" ".join(map(str,_a))\n #print(_s)\n sys.stderr.write(_s+"\\n")\n#-------------------------------------------------------------------\nN,K = map(int, input().split())\t\t# 5 7 2\nA = [0] + list( map(int, input().split()) )\t# 1 2 3 4 5 ...\n\n\nM = [ [0,0] for _ in range(N+1) ]\t\t\t#=> [[], [], [], [], [], [], [], []]\n\np(M)\n\ni = 1\nx = y = 0\nfor k in range(1, K+1):\n\tx,y = M[i]\n\t\n\tif x==0:\n\t\tM[i][0] = A[i]\n\t\tM[i][1] = k\n\t\ti = A[i]\n\t\n\telse:\n\t\t\n\t\tbreak\n\nelse:\n\tans = i\n\tprint(ans)\n\texit(0)\n\n\np(x,y,k)\np("M[1:]", M[1:])\n\nk = k - y\n\nj = k % (k-y)\n\nfor a in range(j):\n\tx,y = M[i]\n\tM[i][0] = A[i]\n\tM[i][1] = k\n\ti = A[i]\n\nprint(i)\t', '#-------------------------------------------------------------------\nimport sys\ndef p(*_a):\n _s=" ".join(map(str,_a))\n #print(_s)\n sys.stderr.write(_s+"\\n")\n#-------------------------------------------------------------------\nN,K = map(int, input().split())\t\t# 5 7 2\nA = [0] + list( map(int, input().split()) )\t# 1 2 3 4 5 ...\n\n\nM = [ [0,0] for _ in range(N+1) ]\t\t\t#=> [[], [], [], [], [], [], [], []]\n\np(M)\n\ni = 1\nx = y = 0\nfor k in range(K):\n\tx,y = M[i]\n\t\n\tif x==0:\n\t\tM[i][0] = A[i]\n\t\tM[i][1] = k\n\t\ti = A[i]\n\t\tcontinue\n\t\n\t\n\ta = K - y\n\tb = a % (k-y)\n\tfor _ in range(b):\n\t\ti = A[i]\n\t\n\tp(x,y,k)\n\tp("M[1:]", M[1:])\n\t\n\tbreak\n\nans = i\nprint(ans)\n\n']
['Runtime Error', 'Runtime Error', 'Accepted']
['s689039794', 's837662701', 's899592121']
[8792.0, 50128.0, 49316.0]
[26.0, 389.0, 916.0]
[716, 711, 646]
p02684
u834224054
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.
['tmp = input().split()\nN = int(tmp[0])\nK = int(tmp[1])\n\ntmp = input().split()\n\nMap = [0]*N\nK = K%N\nrt = [0]*K\n\nfor i in range(N):\n Map[i] = int(tmp[i])\n\ndef trip(self, steps):\n ntown = Map[self-1]\n if steps == K:\n return ntown\n rt[steps]=trip(ntown,steps+1)\n\n\n \nFt = trip(1,1)\nprint(rt[K-1])\n', 'N, K = map(int,input().split())\n\nMap = list(map(int,input().split()))\n\nVisited = [-1]*N\nnow = 1\nOrder = [0]*N\n\nloopcount = 1\nOrder[0] = 1\nVisited[0] = 1\nfor i in range(1,N):\n nt = Map[now-1]\n Order[i] = nt\n loopcount += 1\n if Visited[nt-1] == -1:\n Visited[nt-1] = loopcount\n else:\n break\n now = nt\n \n\nns = Visited[nt-1]\nperiod = loopcount - ns\n\nif period == 0:\n period = loopcount\n ns = 0\n\nKmod = K - ns\nif Kmod > 0:\n Kmod %= period\n\nprint(Order[ns+Kmod])\n\n\n']
['Runtime Error', 'Accepted']
['s092663010', 's192802502']
[33352.0, 32400.0]
[107.0, 212.0]
[313, 502]
p02684
u838644735
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, *A = map(int, open(0).read().split())\n visited = [0]*N\n pos = A[0]\n visited[0] = visited[pos - 1] = 1\n while visited[pos - 1] < 2:\n pos = A[pos - 1]\n visited[pos - 1] += 1\n # print(pos)\n begin = pos\n step_to_begin = 0\n if begin != 0:\n pos = 0\n while pos != begin:\n pos = A[pos - 1]\n step_to_begin += 1\n step_to_begin += 1\n # print(begin, step_to_begin)\n\n if K <= step_to_begin:\n ans = 0\n for i in range(K):\n ans = A[ans - 1]\n print(ans)\n return\n order = 1\n pos = step_to_begin\n while A[pos - 1] != step_to_begin:\n pos = A[pos - 1]\n order += 1\n # print(order)\n remain = (K - step_to_begin) % order\n ans = step_to_begin\n for i in range(remain):\n ans = A[ans - 1]\n print(ans)\n\nif __name__ == '__main__':\n main()\n", 'N,K,*A=map(int,open(0).read().split())\na=1\nwhile K>0:\n if K&1:a=A[a-1]\n A=[A[A[i]-1] for i in range(N)]\n K>>=1\nprint(a)']
['Wrong Answer', 'Accepted']
['s893908460', 's796415233']
[31824.0, 31808.0]
[2206.0, 1896.0]
[903, 119]
p02684
u841856382
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 = input().split()\nN = int(n[0])\nK = int(n[1])\n\nlista = []\nlistb = [1]\nC = 1\nA = list(map(int, input().split()))\n\nfor i in range(N):\n lista.append(int(A[i]))\n \nfor j in range(20000000):\n C = lista[C-1]\n if C not in listb:\n listb.append(C)\n else:\n break\n \nfor i in range(len(listb)):\n if listb[i] == C:\n D = C\n I = i\n\nif K <= N:\n print(C)\nelif K > N and C == N:\n print(listb[K % N -1])\nelse:\n if C-D == 1 or C-D == 0:\n print(C)\n else:\n M = (K-I) % (C-D)\n print(listb[I+M-1])', 'n = input().split()\nN = int(n[0])\nK = int(n[1])\n\nlistb = [1]\nlistc = [0]*N\nC = 1\nA = list(map(int, input().split()))\n \nfor i in range(N):\n C = A[C-1]\n if listc[C-1] == 0:\n listb.append(C)\n listc[C-1] = 1\n else:\n D = listb.index(C)\n listb.append(C)\n break\n\nL = len(listb) -1\n\nif K <= N:\n print(listb[K])\nelse:\n if L-D == 1 or L-D == 0:\n print(C)\n else:\n M = (K-D) % (L-D)\n print(listb[(D+M)%L])']
['Wrong Answer', 'Accepted']
['s674936731', 's686571863']
[32388.0, 33888.0]
[2206.0, 175.0]
[582, 483]
p02684
u843318346
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())\nan = list(map(int, input().split()))\ncopyan = an[:]\ni = copyan[0]\ncopyan[0]=10**6\nmovecount = 1\nroopstart = 0\nif i != 1:\n while True:\n memo = i\n i = copyan[i-1]\n copyan[memo-1]=10**6 + movecount\n \n \n if i >=30000:\n yokei = i%(10**6)\n \n break\n movecount += 1\n roopstart = i\nelse:\n print()\nroopnum = movecount - yokei\nx = (k-yokei)%roopnum \n\ncount = 0\niti = an[roopstart]\nwhile True:\n \n if x == 0:\n print(roopstart)\n exit()\n else:\n iti = an[iti-1]\n \n count += 1\n if count == x:\n print(iti+1)\n exit()\n\n\n\n\n\n\n\n', 'n,k = map(int,input().split())\nan = list(map(int, input().split()))\ncopyan = an[:]\ni = copyan[0]\ncopyan[0]=10**6\nmovecount = 1\n\nif i != 1:\n while True:\n memo = i\n if k == movecount:\n print(i)\n \n exit()\n i = copyan[i-1]\n \n if i >=300000:\n yokei = i%(10**6) \n break\n copyan[memo-1]=10**6 + movecount\n movecount += 1\n \nelse:\n print(1)\n exit()\nroopnum = movecount - yokei\nx = (k-yokei)%roopnum\n\nans = copyan.index(10**6+yokei+x) +1\nprint(ans)\n']
['Runtime Error', 'Accepted']
['s124137275', 's774731843']
[32300.0, 32300.0]
[2206.0, 143.0]
[723, 567]
p02684
u845536647
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.
['print(kari)', 'N, K = map(int, input().split())\nA = list(map(int, input().split()))\nl=[1] \nseen={}\nk=1\nfor i in range (K):\n if not k in seen.keys():\n seen[k]=i\n k=A[k-1]\n l.append(k)\n else:\n r_start=seen[k]\n roop=i-r_start\n K=(K-r_start)%roop+r_start\n break\nans=l[K]\nprint(ans)']
['Runtime Error', 'Accepted']
['s025530436', 's599112167']
[9132.0, 32320.0]
[24.0, 165.0]
[11, 301]
p02684
u845620905
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\nk += 1\npos = -1\nseen = [False] * n\nlast = [0] * n\nhist = []\ncycle = []\ni = 0\ncnt = 0\nwhile(seen[i] == False):\n seen[i] = True\n last[i] = cnt\n cnt += 1\n hist.append(i)\n ne = a[i]-1\n i = ne\n if (seen[i] == True):\n pos = i\n\npreCircle = len(hist.copy())\n\ncycle = hist[last[pos]:]\n\npreCircle -= cnt\n\nif (k < preCircle):\n print(hist[-k-1] + 1)\nelse:\n k -= preCircle\n ans = k % len(cycle)\n print(cycle[ans] + 1)\n\n', 'n, k = map(int, input().split())\na = list(map(int, input().split()))\n\npos = -1\nseen = [False] * n\nlast = [0] * n\nhist = []\ncycle = []\ni = 0\ncnt = 0\nwhile(seen[i] == False):\n seen[i] = True\n last[i] = cnt\n cnt += 1\n hist.insert(0, i)\n ne = a[i]-1\n i = ne\n if (seen[i] == True):\n pos = i\n\npreCircle = len(hist.copy())\n\ncycle = hist[:len(hist) - last[pos]]\n\npreCircle -= cnt\n\nif (k < preCircle):\n print(hist[-k-1] + 1)\nelse:\n k -= preCircle\n ans = k % len(cycle)\n print(cycle[ans] + 1)\n\n', 'n, k = map(int, input().split())\na = list(map(int, input().split()))\n\npos = -1\nseen = [False] * n\nlast = [-1] * n\nhist = []\ni = 0\ncnt = 0\n\nwhile(seen[i] == False):\n seen[i] = True\n last[i] = cnt\n cnt += 1\n hist.append(i)\n ne = a[i]-1\n i = ne\n if (seen[i] == True):\n pos = i\n\ncycle = hist[last[pos]:]\npreCircle = last[pos]\n\nif (k <= preCircle):\n print(hist[k] + 1)\nelse:\n k -= preCircle\n ans = k % len(cycle)\n print(cycle[ans] + 1)\n\n']
['Wrong Answer', 'Wrong Answer', 'Accepted']
['s183318237', 's916046830', 's865898897']
[35496.0, 32388.0, 34460.0]
[189.0, 2206.0, 193.0]
[516, 524, 472]
p02684
u850390157
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: x - 1, map(int, input().split())))\nprint(A)\ncurrent = 0\nmemo = {\n 0: 0\n}\nfor i in range(1, K + 1):\n current = A[current]\n if current in memo:\n # loop length\n length = i - memo[current]\n tmp = (K - memo[current]) % length\n # print(current, tmp)\n for j in range(tmp):\n current = A[current]\n break\n else:\n memo[current] = i\nprint(current + 1)', 'N, K = map(int, input().split())\nA = list(map(lambda x: x - 1, map(int, input().split())))\ncurrent = 0\nmemo = {\n 0: 0\n}\nfor i in range(1, K + 1):\n current = A[current]\n if current in memo:\n # loop length\n length = i - memo[current]\n tmp = (K - memo[current]) % length\n for j in range(tmp):\n current = A[current]\n break\n else:\n memo[current] = i\nprint(current + 1)']
['Wrong Answer', 'Accepted']
['s177413994', 's958069329']
[32252.0, 32368.0]
[154.0, 130.0]
[467, 428]
p02684
u859059120
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())\nseq=list(map(int,input().split()))\nv=[False]*n\npath=[]\ni=seq.index(1)\nwhile True:\n if v[seq[i]-1]==False:\n v[seq[i]-1]=True\n path.append(seq[i])\n i=seq[i]-1\n \n else:\n break\nprint(path[(k%len(path))])\nprint(path)\n \n', 'n,k=map(int,input().split())\nseq=list(map(int,input().split()))\nv=[False]*n\npath=[1]\ni=0\nv[0]=True\nwhile True:\n if v[seq[i]-1]==False:\n v[seq[i]-1]=True\n path.append(seq[i])\n i=seq[i]-1\n \n else:\n t=seq[i]\n break\n\nind=path.index(t)\npath2=path[ind:]\npath1=path[:ind]\nx=len(path1)\nif k>=x:\n k-=x\n #print(t,ind,path1,path2)\n print(path2[(k%len(path2))])\n\nelse:\n print(path1[k])\n \n\n\n \n']
['Runtime Error', 'Accepted']
['s931740911', 's542489602']
[32376.0, 32264.0]
[161.0, 185.0]
[347, 534]
p02684
u866949333
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\nimport math\n\n\ndef comb(n, r):\n return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))\n\n\ndef main():\n N, M, K = map(int, input().split())\n const = 998244353\n\n def hoge(k):\n return comb(N - 1, k) * M * ((M - 1) ** (N - k - 1))\n\n ans = sum([hoge(k) for k in range(K + 1)])\n\n print(ans % const)\n\n\nif __name__ == '__main__':\n main()\n", "import math\nfrom collections import defaultdict\n\n\ndef main():\n N, K = map(int, input().split())\n A = list(map(int, input().split()))\n\n visited = [-1 for _ in range(N)]\n visited[0] = 0\n\n current = 0\n th = -1\n per_loop = -1\n for i in range(1, N+1):\n current = A[current] - 1\n\n if visited[current] >= 0:\n \n per_loop = i - visited[current]\n th = visited[current]\n break\n else:\n visited[current] = i\n\n current = 0\n # print(th, per_loop)\n\n if K < th:\n num_rep = K\n else:\n num_rep = th + (K - th) % per_loop\n\n for i in range(num_rep):\n current = A[current] - 1\n\n print(current + 1)\n\n\nif __name__ == '__main__':\n main()\n"]
['Runtime Error', 'Accepted']
['s332165626', 's930494841']
[9528.0, 32572.0]
[24.0, 155.0]
[409, 791]
p02684
u867408000
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(int,input().split()))\npos = 0\nvisit = [0] * N\nmove = []\nloop = []\nwhile visit[pos]!=2:\n if visit[pos]==0:\n move.append(pos)\n else:\n loop.append(pos)\n visit[pos] += 1\n pos = A[pos] - 1\n print(visit)\nprint(move)\nprint(loop)\nif len(move)>K:\n print(move[K]+1)\nelse:\n print(loop[(K-(len(move)-len(loop)))%len(loop)]+1)\n', 'n, k = map(int, input().split())\ntowns = list(map(int, input().split()))\n\nvisited = [0 for i in range(n+1)]\nmove_count = 0\nposition = 1\nloop_dis = 0\n\nwhile True:\n if visited[position] != 0:\n loop_dis = move_count - visited[position] + 1\n visited[position] = move_count\n break\n else:\n move_count += 1\n visited[position] = move_count\n position = towns[position-1]\n\n\nrest_dis = (k - move_count) % loop_dis\n\nmove_count = 0\n\nif rest_dis == 0:\n print(position)\nelse:\n for i in range(rest_dis):\n move_count += 1\n position = towns[position-1]\n print(position)', '\nN,K = map(int,input().split())\nA = list(map(int,input().split()))\npos = 0\nvisit = [0] * N\nmove = []\nloop = []\nwhile visit[pos]!=2:\n if visit[pos]==0:\n move.append(pos)\n else:\n loop.append(pos)\n visit[pos] += 1\n pos = A[pos] - 1\nif len(move)>K:\n print(move[K]+1)\nelse:\n print(loop[(K-(len(move)-len(loop)))%len(loop)]+1)\n']
['Wrong Answer', 'Wrong Answer', 'Accepted']
['s094323779', 's793816227', 's171027934']
[150488.0, 32360.0, 32620.0]
[2421.0, 231.0, 216.0]
[402, 626, 361]
p02684
u870736713
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())\nN=list(map(int,input().split()))\na={}\n \nb=1\na[1]=1\ncount=0\nwhile(count<=K):\n count+=1\n b=N[b-1]\n #print(b)\n if b in a:\n print(a)\n print(b)\n print(count)\n cycle=count-a[b]\n rest=K-count+1\n ans=rest%cycle\n for i in range(ans-1):\n b=N[b-1]\n print(b)\n break\n a[b]=count\n\nprint(b)', 'N,K=map(int,input().split())\nN=list(map(int,input().split()))\na={}\n\nb=1\na[1]=1\ncount=1\nwhile(True):\n count+=1\n b=N[b-1]\n #print(b)\n if b in a:\n #print(a)\n #print(b)\n #print(count)\n cycle=count-a[b]\n rest=K-count+1\n ans=rest%cycle\n for i in range(ans-1):\n b=N[b-1]\n print(b)\n break\n a[b]=count', 'N,K=map(int,input().split())\nA=list(map(int,input().split()))\n \na={}\n \nb=1\na[1]=1\ncount=1\nwhile(count<=K+1):\n count+=1\n b=A[b-1]\n if b in a:\n cycle=count-a[b]\n rest=K-count+1\n ans=rest%cycle-1\n for i in range(min([ans,rest])):\n b=A[b-1]\n print(b)\n break\n a[b]=count\n if count==K+1:\n print(b)\n break', 'N,K=map(int,input().split())\nA=list(map(int,input().split()))\n\na={}\n \nb=1\na[1]=1\ncount=1\nwhile(True):\n count+=1\n b=A[b-1]\n if b in a:\n cycle=count-a[b]\n rest=K-count+1\n ans=rest%cycle-1\n for i in range(min([ans,rest])):\n b=A[b-1]\n print(b)\n break\n a[b]=count\n if count==K+1:\n print(b)\n break', 'import sys\n\nN,K=map(int,input().split())\nN=list(map(int,input().split()))\na={}\n \nb=1\na[1]=1\ncount=0\nwhile(count<=K+1):\n count+=1\n b=N[b-1]\n #print(b)\n if b in a:\n #print(a)\n #print(b)\n #print(count)\n cycle=count-a[b]\n rest=K-count+1\n ans=rest%cycle\n for i in range(ans-1):\n b=N[b-1]\n count+=1\n if count==K+1:\n print(b)\n sys.exit()\n print(b)\n break\n a[b]=count\n if count==K+1:\n print(b)\n break', 'N,K=map(int,input().split())\nN=list(map(int,input().split()))\na={}\n \nb=1\na[1]=1\ncount=0\nwhile(count<=K+1):\n count+=1\n b=N[b-1]\n #print(b)\n if b in a:\n #print(a)\n #print(b)\n #print(count)\n cycle=count-a[b]\n rest=K-count+1\n ans=rest%cycle\n for i in range(ans-1):\n b=N[b-1]\n count+=1\n if count==K+1:\n break\n print(b)\n break\n a[b]=count\n if count==K+1:\n print(b)\n break', 'N,K=map(int,input().split())\nA=list(map(int,input().split()))\n \na={}\n \nb=1\na[1]=1\ncount=1\nwhile(count<=K+1):\n count+=1\n b=A[b-1]\n if b in a:\n cycle=count-a[b]\n rest=K-count+1\n ans=rest%cycle-1\n for i in range(min([ans,rest])):\n b=A[b-1]\n print(b)\n break\n a[b]=count\n if count==K+1:\n print(b)\n break', '#N,K=map(int,input().split())\n#A=list(map(int,input().split()))\n\nN=6\nK=727202214173249351\nA=[6,5,2,5,3,2]\n\na={}\n \nb=1\na[1]=1\ncount=1\nwhile(True):\n count+=1\n b=A[b-1]\n if b in a:\n cycle=count-a[b]\n rest=K-count+1\n ans=rest%cycle\n for i in range(min([ans,rest])):\n b=A[b-1]\n print(b)\n break\n a[b]=count\n if count==K+1:\n print(b)\n break', 'N,K=map(int,input().split())\nN=list(map(int,input().split()))\na={}\n \nb=1\na[1]=1\ncount=0\nwhile(count<=K+1):\n count+=1\n b=N[b-1]\n #print(b)\n if b in a:\n #print(a)\n #print(b)\n #print(count)\n cycle=count-a[b]\n rest=K-count+1\n ans=rest%cycle\n for i in range(ans-1):\n b=N[b-1]\n print(b)\n break\n a[b]=count\n if count==K+1:\n print(b)\n break', 'N,K=map(int,input().split())\nN=list(map(int,input().split()))\na={}\n \nb=1\na[1]=1\ncount=1\nwhile(True):\n count+=1\n b=N[b-1]\n #print(b)\n if b in a:\n #print(a)\n #print(b)\n #print(count)\n cycle=count-a[b]\n rest=K-count+1\n ans=rest%cycle\n for i in range(ans-1):\n b=N[b-1]\n print(b)\n break\n a[b]=count\nif count==K+1:\n print(b)\n break', 'N,K=map(int,input().split())\nN=list(map(int,input().split()))\na={}\n \nb=1\na[1]=1\ncount=1\nwhile(True):\n count+=1\n b=N[b-1]\n #print(b)\n if b in a:\n #print(a)\n #print(b)\n #print(count)\n cycle=count-a[b]\n rest=K-count+1\n ans=rest%cycle\n for i in range(ans-1):\n b=N[b-1]\n print(b)\n break\n a[b]=count\n if count==K+1:\n print(b)\n break', 'N,K=map(int,input().split())\nA=list(map(int,input().split()))\n\ncount=0\nk=1\ncountlist={}\n\ncount=1\nwhile True: \n k=A[k-1]\n \n if k in countlist:\n cycle=count-countlist[k]\n rest=K-count\n move=rest%cycle\n for i in range(min(rest,move)):\n k=A[k-1]\n print(k)\n break\n \n countlist[k]=count\n \n if count==K:\n print(k)\n break\n \n count+=1']
['Runtime Error', 'Wrong Answer', 'Wrong Answer', 'Wrong Answer', 'Runtime Error', 'Runtime Error', 'Wrong Answer', 'Wrong Answer', 'Runtime Error', 'Runtime Error', 'Wrong Answer', 'Accepted']
['s010544253', 's089811065', 's146045889', 's200825428', 's281500717', 's325488481', 's433263363', 's563981386', 's724957098', 's893880798', 's940696183', 's318297021']
[32580.0, 38804.0, 32352.0, 32360.0, 32372.0, 32300.0, 32352.0, 9152.0, 32372.0, 9072.0, 32380.0, 32384.0]
[204.0, 185.0, 162.0, 141.0, 139.0, 148.0, 156.0, 22.0, 146.0, 21.0, 132.0, 151.0]
[341, 329, 380, 373, 460, 426, 380, 415, 376, 364, 370, 440]
p02684
u872271866
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 main():\n n, k = map(int, input().split(" "))\n a = list(map(lambda i: int(i)-1, input().split(" ")))\n s = [0]\n u = [0]\n f = True\n loop_len = 1\n z = 0\n x = a[0]\n while f:\n if x in s:\n f = False\n z = s.index(x)\n s = s[z:]\n loop_len = len(s)\n \n else:\n s.append(x)\n x = a[x]\n y = (k-z) % loop_len\n print(s)\n print(s[y]+1)\n\n \n\n\n\nif __name__ == "__main__":\n main()', '\ndef main():\n n, k = map(int, input().split(" "))\n a = list(map(lambda i: int(i)-1, input().split(" ")))\n s = [0]\n u = [0]\n f = True\n loop_len = 1\n z = 0\n x = a[0]\n while f:\n if x in s:\n f = False\n b=s.index(x)\n s = s[b:]\n loop_len = len(s) - b\n z = s.index(x)\n else:\n s.append(x)\n x = a[x]\n y = (k-z) % loop_len\n print(s)\n print(s[y]+1)\n\n \n\n\n\nif __name__ == "__main__":\n main()', 'from collections import defaultdict\ndef main():\n n, k = map(int, input().split(" "))\n a = list(map(int, input().split(" ")))\n s = []\n ord = [-1]*(n)\n u = 1\n while ord[u] == -1:\n ord[u] = len(s)\n s.append(u)\n u = a[u-1]\n l = len(s) - ord[u]\n if k < ord[u]:\n print(s[k])\n else:\n print(s[(k-ord[u])%l+ord[u]])\n\n\nif __name__ == "__main__":\n main()', 'from collections import defaultdict\ndef main():\n n, k = map(int, input().split(" "))\n a = list(map(int, input().split(" ")))\n s = []\n ord = [-1]*(n+1)\n u = 1\n while ord[u] == -1:\n ord[u] = len(s)\n s.append(u)\n u = a[u-1]\n l = len(s) - ord[u]\n if k < ord[u]:\n print(s[k])\n else:\n print(s[(k-ord[u])%l+ord[u]])\n\n\nif __name__ == "__main__":\n main()']
['Wrong Answer', 'Runtime Error', 'Runtime Error', 'Accepted']
['s013872538', 's234114695', 's834428431', 's280437703']
[32324.0, 32308.0, 33804.0, 33820.0]
[2206.0, 2206.0, 136.0, 141.0]
[495, 511, 976, 978]
p02684
u872538555
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 search(now, teleporter, limit):\n visit = [-1] * len(teleporter)\n cnt = 0\n \n while visit[now] == -1:\n if limit <= cnt:\n break\n \n visit[now] = cnt\n cnt += 1\n now = teleporter[now]\n \n \n return now, cnt, cnt - visit[now]\n \n\nn, k = map(int, input().split())\na = [int(x) - 1 for x in input().split()]\n\nstart, initial_cnt, repeat_num = search(0, a, 10**5 + 1)\n\n#print(start, initial_cnt, repeat_num)\n\nif k < initial_cnt:\n r = k\nelse:\n r = (k - initial_cnt) % repeat_num\n\n#print(r)\n\nans, _, _ = search(start, a, r)', 'def search(now, teleporter, limit):\n visit = [-1] * len(teleporter)\n cnt = 0\n \n while visit[now] == -1:\n if limit <= cnt:\n break\n \n visit[now] = cnt\n cnt += 1\n now = teleporter[now]\n \n \n return now, cnt, cnt - visit[now]\n \n\nn, k = map(int, input().split())\na = [int(x) - 1 for x in input().split()]\n\nstart, initial_cnt, repeat_num = search(0, a, 10**6)\n\n#print(start, initial_cnt, repeat_num)\n\nif k < initial_cnt:\n start = 0\n r = k\nelse:\n r = (k - initial_cnt) % repeat_num\n\n#print(r)\n\nans, _, _ = search(start, a, r)\n\nprint(ans + 1)']
['Wrong Answer', 'Accepted']
['s803795490', 's662428896']
[32220.0, 32264.0]
[141.0, 144.0]
[590, 616]
p02684
u873616440
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()))\ne = [False] * (N+1)\nidx = 1\nmove = []\nwhile True:\n if e[idx]:\n break\n move.append(idx)\n e[idx] = True\n idx = A[idx-1]\nm = move.index(idx)\nc = len(move) - m\n\nif K <= m:\n print(D[K])\nelse:\n print(D[m+(K-m) % c])', 'N, K = map(int, input().split())\nA = list(map(int, input().split()))\ne = [False] * (N+1)\nidx = 1\nmove = []\nwhile True:\n if e[idx]:\n break\n move.append(idx)\n e[idx] = True\n idx = A[idx-1]\nm = move.index(idx)\nc = len(move) - m\n\nif K <= m:\n print(move[K])\nelse:\n print(move[m+(K-m) % c])\n']
['Runtime Error', 'Accepted']
['s021312022', 's963198130']
[32300.0, 32380.0]
[150.0, 135.0]
[291, 298]
p02684
u875541136
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\nt_zero = [-1] * N\n\nt = 0\na = 1\n\nwhile t_zero[a-1] == -1:\n if t == K:\n break\n t_zero[a-1] = t\n t += 1\n a = A[a-1]\nelse:\n k = (K - t_zero[a-1]) % (t - t_zero[a-1])\n a = t_zero.index(t_zero[a] + k)\nprint(a)', 'N, K = map(int, input().split())\nA = list(map(int, input().split()))\n\nt_zero = [-1] * N\n\nt = 0\na = 1\n\nwhile t_zero[a-1] == -1:\n if t == K:\n break\n t_zero[a-1] = t\n t += 1\n a = A[a-1]\nelse:\n k = (K - t_zero[a-1]) % (t - t_zero[a-1])\n a = t_zero.index(t_zero[a-1] + k) + 1\nprint(a)']
['Runtime Error', 'Accepted']
['s191973717', 's197697637']
[32372.0, 32388.0]
[130.0, 140.0]
[282, 288]
p02684
u891516200
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 = map(int, input().split())\ntown_list = list(map(int, input.split()))\n\nroop = 0\nqueue = [1]\n\nstart = -1\n\nfor now_town in queue:\n if not town_list[now_town - 1] in queue:\n queue.append(town_list[now_town - 1])\n\n \n else:\n start = town_list[now_town - 1]\n break\n\nif start == -1:\n print(queue[K])\n sys.exit()\n\nbefore_len = queue.index(start)\nroop = len(queue) - before_len\n\nprint(queue[before_len + (K - before_len) % roop])\n', 'import sys\nimport time\n\nN, K = map(int, input().split())\ntown_list = list(map(int, input().split()))\n\nqueue = {}\n\nstart = -1\ncount = 1\n\nnow_town = 1\n\nfor i in range(N):\n if town_list[now_town - 1] in queue:\n start = town_list[now_town - 1]\n break\n queue[town_list[now_town - 1]] = count\n now_town = town_list[now_town - 1]\n count += 1\n\nif start == -1:\n print(queue[now_town])\n sys.exit()\n\nbefore_len = len(queue) - queue[start] - 1\nroop = len(queue) - before_len\n\nprint(list(queue.keys())[(K - before_len) % roop + before_len - 1])\n', 'import sys\nimport time\n\nN, K = map(int, input().split())\ntown_list = list(map(int, input().split()))\n\nqueue = {}\n\nstart = -1\ncount = 1\n\nnow_town = 1\n\nfor i in range(K):\n if town_list[now_town - 1] in queue:\n start = town_list[now_town - 1]\n break\n queue[town_list[now_town - 1]] = count\n now_town = town_list[now_town - 1]\n count += 1\n\nif start == -1:\n print(now_town)\n sys.exit()\n\nroop = len(queue) - (queue[start] - 1)\nbefore_len = queue[start]\n\nprint(list(queue.keys())[(K - before_len) % roop + before_len - 1])\n']
['Runtime Error', 'Runtime Error', 'Accepted']
['s627534436', 's967399771', 's604674248']
[8976.0, 38816.0, 32316.0]
[34.0, 195.0, 165.0]
[488, 564, 548]
p02684
u894521144
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, K, A):\n i = 0 \n lst_memo = []\n set_memo = {}\n while i not in set_memo:\n lst_memo.append(i)\n set_memo.append(i)\n i = A[i] - 1 \n key = lst_memo.index(i)\n roop = memo[key:]\n print(roop[(K - key) % len(roop)] + 1) \n\n\nif __name__ == '__main__':\n N, K = list(map(int, input().split()))\n A = list(map(int, input().split()))\n main(N, K, A)", "def main(N, K, A):\n i = 0 \n lst_memo = []\n set_memo = set([])\n while i not in set_memo:\n lst_memo.append(i)\n set_memo.add(i)\n i = A[i] - 1 \n key = lst_memo.index(i)\n if K < key:\n print(lst_memo[K] + 1)\n else:\n roop = lst_memo[key:]\n print(roop[(K - key) % len(roop)] + 1) \n\n\nif __name__ == '__main__':\n N, K = list(map(int, input().split()))\n A = list(map(int, input().split()))\n main(N, K, A)"]
['Runtime Error', 'Accepted']
['s369878112', 's146474986']
[32240.0, 35668.0]
[66.0, 121.0]
[465, 536]
p02684
u896451538
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 collections\n\nn,k = list(map(int,input().split()))\na = list(map(int,input().split()))\na = [int(i)-1 for i in a]\n\nex = collections.defaultdict(lambda:-1)\nstep=1\ncur=0\nex[0]=0\nwhile 1:\n if ex[a[cur]]!=-1:\n cycle = step-ex[a[cur]]\n k-=ex[a[cur]]-1\n #print(k%cycle)\n break\n else:\n ex[a[cur]]=step\n cur=a[cur]\n step+=1\n\nprint(ex,k%cycle)\nfor i in range(k%cycle):\n cur = a[cur]\nprint(cur+1)', 'import collections\n \nn,k = list(map(int,input().split()))\na = list(map(int,input().split()))\na = [int(i)-1 for i in a]\n \nex = collections.defaultdict(lambda:-1)\nstep=1\ncur=0\nex[0]=0\nwhile 1:\n if ex[a[cur]]!=-1:\n cycle = step-ex[a[cur]]\n k-=ex[a[cur]]-1\n #print(k%cycle)\n break\n else:\n if step==k:\n print(a[cur]+1)\n exit()\n ex[a[cur]]=step\n cur=a[cur]\n step+=1\n\n#print(ex,k%cycle)\nfor i in range(k%cycle):\n cur = a[cur]\nprint(cur+1)']
['Wrong Answer', 'Accepted']
['s268024960', 's295647283']
[42232.0, 33900.0]
[305.0, 198.0]
[444, 514]
p02684
u896741788
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()))\nok=set()\nnow=0\nl=[]\nwhile now not in ok and k:\n l.append(now)\n ok.add(now)\n now=a[now]-1\n k-=1\nlength=len(ok)-(h:=l.index(now))\nl=l[h:]\nprint(l,h,length)\nk=(k)%length\n\nprint(l[k]+1)', 'n,k=map(int,input().split())\na=list(map(int,input().split()))\nok=set()\nnow=0\nl=[]\nwhile now not in ok and k:\n l.append(now)\n ok.add(now)\n now=a[now]-1\n k-=1\nif k==0:print(now+1);exit()\nlength=len(ok)-(h:=l.index(now))\nl=l[h:]\nprint(l,h,length)\nk=(k)%length\n\nprint(l[k]+1)', 'def f():\n n,k=map(int,input().split())\n l=list(map(int,input().split()))\n now=1\n for d in range(k.bit_length()):\n k,f=divmod(k,2)\n if f:now=l[now-1]\n l=tuple(l[i-1]for i in l)\n print(now)\nif __name__ == "__main__":\n f()\n']
['Runtime Error', 'Wrong Answer', 'Accepted']
['s304845654', 's510372099', 's355969015']
[35088.0, 35384.0, 32328.0]
[167.0, 160.0, 1587.0]
[255, 283, 259]
p02684
u898058223
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()))\nnext=a[0]\ntown=[0]*n\ntown[0]+=1\nfor i in range(n):\n if town[next-1]==0:\n town[next-1]+=1\n next=a[next-1]\n else:\n start=next\n break\nnext=a[start-1]\ncnt=1\nfor i in range(n):\n if a[next-1]!=start:\n next=a[next-1]\n cnt+=1\n else:\n cnt+=1\n break\nprint(cnt)\nk%=cnt\nnext=a[0]\nfor i in range(k-1):\n next=a[next-1]\nprint(next)', 'n,k=map(int,input().split())\na=list(map(int,input().split()))\ntown=[0]*n\ntown[0]+=1\nnext=a[0]\nfor i in range(n):\n if town[next-1]==0:\n town[next-1]+=1\n next=a[next-1]\n else:\n break\nre=next\nnext=a[re-1]\nt2=0\nfor i in range(n):\n t2+=1\n if a[next-1]==re:\n t2+=1\n break\n else:\n next=a[next-1]\nt1=0\nnext=a[0]\nfor i in range(n):\n t1+=1\n if a[next-1]==re:\n t1+=1\n break\n else:\n next=a[next-1]\nif k<=t1:\n next=a[0]\n for i in range(k-1):\n next=a[next-1]\n print(next)\nelse:\n k-=t1\n k%=t2\n next=re\n for i in range(k):\n next=a[next-1]\n print(next)']
['Wrong Answer', 'Accepted']
['s105662062', 's658853325']
[32244.0, 32400.0]
[178.0, 236.0]
[407, 584]
p02684
u899782392
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\ncnts = [None] * N\n\npos = 0\ncnt = 0\n\nwhile cnt < K:\n print(cnts)\n if cnts[pos] != None:\n loop_size = cnt - cnts[pos]\n cnt += ((K-cnt) // loop_size - 1) * loop_size\n cnts = [None] * N\n cnts[pos] = cnt\n pos = A[pos]\n cnt += 1\n\nprint(pos+1)', 'N, K = map(int, input().split())\nA = list(map(lambda x:int(x)-1, input().split()))\n\ncnts = [None] * N\n\npos = 0\ncnt = 0\n\nwhile cnt < K:\n if cnts[pos] != None:\n loop_size = cnt - cnts[pos]\n cnt += ((K-cnt) // loop_size - 1) * loop_size\n cnts = [None] * N\n cnts[pos] = cnt\n pos = A[pos]\n cnt += 1\n\nprint(pos+1)', 'N, K = map(int, input().split())\nA = list(map(lambda x:int(x)-1, input().split()))\n\ncnts = [None] * N\n\npos = 0\ncnt = 0\n\nwhile cnt < K:\n if cnts[pos] != None:\n loop_size = cnt - cnts[pos]\n cnt += ((K-cnt-1) // loop_size) * loop_size\n cnts = [None] * N\n cnts[pos] = cnt\n pos = A[pos]\n cnt += 1\n\nprint(pos+1)\n\n']
['Runtime Error', 'Time Limit Exceeded', 'Accepted']
['s768670945', 's897019848', 's280731117']
[152100.0, 32356.0, 32264.0]
[2255.0, 2206.0, 196.0]
[356, 340, 340]
p02684
u907223098
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().split())\nr=[0 for i in range(n)]\nl=[int(i) for i in input().split()]\nwazap=[]\ncount=1\nsaver=None\ntemp=0\nwhile r[temp]==0:\n r[temp]=count\n wazap.append(temp)\n temp=l[temp]-1\nstart=wazap[-1]\nfor i in range(len(wazap)):\n if wazap[i]==start-1:\n left=i\n break\nloop=len(wazap)-left\nif k<left:\n print(wazap[k]+1)\nelse:\n print(wazap[(k-left)%loop+left]+1)', 'n,k=(int(i) for i in input().split())\nr=[0 for i in range(n)]\nl=[int(i) for i in input().split()]\nwazap=[]\ncount=1\nsaver=None\ntemp=0\nwhile r[temp]==0:\n r[temp]=count\n wazap.append(temp)\n temp=l[temp]-1\nstart=l[wazap[-1]]\nleft=None\nfor i in range(len(wazap)):\n if wazap[i]==start-1:\n left=i\n break\nloop=len(wazap)-left\nif k<left:\n print(wazap[k]+1)\nelse:\n print(wazap[(k-left)%loop+left]+1)']
['Runtime Error', 'Accepted']
['s459641289', 's648389506']
[33888.0, 33896.0]
[142.0, 175.0]
[388, 401]
p02684
u909991537
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 = (int(i) for i in input().split()) \nA = [int(i) for i in input().split()] \n\ndef has_duplicates(seq):\n return len(seq) != len(set(seq))\n\nlis = [1]\nidx = 0\n\ncheck_l = [0] * N\nfor i in range(N):\n lis.append(A[idx])\n if check_l[A[idx]] != 1:\n lis.pop(-1)\n found = lis.index(A[idx])\n lis.append(A[idx])\n break\n check_l[A[idx]] == 1\n idx = A[idx]-1\n \nprint(lis[found:i+1][(K-found) % (i - found + 1)])', 'import numpy as np\nN, K = (int(i) for i in input().split()) \nA = [int(i) - 1 for i in input().split()] \nN = len(A)\n\ndef get_keys_from_value(d, val):\n return [k for k, v in d.items() if v == val][0]\n\nnow = 0\nlis = []\np = {}\nfor i in range(N):\n now = A[now]\n if now in p:\n break\n \n p[now] = i\n lis.append(now)\n\nif K < i:\n print(lis[K-1] + 1)\nelse:\n print(lis[(K-i-1) % (i - p[now]) + p[now]] + 1)\n \n ']
['Runtime Error', 'Accepted']
['s356474175', 's774354532']
[51052.0, 57864.0]
[174.0, 247.0]
[439, 416]
p02684
u915879510
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\n\n# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template)\ndef main():\n # Failed to predict input format\n N, K = list(map(int, input().split()))\n A = list(map(int, input().split()))\n\n visited = [0] * (N+10)\n seq = []\n\n step = 0\n current = 1\n\n n1 = n2 = 0\n while(step<len(A)+1):\n print(current)\n seq.append(current)\n step += 1\n visited[current] = step\n next_pos = A[current-1]\n if visited[next_pos]>0:\n n1 = visited[next_pos]\n n2 = step - n1 + 1\n print(n1, n2)\n print(n1-1, ((K-(n1-1))%n2))\n print(seq[n1-1+((K-(n1-1))%n2)])\n return\n else:\n visited[next_pos] = step\n current = next_pos\n\n print(seq[K])\n\n return\n\n\nif __name__ == '__main__':\n main()\n", "#!/usr/bin/env python3\n\n\n# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template)\ndef main():\n # Failed to predict input format\n N, K = list(map(int, input().split()))\n A = list(map(int, input().split()))\n\n visited = [0] * (N+1)\n seq = []\n\n step = 0\n current = 1\n\n n1 = n2 = 0\n while(step<len(A)):\n #print(visited)\n seq.append(current)\n step += 1\n visited[current] = step\n next_pos = A[current-1]\n if visited[next_pos]>0:\n n1 = visited[next_pos]\n n2 = step - n1 + 1\n seq.append(next_pos)\n break\n current = next_pos\n\n #print(n1, n2)\n \n #print(seq)\n if n1+n2<=K:\n print(seq[n1-1+((K-(n1-1))%n2)])\n else:\n print(seq[K])\n\n return\n\n\nif __name__ == '__main__':\n main()\n"]
['Wrong Answer', 'Accepted']
['s288407211', 's300946537']
[31020.0, 32388.0]
[216.0, 145.0]
[948, 956]
p02684
u920977317
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\ndef main():\n N,K=map(int,input().split())\n\n A=list(map(lambda x:int(x)-1,input().split()))\n\n loop=[]\n\n slack_que=deque([])\n flag=0\n t=0\n start=0\n counter=0\n while flag==0:\n slack_que.append(t)\n t=A[t]\n A[t]=-1\n if t==-1:\n start=counter\n flag=1\n counter+=1\n slack=list(slack_que)\n slack_num=start\n loop_num=len(slack)-slack_num\n\n # print(slack)\n # print(start)\n # print(slack_num)\n # print(loop_num)\n #\n loop=slack[start:]\n # print(loop)\n if (K-slack_num)<0:\n res=slack[K]+1\n else:\n tele_num=((K-slack_num)%loop_num)\n town=loop[tele_num]\n res=town+1\n\n print(res)\n\nif __name__=="__main__":\n main()\n\n', 'from collections import deque\n\ndef main():\n N,K=map(int,input().split())\n\n A=list(map(lambda x:int(x)-1,input().split()))\n FootPrintA=A[:]\n\n loop=[]\n\n slack_que=deque([])\n flag=0\n t=0\n start=0\n while flag==0:\n slack_que.append(t)\n t_ = FootPrintA[t]\n FootPrintA[t]=-1\n if t_==-1:\n start=slack_que.index(t)\n flag=1\n slack_que.pop()\n t = A[t]\n\n slack=list(slack_que)\n slack_num=start\n loop_num=len(slack)-slack_num\n\n # print(slack)\n # print(start)\n # print(slack_num)\n # print(loop_num)\n #\n loop=slack[start:]\n # print(loop)\n if (K-slack_num)<0:\n res=slack[K]+1\n else:\n tele_num=((K-slack_num)%loop_num)\n town=loop[tele_num]\n res=town+1\n\n print(res)\n\nif __name__=="__main__":\n main()\n\n']
['Wrong Answer', 'Accepted']
['s590218965', 's696915375']
[33916.0, 33924.0]
[82.0, 138.0]
[782, 848]
p02684
u927282564
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 = list(map(int, input().split()))\nA = list(map(int, input().split()))\n\ndef func1(lst, value):\n return [i for i, x in enumerate(lst) if x == value]\n \ntemp=1\ntrace=deque()\ntrace.append(1)\n#trace=[1]\ncircle=[]\nfor i in range(K):\n temp=A[temp-1]\n ans=func1(trace,temp)\n if len(ans)==1:\n #print(temp)\n #print(trace.index(temp))\n #start=trace.index(temp)\n for _ in range(ans[0]):\n trace.popleft()\n #circle=trace[ans[0]:]\n circle.append(1)\n #print(circle)\n break\n trace.append(temp)\n #print("temp",temp)\n\nif len(circle)!=0:\n amari=(K-i)%len(trace)\n print(trace[amari])\nelse:\n print(temp)', 'N, K = list(map(int, input().split()))\nA = list(map(int, input().split()))\n \ndef func1(lst, value):\n return [i for i, x in enumerate(lst) if x == value]\n \ntemp=1\ntrace=[1]\ncircle=[]\nfor i in range(K):\n temp=A[temp-1]\n ans=func1(trace,temp)\n if len(ans)==1:\n circle.append(i-ans[0]+1)\n #print(circle)\n break\n trace.append(temp)\n #print("temp",temp)\n\nif len(circle)!=0:\n amari=(K-i)%circle[0]+1\n for j in range(i+amari):\n temp=A[temp-1]\n\nprint(temp)', 'N, K = list(map(int, input().split()))\nA = list(map(int, input().split()))\n \ncheck=[-1 for i in range(N)]\n \ntemp=1\ncircle=[]\nfor i in range(K):\n temp=A[temp-1]\n if check[temp-1]==-1:\n check[temp-1]=i\n else:\n circle.append(i-check[temp-1])\n #print(circle)\n break\n #print("temp",temp)\n\nif len(circle)!=0:\n amari=(K-i)%circle[0]\n temp=1\n for j in range(i+amari):\n temp=A[temp-1] \n\nprint(temp)']
['Wrong Answer', 'Wrong Answer', 'Accepted']
['s310332570', 's922102829', 's700995899']
[33916.0, 32260.0, 32380.0]
[2206.0, 2206.0, 197.0]
[684, 497, 439]
p02684
u933129390
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()))\nlog = []\nvisited = [False for i in range(n)]\nnow = a[0]-1\nloop = False\nfor i in range(k-1):\n if visited[now] == True:\n loop = True\n break\n log.append(now)\n visited[now] = True\n now = a[now]-1\nelse:\n print(now+1)\nif loop:\n for i in range(len(log)):\n if log[i] == now:\n first = len(log[:i])\n log_list = log[i:]\n loop_num = len(log_list)\n break\n print(log)\n k -= first\n k %= loop_num\n print(log_list[k-1]+1)\n', 'n, k = map(int, input().split())\na = list(map(int, input().split()))\nlog = []\nvisited = [False for i in range(n)]\nnow = a[0]-1\nloop = False\nfor i in range(k-1):\n if visited[now] == True:\n loop = True\n break\n log.append(now)\n visited[now] = True\n now = a[now]-1\nelse:\n print(now+1)\nif loop:\n for i in range(len(log)):\n if log[i] == now:\n first = len(log[:i])\n log_list = log[i:]\n loop_num = len(log_list)\n break\n k -= first\n k %= loop_num\n print(log_list[k-1]+1)\n']
['Wrong Answer', 'Accepted']
['s070285054', 's726704739']
[32300.0, 32376.0]
[160.0, 138.0]
[569, 554]
p02684
u935016954
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 copy import copy, deepcopy\nfrom collections import Counter\nfrom math import sqrt, floor, factorial\nfrom itertools import permutations, combinations, combinations_with_replacement\nfrom operator import mul\nfrom functools import reduce\nimport bisect\n\nMOD = 10**9 + 7\nINF = float('inf')\n\nN, K = list(map(int, input().split()))\n\nA = list(map(int, input().split()))\n\nA = [A[i]-1 for i in range(len(A))]\n\nmem = [0]*N\nmem2 = [0]*N\n\nidx = 0\ncount1 = 0\nwhile True:\n mem[idx] += 1\n if mem[idx] == 2:\n break\n count1 += 1\n idx = A[idx]\n\n\nidx2 = idx\ncount2 = 0\n\ncount2 += 1\nidx2 = A[idx2]\n\nwhile True:\n if idx2 == idx:\n break\n count2 += 1\n idx2 = A[idx2]\n\ntmp = count1 - count2\nprint(tmp, count2)\n\nif tmp >= K:\n ans = 0\n for _ in range(K):\n ans = A[ans]\nelse:\n e = (K - tmp) % count2 + tmp\n ans = 0\n for _ in range(e):\n ans = A[ans]\n\nprint(ans+1)\n", "from copy import copy, deepcopy\nfrom collections import Counter\nfrom math import sqrt, floor, factorial\nfrom itertools import permutations, combinations, combinations_with_replacement\nfrom operator import mul\nfrom functools import reduce\nimport bisect\n\nMOD = 10**9 + 7\nINF = float('inf')\n\nN, K = list(map(int, input().split()))\n\nA = list(map(int, input().split()))\n\nA = [A[i]-1 for i in range(len(A))]\n\nmem = [0]*N\nmem2 = [0]*N\n\nidx = 0\ncount1 = 0\nwhile True:\n mem[idx] += 1\n if mem[idx] == 2:\n break\n count1 += 1\n idx = A[idx]\n\n\nidx2 = idx\ncount2 = 0\n\ncount2 += 1\nidx2 = A[idx2]\n\nwhile True:\n if idx2 == idx:\n break\n count2 += 1\n idx2 = A[idx2]\n\ntmp = count1 - count2\n\nif tmp >= K:\n ans = 0\n for _ in range(K):\n ans = A[ans]\nelse:\n e = (K - tmp) % count2 + tmp\n ans = 0\n for _ in range(e):\n ans = A[ans]\n\nprint(ans+1)\n"]
['Wrong Answer', 'Accepted']
['s800937886', 's983496682']
[32208.0, 32252.0]
[209.0, 175.0]
[902, 883]
p02684
u935241425
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\nleg = [ 0 ] * n\nleg[ 0 ] = 1\n\np = 0\ncity_old = 1\nfor x in range( k ):\n city_new = a[ city_old-1 ]\n if leg[ city_new-1 ] == 0:\n leg[ city_new-1 ] += x + 1\n else:\n loop = x + 1 - leg[ city_new-1 ]\n modk = ( k - ( x + 1 ) ) % loop\n break\n city_old = city_new\n p += 1\n\nif p == k:\n print( city_old )\n exit()\n\n\ncity_old = city_new\nfor x in range( modk ):\n city_new = a[ city_old-1 ]\n city_old = city_new\n\nprint( city_old )\n', 'n, k = map( int, input().split() )\na = [ int( i ) for i in input().split() ]\n\nleg = [ 0 ] * n\nleg[ 0 ] = 0\n\np = 0\ncity_old = 1\nfor x in range( k ):\n city_new = a[ city_old-1 ]\n if leg[ city_new-1 ] == 0 and city_new != 1:\n leg[ city_new-1 ] = x + 1\n else:\n loop = x + 1 - leg[ city_new-1 ]\n modk = ( k - ( x + 1 ) ) % loop\n# print( loop, modk )\n break\n city_old = city_new\n p += 1\n\nif p == k:\n print( city_old )\n exit()\n\n\ncity_old = city_new\nfor x in range( modk ):\n city_new = a[ city_old-1 ]\n city_old = city_new\n\nprint( city_old )\n']
['Runtime Error', 'Accepted']
['s265543670', 's292507755']
[32324.0, 32172.0]
[157.0, 181.0]
[550, 596]
p02684
u941644149
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()))\nway = A[0]\nList = [0 for n in range(N)]\nList[way-1] += 1\ncount = 0\nstring = f"{way}"\n#print(f"string:{string}")\n#print(f"way:{way}")\n\nfor i in range(10**7):\n way = A[way-1]\n List[way-1] += 1 \n count += 1\n #print(f"way:{way}")\n #print(f"count:{count}")\n #print(List)\n if max(List) == 3:\n break\n else:\n string += f"{way}"\n #print(f"string:{string}")\n #print()\n\n#print(List)\n#print()\nnew_list = [i for i in List if i == 1]\nlength = len(new_list)\nn_list = [j for j in List if j >= 2]\n#print(new_list)\n#print(n_list)\namari = (K - length)% len(n_list)\n\nprint(int(n_list[amari]))', 'N, K = map(int, input().split())\nA = list(map(int, input().split()))\nway = A[0]\nList = [0 for n in range(N)]\nList[way-1] += 1\ncount = 0\nstring = f"{way}"\n#print(f"string:{string}")\n#print(f"way:{way}")\n\nfor i in range(10**7):\n way = A[way-1]\n List[way-1] += 1 \n count += 1\n #print(f"way:{way}")\n #print(f"count:{count}")\n #print(List)\n if max(List) == 3:\n break\n else:\n string += f"{way}"\n #print(f"string:{string}")\n #print()\n\n#print(List)\n#print()\nnew_list = [i for i in List if i == 1]\nlength = len(new_list)\nn_list = [j for j in List if j >= 2]\n#print(new_list)\n#print(n_list)\namari = (K - length)% len(n_list)\n\nprint(int(n_list[amari+1]))\n', 'N, K = map(int, input().split())\nA = list(map(int, input().split()))# 0index\nvisi = [-1 for i in range(N)]\nL = [1] \nvisi[0] = 0\nway = A[0] \n#l = []\ncount = 1\n\nwhile visi[way-1] == -1: \n visi[way-1] = count\n count += 1\n L.append(way)\n way = A[way-1]\n\nl = count - visi[way-1]\nl_pre = visi[way-1]\nloop_L = L[l_pre:]\n\n\n\nif K > l_pre:\n index = (K- l_pre)% l\n ans = loop_L[index]\nelse:\n ans = L[K]\nprint(ans)']
['Wrong Answer', 'Runtime Error', 'Accepted']
['s457071922', 's617158903', 's373680862']
[32380.0, 32276.0, 32268.0]
[2206.0, 2206.0, 172.0]
[693, 696, 609]
p02684
u946517952
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())\nalist = list(map(int,input().split()))\n\nmemo = []\np = 1\nvis = ['.']*n\n\nwhile vis[p-1] == '.':\n memo.appened(p)\n vis[p-1] = 'v'\n p = alist[p-1]\n\nloopstart = alist.index(p)\nif k < len(alist):\n print(memo[k])\nelse:\n print(memo[loopstart+(k-loopstart)%(len(alist)-loopstart)])\n", "from collections import deque\nn,k = map(int,input().split())\nalist = list(map(int,input().split()))\n\n\nflag = ['.']*(n+1)\nflag[1] = 'v'\n\nbeen = [1]\n\nfor i in range(k):\n goto = alist[been[-1]-1]\n if flag[goto] == '.':\n been.append(goto)\n flag[goto] = 'v'\n else:\n loopstart = goto\n break\nloop = been[been.index(loopstart):]\nprint(loop[(k-loopstart)%len(loop)])\n\n", 'n,k=list(map(int,input().split()))\nm=list(map(int,input().split()))\na=[]\np=1\nvis=[0]*n\nwhile vis[p-1]==0:\n a.append(p)\n vis[p-1]=1\n p=m[p-1]\n\nl=a.index(p)\nprint(k,l,a)\nif k<len(a):\n print(a[k])\nelse:\n k-=l\n print(a[l+k%(len(a)-l)])\n', "n,k = map(int,input().split())\nalist = list(map(int,input().split()))\nmemo = []\np = 1\nvis = ['.']*n\nwhile vis[p-1] == '.':\n memo.append(p)\n vis[p-1] = 'v'\n p = alist[p-1]\n\nloop = memo.index(p)\nif k < len(memo):\n print(memo[k])\nelse:\n print(memo[loop+(k-loop)%(len(memo)-loop)])\n"]
['Runtime Error', 'Runtime Error', 'Wrong Answer', 'Accepted']
['s085914072', 's273004999', 's716759230', 's989506386']
[32296.0, 32696.0, 32352.0, 32132.0]
[73.0, 150.0, 172.0, 150.0]
[319, 396, 250, 293]
p02684
u948911484
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()))\nvisited = [0]*(n+1)\nturn = [0]*(n+1)\nvisited[1] = 0\nnxt = 1\ni = 1\nwhile True:\n if visited[a[nxt]]:\n nxt2 = a[nxt]\n break\n turn[a[nxt]] = i\n visited[nxt] = 1\n i += 1\n nxt = a[nxt]\n\n\n\nloop = i-turn[nxt2]\nif turn[nxt2] == 0:\n loop = i\n start = 0\nstart = turn[nxt2]\n#print(turn,i,visited)\n#print(start,loop)\nif start == 0 and k % loop == 0:\n print(1)\n quit()', 'n,k = map(int,input().split())\na = [0]+list(map(int,input().split()))\nvisited = [0]*(n+1)\nturn = [0]*(n+1)\nvisited[1] = 0\nnxt = 1\ni = 1\nwhile True:\n if visited[a[nxt]]:\n nxt2 = a[nxt]\n break\n turn[a[nxt]] = i\n visited[nxt] = 1\n i += 1\n nxt = a[nxt]\n\nloop = i-turn[nxt2]\nif turn[nxt2] == 0:\n loop = i\n start = 0\nstart = turn[nxt2]\n#print(turn,i)\n#print(start,loop)\nif start == 0 and k % loop == 0:\n print(1)\n quit()\nif k < i:\n print(turn.index(k))\nelse:\n print(turn.index(((k-start)%loop)+start))']
['Wrong Answer', 'Accepted']
['s119536604', 's360321578']
[32292.0, 32200.0]
[151.0, 162.0]
[439, 512]
p02684
u955125992
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\ns = 1\nplace = [s]\nfirst = set(place)\n\nfor i in range(n):\n s = a[s-1]\n if s in first:\n loops = place.index(s)\n loope = len(place)\n ans = loops + (k-loope) % (loope-loops)\n print(place(ans))\n break\n else:\n place.append(s)\n first.add(s)\nelse:\n print(s)', 'n, k = map(int, input().split())\na = list(map(int, input().split()))\n\ns = 1\nplace = [s]\nfirst = set(place)\n\nfor i in range(1, k+1):\n s = a[s-1]\n if s in first:\n loops = place.index(s)\n loope = len(place)\n ans = loops + (k-loope) % (loope-loops)\n print(place[ans])\n exit()\n else:\n place.append(s)\n first.add(s)\n\nprint(s)']
['Runtime Error', 'Accepted']
['s079010729', 's870741529']
[32228.0, 32364.0]
[190.0, 148.0]
[380, 377]
p02684
u962330718
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=K%N\nans=1\nfor i in range(l):\n ans=A[ans-1]\n \nprint(ans)', 'N,K=map(int,input().split())\nA=list(map(int,input().split()))\nB=[0]*N\npoint=1\nm,l=0,0\nif K>N:\n for i in range(N):\n if B[point-1]==0:\n B[point-1]=i+1\n point=A[point-1]\n else:\n m=i+1-B[point-1]\n l=B[point-1]\n break\n if m!=0:\n m=(K-l)%m\n else:\n m=N\nelse:\n m=K\nans=1\nfor i in range(l+m):\n ans=A[ans-1]\n\nprint(ans)']
['Wrong Answer', 'Accepted']
['s024837721', 's534464200']
[32252.0, 32280.0]
[99.0, 169.0]
[122, 464]
p02684
u962718741
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 A_cnt = [0] * N\n telepo = [1]\n i = 0\n A_cnt[i] = 1\n roop = 0\n while True:\n if A_cnt[i] > 1:\n roop = telepo.index(A[i]) - 1\n break\n A_cnt[A[i] - 1] += 1\n telepo.append(A[i])\n i = A[i] - 1\n K -= roop\n K %= len(telepo) - roop\n\n print(telepo[K + roop - 1])\n\n\nif __name__ == "__main__":\n main()\n', '#!/usr/bin/env python3\n\ndef main():\n N, K = map(int, input().split())\n A = list(map(int, input().split()))\n Acnt = [0] * N\n Acnt[0] = 1\n telepo = [1]\n i = 0\n while True:\n Acnt[A[i] - 1] += 1\n if Acnt[A[i] - 1] > 1:\n break\n telepo.append(A[i])\n i = A[i] - 1\n roop_s = telepo.index(A[i])\n roop = len(telepo) - roop_s\n\n ans = 0\n if K < roop_s:\n ans = K\n else:\n ans = K - roop_s\n ans %= roop\n ans += roop_s\n\n print(telepo[ans])\n\n\nif __name__ == "__main__":\n main()\n']
['Wrong Answer', 'Accepted']
['s362445309', 's206778288']
[32368.0, 32248.0]
[143.0, 143.0]
[460, 570]
p02684
u968404618
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 = []\n\nrec = [-1] * (n+1)\nidx = 1\n\nwhile (rec[idx] == -1):\n rec[idx] = len(Town)\n Town.append(idx)\n idx = A[idx-1]\n\nExce = rec.index(idx)\nCycl = len(Town) - Exce\n\nif Cycl > k:\n print(Town[k])\nelse:\n k -= Exce\n k %= Cycl\n print(Town[Exce+k])', 'def main():\n ## IMPORT MODULE\n #import sys\n\n \n #input=lambda :sys.stdin.readline().rstrip()\n\n #f_inf=float("inf")\n #MOD=10**9+7\n \n if \'get_ipython\' in globals(): \n ## SAMPLE INPUT\n n, k = 4, 5\n A = [3, 2, 4, 1]\n\n else:\n ##INPUT \n #n = input()\n n, k = map(int, input().split())\n A = list(map(int, input().split()))\n\n ## SUBMITION CODES HERE\n Town = []\n rec = [-1] * (n+1) # The town we haven\'t visited yet is -1\n idx = 1\n\n # Loop to the cycle\n while (rec[idx] == -1):\n rec[idx] = len(Town)\n Town.append(idx)\n idx = A[idx-1]\n\n # Pre-cycle\n Exce = rec[idx]\n # Intra-cycle\n Cycl = len(Town) - Exce\n\n if Exce > k:\n print(Town[k])\n else:\n Warp = (k - Exce) % Cycl\n print(Town[Exce+Warp])\n \nmain()']
['Runtime Error', 'Accepted']
['s233318907', 's837770701']
[32248.0, 33364.0]
[178.0, 150.0]
[402, 781]
p02684
u970082363
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())\ntown = [int(i) for i in input().split()]\nwarp = [1]\nchecklist = [0]+[-1]*n\ni = 0\nj = 0\nwhile j<=n:\n #print(town[i])\n warp.append(town[i])\n #print(warp,checklist)\n if checklist[town[i]-1]!=-1:\n s = checklist[town[i]-1]\n warp = warp[s:j]\n #print(checklist[town[i]-1],j)\n #print("a",warp)\n break\n checklist[town[i]-1] = j\n i = town[i]-1\n j += 1\n\n\nm = len(warp)\nprint(warp[(k-s)%m])', 'n,k = (int(x) for x in input().split())\ntown = [int(i) for i in input().split()]\nwarp = [1]\nchecklist = [0]+[-1]*n\ni = 0\nj = 0\nwhile j<=n:\n warp.append(town[i])\n if checklist[town[i]-1]!=-1:\n s = checklist[town[i]-1]\n warp = warp[s:j]\n break\n checklist[town[i]-1] = j\n i = town[i]-1\n j += 1\n\n\nm = len(warp)\nprint(warp[(k-s)%m])\n', 'n,k = (int(x) for x in input().split())\ntown = [int(i) for i in input().split()]\nwarp = [1]\nchecklist = [0]+[-1]*n\ni = 0\nj = 0\ns = 0\nwhile j<=n:\n warp.append(town[i])\n if checklist[town[i]-1]!=-1:\n s = checklist[town[i]-1]\n warp = warp[s:j]\n break\n checklist[town[i]-1] = j\n i = town[i]-1\n j += 1\n\nm = len(warp)\nprint(warp[(k-s)%m])\n', 'n,k = (int(x) for x in input().split())\ntown = [int(i) for i in input().split()]\nwarp = [1]\nchecklist = [0]+[-1]*n\ni = 0\nj = 1\ns = 0\nwhile j<=n:\n warp.append(town[i])\n if checklist[town[i]-1]!=-1:\n s = checklist[town[i]-1]\n loop = warp[s:j]\n break\n checklist[town[i]-1] = j\n i = town[i]-1\n j += 1\nif k>=s:\n m = len(loop)\n print(loop[(k-s)%m])\nelse:\n print(warp[k])']
['Runtime Error', 'Runtime Error', 'Runtime Error', 'Accepted']
['s209728179', 's288103790', 's963708298', 's643635833']
[32248.0, 32328.0, 32332.0, 32272.0]
[225.0, 185.0, 184.0, 208.0]
[475, 365, 370, 410]
p02684
u970197315
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 = 60\ndp = [[-1]*N for _ in range(d+1)]\nfor i in range(n):\n dp[0][i] = a[i]-1\n\nfor i in range(d):\n for j in range(n):\n dp[i+1] = dp[i][dp[i][j]]\n\nans = 0\n\nfor i in range(d):\n if K & (1<<i):\n ans = dp[i][ans]\n\nprint(ans)\n\n\n', 'n,k=map(int,input().split())\na=list(map(int,input().split()))\nvisited=[]\no=(n+1)*[-1]\ncycle,l=1,0\nv=1\nwhile o[v]==-1:\n o[v]=len(visited)\n visited.append(v)\n v=a[v-1]\ncycle=len(visited)-o[v]\nl=o[v]\nif k<l:\n print(visited[k])\nelse:\n k-=l\n k%=cycle\n print(visited[l+k])']
['Runtime Error', 'Accepted']
['s524672081', 's186689057']
[114276.0, 32360.0]
[187.0, 152.0]
[313, 273]
p02684
u970267139
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\nt = [1]\nfor i in range(n):\n t.append(a[t[-1] - 1])\n\nt_sort = sorted(t)\nfor i in range(n):\n if t_sort[i] == t_sort[i + 1]:\n b = t_sort[i]\n break\n\ncount = 0\nfor i in range(n):\n if t[i] == b:\n if count == 0:\n count += 1\n s_first = i\n else:\n s_second = i\n break\ns = s_second - s_first\n\nfor i in range(n - s + 1):\n if t[i] == t[i + s]:\n break\n\nif k < i:\n print(t[k])\nelse:\n print(t[(k - 1) % s + i])', 'n, k = map(int, input().split())\na = list(map(int, input().split()))\n\nt = [1]\nfor i in range(n):\n t.append(a[t[-1] - 1])\n\nt_sort = sorted(t)\nfor i in range(n):\n if t_sort[i] == t_sort[i + 1]:\n b = t_sort[i]\n break\n\ncount = 0\nfor i in range(n):\n if t[i] == b:\n if count == 0:\n count += 1\n s_first = i\n else:\n s_second = i\n break\ns = s_second - s_first\n\nfor i in range(n - s + 1):\n if t[i] == t[i + s]:\n break\n\nif k < i:\n print(t[k])\nelse:\n print(t[(k - 1) % s + i - 1])', 'n, k = map(int, input().split())\na = list(map(int, input().split()))\n\nt = [1]\nfor i in range(n):\n t.append(a[t[-1] - 1])\nprint(t)\n\nfor i in range(n):\n for j in range(i + 1, n):\n if t[i] == t[j]:\n s = j - i\n break\n else:\n continue\n break\n\nif k < i:\n print(t[k])\nelse:\n print(t[(k - i) % s + i])', 'n, k = map(int, input().split())\na_list = list(map(int, input().split()))\nvisit = [1]\n\nfor i in range(n):\n visit.append(a_list[visit[-1] - 1])\n\nfor i in range(n - 1):\n if visit[-1] == visit[n - i - 1]:\n break\ns = i + 1\n\nfor i in range(n - s):\n if visit[i] == visit[i + s]:\n break\n\nif k < i:\n print(visit[k])\nelse:\n print(visit[i + (k - i) % s])']
['Wrong Answer', 'Wrong Answer', 'Wrong Answer', 'Accepted']
['s327558490', 's424048331', 's769963558', 's644358635']
[32176.0, 32312.0, 32372.0, 32160.0]
[274.0, 238.0, 2206.0, 156.0]
[562, 566, 348, 374]
p02684
u970598682
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\narray=map(int,input().split())\n\nA=list(array)\nA.insert(0,0)\n\nvisit=0\nroot=[]\nvisited=1\nonce=0\nwhile True:\n if visit & (0b1<<visited):\n break\n root.append(visited)\n visit|=0b1<<visited\n once+=1\n visited=A[visited]\n\nroop=root[root.index(visited):]\nprint(roop)\nif K<=once:\n print(root[K])\nelse:\n order=K-once\n print(roop[order%len(roop)])', 'N,K=map(int,input().split())\n\narray=map(int,input().split())\n\nA=list(array)\nA.insert(0,0)\n\nvisit=0\nroot=[]\nvisited=1\nonce=0\nwhile True:\n if visit & (0b1<<visited):\n break\n root.append(visited)\n visit|=0b1<<visited\n once+=1\n visited=A[visited]\n\nroop=root[root.index(visited):]\nif K<once:\n print(root[K])\nelse:\n order=K-once\n print(roop[order%len(roop)])']
['Runtime Error', 'Accepted']
['s196385533', 's961182706']
[32384.0, 32328.0]
[1753.0, 1750.0]
[396, 383]
p02684
u971124021
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\nif k == 1:exit(print(A[0]))\nt = 0\ndp = [-1] * n\ndp[0] = 0\nfor i in range(1,k+1):\n t = A[t]-1\n if dp[t] != -1:break\n dp[t] = i\n \nl = i - dp[t]\nK = (k-dp[t])%l + dp[t]\nprint(dp)\nfor i in range(n):\n if dp[i] == K:exit(print(i+1))', 'n,k = list(map(int,input().split()))\nA = list(map(int,input().split()))\n\nif k == 1:exit(print(A[0]))\nt = 0\ndp = [-1] * n\ndp[0] = 0\nfor i in range(1,k+1):\n t = A[t]-1\n if dp[t] != -1:break\n dp[t] = i\nif k == 2:\n exit(print(t+1))\nl = i - dp[t]\nK = (k-dp[t])%l + dp[t]\nprint(dp)\nfor i in range(n):\n if dp[i] == K:exit(print(i+1))\n\n', 'n,k = list(map(int,input().split()))\nA = list(map(int,input().split()))\n\nif k == 1:exit(print(A[0]))\nt = 0\ndp = [-1] * n\ndp[0] = 0\nflg = 0\nfor i in range(1,k+1):\n t = A[t]-1\n if dp[t] != -1:\n flg = 1\n break\n dp[t] = i\nif flg == 0:\n exit(print(t+1))\nelif k == 2:\n exit(print(t+1))\n\nl = i - dp[t]\nK = (k-dp[t])%l + dp[t]\n\nfor i in range(n):\n if dp[i] == K:exit(print(i+1))\n\n\n']
['Runtime Error', 'Runtime Error', 'Accepted']
['s622899748', 's651062444', 's310891835']
[32372.0, 32320.0, 32352.0]
[164.0, 145.0, 137.0]
[304, 333, 385]
p02684
u975445930
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())\nl = list(map(int, input().split()))\n\nkeys = range(1,n+1)\nvalues = [-1]*n\ndic = dict(zip(keys,values))\ndic[1] = 0\nt = 1\ni = 1\n\ndef inverse_lookup(d, x):\n for k,v in d.items():\n if x == v:\n return k\n\nwhile i <= k:\n t = l[t-1]\n if dic[t] != -1:\n m = dic[t]\n d = i-m\n r = (k-i+1)%d\n ans = inverse_lookup(dic, (m+r-1))\n print(ans)\n break\n dic[t] = i\n i += 1\nif i == k+1:\n print(t)', 'n,k = map(int, input().split())\nl = list(map(int, input().split()))\n\nkeys = range(1,n+1)\nvalues = [-1]*n\ndic = dict(zip(keys,values))\ndic[1] = 0\nt = 1\ni = 1\n\ndef inverse_lookup(d, x):\n for k,v in d.items():\n if x == v:\n return k\n\nwhile i <= k:\n t = l[t-1]\n if dic[t] != -1:\n m = dic[t]\n d = i-m\n r = (k-i+1)%d\n if r == 0:\n r = d\n ans = inverse_lookup(dic, (m+r-1))\n print(ans)\n break\n dic[t] = i\n i += 1\nif i == k+1:\n print(t)']
['Wrong Answer', 'Accepted']
['s705837541', 's051821097']
[41088.0, 41000.0]
[199.0, 178.0]
[486, 523]
p02684
u982030663
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 = [i for i in map(int, input().split(" "))]\n\nhist = [0 for i in range(N)]\nroute = []\nloop = []\n\njump = 1\ncur = A[0] - 1\nroute.append(0)\nhist[0] = jump\nwhile hist[cur] == 0:\n jump = jump + 1\n route.append(cur)\n hist[cur] = jump\n cur = A[cur] - 1\n\nloop = route[cur:]\n\nif K < len(route):\n print(route[K] + 1)\nelse:\n K = K - len(route) - 1\n K = K % len(loop)\n print(loop[K] + 1)\n', 'N, K = map(int, input().split(" "))\nA = [i for i in map(int, input().split(" "))]\n\nhist = [-1 for i in range(N)]\nroute = []\nloop = []\n\njump = 0\ncur = A[0] - 1\nroute.append(0)\nhist[0] = jump\nwhile hist[cur] == -1:\n jump = jump + 1\n route.append(cur)\n hist[cur] = jump\n cur = A[cur] - 1\n\nloop = route[hist[cur]:]\n\nprint(cur, hist, route, loop)\n\nif K < len(route):\n print(route[K] + 1)\nelse:\n K = K - len(route)\n print(K)\n K = K % len(loop)\n print(K)\n print(loop[K] + 1)\n', 'N, K = map(int, input().split(" "))\nA = [i for i in map(int, input().split(" "))]\n\nhist = [-1 for i in range(N)]\nroute = []\nloop = []\n\njump = 0\ncur = A[0] - 1\nroute.append(0)\nhist[0] = jump\nwhile hist[cur] == -1:\n jump = jump + 1\n route.append(cur)\n hist[cur] = jump\n cur = A[cur] - 1\n\nloop = route[hist[cur]:]\n\n#print(cur, hist, route, loop)\n\nif K < len(route):\n print(route[K] + 1)\nelse:\n K = K - len(route)\n \n K = K % len(loop)\n \n print(loop[K] + 1)\n']
['Runtime Error', 'Wrong Answer', 'Accepted']
['s192138965', 's415989606', 's172888215']
[34720.0, 37020.0, 33960.0]
[182.0, 250.0, 175.0]
[437, 498, 501]
p02684
u982591663
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()))\nmemo = [-1] * N\nchecked_num = {}\n\nteleporter = A[0]\nmemo[0] = teleporter\nrepeat_point = -1\nflag = False\n\nfor i in range(1, N):\n memo[i] = A[teleporter-1]\n teleporter = A[teleporter-1]\n checked_num[memo[i]] = None\n if memo[i] in checked_num and flag == False:\n repeat_point = i\n flag = True\n\nstart_point = memo.index(memo[repeat_point])\nrepeat = repeat_point - start_point\n\n\nif K <= N:\n print(memo[K-1])\nelse:\n mod = K % repeat\n print(memo[mod-1])\n', 'N, K = map(int, input().split())\nA = list(map(int, input().split()))\nmemo = [-1] * (N+1)\nchecked_num = {1:None}\nmemo[0] = 1\n\nteleporter = A[0]\nmemo[1] = teleporter\nchecked_num[memo[1]] = None\nrepeat_point = -1\nflag = False\n\nfor i in range(2, N+1):\n \n memo[i] = A[teleporter-1]\n teleporter = A[teleporter-1]\n if memo[i] in checked_num and flag == False:\n repeat_point = i\n flag = True\n checked_num[memo[i]] = None\n\n# print(memo)\n\nstart_point = memo.index(memo[repeat_point])\nrepeat = repeat_point - start_point\n\n\nif K <= N:\n print(memo[K])\nelse:\n mod = (K-start_point) % repeat\n \n print(memo[mod+start_point])\n']
['Runtime Error', 'Accepted']
['s383020276', 's794190856']
[36560.0, 36852.0]
[181.0, 190.0]
[591, 758]
p02684
u987326700
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 = [1]\nfor i in range(n): \n l.append(a[l[i]])\n \np = l.index(l[-1])\nif k<=n:\n print(l[k])\n exit()\n \nx = (k-n+1)%(n-p)\nif x==0:\n print(l[n-1])\nelse:\n print(l[x+p-1])\n', 'n,k = map(int,input().split())\na = list(map(int,input().split()))\nl = [1,a[0]]\ntp = a[0]-1\nwhile True: \n if a[tp] not in l: \n l.append(a[tp])\n tp = a[tp] -1\n else:\n l.append(a[tp])\n break\n \nlast = l[-1]\nfirst = l.index(last)\nloop = len(l)-first\n\nif len(l)>k:\n print(l[k])\n exit()\nk -=first\nk %=loop\nif k==0:\n k+=loop\nprint(l[k+first])', 'n,k = map(int,input().split())\na = list(map(int,input().split()))\nl = [1,a[0]]\ntp = a[0]-1\nfor i in range(10**18) \n if a[tp] not in l: \n l.append(a[tp])\n tp = a[tp] -1\n else:\n l.append(a[tp])\n break\n \nlast = l[-1]\n\nloop = len(l[l.index(last):-1])\nfirst = l.index(last)\nif len(l)>k:\n print(l[k])\n exit()\nk -=first\nk %=loop\nif k==0:\n k+=loop\nprint(l[k+first])', 'n,k = map(int,input().split())\na = list(map(int,input().split()))\nl = [1,a[0]]\ntp = a[0]-1\nwhile True: \n if a[tp] not in l: \n l.append(a[tp])\n tp = a[tp] -1\n else:\n l.append(a[tp])\n break\n \nlast = l[-1]\nfirst = l.index(last)\nloop = len(l)-first\n\nif len(l)>k:\n print(l[k])\n exit()\nk -=first\nk %=loop\nif k==0:\n k+=loop\nprint(l[k+first])\nprint(l.index(last))', 'n,k = map(int,input().split())\na = list(map(int,input().split()))\nl = [1]\nfor i in range(n): \n l.append(a[l[i]])\n \np = l.index(l[-1])\nif k<=n:\n print(l[k])\n exit()\n \nx = (k-n+1)%(n-p)\nif x==0:\n print(l[n-1])\nelse:\n print(l[x+p-1])\n', 'n,k = map(int,input().split())\na = list(map(int,input().split()))\nl = [1]\nfor i in range(n): \n l.append(a[l[i]-1])\n \np = l.index(l[-1])\nif k<=n:\n print(l[k])\n exit()\n \nx = (k-n+1)%(n-p)\nif x==0:\n print(l[n-1])\nelse:\n print(l[x+p-1])\n']
['Runtime Error', 'Runtime Error', 'Runtime Error', 'Runtime Error', 'Runtime Error', 'Accepted']
['s212194425', 's251096519', 's390060930', 's405301813', 's579001902', 's961453210']
[32168.0, 32372.0, 8920.0, 32264.0, 32384.0, 32384.0]
[95.0, 2206.0, 24.0, 2207.0, 94.0, 124.0]
[243, 355, 377, 376, 243, 245]
p02684
u991567869
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 = sys.stdin.readline\n\ndef main():\n n, k = map(int, input().split())\n a = list(map(int, input().split()))\n l = [-1]*200010\n now = 0\n l[0] = 0\n ans = -100\n\n for i in range(n):\n k -= 1\n now = a[now] - 1 \n l[now] += 1\n if l[now] == 2: \n loop = l.count(1) + 1\n k %= loop\n if k == 0:\n ans = now + 1\n break\n\n print(max(ans, -1))\n\nmain()', 'import sys\ninput = sys.stdin.readline\n\nn, k = map(int, input().split())\na = list(map(int, input().split()))\nl = 1\nm = []\n\nfor i in range(10000):\n print(i)\n if a[l - 1] in m: \n for j in range(len(m)):\n if m[j] == a[l - 1]:\n cnt = j \n loop = len(m) - cnt \n break\n else:\n m.append(a[l - 1])\n l = a[l - 1]\n continue\n break\n\n\nif (k - cnt)%loop == 0:\n l = loop + cnt - 1\nelse:\n l = (k - cnt)%loop - 1 + cnt\n\nprint(m[l])', 'n, k = map(int, input().split())\na = list(map(int, input().split()))\nl = 1\nm = []\n\nfor i in range(10000):\n if a[l - 1] in m: \n for j in range(len(m)):\n if m[j] == a[l - 1]:\n cnt = j \n loop = len(m) - cnt \n break\n else:\n m.append(a[l - 1])\n l = a[l - 1]\n continue\n break\n\nl = 0 \n\nl = m[(k - cnt)%(loop)]\n\nprint(l)', 'n, k = map(int, input().split())\na = list(map(int, input().split()))\n\nfor i in range(n):\n a[i] -= 1\n\nl = [-1]*n\nl[0] = 0\norder = 0\nnow = 0\n\nwhile True:\n k -= 1\n now = a[now]\n order += 1\n if k == 0:\n break\n if l[now] != -1:\n cycle = order - l[now]\n k %= cycle\n if l[now] == -1:\n l[now] = order\n\nprint(now + 1)']
['Wrong Answer', 'Runtime Error', 'Runtime Error', 'Accepted']
['s627586984', 's919505336', 's976927305', 's092734454']
[31344.0, 31268.0, 32380.0, 32328.0]
[2206.0, 987.0, 909.0, 255.0]
[491, 602, 514, 357]
p02684
u991619971
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=int(input())\nN,K = map(int,input().split())\n#N=int(input())\nA = list(map(int,input().split()))\n#S=str(input())\n\nnow=1\nflag=[0]+[-1]*(N-1)\n\nfor i in range(N):\n now = A[now-1]\n if flag[now-1]==-1:\n flag[now-1]+=i+2\n else:\n loop = i+1 - flag[now-1]\n zan=(K-i-1) - (K-i-1)//loop * loop\n ans = flag.index(flag[now-1]+zan)+1\n exit()\nprint(now)\n', '#N=int(input())\nN,K = map(int,input().split())\n#N=int(input())\nA = list(map(int,input().split()))\n#S=str(input())\n\nnow=1\nflag=[0]+[-1]*(N-1)\n\nfor i in range(N-1):\n now = A[now-1]\n if flag[now-1]==-1:\n flag[now-1]+=i+2\n else:\n loop = i+1 - flag[now-1]\n zan=(K-i-1) - (K-i-1)//loop * loop\n ans = flag.index(flag[now-1]+zan)+1\n print(ans,now,flag,i)\n exit()\n\nprint(A[now-1])\n', '#N=int(input())\nN,K = map(int,input().split())\n#N=int(input())\nA = list(map(int,input().split()))\n#S=str(input())\n\nnow=1\nflag=[0]+[-1]*(N-1)\n\nfor i in range(K):\n now = A[now-1]\n if flag[now-1]==-1:\n flag[now-1]+=i+2\n else:\n loop = i+1 - flag[now-1]\n zan=(K-i-1) - (K-i-1)//loop * loop\n ans = flag.index(flag[now-1]+zan)+1\n print(ans)\n exit()\n\nprint(now)\n']
['Wrong Answer', 'Wrong Answer', 'Accepted']
['s035349115', 's907296751', 's171179555']
[32300.0, 32360.0, 32384.0]
[175.0, 189.0, 155.0]
[385, 423, 405]
p02684
u992471673
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=input().split()\n\nl=len(a)\n\nfor i in range(0,l):\n a[i]=int(a[i])\n\np=1\nh=[]\ngo=0\ni=0\nwhile(go==0):\n i+=1\n \n \n\n h.append(p)\n p=a[p-1]\n if i==k:\n break\n if p in h:\n l=len(h)\n for j in range(0,l):\n if h[j]==p:\n amari=(k-i-1)%(i-j)\n p=h[j+amari]\n go=1\n break\n\n\n\nprint(p)\n', 'n,k=map(int,input().split())\n\n\na = list(map(int, input().split()))\n\nar=[-1 for i in range(n+1)]\np=1\nh=[]\ngo=0\n\n\nfor i in range(1,k+1):\n \n \n \n \n\n ar[p]=i\n p=a[p-1]\n \n if ar[p]>0:\n j=ar[p]\n amari=(k-i-1)%(i-j)\n p=h[j+amari]\n go=1\n break\n\nif go==1:\n p=ar.index(j+amari)\n p=a[p-1]\n\n\nprint(p)\n', 'n,k=map(int,input().split())\n\n\na = list(map(int, input().split()))\n\nar=[-1 for i in range(n+1)]\np=1\n\ngo=0\n\n\nfor i in range(1,k+1):\n \n \n \n \n\n ar[p]=i-1\n p=a[p-1]\n \n if ar[p]>0:\n j=ar[p]\n amari=(k-i)%(i-j)\n \n go=1\n break\n\nif go==1:\n p=ar.index(j+amari)\n \n \n\n\nprint(p)\n']
['Wrong Answer', 'Runtime Error', 'Accepted']
['s732825777', 's954887965', 's065212481']
[25524.0, 32380.0, 32372.0]
[2207.0, 126.0, 143.0]
[413, 353, 334]
p02684
u993435350
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 = [[] for _ in range(N)]\nvisited = [0]\n\nfor i in range(N):\n town[i] = [i,A[i] - 1]\n\ndef teleport(i,con):\n goal = town[i][1]\n if goal in visited or con == K:\n return goal\n else:\n visited.append(goal)\n return teleport(goal,con + 1)\n\nl = teleport(visited[0],0)\n\nprint([i + 1for i in visited])\n\nif K <= len(visited):\n print(visited[K] + 1)\nelse:\n if l in visited:\n cs = visited.index(l)\n cycle = visited[cs:]\n K = K - len(visited) + len(cycle)\n print(cycle[K % len(cycle)] + 1)\n \n', 'N,K = map(int,input().split())\n\nA = list(map(int,input().split()))\ntown = [[] for _ in range(N)]\nvisited = [0]\nflag = [-1] * N\n\nfor i in range(N):\n town[i] = [i,A[i] - 1]\n\nf = 0\n\nwhile flag[f] == -1:\n t = town[f][1]\n flag[f] = t\n f = town[t][0]\n visited.append(t)\n \nl = visited[-1]\nlv = len(visited)\n\nif K <= lv:\n print(visited[K] + 1)\nelse:\n cs = visited.index(l)\n cycle = visited[cs:-1]\n lc = len(cycle)\n print(cycle[(K - lv + 1) % lc] + 1)\n##i']
['Runtime Error', 'Accepted']
['s365466668', 's251347718']
[47640.0, 50452.0]
[171.0, 285.0]
[573, 457]
p02684
u996665352
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 As = list(map(int,input().split()))\n route = [0]\n pre = 0\n i = 0\n r_append = route.append\n for _ in range(N):\n i = As[i]-1\n if i in route:\n pre = route.index(i)\n route = route[pre:]\n break\n r_append(i)\n len_route += 1\n # print(route)\n mod = len(route)\n print(route[(K-pre)%mod]+1)\n\nif __name__ == "__main__":\n main()', 'N,K = map(int,input().split())\nAs = list(map(int,input().split()))\ntowns = [0]*N\nroute = [0]\npre = 0\ni = 0\ntowns[0] = 1\nfor _ in range(K):\n i = As[i]-1\n if towns[i]>0:\n pre = route.index(i)\n route = route[pre:]\n break\n route.append(i)\n towns[i] = 1\nmod = len(route)\nprint(route[(K-pre)%mod]+1)\n']
['Runtime Error', 'Accepted']
['s925308734', 's929352307']
[32296.0, 32376.0]
[70.0, 124.0]
[455, 327]
p02684
u999482355
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 time import time\nN, K = list(map(int, input().split()))\nA = list(map(int, input().split()))\nL = [-1 for i in range(N)]\n\nvisited = []\nnow = 0\nloopstarts = None\nlooplength = None\nt0 = time()\n\nfor i in range(N):\n if L[now] != -1:\n break\n else:\n L[now] = i\n visited.append(now)\n now = A[now]-1\n\nloopstarts = visited.index(now)\nlooplength = len(visited)-loopstarts\n\nif K < loopstarts:\n print(visited[K]+1)\nelse:\n print(visited[loopstarts+(K-loopstarts)%looplength]+1)\nprint(time()-t0)\n', 'from time import time\nN, K = list(map(int, input().split()))\nA = list(map(int, input().split()))\nL = [-1 for i in range(N)]\n\nvisited = []\nnow = 0\nloopstarts = None\nlooplength = None\nt0 = time()\n\nfor i in range(N):\n if L[now] != -1:\n break\n else:\n L[now] = i\n visited.append(now)\n now = A[now]-1\n\nloopstarts = visited.index(now)\nlooplength = len(visited)-loopstarts\n\nif K < loopstarts:\n print(visited[K]+1)\nelse:\n print(visited[loopstarts+(K-loopstarts)%looplength]+1)\n# print(time()-t0)\n']
['Wrong Answer', 'Accepted']
['s014869576', 's325181203']
[33472.0, 33584.0]
[189.0, 192.0]
[525, 527]
p02685
u030726788
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['from math import factorial\n\n\n# return(factorial(n) // (factorial(n-r) * factorial(r)))\n\ndef comb(n,k,p):\n if n<0 or k<0 or n<k: return 0\n if n==0 or k==0: return 1\n a=factorial(n) %p\n b=factorial(k) %p\n c=factorial(n-k) %p\n return (a*power_func(b,p-2,p)*power_func(c,p-2,p))%p\ndef power_func(a,b,p):\n if b==0: return 1\n if b%2==0:\n d=power_func(a,b//2,p)\n return d*d %p\n if b%2==1:\n return (a*power_func(a,b-1,p ))%p\n\n\ndef cmb(n, r, mod):\n if ( r<0 or r>n ):\n return 0\n r = min(r, n-r)\n return g1[n] * g2[r] * g2[n-r] % mod\n\nmod = 998244353 \nN = 10**6\ng1 = [1, 1] \ng2 = [1, 1] \ninverse = [0, 1] 計算用テーブル\n\nfor i in range( 2, N + 1 ):\n g1.append( ( g1[-1] * i ) % mod )\n inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )\n g2.append( (g2[-1] * inverse[-1]) % mod )\n\nn,m,k = map(int,input().split())\n\ns = 0\n\nfor x in range(k+1):\n s += m * (power_func(m-1,n-1-x,998244353)% 998244353) * (cmb(n-1,x,998244353) % 998244353)\n s %= 998244353\nprint(s)', '\ndef power_func(a,b,p):\n if b==0: return 1\n if b%2==0:\n d=power_func(a,b//2,p)\n return d*d %p\n if b%2==1:\n return (a*power_func(a,b-1,p ))%p\n\n\ndef cmb(n, r, mod):\n if ( r<0 or r>n ):\n return 0\n r = min(r, n-r)\n return g1[n] * g2[r] * g2[n-r] % mod\n\nmod = 998244353\ng1 = [1, 1]\ng2 = [1, 1]\ninverse = [0, 1]\n\nn,m,k = map(int,input().split())\n\nfor i in range( 2, n + 1 ):\n g1.append( ( g1[-1] * i ) % mod )\n inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )\n g2.append( (g2[-1] * inverse[-1]) % mod )\n\ns = 0\n\nfor x in range(k+1):\n s += (m * power_func(m-1,n-1-x,998244353) * cmb(n-1,x,998244353)) % 998244353\n s %= 998244353\nprint(s)']
['Time Limit Exceeded', 'Accepted']
['s966620673', 's252536579']
[127632.0, 33236.0]
[2211.0, 1581.0]
[1131, 698]
p02685
u036104576
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['import sys\nimport itertools\nimport numpy as np\n\nMOD = 998244353\nMAX = 10 ** 5 * 2 + 5\n\nfac = [0 for i in range(MAX)]\nfinv = [0 for i in range(MAX)]\ninv = [0 for i in range(MAX)]\n\ndef comInit(mod):\n fac[0], fac[1] = 1, 1\n finv[0], finv[1] = 1, 1\n inv[1] = 1\n for i in range(2, MAX):\n fac[i] = fac[i - 1] * i % mod\n inv[i] = mod - inv[mod % i] * (mod // i) % mod\n finv[i] = finv[i - 1] * inv[i] % mod\n\ndef com(n, r, mod):\n if n < r:\n return 0\n if n < 0 or r < 0:\n return 0\n return fac[n] * (finv[r] * finv[n - r] % mod) % mod\n\ncomInit(MOD)\n\nread = sys.stdin.buffer.read\nreadline = sys.stdin.buffer.readline\nreadlines = sys.stdin.buffer.readlines\n \nn, m, k = map(int, readline().split())\n\nans = 0\nfor x in range(k, -1, -1):\n ans += now * com(n - 1, x, MOD) % MOD\n # ans += tmp\nprint(ans % MOD)', 'import sys\nimport itertools\nimport numpy as np\n\nMOD = 998244353\nMAX = 10 ** 5 * 2 + 5\n\nfac = [0 for i in range(MAX)]\nfinv = [0 for i in range(MAX)]\ninv = [0 for i in range(MAX)]\n\ndef comInit(mod):\n fac[0], fac[1] = 1, 1\n finv[0], finv[1] = 1, 1\n inv[1] = 1\n for i in range(2, MAX):\n fac[i] = fac[i - 1] * i % mod\n inv[i] = mod - inv[mod % i] * (mod // i) % mod\n finv[i] = finv[i - 1] * inv[i] % mod\n\ndef com(n, r, mod):\n if n < r:\n return 0\n if n < 0 or r < 0:\n return 0\n return fac[n] * (finv[r] * finv[n - r] % mod) % mod\n\ncomInit(MOD)\n\nread = sys.stdin.buffer.read\nreadline = sys.stdin.buffer.readline\nreadlines = sys.stdin.buffer.readlines\n \nn, m, k = map(int, readline().split())\n\nans = 0\nfor x in range(k, -1, -1):\n # now *= (m - 1) % MOD\n ans += com(n - 1, x, MOD) % MOD\n # ans += tmp\nprint(ans % MOD)', 'import sys\nimport itertools\nimport numpy as np\n\nMOD = 998244353\nMAX = 10 ** 5 * 2 + 5\n\nfac = [0 for i in range(MAX)]\nfinv = [0 for i in range(MAX)]\ninv = [0 for i in range(MAX)]\n\ndef comInit(mod):\n fac[0], fac[1] = 1, 1\n finv[0], finv[1] = 1, 1\n inv[1] = 1\n for i in range(2, MAX):\n fac[i] = fac[i - 1] * i % mod\n inv[i] = mod - inv[mod % i] * (mod // i) % mod\n finv[i] = finv[i - 1] * inv[i] % mod\n\ndef com(n, r, mod):\n if n < r:\n return 0\n if n < 0 or r < 0:\n return 0\n return fac[n] * (finv[r] * finv[n - r] % mod) % mod\n\ncomInit(MOD)\n\nread = sys.stdin.buffer.read\nreadline = sys.stdin.buffer.readline\nreadlines = sys.stdin.buffer.readlines\n \nn, m, k = map(int, readline().split())\n\nans = 0\ncol = m\nfor x in range(n - 1, -1, -1):\n now = col\n now *= com(n - 1, x, MOD) % MOD\n if x <= k:\n ans += now\n col = (col * (m - 1)) % MOD\n\nprint(ans % MOD)']
['Runtime Error', 'Wrong Answer', 'Accepted']
['s107569072', 's749940532', 's464056409']
[50956.0, 50944.0, 50888.0]
[305.0, 400.0, 463.0]
[852, 873, 922]
p02685
u078181689
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['N,M,K = list(map(int,input().split()))\n\nMOD = 998244353\nF = 10**5\ng1 = [1, 1] \ng2 = [1, 1] \ninverse = [0, 1] 計算用テーブル\n\nfor i in range( 2, F + 1 ):\n g1.append( ( g1[-1] * i ) % MOD )\n inverse.append( ( -inverse[MOD % i] * (MOD//i) ) % MOD )\n g2.append( (g2[-1] * inverse[-1]) % MOD )\n\ndef cmb(n, r, MOD):\n if ( r<0 or r>n ):\n return 0\n r = min(r, n-r)\n return g1[n] * g2[r] * g2[n-r] % MOD\n\nA = [M*pow(M-1,N-1-i,MOD) % MOD for i in range(K+1)]\nA = [A[i] * cmb(N-1,i,MOD) % MOD for i in range(K+1)]\nprint(sum(A) % MOD)', 'N,M,K = list(map(int,input().split()))\nMOD = 998244353\n\nfac = [1] * (N+1)\ninv = [1] * (N+1)\nifac = [1] * (N+1)\nfor i in range(2,N+1):\n fac[i]=(fac[i-1]*i)%MOD\n inv[i]= -(inv[MOD%i]*(MOD//i))%MOD\n ifac[i]=(ifac[i-1]*inv[i])%MOD\n\ncmbarr = [1] * N\nfor r in range(N-1):\n cmbarr[r+1] = cmbarr[r] * (N - 1 - r) * inv[r + 1] % MOD\n\nA = [M*pow(M-1,N-1-i,MOD)*cmbarr[i] % MOD for i in range(K+1)]\nprint(sum(A) % MOD)']
['Runtime Error', 'Accepted']
['s601494973', 's148015327']
[28908.0, 48568.0]
[546.0, 734.0]
[609, 419]
p02685
u089142196
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['def combi(y,x,p=998244353):\n s=fac[y]\n t=fac[x]\n u=fac[y-x]\n ans= (s* pow(t,p-2,p)*pow(u,p-2,p))%p\n return ans\n\n###############################\n\nN,M,K=map(int,input().split())\np=998244353\nfac=[1,1]\nfor i in range(2,N+1):\n fac.append( (fac[-1]*i)%p )\nR = M*(M-1)**(N-K-2)\n\nans=0\nfor i in range(K,-1,-1):\n R = (R*(M-1))%p\n L = combi(N,i)\n ans = (ans + (R*L)%p)%p\nprint(ans)', 'N, M, K = list(map(int, input().split()))\np = 998244353\n\ncomb = 1\nres = 0\nfor n in range(K+1):\n res = (res + comb * M * pow(M-1, N-1-n, p)) % p\n comb = (comb * (N -1 -n) * pow(n + 1, p - 2, p)) % p\n\nprint(res)']
['Runtime Error', 'Accepted']
['s859535889', 's431642632']
[18600.0, 9212.0]
[1746.0, 1270.0]
[380, 215]
p02685
u184817817
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['#E\nn,m,k = list(map(int,input().split()))\nfac = [1,1]\nfinv = [1,1]\ninv = [1,1]\nMOD = 998244353\nfor i in range(2,n):\n fac[i].append(fac[i-1]*i%MOD)\n inv[i].append(MOD-inv[MOD%i]*(MOD//i)%MOD)\n finv[i].append(finv[i-1]*inv[i]%MOD)\ndef comb(n,k):\n return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD\nans = 0\nfor i in range(k+1):\n# print(comb(n-1,i),m*(m-1)**(n-i-1))\n ans += (comb(n-1,i)*(m*(m-1)**(n-i-1)%MOD))%MOD\n ans %= MOD\nprint(ans)', '#E\nn,m,k = list(map(int,input().split()))\nfac = [1]*n\nfinv = [1]*n\ninv = [1]*n\nMOD = 998244353\nfor i in range(2,n):\n fac[i] = fac[i-1]*i%MOD\n inv[i] = MOD-inv[MOD%i]*(MOD//i)%MOD\n finv[i] = finv[i-1]*inv[i]%MOD\ndef comb(n,k):\n return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD\n\nans = 0\nfor i in range(k+1):\n# print(comb(n-1,i),m*(m-1)**(n-i-1))\n ans += comb(n-1,i)*m*pow(m-1,n-i-1,MOD)%MOD\n ans %= MOD\nprint(ans)']
['Runtime Error', 'Accepted']
['s599419892', 's635252262']
[9224.0, 32616.0]
[22.0, 764.0]
[447, 426]
p02685
u201387466
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['import sys\ninput=sys.stdin.readline\nsys.setrecursionlimit(10 ** 6)\nfrom collections import defaultdict\nimport fractions\nimport math\nfrom collections import deque\nfrom bisect import bisect_left\nfrom bisect import insort_left\nimport itertools\nfrom heapq import heapify\nfrom heapq import heappop\nfrom heapq import heappush\nimport heapq\nimport numpy as np\nINF = float("inf")\n\n\n#N = int(input())\n#A = list(map(int,input().split()))\n#S = list(input())\n#S.remove("\\n")\n#N,M = map(int,input().split())\n#S,T = map(str,input().split())\n#A = [int(input()) for _ in range(N)]\n#S = [input() for _ in range(N)]\n#A = [list(map(int,input().split())) for _ in range(N)]\nMOD = 998244353\nN,M,K = map(int,input().split())\ndp = [[0]*(K+1) for _ in range(N+1)]\nfor i in range(1,K+2):\n dp[i][i-1] = M\nfor i in range(1,N+1):\n dp[i][0] = M * pow(M-1,i-1,MOD)\nfor i in range(3,N+1):\n MIN = min(K,i-1)\n for j in range(1,K):\n dp[i][j] = dp[i-1][j-1] + dp[i-1][j]*(M-1)\n dp[i][j] = dp[i][j] % MOD\nans = 0\nfor i in range(K+1):\n ans += dp[N][i]\n ans = ans % MOD\nprint(ans)', 'import sys\ninput=sys.stdin.readline\nsys.setrecursionlimit(10 ** 6)\nfrom collections import defaultdict\nfrom collections import Counter\nimport fractions\nimport math\nfrom collections import deque\nfrom bisect import bisect_left\nfrom bisect import insort_left\nimport itertools\nfrom heapq import heapify\nfrom heapq import heappop\nfrom heapq import heappush\nimport heapq\nimport numpy as np\nINF = float("inf")\n\n\n#N = int(input())\n#A = list(map(int,input().split()))\n#S = list(input())\n#S.remove("\\n")\n#N,M = map(int,input().split())\n#S,T = map(str,input().split())\n#A = [int(input()) for _ in range(N)]\n#S = [input() for _ in range(N)]\n#A = [list(map(int,input().split())) for _ in range(N)]\nMOD = 998244353\nfac = [1, 1]\ninv = [0, 1]\nfinv = [1, 1]\nN,M,K = map(int,input().split())\nfor i in range(2, N+1):\n fac.append(fac[-1] * i % MOD)\n inv.append(MOD - inv[MOD%i] * (MOD//i) % MOD)\n finv.append(finv[-1] * inv[-1] % MOD) \ndef comb_mod(n, r, m):\n if (n<0 or r<0 or n<r): return 0\n r = min(r, n-r)\n return fac[n] * finv[n-r] * finv[r] % m\ns = 0\nfor i in range(K+1):\n s += M*pow(M-1,N-1-i,MOD)*comb_mod(N-1,i,MOD)\n s %= MOD\nprint(s%MOD)']
['Wrong Answer', 'Accepted']
['s111118630', 's765299579']
[2541204.0, 50552.0]
[2275.0, 849.0]
[1116, 1194]
p02685
u221061152
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['n,m,k=map(int,input().split())\nmod = 998244353\nnCi = [1]\nfor i in range(n):\n next = nCi[i]*(n-1-i)*pow(i+1,mod-2,mod)\n next %= mod\n nCi.append(next)\nprint(nCi)\nans = 0\nfor i in range(k+1):\n ans += m*pow(m-1,n-i-1,mod)*nCi[i]\n ans %= mod\nprint(ans)', 'n,m,k=map(int,input().split())\nmod = 998244353\nnCi = [1]\nfor i in range(n):\n next = nCi[i]*(n-1-i)*pow(i+1,mod-2,mod)\n next %= mod\n nCi.append(next)\n#print(nCi)\nans = 0\nfor i in range(k+1):\n ans += m*pow(m-1,n-i-1,mod)*nCi[i]\n ans %= mod\nprint(ans)']
['Wrong Answer', 'Accepted']
['s540003301', 's301264133']
[21176.0, 16892.0]
[1337.0, 1313.0]
[287, 288]
p02685
u221998589
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['#import numpy as np\nimport math\n#from decimal import *\n#from numba import njit\n\ndef main():\n (N, M, K) = map(int, input().split())\n MOD = 998244353\n \n fact = [1]*(N+1)\n factinv = [1]*(N+1)\n for i in range(1,K+1):\n fact[i] = fact[i-1]*i % MOD\n factinv[i] = pow(fact[i], MOD-2, MOD)\n def comb(n, k):\n return fact[n] * factinv[k] * factinv[n-k] % MOD\n\n ans = 0\n for k in range(K+1):\n ans += (comb(N-1,k)*M*pow(M-1, N-k-1, MOD))%MOD\n\n print(ans%MOD)\n\nmain()\n', 'import numpy as np\nimport math\n#from decimal import *\n#from numba import njit\n\ndef main():\n (N, M, K) = map(int, input().split())\n\n c = [0]*(K+1)\n def comb(k):\n if k == 0:\n ans = 1\n else:\n ans = c[k-1]*(N-k+1)//k\n c[k] = ans\n return ans\n\n ans = 0\n for k in range(K+1):\n ans += (comb(k)*M*pow(M-1, N-k-1, 998244353))%998244353\n\n print(ans%998244353)\n\nmain()\n', '#import numpy as np\nimport math\n#from decimal import *\n#from numba import njit\n\ndef main():\n (N, M, K) = map(int, input().split())\n MOD = 998244353\n \n fact = [1]*(N+1)\n factinv = [1]*(N+1)\n for i in range(1,N+1):\n fact[i] = fact[i-1]*i % MOD\n factinv[i] = pow(fact[i], MOD-2, MOD)\n def comb(n, k):\n return fact[n] * factinv[k] * factinv[n-k] % MOD\n\n ans = 0\n for k in range(K+1):\n ans += (comb(N-1,k)*M*pow(M-1, N-k-1, MOD))%MOD\n\n print(ans%MOD)\n\nmain()\n']
['Wrong Answer', 'Wrong Answer', 'Accepted']
['s387703210', 's717130020', 's300775095']
[24644.0, 574952.0, 24604.0]
[1328.0, 2220.0, 1254.0]
[518, 438, 518]
p02685
u243714267
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['n, m, k = map(int, input().split())\nmod = 998244353\ndef powerDX(n, r, mod):\n if r == 0: return 1\n if r%2 == 0:\n return powerDX(n*n % mod, r//2, mod) % mod\n if r%2 == 1:\n return n * powerDX(n, r-1, mod) % mod\n\ndef combination(n, r, mod):\n n1, r = n+1, min(r, n-r)\n numer = denom = 1\n for i in range(1, r+1):\n numer = numer * (n1-i) % mod\n denom = denom * i % mod\n return numer * powerDX(denom, mod-2, mod) % mod\n\nans = 0\nfor i in range(1, k+1):\n ans += m*combination(n-k-1, k, mod)*powerDX(m-1, k, mod)\n ans %= mod\nprint(ans)\n ', 'n, m, k = map(int, input().split())\n\nfor i in range(k):\n x = 1', 'n, m, k = map(int, input().split())\nmod = 998244353\n\ndef powerDX(n, r, mod):\n if r == 0: return 1\n if r%2 == 0:\n return powerDX(n*n % mod, r//2, mod) % mod\n if r%2 == 1:\n return n * powerDX(n, r-1, mod) % mod\n\ndef cmb(n, r, mod):\n if ( r<0 or r>n ):\n return 0\n r = min(r, n-r)\n return g1[n] * g2[r] * g2[n-r] % mod\n \ng1 = [1, 1]\ng2 = [1, 1]\ninverse = [0, 1]\n \nfor i in range(2, n + 1 ):\n g1.append( ( g1[-1] * i ) % mod )\n inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )\n g2.append( (g2[-1] * inverse[-1]) % mod )\n\nans = 0\nfor i in range(0, k+1):\n ans += m*cmb(n-1, i, mod)*pow(m-1, n-i-1, mod)\n ans %= mod\nprint(ans)']
['Wrong Answer', 'Wrong Answer', 'Accepted']
['s733342843', 's822803593', 's380736357']
[9084.0, 9056.0, 33156.0]
[2206.0, 31.0, 766.0]
[564, 63, 662]
p02685
u266014018
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
["class Combination():\n def __init__(self, n, mod=10**9+7):\n self.mod = mod\n self.fac = [1]*(n+1)\n for i in range(1,n+1):\n self.fac[i] = self.fac[i-1] * i % self.mod\n self.invfac = [1]*(n+1)\n self.invfac[n] = pow(self.fac[n], self.mod - 2, self.mod)\n for i in range(n-1, 0, -1):\n self.invfac[i] = self.invfac[i+1] * (i+1) % self.mod\n \n def combination(self, n, r):\n ans = self.fac[n] * self.invfac[r] % self.mod * self.invfac[n-r] % self.mod\n if n >= r:\n return ans\n else:\n return 0\n \n def factorial(self, i):\n return self.fac[i]\n \n def invfactorial(self, i):\n return self.invfac[i]\n\n\ndef main():\n import sys\n def input(): return sys.stdin.readline().rstrip()\n n, m, k = map(int, input().split())\n mod = 998244353\n c = Combination(n,mod)\n ans = 0\n mex = [0]*(k+1)\n mex[0] = pow(m-1,n-k-1,mod)\n for i in range(1,k+1):\n mod[i] = mex[i-1]*(m-1)%mod\n mex = mex[::-1]\n for i in range(k+1):\n ans += c.combination(n-1, i)*mex[i]\n ans %= mod\n ans *= m\n ans %= mod\n print(ans)\n\n\nif __name__ == '__main__':\n main()", "class Combination():\n def __init__(self, n, mod=10**9+7):\n self.mod = mod\n self.fac = [1]*(n+1)\n for i in range(1,n+1):\n self.fac[i] = self.fac[i-1] * i % self.mod\n self.invfac = [1]*(n+1)\n self.invfac[n] = pow(self.fac[n], self.mod - 2, self.mod)\n for i in range(n-1, 0, -1):\n self.invfac[i] = self.invfac[i+1] * (i+1) % self.mod\n \n def combination(self, n, r):\n ans = self.fac[n] * self.invfac[r] % self.mod * self.invfac[n-r] % self.mod\n if n >= r:\n return ans\n else:\n return 0\n \n def factorial(self, i):\n return self.fac[i]\n \n def invfactorial(self, i):\n return self.invfac[i]\n\n\ndef main():\n import sys\n def input(): return sys.stdin.readline().rstrip()\n n, m, k = map(int, input().split())\n mod = 998244353\n c = Combination(n,mod)\n ans = 0\n mex = [0]*(k+1)\n mex[0] = pow(m-1,n-k-1,mod)\n for i in range(1,k+1):\n mex[i] = mex[i-1]*(m-1)%mod\n mex = mex[::-1]\n for i in range(k+1):\n ans += c.combination(n-1, i)*mex[i]\n ans %= mod\n ans *= m\n ans %= mod\n print(ans)\n\n\nif __name__ == '__main__':\n main()"]
['Runtime Error', 'Accepted']
['s710346061', 's084223889']
[26220.0, 34044.0]
[131.0, 284.0]
[1210, 1210]
p02685
u276204978
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['import math\n\nN, M, K = map(int, input().split())\n\ndef combinations(n, r):\n return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))\n\nans = 0\ncol = m\nfor k in range(N-1, -1, -1):\n now = col\n now *= combinations(N-1, k);\n if k <= K:\n ans += now\n col *= m-1\n\nprint(ans)', 'mod = 998244353\n\n\nclass Combination:\n def __init__(self, N):\n self.fac = [1] * (N + 1)\n for i in range(1, N + 1):\n self.fac[i] = (self.fac[i - 1] * i) % mod\n self.invmod = [1] * (N + 1)\n self.invmod[N] = pow(self.fac[N], mod - 2, mod)\n for i in range(N, 0, -1):\n self.invmod[i - 1] = (self.invmod[i] * i) % mod\n\n def calc(self, n, k): # nCk\n return self.fac[n] * self.invmod[k] % mod * self.invmod[n - k] % mod\n\n\nN, M, K = map(int, input().split())\nC = Combination(N)\n\nans = 0\nfor k in range(K + 1):\n now = C.calc(N - 1, k) * M * pow(M - 1, N - k - 1, mod) % mod\n ans = (ans + now) % mod\n\nprint(ans)']
['Runtime Error', 'Accepted']
['s699542804', 's950314224']
[9164.0, 24668.0]
[25.0, 669.0]
[303, 676]
p02685
u285891772
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
["import sys, re\nfrom collections import deque, defaultdict, Counter\nfrom math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd, log2\nfrom itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby\nfrom operator import itemgetter, mul\nfrom copy import deepcopy\nfrom string import ascii_lowercase, ascii_uppercase, digits\nfrom bisect import bisect, bisect_left\nfrom heapq import heappush, heappop\nfrom scipy.special import comb\nfrom functools import reduce\ndef input(): return sys.stdin.readline().strip()\ndef INT(): return int(input())\ndef MAP(): return map(int, input().split())\ndef LIST(): return list(map(int, input().split()))\ndef ZIP(n): return zip(*(MAP() for _ in range(n)))\nsys.setrecursionlimit(10 ** 9)\nINF = float('inf')\nmod = 998244353\n\nN, M, K = MAP()\n\nans = 0\nfor i in range(0, K+1):\n\t#c = factorial(N-1)//factorial(N-i-1)//factorial(i)\n\tc = comb(N-i-1, i, exact=True)\n\tc %= mod\n\tp = M * ((M-1)**(N-K-1))\n\tp %= mod\n\tcp = (c*p)%mod\n\tans += cp\n\nprint(ans%mod)\n\n\n", "import sys, re\nfrom collections import deque, defaultdict, Counter\nfrom math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd\nfrom itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby\nfrom operator import itemgetter, mul\nfrom copy import deepcopy\nfrom string import ascii_lowercase, ascii_uppercase, digits\nfrom bisect import bisect, bisect_left\nfrom heapq import heappush, heappop\nfrom functools import reduce\ndef input(): return sys.stdin.readline().strip()\ndef INT(): return int(input())\ndef MAP(): return map(int, input().split())\ndef LIST(): return list(map(int, input().split()))\ndef ZIP(n): return zip(*(MAP() for _ in range(n)))\nsys.setrecursionlimit(10 ** 9)\nINF = float('inf')\nmod = 998244353\n\nlim = 2*10**5\nfact = [1]*(lim+1)\nfor n in range(1, lim+1):\n\tfact[n] = n*fact[n-1]%mod\n\nfact_inv = [1]*(lim+1)\nfact_inv[lim] = pow(fact[lim], mod-2, mod)\nfor n in range(lim, 0, -1):\n\tfact_inv[n-1] = n*fact_inv[n]%mod\n\ndef C(n, r):\n\treturn (((fact[n]*fact_inv[r])%mod)*fact_inv[n-r])%mod\n\n\nN, M, K = MAP()\n\nans = 0\nfor i in range(K+1):\n\tans += M*pow(M-1, N-1-i, mod)%mod*C(N-1, i)\n\tans %= mod \n\nprint(ans)\n"]
['Wrong Answer', 'Accepted']
['s214202863', 's867046840']
[43544.0, 25436.0]
[2207.0, 670.0]
[1033, 1214]
p02685
u288430479
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['n,m,k = map(int,input().split())\nmod = 998244353\nans = 0\n\ndef comb(n,k,mod):\n nCk = 1\n\n for i in range(n-k+1, n+1):\n nCk *= i\n nCk %= mod\n \n for i in range(1,k+1):\n nCk *= pow(i,mod-2,mod)\n nCk %= mod\n return nCk\nfor i in range(k+1):\n a = pow(m-1, n-i-1, mod)\n b = comb(n-1,i,mod)\n print(a,b)\n ans += a*b\nans *= m\nprint(ans%mod)', 'n,m,k = map(int,input().split())\nmod = 998244353\nans = 0\n\ndef comb(n, r, mod):\n if (r < 0) or (n < r):\n return 0\n r = min(r, n - r)\n return fact[n] * factinv[r] * factinv[n-r] % mod\n \nfact = [1, 1]\nfactinv = [1, 1]\ninv = [0, 1]\n \nfor i in range(2, n+1):\n fact.append((fact[-1] * i) % mod)\n inv.append((-inv[mod % i] * (mod // i)) % mod)\n factinv.append((factinv[-1] * inv[-1]) % mod)\n\n\nfor i in range(k+1):\n a = pow(m-1, n-i-1, mod)\n b = comb(n-1,i,mod)\n ans += (a*b)%mod\nans *= m\nprint(ans%mod)']
['Wrong Answer', 'Accepted']
['s245395539', 's990225381']
[9224.0, 33168.0]
[2206.0, 748.0]
[380, 530]
p02685
u307622233
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['n, m, k = map(int, input().split())\nMOD = 998244353\n\nans = 0\nfor i in range(k + 1):\n ans += (ans + m * pow(m - 1, m - i - 1, MOD)) % MOD\n c = (c * (n - 1 - i) * pow(i + 1, MOD - 2, MOD)) % MOD\n\nprint(ans)\n', 'n, m, k = map(int, input().split())\nMOD = 998244353\n\nans = 0\nc = 1\nfor i in range(k + 1):\n ans = (ans + m * pow(m - 1, n - i - 1, MOD) * c) % MOD\n c = (c * (n - 1 - i) * pow(i + 1, MOD - 2, MOD)) % MOD\n \n\nprint(ans)']
['Runtime Error', 'Accepted']
['s010625163', 's366618014']
[9180.0, 9192.0]
[22.0, 1263.0]
[211, 252]
p02685
u324090406
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['from scipy.special import comb\n\nN, M, K = list(map(int, input().split()))\nmod = 998244353\n\nanswer = 0\nfor i in range(K+1):\n answer += (M * ((comb(N-1, i)) % mod) * (((M-1) ** (N-1-i)) % mod)) % mod\n answer = answer % mod\n\nprint(answer)', 'N, M, K = list(map(int, input().split()))\nmod = 998244353\n\nc = 1\nanswer = 0\n\nfor i in range(K+1):\n answer += (M * c * pow(M-1, N - i - 1, mod)) % mod\n answer %= mod\n c = (c * (N-i-1) * pow(i+1, mod-2, mod)) % mod\n \nprint(answer)']
['Wrong Answer', 'Accepted']
['s466720883', 's888809744']
[43928.0, 9100.0]
[2207.0, 1291.0]
[241, 241]
p02685
u408620326
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
["def main():\n import sys\n sys.setrecursionlimit(10**6)\n input = sys.stdin.readline\n N, M, K = [int(x) for x in input().strip().split()]\n MOD = 998244353\n if M == 1:\n if N == 1:\n print(1)\n exit()\n \n fact = [1] * (N+1)\n inv_fact = [1] * (N+1)\n for i in range(N):\n fact[i+1] = (i * fact[i]) % MOD\n \n inv_fact[-1] = pow(fact[-1], MOD-2, MOD)\n for i in range(N-1):\n inv_fact[N-i-1] = (inv_fact[N-i] * (N - i)) % MOD\n\n def nCr(n, r):\n if r == 0 or r == n:\n return 1\n r = min(r, n - r)\n return fact[n] * inv_fact[n-r] * inv_fact[r] % MOD\n\n ans = (M * pow(M-1, N-1, MOD)) % MOD\n for k in range(K):\n ans = (ans + (M * pow(M-1, N-1, MOD)) * nCr(N-1, k+1)) % MOD\n\n print(ans)\n\nif __name__ == '__main__':\n main()", "def main():\n import sys\n sys.setrecursionlimit(10**6)\n input = sys.stdin.readline\n N, M, K = [int(x) for x in input().strip().split()]\n MOD = 998244353\n if M == 1:\n if N == 1:\n print(1)\n exit()\n \n fact = [1] * (N+1)\n inv_fact = [1] * (N+1)\n for i in range(1, N+1):\n fact[i] = ((i) * fact[i-1]) % MOD\n \n inv_fact[-1] = pow(fact[-1], MOD-2, MOD)\n for i in range(N-1):\n inv_fact[N-i-1] = (inv_fact[N-i] * (N - i)) % MOD\n\n def nCr(n, r):\n if r == 0 or r == n:\n return 1\n r = min(r, n - r)\n return (fact[n] * inv_fact[n-r] * inv_fact[r]) % MOD\n\n ans = (M * pow(M-1, N-1, MOD)) % MOD\n for k in range(K):\n ans = (ans + (M * pow(M-1, N-1-(k+1), MOD)) * nCr(N-1, k+1)) % MOD\n\n print(ans)\n\nif __name__ == '__main__':\n main()"]
['Wrong Answer', 'Accepted']
['s033910013', 's936746568']
[12152.0, 24584.0]
[564.0, 632.0]
[835, 850]
p02685
u461454424
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['#atcoder template\ndef main():\n import sys\n imput = sys.stdin.readline\n \n #input\n N, M, K = map(int, input().split())\n\n #output\n mod = 998244353\n\n n_ = 4*10**5 + 5\n fun = [1]*(n_+1)\n for i in range(1,n_+1):\n fun[i] = fun[i-1]*i%mod\n rev = [1]*(n_+1)\n rev[n_] = pow(fun[n_],mod-2,mod)\n for i in range(n_-1,0,-1):\n rev[i] = rev[i+1]*(i+1)%mod\n def cmb(n,r):\n if n <= 0 or r < 0 or r > n: return 0\n return fun[n]*rev[r]%mod*rev[n-r]%mod\n\n answer = 0\n for i in range(K+1):\n answer += M*pow(M-1, N-(i+1), mod)*cmb(N-1, i) % mod\n\n answer %= mod\n\n if N == 1:\n answer = M\n else:\n continue\n\n print(answer)\n \n\nif __name__ == "__main__":\n main()', '#atcoder template\ndef main():\n import sys\n imput = sys.stdin.readline\n \n #input\n N, M, K = map(int, input().split())\n\n #output\n mod = 998244353\n\n n_ = 4*10**5 + 5\n fun = [1]*(n_+1)\n for i in range(1,n_+1):\n fun[i] = fun[i-1]*i%mod\n rev = [1]*(n_+1)\n rev[n_] = pow(fun[n_],mod-2,mod)\n for i in range(n_-1,0,-1):\n rev[i] = rev[i+1]*(i+1)%mod\n def cmb(n,r):\n if n <= 0 or r < 0 or r > n: return 0\n return fun[n]*rev[r]%mod*rev[n-r]%mod\n\n answer = 0\n for i in range(K+1):\n answer += M*pow(M-1, N-(i+1), mod)*cmb(N-1, i) % mod\n\n answer %= mod\n\n if N == 1:\n print(M)\n exit()\n\n print(answer)\n \n\nif __name__ == "__main__":\n main()']
['Runtime Error', 'Accepted']
['s346210957', 's129469763']
[9176.0, 40528.0]
[24.0, 709.0]
[767, 753]
p02685
u476674874
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['import math\nmod = 998244353\n\nimport numpy as np\ndef create_modC(n, mod):\n if n == 0:\n return (lambda r: 1)\n b = int(math.log(mod-2, 2)) + 1\n fac = np.zeros((b, n+1), dtype=np.int64)\n print(fac.shape)\n inv = np.ones(n+1, dtype=np.int64)\n fac[0, 0], fac[0, 1] = 1, 1\n for i in range(2, n+1):\n fac[0, i] = (fac[0, i-1] * i) % mod\n for i in range(1, b):\n fac[i, :] = (fac[i-1, :] ** 2) % mod\n\n indice = []\n N = mod - 2\n i = 0\n while N > 0:\n if N % 2 == 1:\n indice.append(i)\n N = N >> 1\n i += 1\n \n for i in indice:\n inv *= fac[i,:]\n inv %= mod\n \n fac = fac[0,:]\n\n def _modC(r):\n if r == 0 or r == n:\n return 1\n result = fac[n] * inv[n-r]\n result %= mod\n result *= inv[r] % mod\n result %= mod\n return result\n\n return _modC\n\n\ndef mod_factorial(mod):\n cache = [1,1,2]\n def _factorial(n):\n nonlocal cache\n if n == 0:\n return 1\n length = len(cache)\n v = cache[-1]\n while n >= length:\n v *= length \n v %= mod\n cache.append(v)\n length +=1\n return cache[n]\n return _factorial\n\ndef mod_pow(x, mod):\n xx = [1, x]\n def _pow(y, x=x):\n nonlocal xx\n if y == 0:\n return 1\n while len(xx) <= math.log(y, 2)+1:\n v = xx[-1] * xx[-1]\n v %= mod\n xx.append(v)\n i = 1\n result = 1\n while y > 0:\n if y % 2 == 1:\n result *= xx[i]\n result %= mod\n y = y >> 1\n i += 1\n return result\n return _pow\n\ndef test():\n N, M, K = 3,2,1\n answer = 6\n v = resolve(N, M, K)\n print(f"N={N} M={M} K={K} {v}={answer}")\n N, M, K = 100,100,0\n answer = 73074801\n v = resolve(N, M, K)\n print(f"N={N} M={M} K={K} {v}={answer}")\n N, M, K = 60522,114575,7559\n answer = 479519525\n v = resolve(N, M, K)\n print(f"N={N} M={M} K={K} {v}={answer}")\n\n N, M, K = 200000,190000,190000\n v = resolve(N, M, K)\n print(f"N={N} M={M} K={K} {v}")\n\n N, M, K = 1,1,0\n print(f"N={N} M={M} K={K}")\n v = resolve(N, M, K)\n print(f"N={N} M={M} K={K} {v}")\n\n N, M, K = 200000,200000,190000\n v = resolve(N, M, K)\n print(f"N={N} M={M} K={K} {v}")\n\n for i in range(100):\n N, M = np.random.randint(200000), np.random.randint(200000)\n K = np.random.randint(N-1)\n v = resolve(N, M, K)\n print(f"N={N} M={M} K={K} {v}")\n\n\n\n\ndef main():\n test()\n N, M, K = map(int, input().split())\n v = resolve(N, M, K)\n print(v)\n\n\ndef resolve(N, M, K):\n from datetime import datetime\n result = 0\n m1pow = mod_pow(M-1, mod)\n modC = create_modC(N-1, mod)\n \n for k in range(K+1):\n b = m1pow(N-1-k)\n c = modC(k)\n v = b \n v *= c\n v %= mod\n \n # print(f"b={b} c={c} k={k} {v}")\n result += v\n result %= mod\n \n result *= M\n result %= mod\n\n result = int(result)\n\n return result\n\n \nif __name__ == \'__main__\':\n main()', 'import math\nmod = 998244353\n\nimport numpy as np\ndef create_modC(n, mod):\n if n == 0:\n return (lambda r: 1)\n b = int(math.log(mod-2, 2)) + 1\n fac = np.zeros((b, n+1), dtype=np.int64)\n #print(fac.shape)\n inv = np.ones(n+1, dtype=np.int64)\n fac[0, 0], fac[0, 1] = 1, 1\n for i in range(2, n+1):\n fac[0, i] = (fac[0, i-1] * i) % mod\n for i in range(1, b):\n fac[i, :] = (fac[i-1, :] ** 2) % mod\n\n indice = []\n N = mod - 2\n i = 0\n while N > 0:\n if N % 2 == 1:\n indice.append(i)\n N = N >> 1\n i += 1\n \n for i in indice:\n inv *= fac[i,:]\n inv %= mod\n \n fac = fac[0,:]\n\n def _modC(r):\n if r == 0 or r == n:\n return 1\n result = fac[n] * inv[n-r]\n result %= mod\n result *= inv[r] % mod\n result %= mod\n return result\n\n return _modC\n\n\ndef mod_factorial(mod):\n cache = [1,1,2]\n def _factorial(n):\n nonlocal cache\n if n == 0:\n return 1\n length = len(cache)\n v = cache[-1]\n while n >= length:\n v *= length \n v %= mod\n cache.append(v)\n length +=1\n return cache[n]\n return _factorial\n\ndef mod_pow(x, mod):\n xx = [1, x]\n def _pow(y, x=x):\n nonlocal xx\n if y == 0:\n return 1\n while len(xx) <= math.log(y, 2)+1:\n v = xx[-1] * xx[-1]\n v %= mod\n xx.append(v)\n i = 1\n result = 1\n while y > 0:\n if y % 2 == 1:\n result *= xx[i]\n result %= mod\n y = y >> 1\n i += 1\n return result\n return _pow\n\ndef test():\n N, M, K = 3,2,1\n answer = 6\n v = resolve(N, M, K)\n print(f"N={N} M={M} K={K} {v}={answer}")\n N, M, K = 100,100,0\n answer = 73074801\n v = resolve(N, M, K)\n print(f"N={N} M={M} K={K} {v}={answer}")\n N, M, K = 60522,114575,7559\n answer = 479519525\n v = resolve(N, M, K)\n print(f"N={N} M={M} K={K} {v}={answer}")\n\n N, M, K = 200000,190000,190000\n v = resolve(N, M, K)\n print(f"N={N} M={M} K={K} {v}")\n\n N, M, K = 1,1,0\n print(f"N={N} M={M} K={K}")\n v = resolve(N, M, K)\n print(f"N={N} M={M} K={K} {v}")\n\n N, M, K = 200000,200000,190000\n v = resolve(N, M, K)\n print(f"N={N} M={M} K={K} {v}")\n\n for i in range(100):\n N, M = np.random.randint(200000), np.random.randint(200000)\n K = np.random.randint(N-1)\n v = resolve(N, M, K)\n print(f"N={N} M={M} K={K} {v}")\n\n\n\n\ndef main():\n #test()\n N, M, K = map(int, input().split())\n v = resolve(N, M, K)\n print(v)\n\n\ndef resolve(N, M, K):\n from datetime import datetime\n result = 0\n m1pow = mod_pow(M-1, mod)\n modC = create_modC(N-1, mod)\n \n for k in range(K+1):\n b = m1pow(N-1-k)\n c = modC(k)\n v = b \n v *= c\n v %= mod\n \n # print(f"b={b} c={c} k={k} {v}")\n result += v\n result %= mod\n \n result *= M\n result %= mod\n\n result = int(result)\n\n return result\n\n \nif __name__ == \'__main__\':\n main()']
['Time Limit Exceeded', 'Accepted']
['s060095185', 's473220464']
[78576.0, 78396.0]
[2208.0, 1450.0]
[3173, 3175]
p02685
u506689504
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['import itertools\nimport numpy as np\nfrom scipy.special import comb\n\nmod = 998244353\n\nN,M,K = map(int, input().split())\n\n# DP = [[0]*N for _ in range(N)]\n# DP[0][0] = M\n\n# DP = []\n\n\n# for n in range(1,N):\n# \tfor k in range(N):\n# \t\tif k==0:\n# \t\t\tDP[n][k] = DP[n-1][k]*(M-1) % mod\n# \t\telse:\n# \t\t\tDP[n][k] = (DP[n-1][k]*(M-1) + DP[n][k-1]) % mod\n\n# ans = sum(DP[N-1][:(K+1)]) % mod\n# print(ans)\n\nans = 0\nfor k in range(K+1):\n\tans += comb(N, k)*(M-1)**(N-k-1) % mod\n\nprint(int(ans*M) % mod)', 'import itertools\nimport numpy as np\nfrom scipy.special import comb\n\nmod = 998244353\n\nN,M,K = map(int, input().split())\n\n# DP = [[0]*N for _ in range(N)]\n# DP[0][0] = M\n\n# DP = []\n\n\n# for n in range(1,N):\n# \tfor k in range(N):\n# \t\tif k==0:\n# \t\t\tDP[n][k] = DP[n-1][k]*(M-1) % mod\n# \t\telse:\n# \t\t\tDP[n][k] = (DP[n-1][k]*(M-1) + DP[n][k-1]) % mod\n\n# ans = sum(DP[N-1][:(K+1)]) % mod\n# print(ans)\n\nans = 0\nfor k in range(1,K+1):\n\tans += comb(N, k-1)*(M-1)**(N-k) % mod\n\nprint(int(ans*M) % mod)', 'MOD = 998244353\nMAX = 510000\nfac = [1,1] # factorial\nfinv = [1,1] # inverse of factorial\ninv = [None, 1] # inverse\n\ndef comb_init():\n\tfor i in range(2, MAX):\n\t\tfac.append(fac[i-1] * i % MOD)\n\t\tinv.append(MOD - inv[MOD % i] * (MOD//i) % MOD)\n\t\tfinv.append(finv[i-1] * inv[i] % MOD)\n\ndef inverse_mod(a):\n\t""" Calculate inverse of the integer in a modulo MOD """\n\treturn pow(a, MOD-2, MOD)\n\ndef comb_mod(n, r):\n\t\n\tif n < r: return 0\n\tif n < 0 or r < 0: return 0\n\treturn fac[n]* (finv[r]*finv[n-r] % MOD) % MOD\n\ndef solve(n, m, k):\n\tans = 0\n\tfor k in range(K+1):\n\t\tans = (ans + m*(comb_mod(n-1,k)*pow(m-1, n-k-1, MOD) % MOD) % MOD) % MOD\n\treturn ans\n\ncomb_init()\nN, M, K = map(int, input().split())\nprint(solve(N, M, K))\n']
['Runtime Error', 'Runtime Error', 'Accepted']
['s573868538', 's675538271', 's663837991']
[43904.0, 43816.0, 69576.0]
[1507.0, 1491.0, 1045.0]
[485, 487, 750]
p02685
u521602455
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['N,M,K=map(int,input().split())\nP=998244353\nclass FactInv:\n def __init__(self,N,P):\n fact=[];ifact=[];fact=[1]*(N+1);ifact=[0]*(N+1)\n for i in range(1,N):\n fact[i+1]=(fact[i]*(i+1))%P\n ifact[-1]=pow(fact[-1],P-2,P)\n for i in range(N,0,-1):\n ifact[i-1]=(ifact[i]*i)%P\n self.fact=fact;self.ifact=ifact;self.P=P\n def comb(self,n,k):\n return (self.fact[n]*self.ifact[k]*self.ifact[n-k])%self.P\nFI=FactInv(N+10,P)\nans=0\nfor i in range(0,K+1):\n ans+=(M*FI.comb(N-1,i)*pow(M0,N-1-i,P))%P\n ans%=P\nprint(ans)', 'N,M,K=map(int,input().split())\nP=998244353\nclass FactInv:\n def __init__(self,N,P):\n fact=[];ifact=[];fact=[1]*(N+1);ifact=[0]*(N+1)\n for i in range(1,N):\n fact[i+1]=(fact[i]*(i+1))%P\n ifact[-1]=pow(fact[-1],P-2,P)\n for i in range(N,0,-1):\n ifact[i-1]=(ifact[i]*i)%P\n self.fact=fact;self.ifact=ifact;self.P=P\n def comb(self,n,k):\n return (self.fact[n]*self.ifact[k]*self.ifact[n-k])%self.P\nFI=FactInv(N+10,P)\nans=0\nfor k in range(0,K+1):\n ans+=(M*FI.comb(N-1,k)*pow(M-1,N-1-k,P))%P\n ans%=P\nprint(ans)']
['Runtime Error', 'Accepted']
['s645478103', 's212470863']
[24708.0, 24608.0]
[107.0, 650.0]
[574, 575]
p02685
u537142137
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['#factorial\nmod = 998244353 \nN = 1000000\nmodinv_t = [0, 1]\nmodfact_t = [1, 1]\nmodfactinv_t = [1, 1]\nfor i in range(2,N+1):\n modfact_t.append(modfact_t[-1] * i % MOD)\n modinv_t.append(modinv_t[MOD%i] * (MOD-(MOD//i)) % MOD)\n modfactinv_t.append(modfactinv_t[-1] * modinv_t[-1] % MOD)\n\ndef nPr(n, r):\n return modfact_t[n] * modfactinv_t[n-r] % MOD\n# combination\ndef nCr(n, r):\n return modfact_t[n] * modfactinv_t[n-r] * modfactinv_t[r] % MOD\n#\ndef nHr(n, r):\n return nCr(n+r-1,r)\n#\n\nN, M, K = map(int, input().split()) \n\nt = M*pow(M-1,N-1,mod)\nfor i in range(1,K+1):\n t += M*pow(M-1,N-1-i,mod)*nCr((N-1),i)\n t %= mod\n#\nprint(t)\n', '#factorial\nMOD = 998244353 \nN = 1000000\nmodinv_t = [0, 1]\nmodfact_t = [1, 1]\nmodfactinv_t = [1, 1]\nfor i in range(2,N+1):\n modfact_t.append(modfact_t[-1] * i % MOD)\n modinv_t.append(modinv_t[MOD%i] * (MOD-(MOD//i)) % MOD)\n modfactinv_t.append(modfactinv_t[-1] * modinv_t[-1] % MOD)\n\ndef nPr(n, r):\n return modfact_t[n] * modfactinv_t[n-r] % MOD\n# combination\ndef nCr(n, r):\n return modfact_t[n] * modfactinv_t[n-r] * modfactinv_t[r] % MOD\n#\ndef nHr(n, r):\n return nCr(n+r-1,r)\n#\n\nN, M, K = map(int, input().split()) \n\nt = M*pow(M-1,N-1,MOD) % MOD\nfor i in range(1,K+1):\n t += M*pow(M-1,N-1-i,MOD)*nCr((N-1),i)\n t %= MOD\n#\nprint(t)\n']
['Runtime Error', 'Accepted']
['s281566067', 's054380469']
[9176.0, 127680.0]
[26.0, 1648.0]
[649, 655]
p02685
u539692012
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['MOD = 998244353\nclass mint:\n def __init__(self, i):\n self.i = i\n def __add__(self, m):\n t = self.i + (m.i if isinstance(m, mint) else m)\n if t > MOD:\n t -= MOD\n return mint(t)\n def __radd__(self, m):\n t = self.i + (m.i if isinstance(m, mint) else m)\n if t > MOD:\n t -= MOD\n return mint(t)\n def __mul__(self, m):\n return mint(self.i * m.i % MOD)\n def __sub__(self, m):\n t = self.i - m.i\n if t < 0:\n t += MOD\n return mint(t)\n def __pow__(self, m):\n i = self.i\n res = 1\n while(m > 0):\n if m & 1:\n res = res * i % MOD \n i = i * i % MOD\n m >>= 1\n return mint(res)\n def __truediv__(self, m):\n return mint(self.i * (m ** (MOD - 2)).i % MOD)\n def __repr__(self):\n return repr(self.i)\n\nfact_range = 200005\nfacts = [1] * (fact_range + 1)\nfor i in range(0, fact_range):\n facts[i+1] = facts[i] * (i + 1) % MOD\n\nifacts = [1] * (fact_range + 1)\nifacts[fact_range] = pow(facts[fact_range], MOD - 2, MOD)\nfor i in range(fact_range, 0, -1):\n ifacts[i-1] = ifacts[i] * i % MOD\n\ndef comb(n, k):\n if k < 0 or n < k:\n return mint(0)\n else:\n return mint(facts[n] * ifacts[n-k] % MOD * ifacts[k] % MOD)\n\nn, m, k = map(int, input().split())\nprint(comb(n, k))\n\n\nl = [mint(m) * (mint(m-1) ** (n-1-i)) * comb(n-1, n-1-i) for i in range(n)]\nans = mint(0)\nfor i in range(k+1):\n ans = ans + l[i]\nprint(ans)\n', 'MOD = 998244353\nclass mint:\n def __init__(self, i):\n self.i = i\n def __add__(self, m):\n t = self.i + (m.i if isinstance(m, mint) else m)\n if t > MOD:\n t -= MOD\n return mint(t)\n def __radd__(self, m):\n t = self.i + (m.i if isinstance(m, mint) else m)\n if t > MOD:\n t -= MOD\n return mint(t)\n def __mul__(self, m):\n return mint(self.i * m.i % MOD)\n def __sub__(self, m):\n t = self.i - m.i\n if t < 0:\n t += MOD\n return mint(t)\n def __pow__(self, m):\n i = self.i\n res = 1\n while(m > 0):\n if m & 1:\n res = res * i % MOD \n i = i * i % MOD\n m >>= 1\n return mint(res)\n def __truediv__(self, m):\n return mint(self.i * (m ** (MOD - 2)).i % MOD)\n def __repr__(self):\n return repr(self.i)\n\nfact_range = 200005\nfacts = [1] * (fact_range + 1)\nfor i in range(0, fact_range):\n facts[i+1] = facts[i] * (i + 1) % MOD\n\nifacts = [1] * (fact_range + 1)\nifacts[fact_range] = pow(facts[fact_range], MOD - 2, MOD)\nfor i in range(fact_range, 0, -1):\n ifacts[i-1] = ifacts[i] * i % MOD\n\ndef comb(n, k):\n if k < 0 or n < k:\n return mint(0)\n else:\n return mint(facts[n] * ifacts[n-k] % MOD * ifacts[k] % MOD)\n\nn, m, k = map(int, input().split())\n\n\n\nl = [mint(m) * (mint(m-1) ** (n-1-i)) * comb(n-1, n-1-i) for i in range(n)]\nans = mint(0)\nfor i in range(k+1):\n ans = ans + l[i]\nprint(ans)\n']
['Wrong Answer', 'Accepted']
['s460514398', 's113261103']
[64284.0, 64428.0]
[1646.0, 1583.0]
[1530, 1513]
p02685
u578514593
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['mod = 998244353\n\nclass mod_combination:\n def __init__(self, N):\n self.fac = [1] * (N + 1)\n for i in range(1, N + 1):\n self.fac[i] = (self.fac[i - 1] * i) % mod\n self.invmod = [1] * (N + 1)\n self.invmod[N] = pow(self.fac[N], mod - 2, mod)\n for i in range(N, 0, -1):\n self.invmod[i - 1] = (self.invmod[i] * i) % mod\n \n def calc(self, n, k): # nCk\n return self.fac[n] * self.invmod[k] % mod * self.invmod[n - k] % mod\n\n\nn, m, k = map(int, input().split())\nC = mod_combination(n)\nans = 0\nfor x in range(k + 1):\n ans += C.calc(n - 1, n - x - 1) * pow(m - 1, n - x - 1, mod) % mod\n ans %= mod\nprint(ans)\n', 'mod = 998244353\n\nclass mod_combination:\n def __init__(self, N):\n self.fac = [1] * (N + 1)\n for i in range(1, N + 1):\n self.fac[i] = (self.fac[i - 1] * i) % mod\n self.invmod = [1] * (N + 1)\n self.invmod[N] = pow(self.fac[N], mod - 2, mod)\n for i in range(N, 0, -1):\n self.invmod[i - 1] = (self.invmod[i] * i) % mod\n \n def calc(self, n, k): # nCk\n return self.fac[n] * self.invmod[k] % mod * self.invmod[n - k] % mod\n\n\nn, m, k = map(int, input().split())\nC = mod_combination(n)\nans = 0\nfor x in range(k + 1):\n ans = (ans + C.calc(n - 1, n - x - 1) * m * pow(m - 1, n - x - 1, mod) % mod) % mod\nprint(ans)\n']
['Wrong Answer', 'Accepted']
['s395556801', 's649794543']
[24712.0, 24580.0]
[664.0, 675.0]
[681, 683]
p02685
u594956556
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['N, M, K = map(int, input().split())\n\nMOD = 998244353\ninvmod = [pow(i, MOD-2, MOD) for i in range(200002)]\n\nnow = M * pow(M-1, N-1, MOD)\nans = now\nif K == 1:\n print(ans)\n exit()\n\nfor ki in range(1, K+1):\n now = now * invmod[M-1] * (N-ki) * invmod[ki] % MOD\n ans += now\n \nprint(ans % MOD)\n', 'N, M, K = map(int, input().split())\n\nMOD = 998244353\ninvmod = [pow(i, MOD-2, MOD) for i in range(200002)]\n\nif N == 1:\n print(M)\n exit()\n\nif M == 1:\n if K == N-1:\n print(1)\n else:\n print(0)\n exit()\n \nnow = M * pow(M-1, N-1, MOD)\nans = now\nif K == 0:\n print(ans % MOD)\n exit()\n\nfor ki in range(1, K+1):\n now = now * invmod[M-1] * (N-ki) * invmod[ki] % MOD\n ans += now\n \nprint(ans % MOD)\n']
['Wrong Answer', 'Accepted']
['s408810923', 's253661809']
[17052.0, 17092.0]
[894.0, 903.0]
[292, 402]
p02685
u609307781
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['# E\n\nN, M, K = list(map(int, input().split()))\n\nres = [0] * (K+1)\nres[0] = (M * (M-1)**(N-1 - K)) % p\np = 998244353\nfor n in range(1, K+1):\n res[n] = (res[n-1] * (N-n) * pow(n, p-2, p)) % p\n\nprint(sum(res) % p)', '60522 114575 7559', 'N, M, K = list(map(int, input().split()))\np = 998244353\ncomb = 1\nres = 0\nfor n in range(K+1):\n res = (res + comb * M * pow(M-1, N-1-n, p)) % p\n comb = (comb * (N -1 -n) * pow(n + 1, p - 2, p)) % p\nprint(res)']
['Runtime Error', 'Runtime Error', 'Accepted']
['s109189677', 's892387140', 's635942203']
[10624.0, 9068.0, 9204.0]
[242.0, 26.0, 1268.0]
[213, 17, 213]
p02685
u619144316
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['import math\n\nN ,M, K = map(int,input().split())\nMOD = 998244353\nSUM = 0\n\nfact = [1]\nfact_inv = [1]\nfor i in range(1,N+1):\n fact.append(fact[-1] * i)\n fact_inv.append(pow(fact[-1],MOD-2,MOD))\n\nprint(fact)\n\ndef combinations_count(n, r):\n return (fact[n] * fact_inv[n-r] * fact_inv[r])%MOD\n\n\nfor i in range(K+1):\n if N -1 - i >= 0:\n SUM += M * pow(M-1,N-1-i,MOD) * combinations_count(N-i,i)\n SUM %= MOD\n\nprint(SUM)', 'import math\n\nN ,M, K = map(int,input().split())\nMOD = 998244353\n\ndef getInvs(n, MOD):\n invs = [1] * (n+1)\n for x in range(2, n+1):\n invs[x] = (-(MOD//x) * invs[MOD%x]) % MOD\n return invs\ninvs = getInvs(N+3, MOD)\n\nnum = M\nnums = []\nfor i in reversed(range(1,N)):\n nums.append(num)\n num *= i*(M-1)\n num *= invs[N-i]\n num %= MOD\nnums.append(num)\nnums.reverse()\n\nans = sum(nums[:K+1])\nans %= MOD\nprint(ans)']
['Wrong Answer', 'Accepted']
['s704055874', 's808443580']
[564036.0, 26544.0]
[2223.0, 190.0]
[437, 430]
p02685
u659712937
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['N,M,K=map(int, input().split())\n\n\n\n# import sys\n\nnCr = {}\ndef cmb(n, r):\n if r == 0 or r == n: return 1\n if r == 1: return n\n if (n,r) in nCr: return nCr[(n,r)]\n nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)\n return nCr[(n,r)]\n\na = cmb(n,r)\n\nans=0\n\nfor i in range(K+1):\n temp=(M*(M-1)**(N-1-i)*cmb(N-1,i))%998244353\n ans+=temp\n\nprint(ans%998244353)', 'N,M,K=map(int, input().split())\n\nMOD=998244353\n\nans=0\ntemp2=1\n#temp4=(M-1)**(N-1)%MOD\n\ndef getInvs(n, MOD):\n invs = [1] * (n+1)\n for x in range(2, n+1):\n invs[x] = (-(MOD//x) * invs[MOD%x]) % MOD\n return invs\n\ninvs = getInvs(max(N+3,M-1), MOD)\n\nVec=[0]*N\n#inv=1\n\n# inv*=M-1\n# inv%=MOD\n\n\nfor i in range(N):\n if i == 0:\n temp1=M\n Vec[N-i-1]=temp1\n else:\n temp1*=(M-1)*(N-i)\n temp1*=invs[i]\n temp1%=MOD\n Vec[N-i-1]=temp1\n\n\nans=sum(Vec[:K+1])\n\nprint(ans%MOD)\n']
['Runtime Error', 'Accepted']
['s321058610', 's297688254']
[9236.0, 26100.0]
[22.0, 204.0]
[1001, 791]
p02685
u664373116
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['#n=int(input())\n#n,m=list(map(int,input().split()))\n#a=list(map(int,input().split()))\nimport sys\nsys.setrecursionlimit(10**6)\nimport math\nimport itertools\nfrom collections import defaultdict\nfrom collections import Counter\nimport numpy as np\n\nimport heapq\n\n\nn,k,x=list(map(int,input().split()))\nod=998244353\nans=(pow(n,x,od)*pow(2*n-k,n-x,od))%od\nprint(ans)', '#n=int(input())\n#n,m=list(map(int,input().split()))\n#a=list(map(int,input().split()))\nimport sys\nsys.setrecursionlimit(10**6)\nimport math\nimport itertools\nfrom collections import defaultdict\nfrom collections import Counter\nimport numpy as np\n\nimport heapq\ndef combinations_count(n, r):\n return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))\nod=998244353\nn,k,x=list(map(int,input().split()))\n\nn_c_i=[1]\npow_k=pow(k-1,n,od)\ninv_k=pow(k-1,od-2,od)\neach_cal=[]\nzenpou=[1]\nkouhou=[]\nfor i in range(1,x+1):\n n_c_i.append((n_c_i[-1]*(n-i)*pow(i,od-2,od))%od)\nfor i in range(n-1): \n zenpou.append(zenpou[-1]*(k-1)%od)\nfor i in range(x+1):\n pow_k=pow_k*inv_k%od\n kouhou.append(pow_k)\n each_cal.append(pow(k-1,n-i-1,od))\n\nzenpou.reverse()\n\n\n\n#print(len(each_cal))\nans=0\nfor i in range(x+1):\n \n \n \n ans+=(n_c_i[i]*k*zenpou[i])%od\n ans%=od\n\nprint(ans)\n\n']
['Wrong Answer', 'Accepted']
['s476972745', 's466334352']
[26816.0, 58340.0]
[113.0, 1544.0]
[357, 1043]
p02685
u667084803
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['N, M, K = map(int, input().split())\n\n\ndef power_func(a,n,p):\n bi = str(format(n,"b"))\n res = 1\n for i in range(len(bi)):\n res = (res*res) % p\n if bi[i] == "1":\n res = (res*a) % p\n return res\n\nP = 998244353\ninv_t = [0] + [1]\nfor i in range(2, N+2):\n inv_t += [inv_t[P % i] * (P - int(P / i)) % P]\n\nans = (M * power_func(M-1, N-1, P))%P\ncurr = ans\nfor k in range(K):\n N-k-1 < 0 : break\n curr = (curr*(N-k-1)*inv_t[k+1]*inv_t[M-1]) % P\n ans += curr\n ans %= P\n\nprint(ans)', 'import sys\nN, M, K = map(int, input().split())\n\nif M == 1 :\n if K == N-1:\n print(1)\n else:\n print(0)\n sys.exit()\n\ndef power_func(a, n, p):\n bi = str(format(n,"b"))\n if a == 0:\n return 0\n res = 1\n for i in range(len(bi)):\n res = (res*res) % p\n if bi[i] == "1":\n res = (res*a) % p\n return res\n\nP = 998244353\ninv_t = [0] + [1]\nfor i in range(2, max(M,K)+2):\n inv_t += [inv_t[P % i] * (P - int(P / i)) % P]\n\nans = (M * power_func(M-1, N-1, P))%P\ncurr = ans\nfor k in range(K):\n curr = (curr*(N-k-1)*inv_t[k+1]*inv_t[M-1]) % P\n ans += curr\n ans %= P\n\nprint(ans)']
['Runtime Error', 'Accepted']
['s895912771', 's886902285']
[8992.0, 16944.0]
[22.0, 244.0]
[533, 651]
p02685
u669812251
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['# -*- coding: utf-8 -*-\n\n\nn, m, k = map(int,input().split())\n\ndef cmb(n, r, p):\n if (r < 0) or (n < r):\n return 0\n r = min(r, n - r)\n return fact[n] * factinv[r] * factinv[n-r] % p\n\np = 998244353\nN = n+1 \nfact = [1, 1] # fact[n] = (n! mod p)\nfactinv = [1, 1] # factinv[n] = ((n!)^(-1) mod p)\ninv = [0, 1] \n\nfor i in range(2, N + 1):\n fact.append((fact[-1] * i) % p)\n inv.append((-inv[p % i] * (p // i)) % p)\n factinv.append((factinv[-1] * inv[-1]) % p)\n\nans = 0\nbeki = [1,m-1]\nfor i in range(2,N+1):\n beki.append(((beki[-1]*(m-1)) % p))\n\nfor x in range(k+1):\n ans += m * beki[n-x-1] %p * cmb(n-1,x,p)) %p\n ans = ans % p\nprint(ans)\n', '# -*- coding: utf-8 -*-\n\n\nn, m, k = map(int,input().split())\n\ndef cmb(n, r, p):\n if (r < 0) or (n < r):\n return 0\n r = min(r, n - r)\n return fact[n] * factinv[r] * factinv[n-r] % p\n\np = 998244353\nN = n+1 \nfact = [1, 1] # fact[n] = (n! mod p)\nfactinv = [1, 1] # factinv[n] = ((n!)^(-1) mod p)\ninv = [0, 1] \n\nfor i in range(2, N + 1):\n fact.append((fact[-1] * i) % p)\n inv.append((-inv[p % i] * (p // i)) % p)\n factinv.append((factinv[-1] * inv[-1]) % p)\n\nans = 0\nbeki = [1,m-1]\nfor i in range(2,N+1):\n beki.append(((beki[-1]*(m-1)) % p))\n\nfor x in range(k+1):\n ans += m * beki[n-x-1] %p * cmb(n-1,x,p) %p\n ans = ans % p\nprint(ans)\n']
['Runtime Error', 'Accepted']
['s746591043', 's080863576']
[9104.0, 40592.0]
[21.0, 458.0]
[751, 750]
p02685
u686230543
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['mod = 998244353\n\nn, m, k = map(int, input().split())\ncount = 0\n\ncomb = 1\nfor i in range(k + 1):\n tmp = comb * m * pow(m-1, n-i-1, mod) % mod\n count = (count + tmp) % mod\n comb = comb * (n - i) * pow(i+1, mod-2, mod) % mod\nprint(count)', 'mod = 998244353\n\nn, m, k = map(int, input().split())\ncount = 0\n\ncomb = 1\nfor i in range(k + 1):\n tmp = comb * m * pow(m-1, n-i-1, mod) % mod\n count = (count + tmp) % mod\n comb = comb * (n - i - 1) * pow(i+1, mod-2, mod) % mod\nprint(count)']
['Wrong Answer', 'Accepted']
['s120801355', 's262426952']
[9196.0, 9196.0]
[1270.0, 1284.0]
[237, 241]
p02685
u709304134
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['N,M,K = map(int,input().split())\n# dp = [[0]*(K+2) for _ in range(2)]\n\n# print (1)\n# dp[0][0] = M\n# MOD = 998244353\n# for n in range(N-1):\n# n = n % 2\n# a = (n+1) % 2\n\n# for k in range(K+1):\n \n \n \n# dp[a][k] += dp[n][k] * (M-1)\n# dp[a][k+1] += dp[n][k]\n# dp[a][k] %= MOD\n# dp[a][k+1] %= MOD\n# #print (dp)\n# print(sum(dp[(N+1)%2][:-1]))\nimport numpy as np\nold = np.zeros(K+1).astype(np.int)\nMOD = 998244353\nold[0] = M\nfor n in range(N-1):\n new = old*(M-1)\n new[1:] += old[:K]\n new %= MOD\n old = new\nprint (sum(new)%MOD)', 'N,M,K = map(int,input().split())\nMOD = 998244353\nans = 0\n\n\nfact = [1] * (N+1) \nfactinv = [1] * (N+1) \n\n\n\nfor i in range(N):\n fact[i+1] = fact[i] * (i+1) % MOD \n factinv[i+1] = pow(fact[i+1], MOD-2, MOD)\n\ndef nCk(n,k): \n return fact[n] * factinv[n-k] * factinv[k] % MOD\n\ndef solve(k):\n r = M * pow(M-1,N-1-k,MOD) * nCk(N-1,k)\n return r\n\nfor k in range(K+1):\n ans += solve(k)\n ans %= MOD\nprint (ans)']
['Runtime Error', 'Accepted']
['s629009683', 's775624331']
[29752.0, 24476.0]
[2206.0, 1420.0]
[619, 562]
p02685
u764956288
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['def inverse_mod(a, mod=10**9+7):\n """ Calculate inverse of the integer a modulo mod.\n """\n return pow(a, mod-2, mod)\n\n\ndef combination_mod(n, r, mod=10**9+7):\n \n\n r = min(r, n-r)\n numerator = denominator = 1\n for i in range(r):\n numerator = numerator * (n - i) % mod\n denominator = denominator * (i + 1) % mod\n\n return numerator * inverse_mod(denominator, mod) % mod\n\n\ndef create_inverses_list(n, mod):\n """ Create list of inverses of the integers 0 to n modulo mod.\n """\n \n inv_list = [1] * (n+1)\n for x in range(n+1):\n inv_list[x] = (-(mod//x) * inv_list[mod % x]) % mod\n \n return inv_list\n\n\ndef main():\n n_blocks, n_colors, n_max_pairs = map(int, input().split())\n mod = 998244353\n\n inverses = create_inverses_list(n_max_pairs, mod)\n \n e1 = n_colors * pow(n_colors-1, n_blocks-1, mod)\n e2 = 1\n total = e1 * e2 % mod\n\n for k in range(1, n_max_pairs+1):\n e1 = e1 * inverses[n_colors - 1] % mod\n e2 = e2 * (n_blocks - k) % mod * inverses[k] % mod\n total += e1 * e2 % mod\n total %= mod\n\n print(total)\n\n\nif __name__ == "__main__":\n main()\n', 'def inverse_mod(a, mod=10**9+7):\n """ Calculate inverse of the integer a modulo mod.\n """\n return pow(a, mod-2, mod)\n\n\ndef combination_mod(n, r, mod=10**9+7):\n \n\n r = min(r, n-r)\n numerator = denominator = 1\n for i in range(r):\n numerator = numerator * (n - i) % mod\n denominator = denominator * (i + 1) % mod\n\n return numerator * inverse_mod(denominator, mod) % mod\n\n\ndef create_inverses_table(n, mod=10**9+7):\n """ Create table for inverses of the integers 0 to n modulo mod.\n """\n\n inv_table = [1] + [1] * n\n for x in range(2, n+1):\n inv_table[x] = -(mod//x) * inv_table[mod % x] % mod\n\n return inv_table\n\n\ndef main():\n n_blocks, n_colors, n_max_pairs = map(int, input().split())\n mod = 998244353\n\n inverses = create_inverses_table(max(n_colors, n_max_pairs), mod)\n\n e1 = n_colors * pow(n_colors-1, n_blocks-1, mod) % mod if n_colors > 0 else 1\n e2 = 1\n total = e1 * e2\n\n for k in range(n_max_pairs+1):\n e1 = e1 * inverses[n_colors - 1] % mod\n e2 = e2 * (n_blocks - k) * inverses[k] % mod\n total += e1 * e2\n total %= mod\n\n print(total)\n\n\nif __name__ == "__main__":\n main()\n', 'def inverse_mod(a, mod=10**9+7):\n """ Calculate inverse of the integer a modulo mod.\n """\n return pow(a, mod-2, mod)\n\n\ndef combination_mod(n, r, mod=10**9+7):\n \n\n r = min(r, n-r)\n numerator = denominator = 1\n for i in range(r):\n numerator = numerator * (n - i) % mod\n denominator = denominator * (i + 1) % mod\n\n return numerator * inverse_mod(denominator, mod) % mod\n\n\ndef create_inverses_table(n, mod=10**9+7):\n """ Create table for inverses of the integers 0 to n modulo mod.\n """\n\n inv_table = [0] + [1] * n\n for x in range(2, n+1):\n inv_table[x] = -(mod//x) * inv_table[mod % x] % mod\n\n return inv_table\n\n\ndef solve():\n n_blocks, n_colors, n_max_pairs = map(int, input().split())\n\n if n_colors == 1:\n return int(n_blocks - n_max_pairs == 1)\n\n mod = 998244353\n\n inverses = create_inverses_table(max(n_colors, n_max_pairs), mod)\n\n e1 = n_colors * pow(n_colors-1, n_blocks-1, mod) % mod\n e2 = 1\n total = e1 * e2\n\n for k in range(1, n_max_pairs+1):\n e1 = e1 * inverses[n_colors - 1] % mod\n e2 = e2 * (n_blocks - k) * inverses[k] % mod\n total += e1 * e2\n total %= mod\n\n return total\n\n\ndef main():\n print(solve())\n\n\nif __name__ == "__main__":\n main()\n']
['Runtime Error', 'Wrong Answer', 'Accepted']
['s074682871', 's166479111', 's377954695']
[10588.0, 16820.0, 16732.0]
[26.0, 173.0, 167.0]
[1198, 1228, 1314]
p02685
u781262926
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
["class Factorial():\n def __init__(self, mod=10**9 + 7):\n self.mod = mod\n self._factorial = [1]\n self._size = 1\n self._factorial_inv = [1]\n self._size_inv = 1\n \n def __call__(self, n):\n return self.fact(n)\n \n def fact(self, n):\n ''' n! % mod '''\n if n >= self.mod:\n return 0\n self._make(n)\n return self._factorial[n]\n \n def fact_inv(self, n):\n ''' n!^-1 % mod '''\n if n >= self.mod:\n raise ValueError('Modinv is not exist! arg={}'.format(n))\n self._make_inv(n)\n return self._factorial_inv[n]\n \n def comb(self, n, r):\n ''' nCr % mod '''\n if r > n:\n return 0\n t = self.fact_inv(n-r)*self.fact_inv(r) % self.mod\n return self(n)*t % self.mod\n \n def comb_with_repetition(self, n, r):\n ''' nHr % mod '''\n t = self.fact_inv(n-1)*self.fact_inv(r) % self.mod\n return self(n+r-1)*t % self.mod\n \n def perm(self, n, r):\n ''' nPr % mod '''\n if r > n:\n return 0\n return self(n)*self.fact_inv(n-r) % self.mod\n \n @staticmethod\n def xgcd(a, b):\n '''\n Return (gcd(a, b), x, y) such that a*x + b*y = gcd(a, b)\n '''\n x0, x1, y0, y1 = 0, 1, 1, 0\n while a != 0:\n (q, a), b = divmod(b, a), a\n y0, y1 = y1, y0 - q * y1\n x0, x1 = x1, x0 - q * x1\n return b, x0, y0\n \n def modinv(self, n):\n g, x, _ = self.xgcd(n, self.mod)\n if g != 1:\n raise ValueError('Modinv is not exist! arg={}'.format(n))\n return x % self.mod\n \n def _make(self, n):\n if n >= self.mod:\n n = self.mod\n if self._size < n+1:\n for i in range(self._size, n+1):\n self._factorial.append(self._factorial[i-1]*i % self.mod)\n self._size = n+1\n \n def _make_inv(self, n):\n if n >= self.mod:\n n = self.mod\n self.make(n)\n if self._size_inv < n+1:\n for i in range(self._size_inv, n+1):\n self._factorial_inv.append(self.modinv(self._factorial[i]))\n self._size_inv = n+1\n\nn, m, k = map(int, input().split())\nmod = 998244353\ncomb = Factorial(mod).comb\ns = 0\nfor i in range(k+1, n):\n t = comb(n-1, i)*m % mod\n t = t*pow(m-1, n-1-i, mod) % mod\n s = (s+t) % mod\nans = (pow(m, n, mod)-s) % mod\nprint(ans)\n", "class Factorial():\n def __init__(self, mod=10**9 + 7):\n self.mod = mod\n self._factorial = [1]\n self._size = 1\n self._factorial_inv = [1]\n self._size_inv = 1\n\n def __call__(self, n):\n return self.fact(n)\n\n def fact(self, n):\n ''' n! % mod '''\n if n >= self.mod:\n return 0\n self._make(n)\n return self._factorial[n]\n \n def _make(self, n):\n if n >= self.mod:\n n = self.mod\n if self._size < n+1:\n for i in range(self._size, n+1):\n self._factorial.append(self._factorial[i-1]*i % self.mod)\n self._size = n+1\n\n def fact_inv(self, n):\n ''' n!^-1 % mod '''\n if n >= self.mod:\n raise ValueError('Modinv is not exist! arg={}'.format(n))\n if self._size_inv < n+1:\n self._factorial_inv += [-1] * (n+1-self._size_inv)\n self._size_inv = n+1\n if self._factorial_inv[n] == -1:\n self._factorial_inv[n] = self.modinv(self.fact(n))\n return self._factorial_inv[n]\n \n def _make_inv(self, n, r=2):\n if n >= self.mod:\n n = self.mod - 1\n if self._size_inv < n+1:\n self._factorial_inv += [-1] * (n+1-self._size_inv)\n self._size_inv = n+1\n self._factorial_inv[n] = self.modinv(self.fact(n))\n for i in range(n, r, -1):\n self._factorial_inv[i-1] = self._factorial_inv[i]*i % self.mod\n \n @staticmethod\n def xgcd(a, b):\n '''\n Return (gcd(a, b), x, y) such that a*x + b*y = gcd(a, b)\n '''\n x0, x1, y0, y1 = 0, 1, 1, 0\n while a != 0:\n (q, a), b = divmod(b, a), a\n y0, y1 = y1, y0 - q * y1\n x0, x1 = x1, x0 - q * x1\n return b, x0, y0\n\n def modinv(self, n):\n g, x, _ = self.xgcd(n, self.mod)\n if g != 1:\n raise ValueError('Modinv is not exist! arg={}'.format(n))\n return x % self.mod\n\n def comb(self, n, r):\n ''' nCr % mod '''\n if r > n:\n return 0\n t = self(n)*self.fact_inv(n-r) % self.mod\n return t*self.fact_inv(r) % self.mod\n \n def comb_(self, n, r):\n '''\n nCr % mod\n when r is not large and n is too large\n '''\n c = 1\n for i in range(1, r+1):\n c *= (n-i+1) * self.fact_inv(i)\n c %= self.mod\n return c\n\n def comb_with_repetition(self, n, r):\n ''' nHr % mod '''\n t = self(n+r-1)*self.fact_inv(n-1) % self.mod\n return t*self.fact_inv(r) % self.mod\n\n def perm(self, n, r):\n ''' nPr % mod '''\n if r > n:\n return 0\n return self(n)*self.fact_inv(n-r) % self.mod\n\nn, m, k = map(int, input().split())\nmod = 998244353\nf = Factorial(mod)\nf._make_inv(n-1)\ncomb = f.comb\ns = 0\nfor i in range(k+1, n):\n t = comb(n-1, i)*m % mod\n t = t*pow(m-1, n-1-i, mod) % mod\n s = (s+t) % mod\nans = (pow(m, n, mod)-s) % mod\nprint(ans)"]
['Runtime Error', 'Accepted']
['s364516656', 's484581697']
[9212.0, 24472.0]
[26.0, 835.0]
[2451, 3000]
p02685
u785573018
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['n, m, k = map(int, input().split())\nif n <= k+1:\n print(m**n)\nelse:\n a = [m*((m-1)**(n-1))]\n b = [1]\n c = 0\n for i in range(k):\n b.append(int(b[-1]/(i+1)*(n-i-1)))\n for i in range(k):\n a.append(int((a[-1]*b[i+1])/b[i]/(m-1))\n for i in range(k+1):\n c += a[i]\n print(c%998244353)', 'n, m, k = map(int, input().split())\nz = 998244353\nif n <= k+1:\n a = 1\n for i in range(n):\n a = a*m%z\n print(a)\nelse:\n d = m\n for i in range(n-1):\n d = d*(m-1)%z\n a = [d]\n b = [1]\n c = 0\n for i in range(k):\n b.append(int(b[-1]*(n-i-2)/(i+1)%z))\n for i in range(k):\n a.append(int((a[-1]*b[i+1]%z)/b[i]/(m-1)))\n for i in range(k+1):\n c = (c+a[i])%z\n print(c)', 'n, m, k = map(int, input().split())\nz = 998244353\nif n <= k+1:\n a = 1\n for i in range(n):\n a = a*m%z\n print(a)\nelse:\n a = []\n b = []\n d = [1, 1]\n e = [1, 1]\n f = [0, 1]\n for i in range(2, n):\n d.append(d[-1]*i%z)\n f.append((-f[z%i]*(z//i))%z)\n e.append(e[-1]*f[-1]%z)\n for i in range(k+1):\n b.append(d[n-1]*e[i]*e[n-i-1]%z)\n x = 0\n g = [1]\n for i in range(n-1):\n g.append(g[i]*(m-1)%z)\n for i in range(k+1):\n a.append(b[i]*m*g[n-i-1]%z)\n for i in range(k+1):\n x = (x+a[i])%z\n print(x)']
['Runtime Error', 'Runtime Error', 'Accepted']
['s013339458', 's315019617', 's070072668']
[8996.0, 13872.0, 49644.0]
[23.0, 147.0, 395.0]
[322, 426, 588]
p02685
u788703383
2,000
1,048,576
There are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. * There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353.
['M = 998244353\nn,m,K = map(int,input().split())\nans = 0\nc = 1\nfor k in range(K+1):\n ans += c * m * pow(m-1,n-k-1,M)\n c *= (n-k+1) * pow(k+1,M-2,M)\n ans %= M\n c %= M\nprint(ans)\n \n', 'M = 998244353\nn,m,K = map(int,input().split())\nans = 0\nc = 1\nfor k in range(K+1):\n ans += c * m * pow(m-1,n-k-1,M)\n c *= (n-k-1) * pow(k+1,M-2,M)\n ans %= M\n c %= M\nprint(ans)\n']
['Wrong Answer', 'Accepted']
['s433932695', 's700229452']
[9124.0, 9028.0]
[1273.0, 1274.0]
[192, 187]