結果
問題 | No.1330 Multiply or Divide |
ユーザー |
![]() |
提出日時 | 2025-03-31 17:57:53 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 6,313 bytes |
コンパイル時間 | 199 ms |
コンパイル使用メモリ | 82,380 KB |
実行使用メモリ | 130,136 KB |
最終ジャッジ日時 | 2025-03-31 17:58:44 |
合計ジャッジ時間 | 5,692 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 39 WA * 7 |
ソースコード
import sysfrom collections import dequedef main():input = sys.stdin.read().split()ptr = 0N = int(input[ptr])ptr += 1M = int(input[ptr])ptr += 1P = int(input[ptr])ptr += 1A = list(map(int, input[ptr:ptr+N]))ptr += N# Case 1: Check if any Ai is a power of Pcase1_e = []other_A = []for a in A:e = 0temp = awhile temp % P == 0:e += 1temp = temp // Pif temp == 1:case1_e.append(e)else:other_A.append(a)case1_valid = len(case1_e) > 0# Solve case1 using BFScase1_min = Noneif case1_valid:max_log = 0if P == 1:pass # impossible since P is a prime >=2else:max_e = 0current = 1count = 0while current <= M:current *= Pcount += 1max_e = count -1 if P !=1 else 0unique_e = sorted(list(set(case1_e)))visited = dict()q = deque()visited[0] = 0q.append((0, 0))min_steps = float('inf')while q:k, steps = q.popleft()if steps >= min_steps:continue# Check if current x = P^k is exceeding M (steps must be minimal)if pow(P, k) > M:if steps < min_steps:min_steps = stepscontinue# Check if there is a multiplier that can directly exceed M in one stepif k == 0:for e in unique_e:if P**e > M:if steps +1 < min_steps:min_steps = steps +1# Operation 1: divide if possibleif k > 0:new_k = k -1new_steps = steps +1if new_k not in visited or new_steps < visited.get(new_k, float('inf')):visited[new_k] = new_stepsq.append((new_k, new_steps))# Operation 2: multiply (k==0)if k ==0:for e in unique_e:new_k = enew_x = P**new_knew_steps = steps +1if new_x > M:if new_steps < min_steps:min_steps = new_stepselse:if new_k not in visited or new_steps < visited.get(new_k, float('inf')):visited[new_k] = new_stepsq.append((new_k, new_steps))if min_steps != float('inf'):case1_min = min_steps# Solve case2: only use other_A (non-power elements)# But if case1 is valid, but there are other_A elements, then we can choose either pathcase2_min = None# Check if any Ai is non-power of P (other_A is not empty) or case1 is invalidcase2_possible = len(other_A) > 0 or not case1_validif case2_possible:current_x = 1steps = 0visited = set()has_large = Falsecandidate = Nonewhile True:if current_x > M:candidate = stepsbreak# Check if any Ai can make current_x * Ai > Mfound = Falsemax_ai = 0max_ai_val = 0for a in A:if current_x * a > M:if candidate is None or (steps +1) < candidate:candidate = steps +1found = Trueif a > max_ai_val:max_ai_val = amax_ai = aif found:# At least one Ai can exceed M in next stepif candidate is None:candidate = steps +1else:candidate = min(candidate, steps +1)break# If no Ai can exceed M, proceed with the largest Aiif max_ai_val == 0:candidate = -1breakif max_ai_val ==1:# All ai is 1, which can't increase x. So infinite loopcandidate = -1break# Check if current_x repeats, leading to a loopif current_x in visited:candidate = -1breakvisited.add(current_x)# Check if after adding, it would loop foreverif current_x > M // max_ai_val:# Can't multiply as it would exceed M in next step? Or can I?# current_x * max_ai_val <= M ?# Not surepasscurrent_x *= max_ai_valsteps +=1if current_x > M:candidate = stepsbreakif len(visited) > 40:# Consider practical infinite loopcandidate = -1breakcase2_min = candidate# Now compare case1_min and case2_minmin_ans = Noneif case1_valid and case1_min is not None:min_ans = case1_minif case2_min is not None:if case2_min != -1:if min_ans is None or case2_min < min_ans:min_ans = case2_minif case1_min is None and case2_min is None:print(-1)else:candidates = []if case1_min is not None:candidates.append(case1_min)if case2_min is not None and case2_min !=-1:candidates.append(case2_min)if len(candidates) ==0:print(-1)else:# Also consider if there is a way to combine case1 and case2# Like using case1 path and then case2 path# But this is complicated, for this problem, the answer is the minimum between case1 and case2, but maybe they can be combined# But given time constraints, proceed with the current logicfinal_ans = min(candidates)print(final_ans)returnif __name__ == "__main__":main()