-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoin_problems.py
54 lines (50 loc) · 1.5 KB
/
coin_problems.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'''
扔硬币问题: 连续扔 n 次硬币, 其中连续 k 次(或以上)是正面的概率
'''
from functools import lru_cache
import sys
sys.setrecursionlimit(2000)
def coin_problem_1(n, k):
'''
使用 DP 求解.
'''
@lru_cache(None)
def P(n):
if n < k:
return 0
if n == k:
return pow(2, -k)
return P(n - 1) + (1 - P(n - k - 1)) * pow(2, -(k + 1))
if n < k:
return 0
dp = [0] * n
dp[k - 1] = pow(2, -k)
for i in range(k, n):
dp[i] = dp[i - 1] + (1 - dp[i - k - 1]) * pow(2, -(k + 1))
return dp[-1]
# return P(n)
def coin_problem_2(n, k):
'''
使用转移矩阵求解
'''
import numpy as np
T = np.array([
[0.5, 0.5 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
[0.5, 0 , 0.5 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
[0.5, 0 , 0 , 0.5 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
[0.5, 0 , 0 , 0 , 0.5 , 0 , 0 , 0 , 0 , 0 , 0],
[0.5, 0 , 0 , 0 , 0 , 0.5 , 0 , 0 , 0 , 0 , 0],
[0.5, 0 , 0 , 0 , 0 , 0 , 0.5 , 0 , 0 , 0 , 0],
[0.5, 0 , 0 , 0 , 0 , 0 , 0 , 0.5 , 0 , 0 , 0],
[0.5, 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0.5 , 0 , 0],
[0.5, 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0.5 , 0],
[0.5, 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0.5],
[0, 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1],
])
res = np.zeros((1, 11))
res[0, 0] = 1
for i in range(n):
res = res @ T
return res[0, -1]
print(coin_problem_1(100, 10))
print(coin_problem_2(100, 10))