結果

問題 No.302 サイコロで確率問題 (2)
ユーザー te-shte-sh
提出日時 2017-06-12 16:00:10
言語 D
(dmd 2.106.1)
結果
RE  
実行時間 -
コード長 1,823 bytes
コンパイル時間 1,952 ms
コンパイル使用メモリ 149,272 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-03 14:16:26
合計ジャッジ時間 3,243 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;

// allowable-error: 10 ** -12

void main()
{
  auto n = readln.chomp.to!size_t;
  foreach (_; 0..n) {
    auto k = readln.chomp.to!long;
    auto ak = calcAk(k);

    writefln("%.13f", ak[1] / (1 - ak[0]));
  }
}

auto calcAk(T)(T k)
{
  auto p = real(1)/6, u = real(1);

  auto a = [[p,p,p,p,p,p,u],
            [u,0,0,0,0,0,0],
            [0,u,0,0,0,0,0],
            [0,0,u,0,0,0,0],
            [0,0,0,u,0,0,0],
            [0,0,0,0,u,0,0],
            [0,0,0,0,0,0,u]];

  auto i = [[u,0,0,0,0,0,0],
            [0,u,0,0,0,0,0],
            [0,0,u,0,0,0,0],
            [0,0,0,u,0,0,0],
            [0,0,0,0,u,0,0],
            [0,0,0,0,0,u,0],
            [0,0,0,0,0,0,u]];

  auto ak = repeatedSquare!(real[][], matMul)(a, k, i);
  return [ak[0][1..6].sum, ak[0][6]];
}

T[][] matMul(T)(T[][] a, T[][] b)
{
  import std.traits;
  auto l = b.length, m = a.length, n = b[0].length;
  auto c = new T[][](m, n);
  static if (isFloatingPoint!T) {
    foreach (ref r; c) r[] = T(0);
  }
  foreach (i; 0..m)
    foreach (j; 0..n)
      foreach (k; 0..l)
        c[i][j] += a[i][k] * b[k][j];
  return c;
}

T[] matMulVec(T)(T[][] a, T[] b)
{
  import std.traits;
  auto l = b.length, m = a.length;
  auto c = new T[](m);
  static if (isFloatingPoint!T) {
    c[] = T(0);
  }
  foreach (i; 0..m)
    foreach (j; 0..l)
      c[i] += a[i][j] * b[j];
  return c;
}

T repeatedSquare(T, alias pred = "a * b", U)(T a, U n, T init)
{
  import std.functional;
  alias predFun = binaryFun!pred;

  if (n == 0) return init;

  static T[] buf = [];
  if (buf.empty) buf ~= a;

  auto r = init, i = 0;
  while (n > 0) {
    if ((n & 1) == 1)
      r = predFun(r, buf[i]);
    if (buf.length == ++i) buf ~= predFun(buf[$-1], buf[$-1]);
    n >>= 1;
  }

  return r;
}
0