Submission #14813496
Source Code Expand
# 解説PDF読んで解説放送見てAC
# 逆から操作を考える(終点のNから考える)
# スカスカDP。なので配列を作らずに辞書で管理する必要がある
import math
def calc_cost(n, ans_dict):
min_cost = n * d
inc_2 = int(math.ceil(n / 2))
if inc_2 not in ans_dict:
ans_dict[inc_2] = calc_cost(inc_2, ans_dict)
min_cost = min(min_cost, d * (inc_2 * 2 - n) + a + ans_dict[inc_2])
dec_2 = int(math.floor(n / 2))
if dec_2 not in ans_dict:
ans_dict[dec_2] = calc_cost(dec_2, ans_dict)
min_cost = min(min_cost, d * (- dec_2 * 2 + n) + a + ans_dict[dec_2])
inc_3 = int(math.ceil(n / 3))
if inc_3 not in ans_dict:
ans_dict[inc_3] = calc_cost(inc_3, ans_dict)
min_cost = min(min_cost, d * (inc_3 * 3 - n) + b + ans_dict[inc_3])
dec_3 = int(math.floor(n / 3))
if dec_3 not in ans_dict:
ans_dict[dec_3] = calc_cost(dec_3, ans_dict)
min_cost = min(min_cost, d * (- dec_3 * 3 + n) + b + ans_dict[dec_3])
inc_5 = int(math.ceil(n / 5))
if inc_5 not in ans_dict:
ans_dict[inc_5] = calc_cost(inc_5, ans_dict)
min_cost = min(min_cost, d * (inc_5 * 5 - n) + c + ans_dict[inc_5])
dec_5 = int(math.floor(n / 5))
if dec_5 not in ans_dict:
ans_dict[dec_5] = calc_cost(dec_5, ans_dict)
min_cost = min(min_cost, d * (- dec_5 * 5 + n) + c + ans_dict[dec_5])
ans_dict[n] = min_cost
return min_cost
n_testcase = int(input())
for i in range(n_testcase):
n, a, b, c, d = list(map(int, input().split()))
# テストケースごとに辞書はリセットしないとダメですね
ans_dict = {}
ans_dict[0] = 0
ans_dict[1] = d # 1減らすのが常に最善
ans = calc_cost(n, ans_dict)
print(ans)
# print(ans_dict)
Submission Info
| Submission Time | |
|---|---|
| Task | A - Pay to Win |
| User | Linus |
| Language | Python (3.8.2) |
| Score | 0 |
| Code Size | 1859 Byte |
| Status | WA |
| Exec Time | 408 ms |
| Memory | 11016 KiB |
Judge Result
| Set Name | Sample | All | ||||||
|---|---|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 0 / 400 | ||||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | example0.txt |
| All | 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, example0.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 000.txt | AC | 31 ms | 9192 KiB |
| 001.txt | AC | 31 ms | 9256 KiB |
| 002.txt | AC | 29 ms | 9192 KiB |
| 003.txt | AC | 30 ms | 9136 KiB |
| 004.txt | AC | 25 ms | 9260 KiB |
| 005.txt | WA | 408 ms | 11016 KiB |
| 006.txt | AC | 275 ms | 10008 KiB |
| 007.txt | WA | 323 ms | 10316 KiB |
| 008.txt | WA | 283 ms | 10180 KiB |
| example0.txt | WA | 95 ms | 10648 KiB |