結果

問題 No.1352 Three Coins
ユーザー marroncastlemarroncastle
提出日時 2021-01-20 22:55:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 150 ms / 2,000 ms
コード長 4,057 bytes
コンパイル時間 437 ms
コンパイル使用メモリ 87,028 KB
実行使用メモリ 79,048 KB
最終ジャッジ日時 2023-08-25 02:31:23
合計ジャッジ時間 5,520 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
71,772 KB
testcase_01 AC 87 ms
71,748 KB
testcase_02 AC 101 ms
76,588 KB
testcase_03 AC 90 ms
72,380 KB
testcase_04 AC 88 ms
72,280 KB
testcase_05 AC 91 ms
72,784 KB
testcase_06 AC 138 ms
78,948 KB
testcase_07 AC 87 ms
71,824 KB
testcase_08 AC 143 ms
79,048 KB
testcase_09 AC 97 ms
76,808 KB
testcase_10 AC 118 ms
77,804 KB
testcase_11 AC 94 ms
76,392 KB
testcase_12 AC 84 ms
71,564 KB
testcase_13 AC 96 ms
76,656 KB
testcase_14 AC 141 ms
78,964 KB
testcase_15 AC 97 ms
76,984 KB
testcase_16 AC 86 ms
71,480 KB
testcase_17 AC 90 ms
72,380 KB
testcase_18 AC 101 ms
76,868 KB
testcase_19 AC 87 ms
71,652 KB
testcase_20 AC 87 ms
71,668 KB
testcase_21 AC 150 ms
78,872 KB
testcase_22 AC 88 ms
71,772 KB
testcase_23 AC 88 ms
72,096 KB
testcase_24 AC 90 ms
72,264 KB
testcase_25 AC 86 ms
71,856 KB
testcase_26 AC 90 ms
71,820 KB
testcase_27 AC 85 ms
71,784 KB
testcase_28 AC 121 ms
77,628 KB
testcase_29 AC 86 ms
71,788 KB
testcase_30 AC 134 ms
78,860 KB
testcase_31 AC 88 ms
72,532 KB
testcase_32 AC 117 ms
78,460 KB
testcase_33 AC 86 ms
71,808 KB
testcase_34 AC 87 ms
71,900 KB
testcase_35 AC 84 ms
71,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import deque
import heapq
class Graph:
  def __init__(self, N, M=-1):
    self.V = N
    if M>=0: self.E = M
    self.edge = [[] for _ in range(self.V)]
    self.edge_rev = [[] for _ in range(self.V)]
    self.order = []
    self.to = [0]*self.V
    self.visited = [False]*self.V
    self.dp = [0]*self.V

  def add_edges(self, ind=1, bi=False, rev=False):
    for i in range(self.E):
      a,b = map(int, input().split())
      a -= ind; b -= ind
      self.edge[a].append(b)
      if rev: self.edge_rev[b].append(a)
      self.to[b] += 1
      if bi: self.edge[b].append(a)

  def add_edge(self, a, b, d, bi=False, rev=False):
    self.edge[a].append((d, b))
    if rev: self.edge_rev[b].append(a)
    if bi: self.edge[b].append((d, a))

  def dfs_rec(self, v):
    if self.visited[v]: return self.dp[v]
    for u in self.edge[v]:
      self.dp[v] += self.dfs_rec(u, v)
    self.visited[v] = True
    return self.dp[v]

  def topo_sort(self): #topological sort
    updated = [0]*self.V
    for start in range(self.V):
      if self.to[start] or updated[start]: continue
      stack = deque([start])
      while stack:
        v = stack.popleft()
        self.order.append(v)
        updated[v] = 1
        for u in self.edge[v]:
          self.to[u] -= 1
          if self.to[u]: continue
          stack.append(u)
  def dp(self): #トポソしてから
    # self.dp = [0]*self.V
    for v in self.order: #行きがけ
      for u in self.edge[v]:
        self.dp[u] = max(self.dp[u], self.dp[v]+1) #配るDP
    for v in self.order[::-1]: #帰りがけ
      for u in self.edge_rev[v]:
        self.dp[u] = max(self.dp[u], self.dp[v]+1) #配るDP
  
  def bfs(self, start):
    d = deque([start])
    self.min_cost = [-1]*self.V; self.min_cost[start]=0
    while len(d)>0:
      v = d.popleft()
      for w in self.edge[v]:
        if self.min_cost[w]==-1:
          self.min_cost[w]=self.min_cost[v]+1
          d.append(w)
    return

  #O(ElogV),頂点数10**5以下
  def dijkstra_heap(self,s):
    self.dists = [float('inf')] * self.V; self.dists[s] = 0
    used = [False] * self.V; used[s] = True
    vlist = [] #vlist : [sからの暫定(未確定)最短距離,頂点]のリスト
    #edge[s] : sから出る枝の[重み,終点]のリスト
    for e in self.edge[s]: heapq.heappush(vlist,e) #sの隣の点は枝の重さがそのまま暫定最短距離となる
    while len(vlist):
      #まだ使われてない頂点の中から最小の距離のものを探す→確定させる
      d,v = heapq.heappop(vlist)
      #[d,v]:[sからの(確定)最短距離,頂点]
      if used[v]: continue
      self.dists[v] = d; used[v] = True
      for d,w in self.edge[v]:
        if not used[w]: heapq.heappush(vlist,(self.dists[v]+d,w))

  #O(ElogV),重みが整数の場合のみ
  def dijkstra_fast(self,s,mod):
    self.dists = [float('inf')] * self.V; self.dists[s] = 0 #始点sから各頂点への最短距離
    used = [False] * self.V; used[s] = True
    vlist = []
    #vlist : [sからの暫定(未確定)最短距離,頂点]のリスト
    #edge[s] : sから出る枝の[重み,終点]のリスト
    for w,v in self.edge[s]:
      heapq.heappush(vlist,w*mod+v) #sの隣の点は枝の重さがそのまま暫定最短距離となる
    while len(vlist):
      #まだ使われてない頂点の中から最小の距離のものを探す→確定させる
      #d, v : sからの(確定)最短距離, 頂点
      d,v = divmod(heapq.heappop(vlist),mod)
      if used[v]: continue
      self.dists[v] = d; used[v] = True
      for d,w in self.edge[v]:
        if not used[w]: heapq.heappush(vlist,(self.dists[v]+d)*mod+w)

from math import gcd
def multi_gcd(A):
  g = A[0]
  for a in A:
    g = gcd(g,a)
  return g
A = list(map(int, input().split()))
if multi_gcd(A)>1:
  print('INF')
  exit()
A.sort()
G = Graph(A[0])
for i in range(A[0]):
  G.add_edge(i, (i+A[1])%A[0], (i+A[1])//A[0])
  G.add_edge(i, (i+A[2])%A[0], (i+A[2])//A[0])
G.dijkstra_heap(0)
print(sum(G.dists))
0