結果

問題 No.463 魔法使いのすごろく🎲
ユーザー compass19compass19
提出日時 2016-12-18 02:22:00
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 1,396 bytes
コンパイル時間 389 ms
コンパイル使用メモリ 11,084 KB
実行使用メモリ 30,240 KB
最終ジャッジ日時 2023-08-21 00:29:14
合計ジャッジ時間 10,005 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 AC 136 ms
29,640 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 AC 135 ms
29,588 KB
testcase_07 AC 135 ms
29,708 KB
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 AC 136 ms
29,584 KB
testcase_14 RE -
testcase_15 RE -
testcase_16 WA -
testcase_17 RE -
testcase_18 AC 144 ms
30,000 KB
testcase_19 RE -
testcase_20 WA -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 AC 143 ms
29,488 KB
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 WA -
testcase_35 AC 136 ms
29,620 KB
testcase_36 AC 141 ms
29,960 KB
testcase_37 AC 142 ms
30,088 KB
testcase_38 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import numpy as np

n, m = map(int, input().split())
c = [0] + list(map(int, input().split()))

# mが1の時だけ除外(エラー回避)
if m == 1:
	print(sum(c))
	exit()

# P_list[pos]: posマス目がstartとみなしたときのゴールまでの期待値(魔法なし)
P_list = [0]*(n-1)
# Q_list[pos]: posマス目がstartとみなしたときのゴールまでの期待値(魔法あり) 
Q_list = [0]*(n-1) 


# P_listのゴール手前m-1マスを計算(線形代数)
# 一次線形方程式を解く
key = (m+1) // 2
mat = np.matrix([[0]*(key)]*(key))
vec = np.array([[0]*(key)]).T
for i in range(key):
	tmp = [0]*(key)
	count = m - 1
	pos = i

	while count > 0 and pos >= 0:
		tmp[pos] = tmp[pos] - 1
		pos = pos - 1
		count = count - 1
	while count > 0:
		tmp[pos] = tmp[pos] - 1
		pos = pos + 1
		count = count - 1
	tmp[pos] = tmp[pos] + m
	mat[i] = tmp
	vec[i] = sum([c[n-3-j]*tmp[j] for j in range(key)])

inv_mat = np.linalg.inv(mat)
vec = inv_mat.dot(vec)

# P_listの初期化
for i in range(key):
	P_list[n-2-i] = vec[i][0]
	P_list[n-m-1+i] = vec[i][0]

# 動的計画法
for pos in range(n-m-2, -1, -1):
	# P_list の更新
	P_list[pos] = sum([c[i]+P_list[i] for i in range(pos+1, pos+m+1)]) / m
	# Q_list の更新
	Q_list[pos] = min([c[i]+P_list[i] for i in range(pos+1, pos+m+1)]\
		             +[sum([c[i]+Q_list[i] for i in range(pos+1, pos+m+1)])/m])

print(Q_list[0])
0