D. Take Your Seat

D. Take Your Seat

Duha decided to have a trip to Singapore by plane.

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

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

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 5050.

For each test case, a line contains two integers ?n and ? (1≤?,?≤50)m (1≤n,m≤50).

Output

For each test case, output a line containing Case #x: y z, where x is the test case number starting from 11, y is the answer of the first problem, and z is the answer of the second problem. Both of y and z 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.

Example

input

1
2 3

output

Case #1: 0.500000 0.666667

题意:

有两种情况:

(1) 总共有n人,从1-n编号,对号入座并且按顺序上飞机

          由于第一个人(Duha)忘记自己号码是多少,所以会在n个座位中随意坐下

          当第 i 个乘客会尽量坐在自己的座位上,如果该座位被占有时,他会任意选择一个坐下

          求最后一个乘客(第n号)坐在自己位置上的概率的概率? 

           疯子坐飞机问题 请参考

          概率恒为 1/2

 (2) 总共有m人,从1-m编号,对号入座但是按照任意顺序上飞机

          由于Duha忘记自己号码是多少,所以会在m个座位中随意选择一个坐下

          当第 i 个乘客会尽量坐在自己的座位上,如果该座位被占有时,他会任意选择一个坐下

          求最后一个乘客(第 m个)上飞机的概率?          

从1-n编号定为  A B C D .......

与第一个的不同在于乱序上飞机

分为两种情况考虑:

1. 如果A最后一个上飞机,那么其余的人一定是坐在自己位置上的,第m个一定会坐在自己位置上,概率为 1/m (A选择       一个坐下)

2.如果A是第i个上飞机的,那么前 i-1 个上飞机的人一定是坐在自己位置上的,相当于转化成第一个情况

   将 A 看做“疯子”,那么最后一个坐在自己正确位置的概率恒为 1/2,则这种情况概率为 1/2 * (m-1) / m

   则 P = 1/m + 1/2 * (m-1) / m = (m+1) / 2*m 

 

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#define memset(a,n) memset(a,n,sizeof(a))
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXX = 1e3+10;
int main()
{
    int t;
    double n,m;
    cin >> t;
    for(int i=1; i<=t; i++){
        cin >> n >> m;
        printf("Case #%d: ",i);
        if(n == 1)
            printf("1.000000 ");  // 只有一个人的时候,概率恒为1
        else
            printf("0.500000 ");
        if(m == 1)
            printf("1.000000\n");
        else {
            double ans = (m+1)/(2*m)*1.0;
            printf("%.6lf\n",ans);
        }
    }
}

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值