Shooting gallery#1583

Shooting gallery (Editorial)

The probability that your friend hits the target with a single shot is

p = a / 100

Then the probability that he does not hit the target with one shot is

1 - p = 1 - a / 100

Since all shots are independent, the probability that your friend does not hit the target even once after making shots is equal to

(1 - a / 100.0)^n

The probability that your friend hits the target at least once in shots is the complement of this event:

1 - (1 - a / 100.0)^n

According to the problem statement, the bet makes sense if the probability of at least one hit is strictly less than , that is:

1 - (1 - a / 100.0)^n < 0.5

Let us transform this inequality:

(1 - a / 100.0)^n > 0.5

Thus, it is required to find the maximum integer value of for which this inequality holds. As soon as increases to the point where the probability of missing the target in all shots becomes less than or equal to , the bet ceases to be worthwhile.

Algorithm implementation

Read the input value of the probability .

while (scanf("%d", &a) == 1)
{

The probability of missing the target is computed and stored in the variable .

  miss = 1 - a / 100.0;

The variable stores the value of the probability (1 - a / 100.0)^n.

  p = miss;
  n = 0;

We look for the largest value of for which (1 - a / 100.0)^n > 0.5.

  while (p > 0.5)
  {
    n++;
    p *= miss;
  }

Print the answer.

  printf("%d\n", n);
}