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.
输入描述
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
题解
知识点:概率论。
考虑固定起点,因为起点在哪都一样,只需要考虑 的相对位置。接下来,利用几何概型分类讨论:
- 当 时,概率为 。
- 当 时,概率为 。
- 当 时,概率为 。
时间复杂度
空间复杂度
代码
#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; }
本文来自博客园,作者:空白菌,转载请注明原文链接:https://www.cnblogs.com/BlankYang/p/17661259.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