結果

問題 No.463 魔法使いのすごろく🎲
ユーザー tanzakutanzaku
提出日時 2016-12-11 03:27:56
言語 Java21
(openjdk 21)
結果
AC  
実行時間 172 ms / 2,000 ms
コード長 3,115 bytes
コンパイル時間 4,965 ms
コンパイル使用メモリ 81,180 KB
実行使用メモリ 58,692 KB
最終ジャッジ日時 2023-08-19 20:20:05
合計ジャッジ時間 9,668 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
55,912 KB
testcase_01 AC 126 ms
56,284 KB
testcase_02 AC 125 ms
55,992 KB
testcase_03 AC 136 ms
55,952 KB
testcase_04 AC 132 ms
56,048 KB
testcase_05 AC 148 ms
56,304 KB
testcase_06 AC 119 ms
56,080 KB
testcase_07 AC 127 ms
56,256 KB
testcase_08 AC 125 ms
56,064 KB
testcase_09 AC 157 ms
56,848 KB
testcase_10 AC 124 ms
56,260 KB
testcase_11 AC 146 ms
56,076 KB
testcase_12 AC 132 ms
56,232 KB
testcase_13 AC 117 ms
55,992 KB
testcase_14 AC 149 ms
56,756 KB
testcase_15 AC 154 ms
56,472 KB
testcase_16 AC 118 ms
55,824 KB
testcase_17 AC 150 ms
56,416 KB
testcase_18 AC 118 ms
55,828 KB
testcase_19 AC 127 ms
56,204 KB
testcase_20 AC 118 ms
56,060 KB
testcase_21 AC 166 ms
56,368 KB
testcase_22 AC 162 ms
56,512 KB
testcase_23 AC 171 ms
56,516 KB
testcase_24 AC 165 ms
56,476 KB
testcase_25 AC 158 ms
56,380 KB
testcase_26 AC 165 ms
56,204 KB
testcase_27 AC 164 ms
56,456 KB
testcase_28 AC 163 ms
56,356 KB
testcase_29 AC 163 ms
56,464 KB
testcase_30 AC 168 ms
56,436 KB
testcase_31 AC 158 ms
56,476 KB
testcase_32 AC 172 ms
56,148 KB
testcase_33 AC 170 ms
56,484 KB
testcase_34 AC 159 ms
58,692 KB
testcase_35 AC 122 ms
55,836 KB
testcase_36 AC 124 ms
55,916 KB
testcase_37 AC 130 ms
56,280 KB
testcase_38 AC 112 ms
56,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	static final Random random = new Random(0);
	public static void main(String[] args) {
		try (final Scanner in = new Scanner(System.in)) {
			int n = in.nextInt();
			int m = in.nextInt();
			double[] c = new double[n];

			if (!(n >= 2 && n <= 100)) throw new RuntimeException();
			if (!(m >= 1 && m < n)) throw new RuntimeException();
			
			for (int i = 1; i < n - 1; i++) {
				c[i] = in.nextDouble();
//				c[i] = random.nextInt(1000000000 + 1);
				if (!(c[i] >= 0 && c[i] <= 1e9)) throw new RuntimeException();
			}
			
			double[][] A = new double[n-1][n-1];
			double[][] C = new double[n-1][1];
			for (int i = 0; i < n - 1; i++) {
				for (int j = 1; j <= m; j++) {
					int t = i + j;
					if (t == n - 1) continue;
					if (t >= n) t = n - 2 - (t - n);
					A[i][t] -= 1.0 / m;
				}
				A[i][i] += 1;
				C[i][0] = c[i];
			}
			
			double[][] E0 = mulmat(inverse(A), C);
			double[] ans = new double[n];

			for (int i = n - 1; i >= 0; i--) {
				if (i + m >= n - 1) {
					ans[i] = 0;
					continue;
				}

				double jump = Double.POSITIVE_INFINITY;
				double e = 0;
				for (int j = 1; j <= m; j++) {
					e += (ans[i+j] + c[i+j]) / m;
					jump = Math.min(jump, E0[i+j][0]);
				}
				ans[i] = Math.min(e, jump);
			}
//			dump(c);
//			dump(A);
//			dump(inverse(A));
//			dump(E0);
//			dump(ans);
			System.out.printf("%.10f\n", ans[0]);
		}
	}
	
	static void dump(Object... o) {
		System.err.println(Arrays.deepToString(o));
	}
	
	static <T> void swapRow(T[] ary, int i, int j) {
		final T x = ary[i];
		ary[i] = ary[j];
		ary[j] = x;
	}
	static boolean inverseProcess(double[][] tmp) {
		final int n = tmp.length;
		for(int i = 0; i < n; i++) {
			int r = i;
			for(int j = i + 1; j < n; j++) {
				if(Math.abs(tmp[r][i]) < Math.abs(tmp[j][i])) {
					r = j;
				}
			}
			
			if(i != r) {
				swapRow(tmp, i, r);
			}
			
			if(Math.abs(tmp[i][i]) <= 1e-10) {
				throw new RuntimeException();
			}
			
			for(int j = tmp[i].length - 1; j >= i; j--) {
				tmp[i][j] /= tmp[i][i];
			}
			
			for(int j = 0; j < n; j++) if(i != j) {
				for(int k = tmp[j].length - 1; k >= i; k--) {
					tmp[j][k] -= tmp[j][i] * tmp[i][k];
				}
			}
		}
		return true;
	}
	
	// 逆行列を求める
	static double[][] inverse(double[][] m) {
		final int n = m.length;
		assert(n != 0 && m[0].length == n);
		final double[][] tmp = new double[n][2*n];
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				tmp[i][j] = m[i][j];
			}
			tmp[i][i + n] = 1;
		}
		
		inverseProcess(tmp);
		
		final double[][] ret = new double[n][n];
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				ret[i][j] = tmp[i][j + n];
			}
		}
		
		return ret;
	}
	
	// a [n,v] * b [v,m] => c[n,m]
	static double[][] mulmat(double[][] a, double[][] b) {
		assert(a[0].length == b.length);
		
		final int n = a.length;
		final int v = b.length;
		final int m = b[0].length;
		
		double[][] res = new double[n][m];
		for(int i = 0; i < n; i++)
			for(int j = 0; j < m; j++)
				for(int k = 0; k < v; k++)
				res[i][j] += a[i][k] * b[k][j];
		
		return res;
	}
}
0