错误数据大概如下:
6 80 4
1010100
第二行的字符串长度为 n+1,如果直接遍历字符串,而不是用下标遍历会导致出错。
import math
N = 52
# input().split() 得到第一行的三个字符串,列表推导式将三个字符串变为三个整数,得到三个整数的列表,然后解包为三个变量
n, a, q = [int(item) for item in input().split()]
s = input()
def C(n, m):
if m > n or m < 0 or n < 0:
return 0
else:
res = 1
for i in range(1, m + 1):
res = res * (i + n - m) // i
return res
if q == 0:
print('1.000')
else:
a /= 100
ans = 0
c0 = c1 = 0
# 直接遍历字符串,会遍历多一个字符
# for i in s:
# if i == '1':
# c1 += 1
# else:
# c0 += 1
# 用下标遍历 0 至 n - 1 正确
for i in range(n):
if s[i] == '1':
c1 += 1
else:
c0 += 1
for i in range(q, n + 1):
for j in range(0, i + 1):
ans += C(c1, j) * pow(a, j + c0 - i + j) * pow(1 - a, c1 - j + i - j) * C(c0, i - j)
print('%.3f' % ans)