結果

問題 No.463 魔法使いのすごろく🎲
ユーザー nebukuro09nebukuro09
提出日時 2016-12-17 17:13:30
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 2,041 bytes
コンパイル時間 2,268 ms
コンパイル使用メモリ 159,584 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-02 23:37:54
合計ジャッジ時間 3,648 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,384 KB
testcase_14 AC 4 ms
4,380 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 4 ms
4,376 KB
testcase_22 AC 4 ms
4,380 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 4 ms
4,380 KB
testcase_25 AC 4 ms
4,380 KB
testcase_26 AC 4 ms
4,380 KB
testcase_27 AC 4 ms
4,376 KB
testcase_28 AC 4 ms
4,380 KB
testcase_29 AC 4 ms
4,380 KB
testcase_30 AC 4 ms
4,380 KB
testcase_31 AC 4 ms
4,380 KB
testcase_32 AC 4 ms
4,380 KB
testcase_33 AC 4 ms
4,380 KB
testcase_34 AC 4 ms
4,380 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 2 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 s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto C = [0] ~ readln.split.map!(to!int).array ~ [0];

    auto mat = new real[][](N, N+1);
    foreach (i; 0..N) foreach(j; 0..N+1) mat[i][j] = 0;
    mat[N-1][N-1] = -1;
    foreach (i; 0..N-1) {
        foreach (j; 1..M+1) {
            auto next = (i+j < N) ? i+j : 2*N-i-j-2;
            mat[i][next] += 1.0L / M;
        }
        mat[i][i] -= 1;
        mat[i][N] = -C[i];
    }
            
    auto E = new real[](N);
    gaussianElimination(mat, E);

    auto dp = new real[](N);
    foreach (i; iota(N-1, -1, -1)) {
        if (i+M >= N-1)
            dp[i] = C[i];
        else {
            // mahou tsukawanai
            dp[i] = C[i];
            foreach (j; 1..M+1)
                dp[i] += dp[i+j] / M;
            // tsukau
            foreach (j; 1..M+1)
                dp[i] = min(dp[i], C[i]+E[i+j]);
        }
        
    }

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

0