結果

問題 No.108 トリプルカードコンプ
ユーザー しらっ亭しらっ亭
提出日時 2015-08-08 23:24:20
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 413 ms / 5,000 ms
コード長 831 bytes
コンパイル時間 98 ms
コンパイル使用メモリ 10,756 KB
実行使用メモリ 40,280 KB
最終ジャッジ日時 2023-09-25 07:21:24
合計ジャッジ時間 3,147 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,516 KB
testcase_01 AC 19 ms
8,596 KB
testcase_02 AC 20 ms
8,492 KB
testcase_03 AC 20 ms
8,592 KB
testcase_04 AC 18 ms
8,492 KB
testcase_05 AC 19 ms
8,644 KB
testcase_06 AC 19 ms
8,488 KB
testcase_07 AC 413 ms
40,280 KB
testcase_08 AC 27 ms
9,304 KB
testcase_09 AC 19 ms
8,544 KB
testcase_10 AC 19 ms
8,488 KB
testcase_11 AC 19 ms
8,524 KB
testcase_12 AC 19 ms
8,520 KB
testcase_13 AC 43 ms
10,416 KB
testcase_14 AC 28 ms
9,052 KB
testcase_15 AC 22 ms
8,700 KB
testcase_16 AC 35 ms
9,808 KB
testcase_17 AC 21 ms
8,564 KB
testcase_18 AC 305 ms
26,064 KB
testcase_19 AC 102 ms
13,776 KB
testcase_20 AC 51 ms
10,912 KB
testcase_21 AC 200 ms
24,264 KB
testcase_22 AC 27 ms
8,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict


def solve():
    N = int(input())
    A = list(map(int, input().split()))

    n3 = n2 = n1 = 0
    for a in A:
        if a == 0:
            n3 += 1
        elif a == 1:
            n2 += 1
        elif a == 2:
            n1 += 1

    memo = defaultdict(int)

    def rec(i, j, k):
        if i < 0 or j < 0 or k < 0:
            return 0

        ijk = (i + j + k)
        if ijk == 0:
            return 0

        if (i, j, k) in memo:
            return memo[i, j, k]

        ans = (
            N / ijk +
            rec(i, j, k - 1) * k / ijk +
            rec(i, j - 1, k + 1) * j / ijk +
            rec(i - 1, j + 1, k) * i / ijk
        )
        memo[i, j, k] = ans
        return ans

    print(rec(n3, n2, n1))


def main():
    solve()


if __name__ == '__main__':
    main()
0