Earn Big
InterviewTime limit2sMemory limit512 MB
Given N boxes and N names in a random permutation, compute the probability that every element lies in a cycle of length at most M.
- Level
Medium6 of 10
- Topics
- Combinatorics, Dynamic programming, Math, Probability
- Solved
- No attempts yet
Problem
A group of N people tries the following game to earn big money.
First, the N participants are isolated from one another. From that point on, they may not contact each other or leave any information for other participants. The game organizer leads each participant, one at a time, into a room with N boxes. All boxes are closed at the start of the game, and the organizer closes every box whenever a participant enters the room. Each box holds a slip of paper with the name of a distinct participant. The order of the boxes does not change during the game. A participant may open up to M boxes. If every participant opens a box containing the paper with their own name, the group wins the game and everyone in the group earns big money. If any participant fails to open a box containing the paper with their own name, the group loses and nobody gets money.
If every participant picks boxes at random, the winning probability is (M/N)N. There is a much better strategy, though.
Before discussing that strategy, let us define some concepts. Let P = {p1, p2, ..., pN} be the set of participants and B = {b1, b2, ..., bN} the set of boxes. Define f, a mapping from B to P, so that f(b) is the participant whose name is written on the paper in box b.
Now suppose participant pi picks boxes as follows.
-
Set
x := i. -
If
pihas already openedMboxes, stop with failure. -
piopensbx.- If
f(bx) = pi, stop with success. - If
f(bx) = pj(i != j), setx := jand go to step 2.
- If
If every participant follows this algorithm, the result of the game depends only on the initial arrangement of the boxes, that is, on the definition of f. Let g be a mapping from P to B with g(pi) = bi. The participants win the game if and only if, for every i ∈ {1, 2, ..., N}, there exists k(≤M) such that (f○g)k (pi) = pi.
Your task is to write a program that computes the winning probability of this game. You may assume the boxes are placed at random.
Input
The input consists of one line. It contains two integers N and M (1 ≤ M ≤ N ≤ 1,000) in this order, separated by a space.
Output
For the given N and M, print the winning probability of the game. Print the value with eight digits after the decimal point, and the error must not exceed 10-8.