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

Simpson's rule - Wikipedia, the free encyclopedia

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

1071 ( The area )

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cmath>
  6.  
  7. using namespace std;
  8.  
  9. const double EPS = 1e-;
  10. double A, B, C, P, Q;
  11.  
  12. template<class T> T sqr(T x) { return x * x;}
  13. inline double cal(double x) { return A * sqr(x) + (B - P) * x + C - Q;}
  14. inline double sps(double l, double r) { return (cal(l) + cal(r) + * cal((l + r) / )) / * (r - l);}
  15.  
  16. double work(double l, double r) {
  17. //cout << l << ' ' << r << endl;
  18. double ans = sps(l, r), m = (l + r) / ;
  19. if (fabs(ans - sps(l, m) - sps(m, r)) < EPS) return ans;
  20. else return work(l, m) + work(m, r);
  21. }
  22.  
  23. int main() {
  24. int T;
  25. double l, r;
  26. double x[], y[];
  27. cin >> T;
  28. while (T--) {
  29. for (int i = ; i < ; i++) cin >> x[i] >> y[i];
  30. double p[], q[], d[];
  31. 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 + ];
  32. A = (q[] * d[] - q[] * d[]) / (p[] * q[] - p[] * q[]);
  33. B = (p[] * d[] - p[] * d[]) / (p[] * q[] - p[] * q[]);
  34. C = y[] - B * x[] - A * sqr(x[]);
  35. //cout << A << ' ' << B << ' ' << C << endl;
  36. P = (y[] - y[]) / (x[] - x[]);
  37. Q = y[] - P * x[];
  38. //cout << P << ' ' << Q << endl;
  39. printf("%.2f\n", work(x[], x[]));
  40. }
  41. return ;
  42. }

1724 ( Ellipse )

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cmath>
  6.  
  7. using namespace std;
  8.  
  9. const double EPS = 1e-;
  10. double A, B;
  11.  
  12. template<class T> T sqr(T x) { return x * x;}
  13. inline double cal(double x) { return * B * sqrt( - sqr(x) / sqr(A));}
  14. inline double sps(double l, double r) { return (cal(l) + cal(r) + * cal((l + r) / )) / * (r - l);}
  15.  
  16. double work(double l, double r) {
  17. //cout << l << ' ' << r << endl;
  18. double ans = sps(l, r), m = (l + r) / ;
  19. if (fabs(ans - sps(l, m) - sps(m, r)) < EPS) return ans;
  20. else return work(l, m) + work(m, r);
  21. }
  22.  
  23. int main() {
  24. int T;
  25. double l, r;
  26. cin >> T;
  27. while (T-- && cin >> A >> B >> l >> r) printf("%.3f\n", work(l, r));
  28. return ;
  29. }

  之后还有题会继续更新。

UPD:

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

