luogu 自适应Simpson2

自适应simpson2

题意

求一个不定积分

解法

画出函数的图像,可以知道其在0处函数值趋近于 $ + \infty $,在10处趋近于0,所以我们从0积分到10就可以了(保险起见,积到15)

代码如下:

#include <iostream>
#include <cstdio>
#include <cctype>
#include <cmath>
using namespace std;
typedef long long ll;
template<typename T>
inline void read(T&x)
{
    x=0;T k=1;char c=getchar();
    while(!isdigit(c)){if(c=='-')k=-1;c=getchar();}
    while(isdigit(c)){x=x*10+c-'0';c=getchar();}x*=k;
}
const double eps=1e-7;
double a;
double F(double x){
    return pow(x,a/x-x);
}
double simpson(double a ,  double b){
    double mid=(a+b)/2;
    return  (F(a) + 4*F(mid) + F(b) ) * (b-a)/6 ;
}
double asr(double a,double b,double e,double A){
    double mid = (a+b)/2;
    double L = simpson(a,mid) , R = simpson(mid,b);
    if(fabs(L+R-A) <= 15*eps ) return L+R+(L+R-A)/15.0;
    return asr(a,mid,e/2,L) + asr(mid,b,e/2,R);
}
double asr(double l,double r) {
    return asr(l,r,eps,simpson(l,r));
}

int main()
{
    scanf("%lf",&a);
    if(a<0) printf("orz");
    else printf("%.5lf",asr(eps,15.0)); 
    return 0;
}
posted @ 2018-08-22 19:54  Mr_asd  阅读(76)  评论(0编辑  收藏  举报