luogu 自适应Simpson1

自适应simpson1

题意

求一个定积分 (可以手推公式,但是我不想推怎么办)

解法

用一个又一个的二次函数覆盖原函数,则可以近似的得到原函数的积分。(这就是Simpson)

模板在下面:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#define INF 2139062143
#define MAX 0x7ffffffffffffff
#define del(a,b) memset(a,b,sizeof(a))
#define Rint register int
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;
}
double a1,b1,c1,d1;
double F(double x){
	return (c1*x + d1)/(a1*x + b1);
}
double simpson(double a ,  double b){
	double mid=(a+b)/2;
	return (b-a)/6 * (F(a) + 4*F(mid) + F(b) );
}
double asr(double a,double b,double eps,double A){
	double c = (a+b)/2;
	double L = simpson(a,c) , R = simpson(c,b);
	if(fabs(L+R-A) <= 15*eps ) return L+R+(L+R-A)/15;
	return asr(a,c,eps/2,L) + asr(c,b,eps/2,R);
}
double asr(double l,double r,double eps) {
    return asr(l,r,eps,simpson(l,r));
}
double l,r;
int main()
{
	scanf("%lf%lf%lf%lf%lf%lf",&a1,&b1,&c1,&d1,&l,&r);
	printf("%.6lf",asr(l,r,1e-6)); 
	return 0;
}

posted @ 2018-08-22 19:51  Mr_asd  阅读(73)  评论(0编辑  收藏  举报