結果

問題 No.463 魔法使いのすごろく🎲
ユーザー compass19
提出日時 2016-12-18 02:47:08
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
RE  
実行時間 -
コード長 1,451 bytes
コンパイル時間 152 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 46,296 KB
最終ジャッジ日時 2024-12-14 07:46:42
合計ジャッジ時間 22,761 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 6 WA * 3 RE * 27
権限があれば一括ダウンロードができます

ソースコード

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]*(m-1)
	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
	vec[i] = sum([c[n-3-j]*tmp[j] for j in range(m-1)])
	for j in range(key-1):
		tmp[1+j] += tmp[m-2-j]
	mat[i] = tmp[: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