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

Duha decided to have a trip to Singapore by plane.

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

All passengers got on the plane in the order of their numbers from 1 to n.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 1 to m, and mm passengers including Duha which were also counted from 1 to m.The passenger with number ii held the ticket corresponding to the seat with number i, and Duha was the number 1 passenger as well.

The difference was that: all passengers got on the plane in a random order (which was any one of the 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 T indicating the number of test cases which is up to 50.

For each test case, a line contains two integers n and 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 1, y is the answer of the first problem, and zz is the answer of the second problem.Both of y and z are rounded to 6places, and we guarantee that their 7-th places after the decimal point in the precise answer would not be 4 or 5.

样例输入

1
2 3

样例输出

Case #1: 0.500000 0.666667

 

解题思路:

数学题,推得公式即可。以下叙述做题时的推理过程。

无论何种情况

当Duha坐对时,第n名乘客一定坐对。

当其他人坐到第n名乘客的座位上时,第n名乘客才会坐错。

其他乘客发现自己位置被占时,处境和Duha相似。

对于第一问,登机顺序固定。第n名乘客登机后没有选择的余地,只能做余下的位置。

首先考虑两个特殊情况:n=1时,第n名乘客一定坐对(因为只有Duha自己);n=2时,第n名乘客坐对的概率为1/2。

对于一般情况,不妨先举个简单的例子帮助理解。假设n=4,则Duha登机后,坐对的概率为1/4。坐到第4号位的概率为1/4,此时第4名乘客一定坐错。坐到2号和3号座位的概率相等,均为1/4。如果Duha做到了2号位,那么对于第二名乘客,有4-2+1=3种选择方案,坐去1号位的概率为1/3(此时第4名乘客一定坐对),坐去4号位的概率为1/3(此时第4名乘客一定坐错),做到其他位置的概率相等。最后,如果3号位的乘客发现作为被占,那么他只有两种选择,选择1号位和4号位的概率均为1/2。

不难进行推广。当Duha登机时,有n个位置空余,坐到各座位的可能性相等,此时有1/n的概率坐对,有1/n概率做到第n各座位。假设Duha坐错到位置k,那么k之前的所有人一定坐对。而对于乘客k,其处境与Duha相似,此时共有n-k+1个空位,所以其坐在各座位的概率均为1/(n-k+1),如果坐在了1号和n号之外的位置上,能够以此类推。

不难发现除了只有Duha一人的情况之外,第n名乘客坐对的概率恒为1/2。

对于第二问,登机顺序不定,只有Duha一个特殊元素。

Duha坐在任一座位的概率均为1/m。这个好理解,因为顺序不定,Duha还是想坐哪儿就坐哪儿。

在Duha之前登机的乘客,一定能够坐对。任一乘客在Duha前登机的概率均为1/2。

在Duha之后登机的乘客,坐在任意位置的概率也为1/m。这里可能较难理解,稍微解释一下。Duha坐在正确位置的概率为1/m。因为登机顺序不确定,所以当Duha坐错时,具体是谁都有可能,会等可能地选择其它座位坐下。因为其余的m-1各座位一定坐满,(1-1/m)/(m-1)= 1/m。由此也不难看出,当某一乘客位置被占时,其处境与Duha相似。

所以最后一名乘客坐对的概率为(1+1/m)/ 2   = (m+1)/ 2m 。m=1时,该等式同样成立。

 

AC代码:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    scanf("%d",&t);
    int c=t;
    while(t--)
    {
        int n,m;
        scanf("%d %d",&n,&m);
        if(n == 1)
            printf("Case #%d: %lf %lf\n",c-t,(double)1,(double)(m+1)/(2*m));
        else
        printf("Case #%d: %lf %lf\n",c-t,(double)1/2,(double)(m+1)/(2*m));
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值