Probability of getting 'k' heads with 'n' coins
You can use Dynamic Programming as $N$th turn's outcome is mutually independent to $N-1$th and there are two possible cases here :
- $K$ heads already came in $N-1$ turns
- $K-1$ heads already came in $N-1$ turns
$dp[i][j]$ : probability of getting $j$ heads in $i$ trials.
So, $dp[n][k] = dp[n - 1][k]\cdot (1 - P[n]) + dp[n - 1][k - 1]\cdot p[n]$
Consider the function
$[ (1-P_1) + P_1x] \times [(1-P_2) + P_2 x ] \ldots [(1-P_n) + P_n x ]$
Then, the coefficient of $x^k$ corresponds to the probability that there are exactly $k$ heads.
The coefficient of $x^k$ in this polynomial is $\sum_{k-\mbox{subset} S} [\prod_{i\in{S}} \frac{1-p_i}{p_i} \prod_{j \not \in S} p_j] $
Let us say p = Pi, and q = 1 - Pi. Then the probability of a given sequence, e.g., 100010..., in which k heads appear in n flips e.g pqqqpq... can be given by $$p^k.q^{n-k}$$ There are a total of $2^n$ possible sequences. Only some of these give k heads and n - k tails. Their number is $$\frac{n!}{k! (n-k)!} = {n \choose k}$$ Since any one or another of these sequences will do, the probability that exactly k heads occur in n flips would be $${n \choose k}p^k.q^{n-k}$$ [Source]