結果
問題 | No.463 魔法使いのすごろく🎲 |
ユーザー | tanzaku |
提出日時 | 2016-12-11 03:27:56 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 217 ms / 2,000 ms |
コード長 | 3,115 bytes |
コンパイル時間 | 2,910 ms |
コンパイル使用メモリ | 85,076 KB |
実行使用メモリ | 43,444 KB |
最終ジャッジ日時 | 2024-05-07 03:15:39 |
合計ジャッジ時間 | 11,357 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 159 ms
41,876 KB |
testcase_01 | AC | 158 ms
41,720 KB |
testcase_02 | AC | 155 ms
42,028 KB |
testcase_03 | AC | 137 ms
41,444 KB |
testcase_04 | AC | 163 ms
42,372 KB |
testcase_05 | AC | 176 ms
42,356 KB |
testcase_06 | AC | 147 ms
41,612 KB |
testcase_07 | AC | 159 ms
42,400 KB |
testcase_08 | AC | 154 ms
41,820 KB |
testcase_09 | AC | 192 ms
42,572 KB |
testcase_10 | AC | 153 ms
41,656 KB |
testcase_11 | AC | 178 ms
41,952 KB |
testcase_12 | AC | 163 ms
42,044 KB |
testcase_13 | AC | 146 ms
41,372 KB |
testcase_14 | AC | 188 ms
42,440 KB |
testcase_15 | AC | 183 ms
42,152 KB |
testcase_16 | AC | 148 ms
42,064 KB |
testcase_17 | AC | 186 ms
42,548 KB |
testcase_18 | AC | 149 ms
41,776 KB |
testcase_19 | AC | 151 ms
41,768 KB |
testcase_20 | AC | 148 ms
41,768 KB |
testcase_21 | AC | 201 ms
42,564 KB |
testcase_22 | AC | 187 ms
42,720 KB |
testcase_23 | AC | 206 ms
43,140 KB |
testcase_24 | AC | 217 ms
43,332 KB |
testcase_25 | AC | 206 ms
43,444 KB |
testcase_26 | AC | 198 ms
42,956 KB |
testcase_27 | AC | 199 ms
42,760 KB |
testcase_28 | AC | 186 ms
42,516 KB |
testcase_29 | AC | 199 ms
42,420 KB |
testcase_30 | AC | 185 ms
42,764 KB |
testcase_31 | AC | 190 ms
42,364 KB |
testcase_32 | AC | 191 ms
42,972 KB |
testcase_33 | AC | 208 ms
42,952 KB |
testcase_34 | AC | 196 ms
42,524 KB |
testcase_35 | AC | 148 ms
42,052 KB |
testcase_36 | AC | 149 ms
41,732 KB |
testcase_37 | AC | 151 ms
42,076 KB |
testcase_38 | AC | 132 ms
40,972 KB |
ソースコード
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; } }