Skip to content
QuantReadySign In
#012easyProbability

Generalized Monty Hall Problem

Time Limit: 2sMemory: 256MB

Problem

You are on a game show with n doors. Behind exactly one door is a prize; the rest hide goats.

  1. You pick one door.
  2. The host, who knows where the prize is, opens k other doors that reveal goats.
  3. You switch to one of the remaining nk1n - k - 1 doors, chosen uniformly at random.

What is the probability you win the prize by switching?

Input Format

Two space-separated integers: n k

Output Format

The probability of winning by switching, rounded to 4 decimal places.

Examples

Example 1
Input(Two space-separated integers: n k)
3 1
Output
0.6667

Classic Monty Hall: 3 doors, host opens 1. P(win|switch) = 2/3.

Example 2
Input(Two space-separated integers: n k)
4 2
Output
0.7500

4 doors, host opens 2 goat doors. P(win|switch) = 3/(4*1) = 3/4.

Constraints

  • 3 ≤ n ≤ 1000 (number of doors)
  • 1 ≤ k ≤ n-2 (doors opened by host)
  • Output rounded to 4 decimal places
Loading interactive editor…