辛普森积分法 - 维基百科,自由的百科全书

Simpson's rule - Wikipedia, the free encyclopedia

  利用这个公式,用二分的方法来计算积分。

1071 ( The area )

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath> using namespace std; const double EPS = 1e-;
double A, B, C, P, Q; template<class T> T sqr(T x) { return x * x;}
inline double cal(double x) { return A * sqr(x) + (B - P) * x + C - Q;}
inline double sps(double l, double r) { return (cal(l) + cal(r) + * cal((l + r) / )) / * (r - l);} double work(double l, double r) {
//cout << l << ' ' << r << endl;
double ans = sps(l, r), m = (l + r) / ;
if (fabs(ans - sps(l, m) - sps(m, r)) < EPS) return ans;
else return work(l, m) + work(m, r);
} int main() {
int T;
double l, r;
double x[], y[];
cin >> T;
while (T--) {
for (int i = ; i < ; i++) cin >> x[i] >> y[i];
double p[], q[], d[];
for (int i = ; i < ; i++) p[i] = sqr(x[i]) - sqr(x[i + ]), q[i] = x[i] - x[i + ], d[i] = y[i] - y[i + ];
A = (q[] * d[] - q[] * d[]) / (p[] * q[] - p[] * q[]);
B = (p[] * d[] - p[] * d[]) / (p[] * q[] - p[] * q[]);
C = y[] - B * x[] - A * sqr(x[]);
//cout << A << ' ' << B << ' ' << C << endl;
P = (y[] - y[]) / (x[] - x[]);
Q = y[] - P * x[];
//cout << P << ' ' << Q << endl;
printf("%.2f\n", work(x[], x[]));
}
return ;
}

1724 ( Ellipse )

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath> using namespace std; const double EPS = 1e-;
double A, B; template<class T> T sqr(T x) { return x * x;}
inline double cal(double x) { return * B * sqrt( - sqr(x) / sqr(A));}
inline double sps(double l, double r) { return (cal(l) + cal(r) + * cal((l + r) / )) / * (r - l);} double work(double l, double r) {
//cout << l << ' ' << r << endl;
double ans = sps(l, r), m = (l + r) / ;
if (fabs(ans - sps(l, m) - sps(m, r)) < EPS) return ans;
else return work(l, m) + work(m, r);
} int main() {
int T;
double l, r;
cin >> T;
while (T-- && cin >> A >> B >> l >> r) printf("%.3f\n", work(l, r));
return ;
}

  之后还有题会继续更新。

UPD:

  就是因为见过这题,所以才学这个公式的。1y~

ACM-ICPC Live Archive

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath> using namespace std; double coe[][];
const double EPS = 1e-; int k;
double cal(double x, double *c) {
double ret = c[];
for (int i = ; i <= k; i++) ret *= x, ret += c[i];
return ret;
} inline double cal(double x, double *p, double *q) { return cal(x, p) / cal(x, q);}
inline double cal(double x, double y, double *p, double *q) { return max(cal(x, p, q) - y, 0.0);}
inline double simpson(double y, double l, double r, double *p, double *q) { return (cal(l, y, p, q) + cal(r, y, p, q) + * cal((l + r) / , y, p, q)) * (r - l) / ;} inline double getpart(double y, double l, double r, double *p, double *q) {
double sum = simpson(y, l, r, p, q);
//cout << l << ' ' << r << ' ' << sum << endl;
if (fabs(sum - simpson(y, l, (l + r) / , p, q) - simpson(y, (l + r) / , r, p, q)) < EPS) return sum;
return getpart(y, l, (l + r) / , p, q) + getpart(y, (l + r) / , r, p, q);
} inline double getarea(double y, double l, double r, double *p, double *q) {
double ret = , d = (r - l) / ;
for (int i = ; i < ; i++) {
ret += getpart(y, l + d * i, l + d * (i + ), p, q);
}
return ret;
} double dc2(double l, double r, double a, double w) {
double m;
while (r - l > EPS) {
m = (l + r) / 2.0;
//cout << m << ' ' << getarea(m, 0, w, coe[0], coe[1]) - getarea(m, 0, w, coe[2], coe[3]) << endl;
if (getarea(m, , w, coe[], coe[]) - getarea(m, , w, coe[], coe[]) > a) l = m;
else r = m;
}
return l;
} int main() {
//freopen("in", "r", stdin);
//freopen("out", "w", stdout);
double w, d, a;
while (cin >> w >> d >> a >> k) {
for (int i = ; i < ; i++) for (int j = ; j <= k; j++) cin >> coe[i][j];
for (int i = ; i < ; i++) reverse(coe[i], coe[i] + k + );
//cout << getarea(-5.51389, 0, w, coe[0], coe[1]) - getarea(-5.51389, 0, w, coe[2], coe[3]) << endl;
//cout << cal(3, coe[0], coe[1]) << endl;
printf("%.5f\n", -dc2(-d, , a, w));
}
return ;
}

