Let us consider the game from the perspective of probability theory:
Each throw is independent of the others;
The probability of winning on a single throw is ;
The probability of failure on a single throw is 1 - p;
The players take turns in a cycle: 1, 2, ..., n, 1, 2, ...;
The game ends immediately as soon as someone wins.
We are interested in the probability that the -th player wins.
The fact that the -th player wins means that:
no one has won before his winning throw;
the winning event occurs on his turn.
Moreover, the -th player may win:
on his first throw;
on his second throw;
on his third throw;
and so on.
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:
The first i - 1 players do not win. The probability of this event is (1 - p)^{i - 1}
The -th player wins. The probability of this event is .
Since the events are independent, the probability that the -th player wins on his first turn is equal to
The -th player wins on his second turn if:
All players make one throw each and lose: (1 - p)^n
The first i - 1 players lose again in the second round. The probability of this event is (1 - p)^{i - 1}.
The -th player wins. The probability of this event is .
The probability that the -th player wins on his second turn is equal to
By reasoning in the same way, we obtain that the probability for the -th player to win on his -th turn is equal to
Now it remains to sum the obtained probabilities. The overall probability that the -th player wins is equal to
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
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);
}
}