2020牛客暑期多校训练营(第二场)——K

Keyboard Free

链接:https://ac.nowcoder.com/acm/contest/5667/K
来源:牛客网

时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
Given three concentric circles whose radiuses are r_1, r_2, r_3r1​,r2​,r3​ respectively, and {A,B,C}A,B,C are the moving points on the given three circles respectively. Determine the expected area of \triangle ABC△ABC.

输入描述:
The first line contains one integer T~(1 \leq T \leq 1000)T (1≤T≤1000), denoting the number of test cases.
For each test case:
One line containing three integers r_1, r_2, r_3~(1\leq r_1,r_2,r_3 \leq 100)r1​,r2​,r3​ (1≤r1​,r2​,r3​≤100), denoting the radiuses of three given concentric circles.

输出描述:
Print {T}T lines each containing one real number with one decimal places after the decimal point, denoting the answer to curresponding test case.
It’s guaranteed that the second decimal place after the decimal point is neither 4 nor 5.

示例1
输入
2
1 1 1
2 3 5
输出
0.5
5.5

说明
For test case 1, the accurate answer is \frac{3}{2\pi} = 0.47746482927568600730665129011754\cdots2π3​=0.47746482927568600730665129011754⋯.
题意:给定三个同心圆,每个圆上取一个点,求这三个点确定的三角形面积的期望。

固定a点,b点在上半圆上移动,c点在整个圆上移动,遍历b点的每个角度求解,出题人讲题时说,分成一千份左右就可以满足精度,具体怎么证明的我也不知道,然后开始考虑c点的位置,分成三部分考虑,分别是图上的粉色,蓝色,绿色荧光笔三部分,圆心到直线ab的距离可以利用图中绿色三角形的等面积法求出。
粉色部分:对 esp1=h-r3sin(x)积分
蓝色部分:对 esp2=h+r3
sin(x)积分
绿色部分:对 esp3=r3sin(x)-h积分
最终结果为 (esp1+esp2+esp3)/2
PI

#include
#define PI acos(-1)
using namespace std;
typedef long double ld;
const int N=1050;
int r[4];
ld cal(ld x1,ld y1,ld x2,ld y2) {
	return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		for(int i=1; i<=3; ++i)scanf("%d",&r[i]);
		sort(r+1,r+4);
		ld ans=0.0;
		for(int i=0; i<N; ++i) {
			ld a=1.0*(i+0.5)*PI/N;
			ld x=1.0*r[2]*cos(a),y=1.0*r[2]*sin(a);
			ld l=cal(1.0*r[1],0.0,1.0*x,1.0*y);//a与b之间的距离 
			ld d=1.0*r[1]*y/l;//等面积法求出原点到ab之间的距离 
			ld b=asin(d/r[3]);//反三角函数求出角x的角度 
			ld exp=1.0*2*r[3]*(b*sin(b)+cos(b))/PI;//求期望 
			ans+=exp*l/2;
		}
		ans/=N;
		printf("%.1Lf\n",ans);
	}
	return 0;
}

这道题看了好几天,数学知识好多都忘记了,看了很多大佬的博客,顺便学了学自适应辛普森求积分法。
贴一下大佬博客的链https://blog.csdn.net/Ab_Ever/article/details/76359591

double f(double x) {
	return sin(x);//需要进行积分的函数
}
double  Simpson(double l,double r) {
	double mid=(l+r)/2.0;
	return (f(l)+4.0*f(mid)+f(r))*(r-l)/6.0;
}
double solve(double l,double r,double eps/*精度要求*/) {
	double mid=(l+r)/2.0;
	double fq=Simpson(l,r);
	double fl=Simpson(l,mid);
	double fr=Simpson(mid,r);
	if(fabs(fq-fl-fr)<=15.0*eps)return fl+fr+(fl+fr-fq)/15.0;
	return solve(l,mid,eps/2.0)+solve(mid,r,eps/2.0);
}

你可能感兴趣的:(数论与数学,几何)