A question of the cyber competition, derivation and simplification of the probability formula.
Description
There is an aircraft carrying N (2 <= n <= 1000) and N passengers are on the plane. The first passenger is a fool. He finds a location to sit down. After the second passenger is on the plane, if he finds that his location is occupied, he also finds a location to sit down, this is true for all passengers. Now, what is the probability that a person at K (1 <k <= N) will be able to sit on his or her own flight?
Input
Test data in a single group.
First, a group of N and m respectively indicate that the plane has n seats and M (M <= 1000) queries.
Next, the number of M is represented by the number of K (2 <= k <= N) individuals.
Output
The probability is output, which must be in the form of the simplest score (that is, there is no common divisor of 1 between the numerator and denominator ).
Sample Input
100 1
100
Sample output
1/2
Slyar: probability problem. First of all, it is clear that No. 1 is a dumb and he will not go to his own location to sit, while the people on the plane at the back know his location, if the account is occupied, he will go anywhere to sit. That is to say, if the first day is right, then everyone in the back can sit right.
Suppose there are a total of 100 passengers. We make f (k) the probability that the K passengers cannot sit in their position, the reason why he could not sit in his position was that a passenger was sitting in his position. We started from k = 2:
F (2) = 1/100 // number 1 (dumb) sits at number 2, with a probability of 1/100
F (3) = 1/100 + F (2) * (1/99) // it may be that the first day is the third day, or the second day is the third day.
F (4) = 1/100 + F (2) * (1/99) + f (3) * (1/98) // account for account 1 + account 2 + account 3
F (5) = 1/100 + F (2) * (1/99) + f (3) * (1/98) + f (4) * (1/97)
It is an obvious recursive process, and we can find that:
F (3) = F (2) * (1 + 1/99)
F (4) = f (3) * (1 plus 1/98)
If there are n passengers in total, the expression of F (k) is as follows:
F (K) = (1/n) * (1 + 1/(N-1) * (1 + 1/(N-2 )) * (1 + 1/(N-3 ))... (1 + 1/(n-k + 3) * (1 + 1/(n-k + 2 ))
F (K) = 1/(n-k + 2)
So k's probability of sitting in his seat is:
1-F (K) = (n-k + 1)/(n-k + 2)
It's that simple...
An interesting question relies entirely on the derivation of mathematical formulas.
# include
# include
int main ( ) { int I , K , n , m ; scanf ( "% d" , & n , & m ) ; for ( I = 0 ; I m ; I ++ ) { scanf ( "% d" , & K ) ; printf ( "% d/% d \ n " , n - K + 1 , n - K + 2 ) ; } return 0 ; }