CCCGuide

Problem S5: Josh’s Double Bacon Deluxe

2020 · S5 · Original problem · DMOJ

Problem Description

On their way to the contest grounds, Josh, his coach, and his N−2N-2 teammates decide to stop at a burger joint that offers MM distinct burger menu items. After ordering their favourite burgers, the team members line up, with the coach in the first position and Josh last, to pick up their burgers. Unfortunately, the coach forgot what he ordered. He picks a burger at random and walks away. The other team members, in sequence, pick up their favourite burger if available, or a random remaining burger if there are no more of their favourite burger. What is the probability that Josh, being last in line, will get to eat his favourite burger?

Input Specification

The first line contains the number NN (3≤N≤1 000 000)(3\leq N\leq 1~000~000), the total number of people and burgers. The next line contains NN numbers, the ii-th being bib_i (1≤bi≤M≤500 000)(1\leq b_i\leq M\leq 500~000), denoting the item number of the ii-th person’s favourite burger. The first person in line is the coach, and the NN-th person is Josh.

For 4 of the 15 available marks, N≤100 000N\leq 100~000 and M≤1000M\leq 1000.

For an additional 5 of the 15 available marks, M≤5000M\leq 5000.

Output Specification

Output a single number PP, the probability that Josh will get to eat his favourite burger, bNb_N. If the correct answer is CC, the grader will view PP correct if ∣P−C∣<10−6|P-C|<10^{-6}.

Sample Input 1

3
1 2 3

Output for Sample Input 1

0.5

Explanation of Output for Sample Input

The coach randomly chooses between the three burgers. With probability 1/31/3, he chooses his favourite burger (burger 1), and Josh gets to eat his favourite burger (burger 3). With probability 1/31/3, he chooses Josh’s favourite burger, and Josh fails to eat his favourite burger. With probability 1/31/3, he chooses the second person’s burger, there is a 1/21/2 chance that the second person chooses Josh’s burger, denying Josh the pleasure of eating his favourite burger.

Sample Input 2

7
1 2 3 1 1 2 3

Output for Sample Input 2

0.57142857