結果
問題 | No.2146 2 Pows |
ユーザー |
![]() |
提出日時 | 2025-03-26 15:50:34 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,803 bytes |
コンパイル時間 | 242 ms |
コンパイル使用メモリ | 82,540 KB |
実行使用メモリ | 74,420 KB |
最終ジャッジ日時 | 2025-03-26 15:51:45 |
合計ジャッジ時間 | 5,590 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 3 TLE * 1 -- * 28 |
ソースコード
import heapqdef main():import sysinput = sys.stdin.readdata = input().split()N = int(data[0])A = int(data[1])B = int(data[2])C = int(data[3])m_max = 60 # Sufficiently large to cover possible exponentss = [pow(2, m, N) for m in range(m_max)]INF = float('inf')dist = [[INF] * m_max for _ in range(N)]heap = []# Initialize for each possible exponent mfor m in range(m_max):sm = s[m]cost = A + B + C * mif dist[sm][m] > cost:dist[sm][m] = costheapq.heappush(heap, (cost, sm, m))while heap:current_cost, r, m = heapq.heappop(heap)if current_cost > dist[r][m]:continue# Transition a: add another element of current exponent mnew_r = (r + s[m]) % Nnew_cost = current_cost + Aif new_cost < dist[new_r][m]:dist[new_r][m] = new_costheapq.heappush(heap, (new_cost, new_r, m))# Transition b: add a new exponent m' > mfor m_prime in range(m + 1, m_max):sm_p = s[m_prime]new_r_b = (r + sm_p) % Nadded_cost = A + B + C * (m_prime - m)new_cost_b = current_cost + added_costif new_cost_b < dist[new_r_b][m_prime]:dist[new_r_b][m_prime] = new_cost_bheapq.heappush(heap, (new_cost_b, new_r_b, m_prime))# Compute the minimal cost for each residueans = [INF] * Nfor r in range(N):min_val = min(dist[r][m] for m in range(m_max))ans[r] = min_val if min_val != INF else -1 # Ensure no -1 as problem guarantees a solutionfor k in range(N):print(ans[k])if __name__ == '__main__':main()