結果
問題 | No.1330 Multiply or Divide |
ユーザー |
![]() |
提出日時 | 2025-03-20 20:27:16 |
言語 | PyPy3 (7.3.15) |
結果 |
MLE
|
実行時間 | - |
コード長 | 2,764 bytes |
コンパイル時間 | 232 ms |
コンパイル使用メモリ | 82,328 KB |
実行使用メモリ | 624,664 KB |
最終ジャッジ日時 | 2025-03-20 20:28:56 |
合計ジャッジ時間 | 4,092 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 1 MLE * 1 -- * 44 |
ソースコード
import heapqfrom collections import defaultdictdef main():import sysinput = sys.stdin.readdata = input().split()idx = 0N = int(data[idx]); idx +=1M = int(data[idx]); idx +=1P = int(data[idx]); idx +=1A = list(map(int, data[idx:idx+N]))idx +=N# Preprocess A into B_i and m_i (A_i = B_i * P^m_i)B_list = []m_list = []for a in A:if a == 0:m = 0B = 0else:m = 0tmp = awhile tmp % P == 0:m +=1tmp = tmp // PB = tmpB_list.append(B)m_list.append(m)heap = []visited = defaultdict(lambda: float('inf'))initial_k = 1initial_e = 0initial_steps = 0heapq.heappush(heap, (initial_steps, initial_k, initial_e))visited[(initial_k, initial_e)] = initial_stepsanswer = -1while heap:steps, k, e = heapq.heappop(heap)# Check if current steps is higher than recordedif steps > visited[(k, e)]:continue# Check division possibilityif e > 0:# must dividenew_e = e -1new_k = knew_x = new_k * (P ** new_e)new_steps = steps +1if new_x > M:answer = new_stepsprint(answer)returnelse:if new_steps < visited[(new_k, new_e)]:visited[(new_k, new_e)] = new_stepsheapq.heappush(heap, (new_steps, new_k, new_e))else:# e == 0: can multiply by any A_ifor i in range(N):B_i = B_list[i]m_i = m_list[i]# Compute new_x after multiplying by B_i and P^m_inew_x_multiply = k * B_i * (P ** m_i)if new_x_multiply > M:current_candidate = steps +1if answer == -1 or current_candidate < answer:answer = current_candidateprint(answer)returncontinueelse:new_k_after = k * B_inew_steps_after = steps + m_i +1new_e_after = 0if new_steps_after < visited.get((new_k_after, new_e_after), float('inf')):visited[(new_k_after, new_e_after)] = new_steps_afterheapq.heappush(heap, (new_steps_after, new_k_after, new_e_after))print(answer)if __name__ == '__main__':main()