——written by Lyon

Simpson公式的应用(HDU 1724/ HDU 1071)的更多相关文章

  1. HDU 1724 Ellipse 【自适应Simpson积分】

    Ellipse Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  2. hdu 1724 Ellipse simpson积分

    /* hdu 1724 Ellipse simpson积分 求椭圆的部分面积 simpson积分法 http://zh.wikipedia.org/zh-tw/%E8%BE%9B%E6%99%AE%E ...

  3. HDU 1724 Ellipse 自适应simpson积分

    simpson公式是用于积分求解的比较简单的方法(有模板都简单…… 下面是simpson公式(很明显 这个公式的证明我并不会…… (盗图…… 因为一段函数基本不可能很规则 所以我们要用自适应积分的方法 ...

  4. HDU 1724 Ellipse (自适应辛普森积分)

    题目链接:HDU 1724 Problem Description Math is important!! Many students failed in 2+2's mathematical tes ...

  5. HDU 1724:Ellipse(自适应辛普森积分)

    题目链接 题意 给出一个椭圆,问一个[l, r] 区间(蓝色区域)的面积是多少. 思路 自适应辛普森积分 具体一些分析如上. 很方便,套上公式就可以用了. 注意 eps 的取值影响了跑的时间,因为决定 ...

  6. HDU 1724 自适应辛普森法

    //很裸的积分题,直接上模板 #include<stdio.h> #include<math.h> int aa, bb; //函数 double F(double x){ - ...

  7. csu 1806 & csu 1742 (simpson公式+最短路)

    1806: Toll Time Limit: 5 Sec  Memory Limit: 128 MB  Special JudgeSubmit: 256  Solved: 74[Submit][Sta ...

  8. simpson公式求定积分(模板)

    #include<cstdio> #include<cmath> #include <algorithm> using namespace std; double ...

  9. HDU - 2222,HDU - 2896,HDU - 3065,ZOJ - 3430 AC自动机求文本串和模式串信息(模板题)

    最近正在学AC自动机,按照惯例需要刷一套kuangbin的AC自动机专题巩固 在网上看过很多模板,感觉kuangbin大神的模板最为简洁,于是就选择了用kuangbin大神的模板. AC自动机其实就是 ...

随机推荐

  1. NLog系列之NLong.config变量配置篇

    $ {cached} -  将缓存应用于另一个布局输出. $ {db-null} - 为数据库渲染DbNull $ {exception} - 通过调用Logger方法之一提供的异常信息 $ {lev ...

  2. oracle-PL/SQL2

    一 存储过程 存储过程是SQL 语句和可选控制流语句的预编译集合,以一个名称存储并作为一个单元处理.在 ORACLE SERVER上建立存储过程,可以被多个应用程序调用,可以向存储过程传递参数,也可以 ...

  3. Codevs1922 骑士共存问题

    1922 骑士共存问题 题目描述 Description 在一个n*n个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示.棋盘上某些方格设置了障碍,骑士不得进入. 对于给定的n*n个方格的国 ...

  4. 【react】react-bookManager

    作者可能是本意想要做一个图书管理系统,不过添加书籍的时候报错,所以简单的页面我们简单的看看 先上github地址:https://github.com/hesisi/react-bookManager ...

  5. web前端学习(二)html学习笔记部分(1) -- html5新增的元素及特性等等

    检查,在浏览器中可以调整设备类型 html5实现水池效果. lang:en为英文语言,中文语言zh <html lang="en"> <head> < ...

  6. Java static 关键字学习

    static:意为静态的,简单理解就是公共的.独立于实例变量之外的1.概述:static是Java中常用的关键字,一般用于变量.方法.静态代码块.内部类上.静态导包2.用法: a.用于变量上表示该变量 ...

  7. 使用Redis管道提升性能

    首发于 樊浩柏科学院 Redis 的 管道 (pipelining)是用来打包多条无关命令批量执行,以减少多个命令分别执行带来的网络交互时间.在一些批量操作数据的场景,使用管道可以显著提升 Redis ...

  8. Kubernetes 调度器实现初探

    Kubernetes 调度器 Kubernetes 是一个基于容器的分布式调度器,实现了自己的调度模块.在Kubernetes集群中,调度器作为一个独立模块通过pod运行.从几个方面介绍Kuberne ...

  9. 给大家分享一张CSS选择器优选级图谱 !

  10. oracle-ORA-27102错误

    out of memory HP-UX Error: 12: Not enough space ORA-30019: Illegal rollback Segment operation in Aut ...