結果

問題 No.174 カードゲーム(Hard)
ユーザー nohtaraynohtaray
提出日時 2019-06-15 00:48:12
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 2,464 bytes
コンパイル時間 118 ms
コンパイル使用メモリ 11,192 KB
実行使用メモリ 9,928 KB
最終ジャッジ日時 2023-08-09 20:36:57
合計ジャッジ時間 1,300 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 -
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
import os
from collections import Counter, deque
from fractions import gcd
from functools import lru_cache
from functools import reduce
import functools
import heapq
import itertools
import math
import numpy as np
import re
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")

N, PA, PB = sys.stdin.readline().rstrip().split()
A = list(map(int, sys.stdin.readline().split()))
B = list(map(int, sys.stdin.readline().split()))
N = int(N)
PA = float(PA)
PB = float(PB)
A.sort()
B.sort()


def probabilities(cards, first_p):
    """
    :param list of int cards:
    :param float first_p: 最初のカードを選ぶ確率
    :return:
    """
    cards.sort()
    # ret[i][j]: i 試合目で cards[j] を出す確率
    ret = np.zeros((N, N))
    # dp[bit]: 持ってるカードが bit になる確率
    # 左から i 番目のビットが立ってたら cards[i] が手持ちにある
    # dp する過程で ret を計算する
    dp = np.zeros(1 << N)
    # 初期
    dp[(1 << N) - 1] = 1
    states = np.array([(1 << N) - 1])
    for step in range(N):
        nexts = np.zeros(1 << N, dtype=bool)
        last = step == N - 1
        for b in range(N):
            mask = 1 << b
            # mask が立ってるやつ
            sts = states[np.where(np.bitwise_and(states, mask))]
            # mask 以下が全部ゼロ == mask の桁が一番下
            firsts_i = np.bitwise_and(sts, mask - 1) == 0
            # つぎ
            next_sts = np.bitwise_xor(sts, mask)
            nexts[next_sts] = 1

            if last:
                p = dp[sts]
                dp[next_sts] += p
                ret[step][b] += p
            else:
                p1 = dp[sts[firsts_i]] * first_p
                dp[next_sts[firsts_i]] += p1
                p2 = dp[sts[~firsts_i]] * (1 - first_p) / (N - step - 1)
                dp[next_sts[~firsts_i]] += p2
                ret[step][b] += sum(p1) + sum(p2)

        states = np.arange(0, 1 << N)[nexts]
    return ret


np.set_printoptions(suppress=True)

AP = probabilities(cards=A, first_p=PA)
BP = probabilities(cards=B, first_p=PB)

A = np.array(A)
B = np.array(B)
Ai, Bi = list(zip(*itertools.product(range(N), repeat=2)))
Ai = np.array(Ai)
Bi = np.array(Bi)
ai = Ai[A[Ai] > B[Bi]]
bi = Bi[A[Ai] > B[Bi]]
ans = 0
for step in range(N):
    ans += sum((A[ai] + B[bi]) * AP[step][ai] * BP[step][bi])
print(ans)
0