結果

問題 No.75 回数の期待値の問題
ユーザー nebukuro09nebukuro09
提出日時 2016-12-19 21:08:23
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 18 ms / 5,000 ms
コード長 1,526 bytes
コンパイル時間 3,498 ms
コンパイル使用メモリ 155,656 KB
実行使用メモリ 4,936 KB
最終ジャッジ日時 2023-09-02 23:54:58
合計ジャッジ時間 3,422 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 10 ms
4,380 KB
testcase_18 AC 15 ms
4,936 KB
testcase_19 AC 18 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;


int gaussianElimination(ref real[][] org_mat, ref real[] ret) {
    int N = org_mat.length.to!int;

    auto mat = new real[][](N, N+1);
    foreach (i; 0..N)
        foreach (j; 0..N+1)
            mat[i][j] = org_mat[i][j];

    foreach (i; 0..N) {
        real maxval = 0;
        int maxrow = -1;
        foreach (j; i..N)
            if (abs(mat[j][i]) > maxval)
                maxval = abs(mat[j][i]), maxrow = j;
        if (maxval == 0)
            return -1;
        swap(mat[i], mat[maxrow]);

        foreach (j; i+1..N+1)
            mat[i][j] /= mat[i][i];
        mat[i][i] = 1;

        foreach (j; 0..N) {
            if (j == i) continue;
            real mul = mat[j][i];
            foreach (k; i..N+1)
                mat[j][k] -= mul*mat[i][k];
        }
    }

    foreach (i; 0..N)
        ret[i] = mat[i][N];

    return 0;
}


void main() {
    auto K = readln.chomp.to!int;

    auto mat = new real[][](K, K+1);
    foreach (i; 0..K) foreach(j; 0..K+1) mat[i][j] = 0;
    foreach (i; 0..K) {
        foreach (j; 1..7) {
            auto next = (i+j > K) ? 0 : i+j;
            mat[i][K] -= 1.0L / 6;
            if (i + j != K)
                mat[i][next] += 1.0L/6;
        }
        mat[i][i] -= 1;
    }

    auto E = new real[](K);
    gaussianElimination(mat, E);

    writefln("%.9f", E[0]);
}
0