Happy Running 概率DP

链接:https://ac.nowcoder.com/acm/problem/15532
来源:牛客网

Happy Running, an application for runners, is very popular in CHD.
In order to lose weight, fdf decides to run k meters per day with Happy Running. Let’s regard the school as a circular runway with total length of x meters, and fdf could start running clockwise at a random point on the runway. Then Happy Running will randomly show two punching-card points (打卡点). Because of the poor physical strength, fdf decides to finish running only by completing the punch, whether or not complete the goal of k meters.
One day, before running, fdf wants to know whether he can achieve the goal today, and he asks you to help him to calculate the probability of completing the goal running k meters.
Note that, fdf should punch card one by one which means that he will punch card at the first point before the second, even if he reaches the second point first.
It is guaranteed that punching-card points and the point fdf starts running will appears randomly with equal probability and the three points won’t be coincide.

输入描述:
The first line contains an integer number T, the number of test cases.
ith of each next T lines contains two integer numbers k,x(1 ≤ k,x ≤ 109).
输出描述:
For each test case print the probability of completing the goal, round to two decimal places.

虽然说这只是一道概率DP的引出题,但是还是值得仔细思考的
我们设a点的人的位置是A,b点的人的位置是B,然后我们对k和x进行分类讨论:
1.k<x 
		1.1 A在B后面:这种情况下一定能保证最后的S大于k,因为必定会要多跑一圈,因此s>x,所以成立
		1.2A在B前面时,我们列出关系式:
						B>=k   A<=x  B<=x
		因此可以配合画图得出概率P=(2*x*x-k*k)/(2*x*x)

在这里插入图片描述

2.k==x 这个蒙一下也知道是0.5,至于怎么来的,我们先固定A在起点,然后B随便选,选在A前面的概率是0.5,选在A后面的概率也是0.5,因此答案就是0.5

3.k>x	直接上表达式:
		B<A   B+x>=k  B<=x   A<=x

在这里插入图片描述

4.k>2*x 此时无论如何也无法得到A和B签到相同且都是第一次达到,因此答案为0

AC代码:(很短~)

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

const double eps = 1e-9;
const int N = 1010; 
int T;
double x,k,ans;
    
int main(){
    int t;
    cin >> t;
    while(t--){
        cin >> k >> x;
        if(k<x){
            ans= ((double)2.0*x*x-k*k)/(2.0*x*x);
        }
        else if(fabs(k-x)<eps){
            ans=0.5;
        }
        else if(k>=x&&k<2*x){
            ans=(double)(2.0*x-k)*(2.0*x-k)/(2.0*x*x);
        }
        else ans=0;
        printf("%.2lf\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值