题目描述
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).

思路:参考了某位大佬的题解写的真是妙啊(虽然没咋看懂)。因为打卡地点和起点都是随机出现的,所以我们固定一个跑步的起点,然后分别按照两次打卡的起点进行讨论。我们假设第一次打卡在跑道上的位置到起点的距离为S,第二次打卡的距离为T。
情况1:k<x的丑图如下:
图片说明
阴影部分的面积就是我们要的答案,在y=x斜线的下方就是S在T后面的情况,所以这种情况下一定跑的距离比k大。另一部分就是S在T前面,所以T就是此次跑步的终点,那么终点大于k的概率就是答案。
情况2x<k<2x:这种情况下,要跑距离k,那么我们肯定要跑一圈多,所以斜线下方的是可能答案,然后我们只要跑完一圈后接着跑k-x就行。丑图:
图片说明
情况3:k>2
x,答案为0。
剩下的就只需要算面积求概率了。
代码:

#include<iostream>
using namespace std;
long long T,n,k;
int main()
{
    ios::sync_with_stdio(false);
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&k,&n);
        double ans=0;
        if(k<n)    ans=(1-1.0*k*k/(n*n))/2+0.5;
        else    if(k==n)    ans=0.5;
        else    if(k<2*n)    ans=1.0*(2*n-k)*(2*n-k)/(2*n*n);
        printf("%.2lf\n",ans);
    }
    return 0;
}