20180717 D. Take Your Seat

The 2018 ACM-ICPC Chinese Collegiate Programming Contest

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 11to 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 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

#include <iostream>
#include<cstdio>
using namespace std;
//答案
/*
先考虑第一问: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人坐飞机,第i(随机)位乘客(疯子)在座位中随便选一个坐下,第n人正确坐到自己坐位的概率是?。
等概率随机一个排列相当于等概率随机 1 的登机时刻。
如果 1 是第 i 个登机的,那么登机的前 i − 1 人都能坐对,
相当于人数变为 n − i + 1。
因此概率为 gn = f1+f2+ n···+fn = n2 +1
*/

/*
由第一问可知只要疯子不是最后一个上去,其坐对的概率为1/2,疯子最后上,则前面的人都坐对,疯子只剩自己的座位了,也会坐对

所以为1/2*(n-1/n)+1/2
即(n+1/2n)

*/

//b==1时答案为1,其他始终为1/2
//第二问为(c+1)/(c*2)


int main()
{int n,m;
float b,c;

cin>>n;

   for(int i=1;i<=n;i++)
   {cin>>b>>c;
   printf("Case #%d: ",i);
   if(b==1)
   {
   printf("%.6f",b);
   }
   else
   printf("%.6f",0.5);
   printf(" %.6f\n",(c+1)/(c*2));

   }
   return 0;
}
 

猜你喜欢

转载自blog.csdn.net/z20172123/article/details/81084650