結果

問題 No.65 回数の期待値の練習
ユーザー ぴろずぴろず
提出日時 2014-12-12 09:38:38
言語 Java21
(openjdk 21)
結果
AC  
実行時間 128 ms / 5,000 ms
コード長 2,247 bytes
コンパイル時間 2,202 ms
コンパイル使用メモリ 73,852 KB
実行使用メモリ 56,144 KB
最終ジャッジ日時 2023-09-02 14:17:04
合計ジャッジ時間 6,094 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,656 KB
testcase_01 AC 126 ms
55,852 KB
testcase_02 AC 128 ms
56,144 KB
testcase_03 AC 125 ms
55,352 KB
testcase_04 AC 123 ms
55,876 KB
testcase_05 AC 126 ms
55,424 KB
testcase_06 AC 126 ms
55,988 KB
testcase_07 AC 126 ms
55,756 KB
testcase_08 AC 126 ms
55,780 KB
testcase_09 AC 127 ms
55,556 KB
testcase_10 AC 127 ms
55,604 KB
testcase_11 AC 127 ms
55,720 KB
testcase_12 AC 127 ms
55,812 KB
testcase_13 AC 128 ms
55,700 KB
testcase_14 AC 127 ms
55,532 KB
testcase_15 AC 127 ms
55,980 KB
testcase_16 AC 124 ms
53,676 KB
testcase_17 AC 126 ms
56,104 KB
testcase_18 AC 127 ms
55,380 KB
testcase_19 AC 126 ms
55,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no065;

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][k];
		double[] b = new double[k];
		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] = - 1 / 6D;
				}
			}
		}
		System.out.println(Matrix.solveEquation(a, b)[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