Розбір
A Bernoulli trial is an experiment with two possible outcomes:
Success (the event occurs) with probability ,
Failure (the event does not occur) with probability .
A trial is called "Bernoulli" if it is independent and the probability of success remains constant throughout all trials.
The Bernoulli trial scheme is a series of independent trials, each of which is a Bernoulli trial. Let the number of trials be denoted by . In each trial, the probability of success is and the probability of failure is , and these probabilities remain constant.
An example could be repeatedly flipping a coin. If we are interested in how many times heads will appear out of flips, this would represent a Bernoulli trial scheme.
The number of successes in a series of Bernoulli trials follows a binomial distribution. Let be a random variable representing the number of successes in trials. Then:
where
is the probability of success in each trial,
is the number of successes out of n trials.
To compute the value of the binomial coefficient with real numbers, we will use the following approach:
It is known that
Store the values of in the cells of an array . Then we calculate
After that
Example
In the first test, trials are conducted. The probability of event occurring in each trial is . We want to find the probability that event A occurs exactly times. The desired probability is:
Algorithm realization
Declare an array , where .
#define MAX 1001 double lf[MAX];
Read the input data.
scanf("%d %d", &n, &k); scanf("%lf", &p); q = 1 - p;
Fill the array .
lf[1] = 0; for (i = 2; i <= n; i++) lf[i] = lf[i - 1] + log(i);
Compute the value of the binomial coefficient .
res = lf[n] - lf[k] - lf[n - k]; res = exp(res);
Compute the answer .
for (i = 0; i < k; i++) res = res * p; for (i = 0; i < n - k; i++) res = res * q;
Print the answer.
printf("%lf\n", res);