Kangho's Invitations
Time limit2sMemory limit512 MB
Given each friend's single disliked friend, find the expected number of friends who accept when a uniformly random invitation order is used.
- Level
Medium6 of 10
- Topics
- Probability, Math, Combinatorics
- Solved
- No attempts yet
Problem
Kangho wants to invite friends to a party. The friends are numbered through .
Each friend dislikes at most one other friend. The friend that friend dislikes is , and if equals , then friend dislikes nobody.
Kangho invites the friends one at a time. Friend accepts the invitation only if friend has not been invited yet, or friend was already invited and refused. In other words, friend refuses exactly when friend was invited earlier and accepted.
The number of friends who accept depends on the order of the invitations. Kangho therefore picks one of the orders uniformly at random and invites the friends in that order.
Write a program that computes the expected number of friends who accept the invitation.
Input
The first line contains the number of friends ().
The second line contains , separated by spaces ().
Output
Print the expected number of friends who accept, rounded to ten digits after the decimal point, on one line. Print exactly ten digits after the point, keeping trailing zeros. No answer in the test data lies on a rounding boundary.
Note
Take with , , . Friends and dislike nobody, and friend dislikes friend . There are six orders. With the order , friends and accept but friend refuses. With the order , all three accept. Averaging over all six orders gives an expected value of .