結果

問題 No.75 回数の期待値の問題
ユーザー ぴろずぴろず
提出日時 2014-12-11 12:42:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 164 ms / 5,000 ms
コード長 2,323 bytes
コンパイル時間 2,747 ms
コンパイル使用メモリ 73,936 KB
実行使用メモリ 57,032 KB
最終ジャッジ日時 2023-09-02 13:58:37
合計ジャッジ時間 6,065 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
55,860 KB
testcase_01 AC 124 ms
56,040 KB
testcase_02 AC 124 ms
55,988 KB
testcase_03 AC 123 ms
55,980 KB
testcase_04 AC 124 ms
55,632 KB
testcase_05 AC 124 ms
55,708 KB
testcase_06 AC 122 ms
55,564 KB
testcase_07 AC 123 ms
55,412 KB
testcase_08 AC 124 ms
55,412 KB
testcase_09 AC 126 ms
55,984 KB
testcase_10 AC 124 ms
55,984 KB
testcase_11 AC 124 ms
56,092 KB
testcase_12 AC 123 ms
55,864 KB
testcase_13 AC 126 ms
55,604 KB
testcase_14 AC 125 ms
55,704 KB
testcase_15 AC 124 ms
55,824 KB
testcase_16 AC 157 ms
56,740 KB
testcase_17 AC 159 ms
56,788 KB
testcase_18 AC 159 ms
56,724 KB
testcase_19 AC 164 ms
57,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no076;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		double[][] a = new double[k+1][k+1];
		double[] b = new double[k+1];
		for(int i=0;i<k;i++) {
			a[i][i] = 1;
			b[i] = 1;
			for(int j=1;j<=6;j++) {
				if (i + j <= k) {
					a[i][i+j] -= 1D / 6;
				}else{
					a[i][0] -= 1D / 6;
				}
			}
		}
		a[k][k] = 1;
		double[] x = Matrix.solveEquation(a, b);
		System.out.println(x[0]);
	}

}
class Matrix {
	public static final double EPS = 1E-8;
	public static double[][] add(double[][] a,double[][] b) {
		int n = a.length;
		int m = a[0].length;
		double[][] c = new double[n][m];
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				c[i][j] = a[i][j] + b[i][j];
			}
		}
		return c;
	}
	public static double[][] muliply(double[][] a,double k) {
		int n = a.length;
		int m = a[0].length;
		double[][] c = new double[n][m];
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				c[i][j] = a[i][j] * k;
			}
		}
		return c;
	}
	public static double[][] muliply(double[][] a,double[][] b) {
		int n = a.length;
		int m = b[0].length;
		double[][] c = new double[n][m];
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				for(int k=0;k<n;k++) {
					c[i][j] += a[i][k] * b[k][j];
				}
			}
		}
		return c;
	}
	//disruptive
	public static double[] solveEquation(double[][] A,double[] b) {
		int n = A.length;
		double[][] B = new double[n][n+1];
		for(int i=0;i<n;i++) {
			for(int j=0;j<n;j++) {
				B[i][j] = A[i][j];
			}
			B[i][n] = b[i];
		}
		for(int i=0;i<n;i++) {
			int pivot = i;
			for(int j=i;j<n;j++) {
				if (Math.abs(B[j][i]) > Math.abs(B[pivot][i])) {
					pivot = j;
				}

			}
			swapRow(B, i, pivot);
			if (Math.abs(B[i][i]) < EPS) {
				return null;
			}
			for(int j=i+1;j<=n;j++) {
				B[i][j] /= B[i][i];
			}
			for(int j=0;j<n;j++) {
				if (i==j) {
					continue;
				}
				for(int k=i+1;k<=n;k++) {
					B[j][k] -= B[j][i] * B[i][k];
				}
			}
		}
		double[] x = new double[n];
		for(int i=0;i<n;i++) {
			x[i] = B[i][n];
		}
		return x;
	}
	public static void swapRow(double[][] A,int i,int j) {
		double[] temp = A[i];
		A[i] = A[j];
		A[j] = temp;
	}
}
0