The 2018 ACM-ICPC Chinese Collegiate Programming Contest--Take Your Seat

Duha decided to have a trip to Singapore by plane.

The airplane had nn seats numbered from 11to nn, and nn passengers including Duha which were also counted from 11 to nn.The passenger with number ii held the ticket corresponding to the seat with number ii, and Duha was the number 11 passenger.

All passengers got on the plane in the order of their numbers from 11 to nn.However, before they got on the plane Duha lost his ticket (and Duha was the only passenger who lost the ticket), so he could not take his seat correctly.He decided to take a seat randomly.And after that, while a passenger got on the plane and found that his/her seat has been occupied, he/she selected an empty seat randomly as well.A passenger except Duha selected the seat displayed in his/her ticket if it had not been occupied by someone else.

The first problem you are asked to calculate in this problem is the probability of the last passenger to get on the plane that took his/her correct seat.

Several days later, Duha finished his travel in Singapore, and he had a great time.

On the way back, he lost his ticket again.And at this time, the airplane had mm seats numbered from 11 to mm, and mm passengers including Duha which were also counted from 11 to mm.The passenger with number iiheld the ticket corresponding to the seat with number ii, and Duha was the number 11passenger as well.

The difference was that: all passengers got on the plane in a random order (which was any one of the m!m! different orders with the same chance).Similarly, Duha or a passenger who found his/her seat had been occupied selected an empty seat randomly.

The second problem you are asked to calculate in this problem is the probability of the last passenger to get on the plane that took his/her right seat on the return trip.

Input Format

The input contains several test cases, and the first line is a positive integer TT indicating the number of test cases which is up to 5050.

For each test case, a line contains two integers nn and m~(1\le n, m\le 50)m (1≤n,m≤50).

Output Format

For each test case, output a line containing Case #x: y z, where xx is the test case number starting from 11, yy is the answer of the first problem, and zz is the answer of the second problem.Both of yy and zz are rounded to 66 places, and we guarantee that their 77-th places after the decimal point in the precise answer would not be 44 or 55.

样例输入

1
2 3

样例输出

Case #1: 0.500000 0.666667

题目来源

The 2018 ACM-ICPC Chinese Collegiate Programming Contest

题解:参考  https://blog.csdn.net/qq_36782366/article/details/81114846

            先考虑第一问:n人坐飞机,第一个乘客在座位中随便选一个坐下,第n人正确坐到自己坐位的概率是?
            当 n = 1 时概率为 fn = 1,当 n = 2 时概率为 fn = 1 2,以下考虑 n ≥ 3。
如果 1 坐到 1 上,那么后面所有人都会坐对,如果 1 坐到 n上,那么 n 肯定不能坐对。
如果 1 坐到 k 上,那么 2; 3; · · · ; k − 1 都会坐对,此时 k 相当于一开始的 1,而人数变为 n − k + 1。
因此 fn = (1+f2+f3+···+fn−1)/n,可得 fn = 1 /2。注意这里的n>=2.

           再考虑第二问:n人坐飞机,第i(随机)位乘客是朵哈,他在座位中随便选一个坐下,第n人正确坐到自己坐位的概率是?。
           如果 1 是第 i 个登机的,那么登机的前 i − 1 人都能坐对,相当于人数变为 n − i + 1。

因此最后的概率为gn=(fm+fm-1+....f2+f1)/m,又因为当n大于等于2时,fn=1/2.

           所以gn=(m+1)/(m*2);

#include<stdio.h>
#include<string.h>
int main(){
    int t;
    scanf("%d",&t);
        int i;
        for(i=1;i<=t;i++){
            int n,m;
            scanf("%d %d",&n,&m);
            if(n!=1)
                printf("Case #%d: %.6lf %.6lf\n",i,0.5,(m+1.0)/(2.0*m));
            else
                printf("Case #%d: %.6lf %.6lf\n",i,1.0,(m+1.0)/(2.0*m));
        }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/black_horse2018/article/details/81190629