【数学概率】Codeforces - 80D. Falling Anvils

前言

难得遇见一个自己比较有思路的题,但是还是由于“太年轻”,对于不等式还不是很清楚该如何处理,看了其他同志的题解才清楚“还能有这种操作”qwq.....

特别鸣谢:https://blog.csdn.net/dragon60066/article/details/76851221

题目

题目描述

For some reason in many American cartoons anvils fall from time to time onto heroes' heads. Of course, safes, wardrobes, cruisers, planes fall sometimes too... But anvils do so most of all.

Anvils come in different sizes and shapes. Quite often they get the hero stuck deep in the ground. But have you ever thought who throws anvils from the sky? From what height? We are sure that such questions have never troubled you!

It turns out that throwing an anvil properly is not an easy task at all. Let's describe one of the most popular anvil throwing models.

Let the height p of the potential victim vary in the range [0;a] and the direction of the wind qvary in the range [ - b;b]. p and q could be any real (floating) numbers. Then we can assume that the anvil will fit the toon's head perfectly only if the following equation has at least one real root:

Determine the probability with which an aim can be successfully hit by an anvil.

You can assume that the p and q coefficients are chosen equiprobably and independently in their ranges.

Input

The first line contains integer t (1 ≤ t ≤ 10000) — amount of testcases.

Each of the following t lines contain two space-separated integers a and b (0 ≤ a, b ≤ 106).

Pretests contain all the tests with 0 < a < 10, 0 ≤ b < 10.

Output

Print t lines — the probability of a successful anvil hit for each testcase. The absolute or relative error of the answer should not exceed 10 - 6.

Sample Input

2
4 2
1 2

Sample Output

0.6250000000
0.5312500000

题目大意

0<=p<=a,-b<=q<=b;

求出       有至少一个实数根的概率

使得上述一元二次方程至少有一个实数根,即△=p-4*q>=0,即p>=4q

可以画图感知一下qwq...(数形结合~

分两种情况

答案的概率就是灰蓝色阴影面积与总面积的比值

因为其他题解写的很模糊,自己看不懂,好不容易看懂后就手动画了个图标注清楚了些

(1)当a/4<=b即a<=4b时

 

(2)当a/4>b即a>4b时

代码

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int t,a,b;
double ans;
//求p>=4q的概率,其中0<=p<=a,-b<=q<=b
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&a,&b);//非负整数
		if(b==0)
			ans=1.0;
		else
		{
			if(a<=4*b)
				ans=(double)a*0.5/(b*8)+0.5;
			else
				ans=(double)(a-b*2)*0.5/a+0.5;
		}
		printf("%.6f\n",ans);
	}
	return 0; 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值