ACM-ICPC Live Archive

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <cmath>
  6.  
  7. using namespace std;
  8.  
  9. double coe[][];
  10. const double EPS = 1e-;
  11.  
  12. int k;
  13. double cal(double x, double *c) {
  14. double ret = c[];
  15. for (int i = ; i <= k; i++) ret *= x, ret += c[i];
  16. return ret;
  17. }
  18.  
  19. inline double cal(double x, double *p, double *q) { return cal(x, p) / cal(x, q);}
  20. inline double cal(double x, double y, double *p, double *q) { return max(cal(x, p, q) - y, 0.0);}
  21. 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) / ;}
  22.  
  23. inline double getpart(double y, double l, double r, double *p, double *q) {
  24. double sum = simpson(y, l, r, p, q);
  25. //cout << l << ' ' << r << ' ' << sum << endl;
  26. if (fabs(sum - simpson(y, l, (l + r) / , p, q) - simpson(y, (l + r) / , r, p, q)) < EPS) return sum;
  27. return getpart(y, l, (l + r) / , p, q) + getpart(y, (l + r) / , r, p, q);
  28. }
  29.  
  30. inline double getarea(double y, double l, double r, double *p, double *q) {
  31. double ret = , d = (r - l) / ;
  32. for (int i = ; i < ; i++) {
  33. ret += getpart(y, l + d * i, l + d * (i + ), p, q);
  34. }
  35. return ret;
  36. }
  37.  
  38. double dc2(double l, double r, double a, double w) {
  39. double m;
  40. while (r - l > EPS) {
  41. m = (l + r) / 2.0;
  42. //cout << m << ' ' << getarea(m, 0, w, coe[0], coe[1]) - getarea(m, 0, w, coe[2], coe[3]) << endl;
  43. if (getarea(m, , w, coe[], coe[]) - getarea(m, , w, coe[], coe[]) > a) l = m;
  44. else r = m;
  45. }
  46. return l;
  47. }
  48.  
  49. int main() {
  50. //freopen("in", "r", stdin);
  51. //freopen("out", "w", stdout);
  52. double w, d, a;
  53. while (cin >> w >> d >> a >> k) {
  54. for (int i = ; i < ; i++) for (int j = ; j <= k; j++) cin >> coe[i][j];
  55. for (int i = ; i < ; i++) reverse(coe[i], coe[i] + k + );
  56. //cout << getarea(-5.51389, 0, w, coe[0], coe[1]) - getarea(-5.51389, 0, w, coe[2], coe[3]) << endl;
  57. //cout << cal(3, coe[0], coe[1]) << endl;
  58. printf("%.5f\n", -dc2(-d, , a, w));
  59. }
  60. return ;
  61. }

——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. linux 下建立桌面快捷方式

    这段时间从windows转到了Linux,发现桌面上没有快捷方式很不适应,找了好久资料,找到解决方法,记录下来以后备用 1.首先建立一个新文件 ``` vi quick.desktop //后缀为de ...

  2. 在Mac下安装MySQL

    在Mac下安装MySQL   最近开始将开发工具都转移到 Mac 上了,其中也会莫名其妙的遇到一些坑,不如干脆将整个流程都记录下来,方便以后查找. 下载与安装 首先进入 MySQL 官网,选择免费的C ...

  3. Uva1252 Twenty Questions

    Twenty Questions https://odzkskevi.qnssl.com/15b7eb4cd1f75f63cee3945b0b845e4f?v=1508411736 [题解] dp[S ...

  4. vue 报错解决:TypeError: Cannot read property '_t' of undefined"

    前端报错如下: [Vue warn]: Error in render: "TypeError: Cannot read property '_t' of undefined" 是 ...

  5. WordPress资料收集,以后整理

    WordPress主题开发:实现分页功能 http://www.cnblogs.com/tinyphp/p/6361901.html WordPress如何调取显示指定文章 https://www.d ...

  6. JetBrains PyCharm 2017.2 字体放大缩小 功能

  7. Openck_Swift源代码分析——添加、删除设备时算法详细的实现过程

    1 初始加入设备后.上传Object的详细流程  前几篇博客中,我们讲到环的基本原理即详细的实现过程,加入我们在初始创建Ring是执行例如以下几条命令: •swift-ring-builder obj ...

  8. Leetcode867.Transpose Matrix转置矩阵

    给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引. 示例 1: 输入:[[1,2,3],[4,5,6],[7,8,9]] 输出:[[1,4,7] ...

  9. windows10 中微信(UWP)版本不显示通知消息

    前言: 前段时间笔者更换了升级了WINDOWS10系统,从应用商店安装微信后,使用期间不会推送消息通知,右下角的通知栏也无法添加微信图标.搜索百度和Google后,发现很多人都是这样,这是微信(UWP ...

  10. DOM修改元素的方法总结

    今天我们要谈谈DOM元素的修改(包括修改内容,属性,样式).修改内容的方法----3种:elem.innerHTML:获取或设置元素开始标签到结束标签之间的原始HTML代码片段:elem.textCo ...