What is the probability?#1571

What is the probability? (Editorial)

Let us consider the game from the perspective of probability theory:

We are interested in the probability that the -th player wins.

The fact that the -th player wins means that:

Moreover, the -th player may win:

The final probability is equal to the sum of the probabilities of all these cases.

For the -th player to win on his first turn, two conditions must be satisfied:

Since the events are independent, the probability that the -th player wins on his first turn is equal to

p \cdot (1 - p)^{i - 1}

The -th player wins on his second turn if:

The probability that the -th player wins on his second turn is equal to

p \cdot (1 - p) ^ {i - 1} \cdot (1 - p)^n

By reasoning in the same way, we obtain that the probability for the -th player to win on his -th turn is equal to

p \cdot (1 - p)^{i - 1} \cdot (1 - p)^{n(k - 1)}

Now it remains to sum the obtained probabilities. The overall probability that the -th player wins is equal to

p \cdot (1 - p)^{i-1} (1 + (1 - p)^n + (1 - p)^{2n} + .. + (1 - p)^{kn} + ...)

The expression in parentheses is an infinite geometric progression with first term equal to and common ratio (1 - p)^n. The winning probability is equal to

p \cdot (1 - p)^{i - 1} \frac{1}{1-(1-p)^n} = \frac{p \cdot (1 - p)^{i - 1}}{1 - (1 - p)^n}

It should be noted separately that when p = 0, the answer is .

Algorithm implementation

Read the input data.

scanf("%d",&t);
while(t--)
{
  scanf("%d %lf %d",&n,&p,&i);

If the probability is equal to , print .

  if (p == 0) printf("0\n");
  else
  {

Otherwise, compute the required probability using the formula given above. Print the result with digits after the decimal point.

    res = p * pow(1 - p,i - 1) / (1 - pow(1 - p,n));
    printf("%.4lf\n",res);
  }
}