用动态规划,python写的 dic = {} def factorial(n): if n == 0: return 1 if n == 1: return 1 if n in dic: return dic[n] else: dic[n] = n * factorial(n-1) return dic[n] def comb(a, b): return int(factorial(a) / factorial(b) / factorial(a-b)) dp_table = [[0 for _ in range(n)] for _ in range(m)] dp_table[0][0] = 1 for j in range(1, n): dp_table[0][j] = 0 for i in range(1, m): for j in range(0, n): if j == 0: dp_table[i][j] = 1 + dp_table[i-1][j] * (1-a) + dp_table[i-1][j+1] * a elif j == n - 1: for k in range(n): dp_table[i][j] += comb(n-1, k) * b**k * (1-b)**(n-1-k)*dp_table[i-1][j-k] else: for k in range(j+1): dp_table[i][j] += comb(j, k) * b**k * (1-b)**(j-k)*(1-a)*dp_table[i-1][j-k] dp_table[i][j] += comb(j, k) * b**k * (1-b)**(j-k)*a*dp_table[i-1][j-k+1] print(dp_table[m-1][0])