Take Your Seat(概率计算)

Duha decided to have a trip to Singapore by plane.

The airplane had nn seats numbered from 11 to 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 ii held the ticket corresponding to the seat with number ii , 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!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 TT 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

For each test case, output a line containing Case #x: y z, where x is the test case number starting from 1 , 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 6 places, and we guarantee that their 7 -th places after the decimal point in the precise answer would not be 4 or 5 .

Example

 

 

INput
1
2 3
OUtput
Case #1: 0.500000 0.666667

题意:第一次是n个人坐飞机,按照1到n的顺序登机,第一个人登机牌丢了,他随机做一个座位,2到n个人上来按照自己的登机牌坐座位,如果他的座位被坐了,就在随机找一个座位,否则就做自己的座位,问第n个人坐对的概率是多少。

第二次跟第一次的区别是不是从1-n按顺序上车了,而是随机找人上车,1号还是丢了登机牌,问问第n个人坐对的概率是多少。

思路:第一问,当有两个人时,1号可以坐在1位置上(第二人一定坐对),1号还可以坐在2位置上(第二人一定做不对),所以概率时1/2,多举几组例子,发现都是1/2.

第二问,如果一号是最后一个上车的,那么第n个人一定坐对,否则,如果是第i个上车的,那么前n-i个人肯定会坐在自己的位置上,问题又回到了1号身上,他又相当于第一个上车的,所以只要1号不是最后一个上车的,概率总是1/2,所以是

1/m+(m-1)/m*1/2化简之后就是(m+1)/m*(1/2).

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int mod=1e9+7;
const int M=1e5 + 10;
int main()
{
    int t,i,j,n,m,e=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        double x,y;
        if(n==1)
            x=1.0;
        else
            x=(double) 1/2;
        if(m==1)
            y=1.0;
        else
            y=(double)(m+1)/(2.0*m);
        printf("Case #%d: %.6lf %.6lf\n",e++,x,y);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值