0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

TopCoder SRM802 Div.1East DisjointDiceValues 確率: さいころをn回降ってr種類出る数、a回とb回振って目の重複のない

Last updated at Posted at 2021-03-31

2007 神戸大・文理(後)

さいころを n 回投げたとき、出た目の数が3種類になる確率を求めよ。

の強化です。クーポンコレクター問題の親戚。

題意

  • Aさんがa個のさいころとBさんがb個のさいころを振ったとき, AさんとBさんで1つも同じ目が出る確率を求めよ
    • 例えば[1,1,3]と[2,3,4]なら3が出ているのでOK。[1,2,3]と[6,6,6]なら同じ目がないのでNG

こう考えた

記事の下のリンクを読みます。$n$回さいころを振って$r$種類の目出る確率$f(n,r)$は以下のように示せます。
(頭に紹介した大学の入試ではr=3の時のみ問っていました)
$$ f(n,r) = \frac{{}_6 C_r}{6^n} \cdot \sum _{i=1} ^{r} (-1)^{r-i} \cdot i^n \cdot {}_n C _{r} $$
このとき、求める確率$p$は以下のようになります。
$$ p = f(a,5) \cdot (1 - (\frac{1}{6})^b) + \
f(a,4) \cdot (1 - (\frac{2}{6})^b) + ... \
f(a,1) \cdot (1 - (\frac{5}{6})^b)
$$

class DisjointDiceValues:
    def getProbability(self, a,b):
        # 組み合わせ
        def nCr(n, r):
            import math
            # nCrのr>nは組み合わせが存在しないので0を返す
            # raiseすべきのこともあるかも
            if r > n:
                return 0
            return math.factorial(n) // ((math.factorial(n - r) * math.factorial(r)))
        def do(n, r):  # n回降ってr種類出る数
            res = (1.0 / 6.0 ** (n*1.0)) * nCr(6.0, r)
            tmp = 0
            for i in range(r + 1):
                tmp += ((-1) ** (r - i)) * float(nCr(r, i)) * float((i ** n))
            return res * tmp
        res = 0
        res += (do(a, 6)) * (1 - (0 / 6.0) ** b)
        res += (do(a, 5)) * (1 - (1 / 6.0) ** b)
        res += (do(a, 4)) * (1 - (2 / 6.0) ** b)
        res += (do(a, 3)) * (1 - (3 / 6.0) ** b)
        res += (do(a, 2)) * (1 - (4 / 6.0) ** b)
        res += (do(a, 1)) * (1 - (5 / 6.0) ** b)
        res += (do(a, 0)) * (1 - (6 / 6.0) ** b)

        print(res)
        return(res)

link

サイコロの目が3種類になる確率

n個のサイコロの目がk種類になる確率

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?