結果

問題 No.301 サイコロで確率問題 (1)
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-18 07:37:27
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 27 ms / 1,000 ms
コード長 2,854 bytes
コンパイル時間 2,073 ms
コンパイル使用メモリ 164,728 KB
実行使用メモリ 10,944 KB
最終ジャッジ日時 2023-09-03 22:17:29
合計ジャッジ時間 2,656 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
10,476 KB
testcase_01 AC 27 ms
10,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken().to!int; }
long readLong() { return readToken().to!long; }
real readReal() { return readToken().to!real; }

void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); }


alias P = Tuple!(real[], real);

P multiply(P a, P b) {
  P ret = tuple(new real[11], 0.0);
  ret[0][] = 0.0;
  foreach (i; 0 .. 6) foreach (j; 0 .. 6) {
    ret[0][i + j] += a[0][i] * b[0][j];
  }
  ret[1] = a[0].sum * b[1] + a[1];
  foreach_reverse (i; 6 .. 11) {
    foreach (j; 1 .. 7) {
      ret[0][i - j] += ret[0][i] / 6.0;
    }
    ret[1] += ret[0][i];
  }
  ret[0].length = 6;
  return ret;
}

long N;

void main() {
  enum LIM = 10^^5;
  auto x = new real[LIM];
  auto y = new real[LIM];
  x[] = 0.0;
  y[] = 0.0;
  foreach (i; 1 .. LIM) {
    x[i] += 1.0;
    foreach (j; 1 .. 7) {
      if (i - j >= 0) {
        x[i] += x[i - j] / 6.0;
        y[i] += y[i - j] / 6.0;
      } else {
        y[i] += 1.0 / 6.0;
      }
    }
  }
  
  try {
    for (; ; ) {
      const T = readInt();
      foreach (caseId; 0 .. T) {
        N = readLong();
        /*
        P a = tuple(new real[6], 0.0);
        a[0][] = 0.0;
        a[0][0] = 1.0;
        foreach_reverse (h; 0 .. bsr(N) + 1) {
          a = multiply(a, a);
          if ((N >> h) & 1) {
            a[0] = 0.0 ~ a[0];
            foreach (i; 0 .. 6) {
              a[0][i] += a[0][6] / 6.0;
            }
            a[1] += a[0][6];
            a[0].length = 6;
          }
        }
        debug {
          writeln(a);
        }
        real xx = a[1], yy = 0.0;
        foreach (i; 0 .. 6) {
          xx += a[0][i] * x[i];
          yy += a[0][i] * y[i];
        }
        debug {
          writefln("%.20f %.20f", xx, yy);
        }
        const ans = xx / (1.0 - yy);
        */
        real ans;
        if (N >= LIM) {
          ans = N + 5.0 / 3.0;
        } else {
          ans = x[cast(size_t)(N)] / (1.0 - y[cast(size_t)(N)]);
        }
        writefln("%.20f", ans);
      }
    }
  } catch (EOFException e) {
  }
}
0