HDU 1724 Ellipse (自适应辛普森积分)
题目链接:HDU 1724
Problem Description
Math is important!! Many students failed in 2+2’s mathematical test, so let's AC this problem to mourn for our lost youth..
Look this sample picture:
A ellipses in the plane and center in point O. the L,R lines will be vertical through the X-axis. The problem is calculating the blue intersection area. But calculating the intersection area is dull, so I have turn to you, a talent of programmer. Your task is tell me the result of calculations.(defined PI=3.14159265 , The area of an ellipse A=PIab )
Input
Input may contain multiple test cases. The first line is a positive integer N, denoting the number of test cases below. One case One line. The line will consist of a pair of integers a and b, denoting the ellipse equation \(\frac{x^2}{a^2} + \frac{y^2}{b^2} = 1\), A pair of integers l and r, mean the L is (l, 0) and R is (r, 0). (-a <= l <= r <= a).
Output
For each case, output one line containing a float, the area of the intersection, accurate to three decimals after the decimal point.
Sample Input
2
2 1 -2 2
2 1 0 2
Sample Output
6.283
3.142
Source
Solution
题意
给定椭圆和两条直线,求上图阴影部分的面积。
思路
自适应辛普森积分
Simpson 积分是数值计算中用来近似求解积分值的一种方法。公式如下:
\]
普通的 Simpson 积分误差比较大,一般使用自适应 Simpson 积分。
代码中的自适应 Simpson 积分来自 Kuangbin 的模板。
Code
#include <bits/stdc++.h>
using namespace std;
typedef double db;
const db eps = 1e-8;
db a, b, l, r;
db F(db x) {
return sqrt((1 - x * x / a / a) * b * b);
}
db simpson(db a, db b) {
db c = a + (b - a) / 2;
return (F(a) + 4 * F(c) + F(b)) * (b - a) / 6;
}
db asr(db a, db b, db eps, db A) {
db c = a + (b - a) / 2;
db L = simpson(a, c), R = simpson(c, b);
if(fabs(L + R - A) <= 15 * eps) return L + R + (L + R - A) / 15.0;
return asr(a, c, eps / 2, L) + asr(c, b, eps / 2, R);
}
db asr(db a, db b, db eps) {
return asr(a, b, eps, simpson(a, b));
}
int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%lf%lf%lf%lf", &a, &b, &l, &r);
printf("%.3lf\n", 2.0 * asr(l, r, eps));
}
return 0;
}
HDU 1724 Ellipse (自适应辛普森积分)的更多相关文章
- hdu 1724 Ellipse —— 自适应辛普森积分
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1724 函数都给出来了,可以用辛普森积分: 一开始 eps = 1e-8 TLE了,答案只要三位小数,那么 ...
- HDU - 1724 Ellipse 自适应辛普森模板
OJ 题解传送门 //Achen #include<algorithm> #include<iostream> #include<cstring> #include ...
- HDU 1724 Ellipse 自适应simpson积分
simpson公式是用于积分求解的比较简单的方法(有模板都简单…… 下面是simpson公式(很明显 这个公式的证明我并不会…… (盗图…… 因为一段函数基本不可能很规则 所以我们要用自适应积分的方法 ...
- hdu 1724 : Ellipse 【Simpson积分】
题目链接 题意:给出椭圆方程中的a和b,再给出l.r,求l到r的积分的二倍. 输出时要求精度控制为保留到小数点后3位,如下代码中,eps设为1e-9 1e-8时均TLE,1e-4可以AC,1e-3会W ...
- HDU 1724:Ellipse(自适应辛普森积分)
题目链接 题意 给出一个椭圆,问一个[l, r] 区间(蓝色区域)的面积是多少. 思路 自适应辛普森积分 具体一些分析如上. 很方便,套上公式就可以用了. 注意 eps 的取值影响了跑的时间,因为决定 ...
- hdu 1724 Ellipse simpson积分
/* hdu 1724 Ellipse simpson积分 求椭圆的部分面积 simpson积分法 http://zh.wikipedia.org/zh-tw/%E8%BE%9B%E6%99%AE%E ...
- 【自适应辛普森积分】hdu1724 Ellipse
Ellipse Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU 1724 Ellipse 【自适应Simpson积分】
Ellipse Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU 1724 Ellipse
Problem Description Math is important!! Many students failed in 2+2’s mathematical test, so let's AC ...
随机推荐
- dex2jar反编译大文件内存溢出的问题
@echo off REM better invocation scripts for windows from lanchon, release in public domain. thanks! ...
- sqlserver怎么copy一张表为另一张表
SQL Server中,如果目标表存在:insert into 目标表 select * from 原表;SQL Server中,,如果目标表不存在:select * into 目标表 from 原表 ...
- Linux对用户态的动态内存管理
Linux对内核态内存分配请求与用户态内存分配请求处理上分别对待 Linux本身信任自己,因此Linux内核请求分配多少内存,就会马上分配相同数量的内存出来. 但内核本身不相信应用程序,而且通常应用程 ...
- STL中的查找
一.查找 1.头文件 #include <algorithm> 2.使用方法 1.binary_search:查找某个元素是否出现.O(logn) a.函数模板:binary_search ...
- js 动态绑定解绑事件
function addEvent(obj, type, handle) { if (obj.addEventListener) { obj.addEventListener(type, handle ...
- 【记录】解决uni-app 用nginx反向代理出现Invalid Host header问题
之前解决过一次,后来给忘记了,今天又遇到这个问题,现记录一下 修改uni-app的manifest.json文件 - >源码视图 添加以下代码: "disableHostCheck& ...
- 愚蠢的sql语法错误(sum (xxx))
sum和()之间打了一个空格,导致一致报sql语法错误,看了半天不知道怎么回事orz
- Spring整合Struts2的配置与测试
整合目的 让Spring的IOC容器管理Struts2的Action 整合步骤 1.新建一个Web项目 2.加入Spring的jar包和添加Spring的配置文件 3.在Web.xml中配置Conte ...
- asp.net相关的一些代码
显示目录下的内容 using System.IO; DirectoryInfo di = new DirectoryInfo(Server.MapPath("Views/video" ...
- PostgreSQL按年月日分组(关键词:extract time as Year/Month/Day)
Select EXTRACT(year from cast(joindate as timestamp)) as Year, EXTRACT(month from cast(joindate as t ...