洛谷P4525 【模板】自适应辛普森法1(simpson积分)

时间:2022-06-17
本文章向大家介绍洛谷P4525 【模板】自适应辛普森法1(simpson积分),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

题目描述

计算积分

结果保留至小数点后6位。

数据保证计算过程中分母不为0且积分能够收敛。

输入输出格式

输入格式:

一行,包含6个实数a,b,c,d,L,R

输出格式:

一行,积分值,保留至小数点后6位。

输入输出样例

输入样例#1: 复制

1 2 3 4 5 6

输出样例#1: 复制

2.732937

说明

a,b,c,d∈[-10,10]

-100≤L<R≤100 且 R-L≥1

辛普森积分是用$y = Ax^2 +Bx +c$去拟合给定的函数

$$int_a^bf(x)dxapproxfrac{(b-a)(f(a)+f(b)+4f(frac{a+b}{2}))}{6}$$

自适应的含义是根据不同的区间大小选用不同的eps

// luogu-judger-enable-o2
#include<cstdio>
#include<cmath>
double a, b, c, d, L, R;
double F(double x) {
    return (c * x + d) / (a * x + b);
}
double sim(double l, double r) {
    return (F(l) + F(r) + 4 * F((l + r) / 2)) * (r - l) / 6;
}
double asr(double L, double R, double eps, double ans) {
    double mid = (L + R) / 2;
    double LL = sim(L, mid), RR = sim(mid, R);
    if(fabs(LL + RR - ans) < eps) return LL + RR;
    else return asr(L, mid, eps / 2, sim(L, mid)) + asr(mid, R, eps / 2, sim(mid, R));
}
main() {
    #ifdef WIN32
    freopen("a.in", "r", stdin);
    #endif
    scanf("%lf %lf %lf %lf %lf %lf", &a, &b, &c, &d, &L, &R);
    printf("%lf", asr(L, R, 1e-6, sim(L, R)));
}