Let's compute the probability of the opposite event — that the last two balls removed are of different colors. For this to happen, exactly half of the first n - 2 removed balls must be white, and the other half must be black.
Consider a Bernoulli trial scheme, where the probability of success (e.g., drawing a white ball) is p = 1/2, and the probability of failure is q = 1 - p = 1/2. The probability that exactly (n - 2) / 2 of the first n - 2 removed balls are white is given by the formula:
It remains to compute the value of for each input and print 1 - P(X).
Although lies within the range , its direct computation may lead to numerical issues. To avoid this, compute its logarithm:
After computing the right-hand side of the equation, exponentiate the result to obtain . The logarithm of a factorial is computed as the sum of logarithms:
Since the program processes multiple test cases, all results should be precomputed and stored in an array.
Example
For n = 6, the probability that the last two balls removed will be of different colors is
The probability that the last two balls removed will be of the same color is
Algorithm implementation
Declare the necessary arrays. In the cell , store the value C_{i-2}^{i/2-1} p^{i/2 - 1} q^{i/2 - 1}.
#define MAX 100001
double lf[MAX], ans[MAX];Fill the array , where stores the value .
res = lf[1] = 0.0;
for (res = 0, i = 2; i < MAX; i++)
{
res += log((double)i);
lf[i] = res;
}For each value of , compute ln~C_{i-2}^{i/2-1} p^{i/2 - 1} q^{i/2 - 1} and store the result in .
for (i = 2; i < MAX; i += 2)
{
res = lf[i/2-1];
res = lf[i-2] - (i - 2) * log(2.0) – res - res;
ans[i] = exp(res);
}For each input value of , compute and print the result.
while (scanf("%d",&n) == 1)
printf("%.8lf\n",1 - ans[n]);