結果
問題 | No.425 ジャンケンの必勝法 |
ユーザー | 37zigen |
提出日時 | 2016-09-23 03:53:03 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 165 ms / 2,000 ms |
コード長 | 3,698 bytes |
コンパイル時間 | 3,600 ms |
コンパイル使用メモリ | 82,208 KB |
実行使用メモリ | 42,636 KB |
最終ジャッジ日時 | 2024-11-17 19:51:25 |
合計ジャッジ時間 | 7,436 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 114 ms
41,604 KB |
testcase_01 | AC | 125 ms
41,472 KB |
testcase_02 | AC | 117 ms
41,312 KB |
testcase_03 | AC | 124 ms
41,700 KB |
testcase_04 | AC | 115 ms
41,060 KB |
testcase_05 | AC | 110 ms
41,424 KB |
testcase_06 | AC | 122 ms
41,280 KB |
testcase_07 | AC | 112 ms
40,064 KB |
testcase_08 | AC | 116 ms
41,500 KB |
testcase_09 | AC | 115 ms
40,156 KB |
testcase_10 | AC | 107 ms
39,948 KB |
testcase_11 | AC | 165 ms
42,616 KB |
testcase_12 | AC | 164 ms
42,008 KB |
testcase_13 | AC | 164 ms
42,236 KB |
testcase_14 | AC | 161 ms
42,452 KB |
testcase_15 | AC | 116 ms
41,500 KB |
testcase_16 | AC | 128 ms
41,556 KB |
testcase_17 | AC | 165 ms
42,452 KB |
testcase_18 | AC | 121 ms
41,572 KB |
testcase_19 | AC | 126 ms
41,192 KB |
testcase_20 | AC | 160 ms
42,636 KB |
testcase_21 | AC | 122 ms
41,344 KB |
testcase_22 | AC | 111 ms
40,152 KB |
ソースコード
package No400番台; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class C { public static void main(String[] args) { new C().run(); } void run() { solver(); } double EPS = 1e-6; void solver() { Scanner sc = new Scanner(System.in); int p = sc.nextInt(); int q = sc.nextInt(); ArrayList<Integer> list = new ArrayList<>(); int tmp = p; if (q != 0) { while (tmp != 100) { tmp = Math.min(100, tmp + q); list.add(tmp); } tmp = p; while (tmp != 0) { tmp = Math.max(0, tmp - q); list.add(tmp); } tmp = 0; while (tmp != 100) { tmp = Math.min(100, tmp + q); list.add(tmp); } tmp = 100; while (tmp != 0) { tmp = Math.max(0, tmp - q); list.add(tmp); } } list.add(p); list.sort(null); int idx = -1; for (int i = 0; i < list.size(); i++) { if (Math.abs(list.get(i) - p) < EPS) idx = i; while (i + 1 < list.size() && Math.abs(list.get(i) - list.get(i + 1)) < EPS) { list.remove(i + 1); } } int n = list.size(); double[][] vec = new double[n][1]; double[][] m = new double[n][n]; for (int i = 0; i < n; i++) { m[i][i] = 1.0; } for (int i = 0; i < n; i++) { vec[i][0] += list.get(i) / 100.0 * 0.5 + (100 - list.get(i)) / 100.0 * 1.0 / 3.0; int idx1 = list.indexOf(list.get(i) - q); if (idx1 == -1) m[i][0] -= list.get(i) / 100.0 * 0.5; else m[i][Math.max(0, idx1)] -= list.get(i) / 100.0 * 0.5; int idx2 = list.indexOf(list.get(i) + q); if (idx2 == -1) m[i][n - 1] -= (100 - list.get(i)) / 100.0 * 1.0 / 3.0; else m[i][idx2] -= (100 - list.get(i)) / 100.0 * 1.0 / 3.0; } m = Rev(m); vec = MtPrd(m, vec); System.out.println(1.0 / 3.0 + 1.0 / 3.0 * vec[idx][0]); } public static double[][] Rev(double[][] OM) { int n = OM.length, m = OM[0].length; if (n != m) return null; double[][] M = new double[n][2 * n]; m = 2 * n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { M[i][j] = OM[i][j]; } M[i][n + i] = 1; } double[][] res = operateElementarily(M); if (res == null) return null; // resotration double[][] ret = new double[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ret[i][j] = res[i][j + n]; } } return ret; } public static double[][] operateElementarily(double[][] M) { int n = M.length, m = M[0].length; int rank = n - 1; // Forward Elimination for (int i = 0; i < n; i++) { // select pivot double max = 1E-9; int maxj = -1; for (int j = i; j < n; j++) { double v = Math.abs(M[j][i]); if (v > max) { max = v; maxj = j; } } if (maxj == -1) { rank = i - 1; break; } if (maxj != i) { double[] dum = M[i]; M[i] = M[maxj]; M[maxj] = dum; } double D = M[i][i]; M[i][i] = 1; for (int j = i + 1; j < m; j++) { M[i][j] /= D; } for (int j = i + 1; j < n; j++) { double B = -M[j][i]; M[j][i] = 0; for (int k = i + 1; k < m; k++) { M[j][k] += M[i][k] * B; } } } // Back Substitution for (int i = rank; i >= 0; i--) { for (int j = rank; j >= i + 1; j--) { double B = -M[i][j]; M[i][j] = 0; for (int k = rank + 1; k < m; k++) { M[i][k] += B * M[j][k]; } } } return M; } double[][] MtPrd(double[][] A, double[][] B) { double[][] C = new double[A.length][B[0].length]; for (int i = 0; i < A.length; i++) { for (int j = 0; j < B[0].length; j++) { for (int k = 0; k < A[0].length; k++) { C[i][j] += A[i][k] * B[k][j]; } } } return C; } void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }