結果

問題 No.248 ミラー君の宿題
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-04-27 11:30:52
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 953 ms / 5,000 ms
コード長 4,591 bytes
コンパイル時間 2,192 ms
コンパイル使用メモリ 161,100 KB
実行使用メモリ 31,676 KB
最終ジャッジ日時 2023-09-04 00:54:03
合計ジャッジ時間 9,136 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
4,376 KB
testcase_01 AC 17 ms
4,380 KB
testcase_02 AC 22 ms
4,376 KB
testcase_03 AC 27 ms
4,376 KB
testcase_04 AC 61 ms
4,376 KB
testcase_05 AC 36 ms
4,604 KB
testcase_06 AC 70 ms
4,628 KB
testcase_07 AC 140 ms
12,872 KB
testcase_08 AC 14 ms
4,376 KB
testcase_09 AC 528 ms
11,784 KB
testcase_10 AC 528 ms
11,772 KB
testcase_11 AC 139 ms
9,800 KB
testcase_12 AC 307 ms
14,956 KB
testcase_13 AC 484 ms
17,588 KB
testcase_14 AC 818 ms
31,292 KB
testcase_15 AC 894 ms
31,104 KB
testcase_16 AC 953 ms
31,676 KB
testcase_17 AC 80 ms
7,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, 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; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


int N;
long[] P;

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        N = readInt();
        P = new long[N];
        foreach (i; 0 .. N) {
          P[i] = readLong();
        }
        
        debug {
          writeln("P = ", P);
        }
        
        auto prodP = new real[1 << N];
        auto probZero = new real[1 << N];
        auto probNonZero = new real[1 << N];
        foreach (a; 0 .. 1 << N) {
          prodP[a] = 1.0;
          probZero[a] = 1.0;
          probNonZero[a] = 1.0;
          foreach (i; 0 .. N) {
            if ((a >> i) & 1) {
              prodP[a] *= cast(real)(P[i]);
              probZero[a] *= cast(real)(1) / cast(real)(P[i]);
              probNonZero[a] *= cast(real)(P[i] - 1) / cast(real)(P[i]);
            }
          }
        }
        
        auto E = new int[N];
        foreach (i; 0 .. N) {
          E[i] = bsf(P[i] - 1);
        }
        const maxE = E.maxElement;
        auto probPlus = new real[][](1 << N, maxE + 1);
        auto probMinus = new real[][](1 << N, maxE + 1);
        foreach (a; 0 .. 1 << N) {
          probPlus[a][] = 1.0;
          probMinus[a][] = 1.0;
          foreach (k; 0 .. maxE + 1) {
            foreach (i; 0 .. N) {
              if ((a >> i) & 1) {
                probPlus[a][k] *= (cast(real)((P[i] - 1) >> E[i] << min(k, E[i])) / cast(real)(P[i]));
                probMinus[a][k] *= (k < E[i]) ? (cast(real)((P[i] - 1) >> E[i] << k) / cast(real)(P[i])) : 0.0;
              }
            }
          }
        }
        
        auto dp = new real[1 << N];
        foreach (a; 1 .. 1 << N) {
          if (a & (a - 1)) {
            debug {
              if (N <= 3) {
                writeln("a = ", a);
              }
            }
            real nmr = 0.0, dnm = 1.0;
            {
              // P[i] | r for all i
              debug {
                if (N <= 3) {
                  writeln("  ", prodP[a] * probZero[a]);
                }
              }
              dnm -= probZero[a];
            }
            for (int b = a; --b &= a; ) {
              // P[i] | r for i in b
              debug {
                if (N <= 3) {
                  writeln("  b = ", b, ": ", prodP[a] * probZero[b] * probNonZero[a ^ b]);
                }
              }
              nmr += probZero[b] * probNonZero[a ^ b] * (dp[b] + dp[a ^ b]);
            }
            {
              // r^(2^k) == -1 for all i
              real prob = probPlus[a][0];
              foreach (k; 0 .. maxE + 1) {
                prob += probMinus[a][k];
              }
              debug {
                if (N <= 3) {
                  writeln("  ", prodP[a] * prob);
                }
              }
              dnm -= prob;
            }
            for (int b = a; --b &= a; ) {
              // r^(2^k) == -1 (P[i]) for i in b
              real prob = 0.0;
              foreach (k; 0 .. maxE + 1) {
                prob += probMinus[b][k] * probPlus[a ^ b][k];
              }
              debug {
                if (N <= 3) {
                  writeln("  b = ", b, ": ", prodP[a] * prob);
                }
              }
              nmr += prob * (dp[b] + dp[a ^ b]);
            }
            dp[a] = (1.0 + nmr) / dnm;
          } else {
            dp[a] = 1.0;
          }
        }
        writefln("%.9f", dp[$ - 1]);
      }
    }
  } catch (EOFException e) {
  }
}
0