結果

問題 No.2744 Power! or +1
ユーザー PNJPNJ
提出日時 2024-04-20 13:52:57
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,276 bytes
コンパイル時間 318 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 177,552 KB
最終ジャッジ日時 2024-04-20 13:53:16
合計ジャッジ時間 11,111 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,966 ms
177,552 KB
testcase_01 AC 1,479 ms
140,184 KB
testcase_02 AC 391 ms
89,272 KB
testcase_03 AC 425 ms
92,180 KB
testcase_04 AC 221 ms
84,480 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 43 ms
52,944 KB
testcase_08 AC 1,426 ms
153,488 KB
testcase_09 AC 84 ms
72,448 KB
testcase_10 AC 108 ms
77,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

def dijkstra(s, n, edge):
    dist = [float("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())
fact = [1]
for i in range(1,N+1):
  f = fact[-1]*i
  f %= N
  fact.append(f)

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):
  for k in range(2,41):
    xx = pow(x,k)
    if xx >= A*N:
      break
    if xx % N == 0:
      G[x].append((0,pow(B,k)))
      G[x+N].append((0,pow(B,k)))
      break
    if xx >= N:
      G[x].append((N+(xx%N),pow(B,k)))
    else:
      G[x].append((xx,pow(B,k)))
    G[x+N].append((N + xx % N,pow(B,k)))
  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 + (f % N),C))
    else:
      G[x].append((f,C))

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