結果

問題 No.425 ジャンケンの必勝法
ユーザー 37zigen37zigen
提出日時 2016-09-23 03:07:33
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,556 bytes
コンパイル時間 4,043 ms
コンパイル使用メモリ 82,064 KB
実行使用メモリ 56,148 KB
最終ジャッジ日時 2024-04-28 20:33:02
合計ジャッジ時間 8,325 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
49,888 KB
testcase_01 AC 132 ms
41,272 KB
testcase_02 AC 133 ms
41,280 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 131 ms
41,232 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 120 ms
40,084 KB
testcase_16 AC 134 ms
41,396 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 134 ms
41,416 KB
testcase_22 AC 136 ms
41,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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;
		}
		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);
			m[i][Math.max(0, idx1)] -= list.get(i) / 100.0 * 0.5;

			int idx2 = list.indexOf(list.get(i) + q);
			m[i][Math.max(n - 1, 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));
	}
}
0