def B(n,p,k): q = 1 for j in range(k): q = q*p*(n-j)/(k-j) q = q*(1-p)**(n-k) return q