結果

問題 No.2744 Power! or +1
ユーザー Nzt3Nzt3
提出日時 2024-04-17 20:43:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 754 ms / 3,000 ms
コード長 985 bytes
コンパイル時間 351 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 105,464 KB
最終ジャッジ日時 2024-04-17 20:44:13
合計ジャッジ時間 3,670 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 682 ms
105,464 KB
testcase_01 AC 46 ms
63,104 KB
testcase_02 AC 39 ms
59,776 KB
testcase_03 AC 40 ms
59,648 KB
testcase_04 AC 221 ms
80,328 KB
testcase_05 AC 754 ms
104,016 KB
testcase_06 AC 268 ms
82,924 KB
testcase_07 AC 38 ms
52,736 KB
testcase_08 AC 48 ms
64,640 KB
testcase_09 AC 36 ms
52,872 KB
testcase_10 AC 38 ms
52,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
N,A,B,C=map(int,input().split())
fac=[1]*N
fac_over=[0]*N
for i in range(1,N):
  fac[i]=fac[i-1]*i
  fac_over[i]=fac_over[i-1]
  if fac[i]>=N:
    fac[i]%=N
    fac_over[i]=1

dist=[N*A]*(N+1)
dist[1]=0
q=[(0,1)]
while q:
  d,v=heapq.heappop(q)
  if v==0:
    break
  if dist[v]<d:
    continue
  if v==N:
    # 操作3によりNの倍数になる
    if dist[0]>d+C:
      dist[0]=d+C
      heapq.heappush(q,(d+C,0))
    continue
  # 操作1
  if dist[(v+1)%N]>d+A:
    dist[(v+1)%N]=d+A
    heapq.heappush(q,(d+A,(v+1)%N))
  
  # 操作2
  b,v2=B,v
  ov=0
  while d+b<N*A:
    if ov and dist[N]>d+b:
      dist[N]=d+b
      heapq.heappush(q,(d+b,N))
    if dist[v2]>d+b:
      dist[v2]=d+b
      heapq.heappush(q,(d+b,v2))
    b*=B
    v2*=v
    if v2>=N:
      ov=1
      v2%=N

  # 操作3
  if fac_over[v] and dist[N]>d+C:
      dist[N]=d+C
      heapq.heappush(q,(d+C,N))
  if dist[fac[v]]>d+C:
    dist[fac[v]]=d+C
    heapq.heappush(q,(d+C,fac[v]))

print(dist[0])
0