P4526 【模板】自适应辛普森法2

P4526 【模板】自适应辛普森法2

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const double eps = 1e-8;
 4 double a;
 5 inline double f(double x) {
 6     return pow(x,a/x-x);
 7 }
 8 inline double simpson(double l, double r) {
 9     double mid = (l+r)/2;
10     return (f(l)+4*f(mid)+f(r))*(r-l)/6;
11 }
12 inline double asr(double l, double r, double eps, double ans) {
13     double mid = (l+r)/2;
14     double ll = simpson(l,mid), rr = simpson(mid,r);
15     if (fabs(ll+rr-ans) <= 15*eps) return ll+rr+(ll+rr-ans)/15;
16     return asr(l,mid,eps/2,ll)+asr(mid,r,eps/2,rr);
17 }
18 inline double asr(double l, double r, double eps) {
19     return asr(l,r,eps,simpson(l,r));
20 }
21 int main() {
22     scanf("%lf",&a);
23     if (a < 0) puts("orz");
24     else printf("%.5f\n",asr(eps,20.0,eps));
25     return 0;
26 }

 

posted @ 2019-11-01 19:47  麻辣猪仔  阅读(156)  评论(0编辑  收藏  举报