結果

問題 No.174 カードゲーム(Hard)
ユーザー te-shte-sh
提出日時 2017-05-01 15:37:40
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 439 ms / 2,000 ms
コード長 1,857 bytes
コンパイル時間 2,138 ms
コンパイル使用メモリ 158,392 KB
実行使用メモリ 39,520 KB
最終ジャッジ日時 2023-09-03 13:07:29
合計ジャッジ時間 6,916 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,356 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 439 ms
38,188 KB
testcase_03 AC 439 ms
39,520 KB
testcase_04 AC 439 ms
37,392 KB
testcase_05 AC 438 ms
36,620 KB
testcase_06 AC 438 ms
36,896 KB
testcase_07 AC 439 ms
38,460 KB
testcase_08 AC 439 ms
38,116 KB
testcase_09 AC 437 ms
36,312 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

void main()
{
  auto rd = readln.split, n = rd[0].to!size_t, pa = rd[1].to!real, pb = rd[2].to!real;
  auto ai = readln.split.to!(int[]); ai.sort();
  auto bi = readln.split.to!(int[]); bi.sort();

  auto paij = calc(n, ai, pa);
  auto pbij = calc(n, bi, pb);

  auto r = real(0);
  foreach (i; 0..n)
    foreach (ja; 0..n)
      foreach (jb; 0..n)
        if (ai[ja] > bi[jb])
          r += paij[ja][i] * pbij[jb][i] * (ai[ja] + bi[jb]);

  writefln("%.10f", r);
}

auto calc(size_t n, int[] ai, real p)
{
  auto dp = new real[](1 << n), pij = new real[][](n, n);
  dp[$-1] = 1; dp[0..$-1] = 0;
  foreach (ref pi; pij) pi[] = 0;

  foreach_reverse (i; 1..(1 << n)) {
    auto f = i.bsf, r = i.bsr, c = i.popcnt;

    auto pf = dp[i] * (c == 1 ? 1 : p);
    dp[i.bitComp(f)] += pf;
    pij[f][n - c] += pf;

    foreach (j; f+1..r+1)
      if (i.bitTest(j)) {
        auto pj = dp[i] * (1 - p) / (c - 1);
        dp[i.bitComp(j)] += pj;
        pij[j][n - c] += pj;
      }
  }

  return pij;
}

pragma(inline) {
  pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; }
  pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); }
  pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); }
  pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); }

  pure T bitSet(T)(T n, size_t s, size_t e) { return n | ((T(1) << e) - 1) & ~((T(1) << s) - 1); }
  pure T bitReset(T)(T n, size_t s, size_t e) { return n & (~((T(1) << e) - 1) | ((T(1) << s) - 1)); }
  pure T bitComp(T)(T n, size_t s, size_t e) { return n ^ ((T(1) << e) - 1) & ~((T(1) << s) - 1); }

  import core.bitop;
  pure int bsf(T)(T n) { return core.bitop.bsf(ulong(n)); }
  pure int bsr(T)(T n) { return core.bitop.bsr(ulong(n)); }
  pure int popcnt(T)(T n) { return core.bitop.popcnt(ulong(n)); }
}
0