NC15532 Happy Running

题目链接

题目

题目描述

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. kx 时,概率为 1k22x2
  2. x<k2x 时,概率为 (2xk)22x2
  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;
}
posted @   空白菌  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示