結果

問題 No.302 サイコロで確率問題 (2)
ユーザー rpy3cpprpy3cpp
提出日時 2015-11-14 00:58:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 285 ms / 6,000 ms
コード長 1,751 bytes
コンパイル時間 291 ms
コンパイル使用メモリ 87,296 KB
実行使用メモリ 78,936 KB
最終ジャッジ日時 2023-10-11 17:33:34
合計ジャッジ時間 3,797 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 226 ms
78,432 KB
testcase_01 AC 99 ms
72,008 KB
testcase_02 AC 97 ms
71,660 KB
testcase_03 AC 285 ms
78,936 KB
testcase_04 AC 96 ms
71,808 KB
testcase_05 AC 96 ms
71,976 KB
testcase_06 AC 97 ms
72,088 KB
testcase_07 AC 96 ms
72,004 KB
testcase_08 AC 96 ms
72,084 KB
testcase_09 AC 95 ms
71,808 KB
testcase_10 AC 96 ms
72,020 KB
testcase_11 AC 96 ms
72,016 KB
testcase_12 AC 96 ms
72,016 KB
testcase_13 AC 95 ms
71,860 KB
testcase_14 AC 96 ms
71,684 KB
testcase_15 AC 96 ms
71,864 KB
testcase_16 AC 98 ms
71,820 KB
testcase_17 AC 96 ms
72,200 KB
testcase_18 AC 96 ms
71,756 KB
testcase_19 AC 97 ms
71,596 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections
import math

def solve(N, L, R):
    if R < N:
        return 0
    if L > 6 * N:
        return 0
    if L < N and R > 6 * N:
        return 1
    if N < 1800:  # テストケースみて決めました。
        return solve_exact(N, L, R)
    else:
        return solve_approximate(N, L, R)

def solve_exact(N, L, R):
    fs = {0: 1}
    for n in range(N-1, -1, -1):
        nfs = collections.defaultdict(float)
        for v, p in fs.items():
            if p < 0.1**6/(5 * (N - n)):  # p が十分に小さいときは無視する。
                continue
            for dice in range(1, 7):
                nv = v + dice
                if nv + n <= R and nv + n * 6 >= L:  # [L, R] に入る見込みがないときは枝刈り
                    nfs[nv] += p/6
        fs = nfs
    cum = 0.0
    for v, p in fs.items():
        if L <= v and v <= R:
            cum += p
    return cum


def solve_approximate(N, L, R):
    '''正規分布で近似する。
    '''
    E = N * 3.5          # さいころを N回振ったときの目の和の期待値
    Var = N * 35.0/12.0  # さいころを N回振ったときの目の和の分散 = 1回さいころを振ったときの目の分散 * N
    if R >= N * 6:
        pR = 1
    elif R < N:
        pR = 0
    else:
        r = (R + 0.5 - E)/math.sqrt(Var)
        pR = phi(r)
    if L >= N * 6:
        pL = 1
    elif L < N:
        pL = 0
    else:       
        l = (L - 0.5 - E)/math.sqrt(Var)
        pL = phi(l)
    return pR - pL
                

def phi(x):
    '''標準正規分布の累積分布関数
    '''
    return (1.0 + math.erf(x / math.sqrt(2.0))) / 2.0


N = int(input())
L, R = map(int, input().split())
print('{:f}'.format(solve(N, L, R)))
0