NC15532 Happy Running

发布时间 2023-08-28 03:10:37作者: 空白菌

题目链接

题目

题目描述

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.

img

输入描述

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 ≤ 10^9).

输出描述

For each test case print the probability of completing the goal, round to two decimal places.

示例1

输入

3
2 2
4 3
2 1

输出

0.50
0.22
0.00

题解

知识点:概率论。

考虑固定起点,因为起点在哪都一样,只需要考虑 \(a,b\) 的相对位置。接下来,利用几何概型分类讨论:

  1. \(k\leq x\) 时,概率为 \(1- \dfrac{k^2}{2x^2}\)
  2. \(x < k \leq 2x\) 时,概率为 \(\dfrac{(2x-k)^2}{2x^2}\)
  3. \(2x < k\) 时,概率为 \(0\)

时间复杂度 \(O(1)\)

空间复杂度 \(O(1)\)

代码

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

bool solve() {
    int k, x;
    cin >> k >> x;
    if (k <= x) cout << fixed << setprecision(2) << 1 - (long double)k * k / 2 / x / x << '\n';
    else if (k <= 2 * x) cout << fixed << setprecision(2) << (long double)(2 * x - k) * (2 * x - k) / 2 / x / x << '\n';
    else cout << fixed << setprecision(2) << 0 << '\n';
    return true;
}

int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--) {
        if (!solve()) cout << -1 << '\n';
    }
    return 0;
}