fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const long double PI = acos(-1);
  5. int N, A, B, slices, stacks;
  6. int coef[100], sq_coef[100];
  7. double ind_coef[100];
  8.  
  9. double power_(double x, int y) {
  10. double ans = 1;
  11. for (int i = 0; i < y; i++)
  12. ans *= x;
  13. return ans;
  14. }
  15.  
  16. void square_pol() {
  17. memset(sq_coef, 0, sizeof(sq_coef));
  18.  
  19. for (int i = 0; i <= N; i++) {
  20. for (int k = 0; k <= N; k++) {
  21. int c = i + k;
  22. sq_coef[c] += coef[i] * coef[k];
  23. }
  24. }
  25. }
  26.  
  27. double evaluate(double x) {
  28. double ans = 0;
  29. for (int i = 0; i <= N; i++) {
  30. ans += coef[i] * power_(x, i);
  31. }
  32. return ans;
  33. }
  34.  
  35. double indefinite_evaluate(double x) {
  36. double ans = 0;
  37. for (int i = 0; i <= 2*N + 1; i++) {
  38. ans += ind_coef[i] * power_(x, i);
  39. }
  40. return ans;
  41. }
  42.  
  43. void indefinite_integral() {
  44. for (int i = 2*N + 1; i > 0; i--) {
  45. ind_coef[i] = sq_coef[i-1]*1.0 / (i);
  46. }
  47. }
  48.  
  49. double definite_integral() {
  50. square_pol();
  51. indefinite_integral();
  52. return PI * (indefinite_evaluate(B) - indefinite_evaluate(A));
  53. }
  54.  
  55. double approximate() {
  56. double theta = 2*PI / slices;
  57. double width = (B - A)* (1.0) / stacks * 1.0;
  58.  
  59. double volume = 0;
  60. double start_ = A, end_ = A + width;
  61. for (int i = 0; i < stacks; i++) {
  62.  
  63. double px1 = evaluate(start_);
  64. double px2 = evaluate(end_);
  65. double m = (px2 - px1) / (end_ - start_);
  66. double c = px2 - m * end_;
  67.  
  68. volume += (end_ - start_) * c * c;
  69. volume += m * c * (end_ * end_ - start_ * start_);
  70. volume += m * m * (end_*end_*end_ - start_*start_*start_) / 3;
  71.  
  72. start_ += width;
  73. end_ += width;
  74. }
  75.  
  76. return volume * sin(theta) / 2;
  77. }
  78.  
  79. int main() {
  80.  
  81. int T; cin >> T;
  82. for (int t = 0; t < T; t++) {
  83. cin >> N;
  84. for (int i = N; i >= 0; i--) {
  85. cin >> coef[i];
  86. }
  87.  
  88. cin >> A >> B;
  89. cin >> slices >> stacks;
  90.  
  91. double real_volume = definite_integral();
  92. double app_volume = approximate() * slices;
  93.  
  94. cout << "Case " << t + 1 << ": ";
  95. cout << fixed << setprecision(4);
  96. cout << fabs((real_volume - app_volume) / real_volume) * 100.0 << "\n";
  97. }
  98.  
  99. return 0;
  100. }
Success #stdin #stdout 0s 3476KB
stdin
2 2 1 -4 5 1 4 4 3 1 1 0 1 4 4 3 
stdout
Case 1: 27.9042
Case 2: 36.3380