結果

問題 No.2744 Power! or +1
ユーザー PNJPNJ
提出日時 2024-04-20 14:34:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,576 ms / 3,000 ms
コード長 1,481 bytes
コンパイル時間 171 ms
コンパイル使用メモリ 82,552 KB
実行使用メモリ 304,156 KB
最終ジャッジ日時 2024-04-20 14:34:41
合計ジャッジ時間 9,070 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,576 ms
304,156 KB
testcase_01 AC 126 ms
118,736 KB
testcase_02 AC 59 ms
73,856 KB
testcase_03 AC 60 ms
75,648 KB
testcase_04 AC 240 ms
95,360 KB
testcase_05 AC 1,703 ms
260,832 KB
testcase_06 AC 640 ms
157,240 KB
testcase_07 AC 36 ms
53,672 KB
testcase_08 AC 176 ms
143,652 KB
testcase_09 AC 38 ms
54,292 KB
testcase_10 AC 43 ms
60,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
def dijkstra(s, n, edge):
    dist = [inf]*n
    dist[s] = 0
    hq = [[0,s]]
    heapq.heapify(hq)
    while len(hq) > 0:
        d,i = heapq.heappop(hq)
        if dist[i] < d:
            continue
        for j,d_1 in edge[i]:
            if dist[j] > (dist[i] + d_1):
                dist[j] = dist[i] + d_1
                heapq.heappush(hq, [dist[j],j])
    return dist

N,A,B,C = map(int,input().split())
inf = min((N-1)*A,2*A+4*C)
fact = [1]
f = 1
for i in range(1,N+1):
  f = f*i % N
  fact.append(f)

P = [2,3,5,7,11,13,17,19]
B = [min(inf,pow(B,P[i])) for i in range(len(P))]
G = [[] for i in range(2*N)]
for x in range(1,N):
  G[N+x].append((0,C))
G[N].append((0,0))
for x in range(1,2*N):
  G[x].append(((x+1) % (2*N),A))
for x in range(2,N):
  f = 0
  for i in range(len(P)):  # 素数乗だけでいい
    k = P[i]
    if f == 0:
      xx = pow(x,k)
      if xx >= N:
        f = 1
        xx %= N
    else:
      xx = pow(x,k,N)
    b = B[i]
    if b >= inf:
      break
    if xx % N == 0:
      G[x].append((0,b))
      G[x+N].append((0,b))
      break
    if f:
      G[x].append((N+xx,b))
    else:
      G[x].append((xx,b))
    G[x+N].append((N + xx,b))

  if fact[x] == 0:
    G[x].append((0,C))
    continue
  if x >= 9:
    G[x].append((N + fact[x],C))
  else:
    f = 1
    for i in range(1,x+1):
      f *= i
    if f > N:
      G[x].append((N + fact[x],C))
    else:
      G[x].append((fact[x],C))

dist = dijkstra(1, 2*N, G)
print(dist[0])
0