結果

問題 No.463 魔法使いのすごろく🎲
ユーザー 37zigen37zigen
提出日時 2016-12-15 13:28:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 226 ms / 2,000 ms
コード長 2,621 bytes
コンパイル時間 3,952 ms
コンパイル使用メモリ 77,288 KB
実行使用メモリ 57,932 KB
最終ジャッジ日時 2023-08-20 07:33:11
合計ジャッジ時間 12,292 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 148 ms
56,160 KB
testcase_01 AC 145 ms
56,148 KB
testcase_02 AC 148 ms
55,956 KB
testcase_03 AC 144 ms
55,712 KB
testcase_04 AC 153 ms
56,404 KB
testcase_05 AC 158 ms
56,004 KB
testcase_06 AC 138 ms
55,704 KB
testcase_07 AC 145 ms
56,080 KB
testcase_08 AC 145 ms
55,988 KB
testcase_09 AC 226 ms
57,316 KB
testcase_10 AC 143 ms
56,144 KB
testcase_11 AC 155 ms
53,956 KB
testcase_12 AC 150 ms
55,752 KB
testcase_13 AC 136 ms
56,048 KB
testcase_14 AC 201 ms
57,464 KB
testcase_15 AC 162 ms
56,248 KB
testcase_16 AC 142 ms
56,016 KB
testcase_17 AC 196 ms
57,492 KB
testcase_18 AC 136 ms
55,984 KB
testcase_19 AC 145 ms
56,188 KB
testcase_20 AC 137 ms
56,268 KB
testcase_21 AC 198 ms
57,628 KB
testcase_22 AC 167 ms
55,940 KB
testcase_23 AC 171 ms
56,524 KB
testcase_24 AC 207 ms
57,932 KB
testcase_25 AC 209 ms
57,312 KB
testcase_26 AC 212 ms
57,324 KB
testcase_27 AC 194 ms
56,636 KB
testcase_28 AC 209 ms
57,684 KB
testcase_29 AC 195 ms
57,664 KB
testcase_30 AC 177 ms
56,060 KB
testcase_31 AC 206 ms
57,116 KB
testcase_32 AC 207 ms
56,388 KB
testcase_33 AC 198 ms
57,484 KB
testcase_34 AC 168 ms
56,404 KB
testcase_35 AC 141 ms
55,972 KB
testcase_36 AC 138 ms
55,840 KB
testcase_37 AC 143 ms
55,892 KB
testcase_38 AC 122 ms
55,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.util.*;

public class Q463 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		double[] c = new double[n];
		for (int i = 1; i < n - 1; ++i) {
			c[i] = sc.nextDouble();
		}

		// 魔法を使わずにマスi(0<=i<n-1)からn-1まで行くのに必要な回数の期待値
		double[] dp1 = new double[n];

		// A(Evec+a)=Evec
		// (A-E)x=-Aa
		double[][] mx = new double[n - 1][n - 1];
		for (int i = 0; i < n - 1; ++i) {
			double p = 1.0 / m;
			for (int j = 1; j <= m; ++j) {
				if (i + j < n - 1) {
					mx[i][i + j] += p;
				} else if (i + j == n - 1) {
					continue;
				} else {
					int pos = (n - 1) - ((i + j) - (n - 1));
					mx[i][pos] += p;
				}
			}
		}
		double[] v = new double[n - 1];
		for (int i = 0; i < n - 1; ++i) {
			v[i] = -c[i];
		}
		v = mul(mx, v);
		for (int i = 0; i < n - 1; ++i) {
			mx[i][i] -= 1;
		}

		// [mx]x=vをxについて解きたい.
		for (int i = 0; i < n - 1; ++i) {
			int pos = i;
			while (pos < n - 1 && mx[pos][i] == 0) {
				++pos;
			}
			if (pos == n - 1)
				continue;
			double[] tmp = mx[i];
			mx[i] = mx[pos];
			mx[pos] = tmp;
			double tmp3 = v[i];
			v[i] = v[pos];
			v[pos] = tmp3;
			double tmp2 = mx[i][i];
			for (int j = 0; j < n - 1; ++j) {
				mx[i][j] /= tmp2;
			}
			v[i] /= tmp2;

			for (int j = 0; j < n - 1; ++j) {
				if (mx[j][i] == 0)
					continue;
				if (j == i)
					continue;
				double tmp4 = mx[j][i];
				for (int k = 0; k < n - 1; ++k) {
					mx[j][k] -= mx[i][k] * tmp4;
				}
				v[j] -= v[i] * tmp4;
			}
		}
		double[] e = new double[n];
		Arrays.fill(e, Double.MAX_VALUE);
		e[n - 1] = 0;
		outer: for (int i = n - 2; i >= 0; --i) {
			double e1 = 0;
			for (int j = m; j >= 1; --j) {
				if (i + j >= n - 1) {
					e[i] = 0;
					continue outer;
				} else {
					e1 += 1.0 / m * (e[i + j] + c[i + j]);
				}
			}
			double e2 = 1L << 60;
			for (int j = m; j >= 1; --j) {
				e2 = Math.min(e2, v[i + j] + c[i + j]);
			}
			e[i] = Math.min(e1, e2);
		}
		System.out.printf("%.20f",e[0]);
	}

	static void show(double[][] mx) {
		int n = mx.length;
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				System.out.print(mx[i][j] + " ");
			}
			System.out.println();
		}
	}

	static double[] mul(double[][] a, double[] v) {
		int n = a.length;
		double[] ret = new double[n];
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				ret[i] += a[i][j] * v[j];
			}
		}
		return ret;

	}

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}
0