欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HDU6362 oval-and-rectangle(2018HDU多校联赛第六场,数学,积分)

程序员文章站 2022-06-09 19:36:41
...

Problem Description

Patrick Star find an oval.
The half of longer axes is on the x-axis with length a.
The half of shorter axes is on the y-axis with length b.
Patrick Star plan to choose a real number c randomly from [0,b], after that, Patrick Star will get a rectangle :
\1. The four vertexes of it are on the outline of the oval.
\2. The two sides of it parallel to coordinate axis.
\3. One of its side is y=c.
Patrick Star want to know the expectations of the rectangle’s perimeter.

Input

The first line contain a integer T (no morn than 10), the following is T test case, for each test case :
Each line contains contains two integer a, b (0

Output

For each test case output one line denotes the expectations of the rectangle’s perimeter .
You should keep exactly 6 decimal digits and ignore the remain decimal digits.
It is guaranted that the 7-th decimal digit of answer wont be 0 or 9.

Sample Input

1
2 1

Sample Output

8.283185

思路

留下了数学渣的泪水…

题目给了一个椭圆,然后让你求内接矩形的周长的期望,(a,b已知)

做法是先把椭圆的周长加起来再除以变化范围。

HDU6362 oval-and-rectangle(2018HDU多校联赛第六场,数学,积分)

比如这个图,我们知道焦点在x轴的椭圆的公式为x2a2+y2b2=1,而y的变化范围是[0,b],那么由公式变形可以知道:

x2=a2(1y2b2)

x=a1y2b2

然后就可以知道椭圆上的点的坐标为(a1y2b2,y)

那么周长为:

C=4(x+y)=4y+4a1y2b2

然后就需要求出积分:

y=0b(4y+4a1y2b2)dy

换元,设y=bsin(α), dy=bcos(α)dα.

原式 =α=01/2π(4bsin(α)+4acos(α))bcos(α)dα

=α=01/2π(2b2sin(2α)+2ab(cos(2α)+1))dα

=[b2cos(2α)+ab(sin(2α)+2α)]01/2π

=[b2+πab][b2]

=2b2+πab

因为变化范围是[0,b],所以最后算期望除以 b

=2b+πa

代码

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
const double pi=acos(-1.0);
const int N=1e5+10;
void solve()
{
    double a,b;
    scanf("%lf%lf",&a,&b);
    double ans=2*b+pi*a;
    printf("%.6f\n",ans-0.0000005);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)solve();
    return 0;
}
相关标签: 积分