Mlxa

Сумма k-ых степеней

Oct 7th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define all(x) begin(x), end(x)
  3. using namespace std;
  4. typedef long long ll;
  5. typedef long double ld;
  6. #define all(x) begin(x), end(x)
  7.  
  8. struct poly {
  9.     vector<ld> a;
  10.     poly() {}
  11.     poly(vector<ld> b) : a(b) {}
  12.     int size() {
  13.         return (int)a.size();
  14.     }
  15.     ld &operator[](int i) {
  16.         assert(0 <= i && i < size());
  17.         return a[i];
  18.     }
  19.     void pop_zero() {
  20.         while (a.back() == 0) {
  21.             a.pop_back();
  22.         }
  23.     }
  24.     ld operator()(ld x) {
  25.         ld pw = 1, ans = 0;
  26.         for (int i = 0; i < size(); ++i) {
  27.             ans += pw * a[i];
  28.             pw *= x;
  29.         }
  30.         return ans;
  31.     }
  32. };
  33.  
  34. poly operator*(poly a, poly b) {
  35.     poly c;
  36.     c.a.assign(a.size() + b.size(), 0);
  37.     for (int i = 0; i < a.size(); ++i) {
  38.         for (int j = 0; j < b.size(); ++j) {
  39.             c[i + j] += a[i] * b[j];
  40.         }
  41.     }
  42.     c.pop_zero();
  43.     return c;
  44. }
  45.  
  46. poly operator+(poly a, poly b) {
  47.     poly c;
  48.     c.a.assign(max(a.size(), b.size()) + 1, 0);
  49.     for (int i = 0; i < a.size(); ++i) {
  50.         c[i] += a[i];
  51.     }
  52.     for (int j = 0; j < b.size(); ++j) {
  53.         c[j] += b[j];
  54.     }
  55.     c.pop_zero();
  56.     return c;
  57. }
  58.  
  59. signed main() {
  60. #ifdef LC
  61.     assert(freopen("input.txt", "r", stdin));
  62. #endif
  63.     ios::sync_with_stdio(0);
  64.     cin.tie(0);
  65.    
  66.     int n = 6;
  67.     vector<ld> x(n), y(n);
  68.     for (int i = 0; i < n; ++i) {
  69.         x[i] = i;
  70.         y[i] = powl(i, n - 2);
  71.         if (i) {
  72.             y[i] += y[i - 1];
  73.         }
  74.     }
  75.    
  76.     poly p;
  77.    
  78.     for (int i = 0; i < n; ++i) {
  79.         poly cur({1});
  80.         ld down = 1;
  81.         for (int j = 0; j < n; ++j) {
  82.             if (i == j) {
  83.                 continue;
  84.             }
  85.             cur = cur * poly({-x[j], 1});
  86.             down *= x[i] - x[j];
  87.         }
  88.         for (ld &e : cur.a) {
  89.             e *= y[i] / down;
  90.         }
  91.         p = p + cur;
  92.     }
  93.    
  94.     for (int i = 0; i < p.size(); ++i) {
  95.         cout << fixed << setprecision(6) << p[i] << " x^" << i << endl;
  96.     }
  97.    
  98.     const ld EPS = 1e-9;
  99.     mt19937 rnd;
  100.     vector<ld> roots;
  101.    
  102.     while (clock() < CLOCKS_PER_SEC) {
  103.         ld x = rnd() / ld(rnd() + 1);
  104.         ld f = abs(p(x));
  105.         for (ld st = 1; st > 1e-12; st *= 0.9) {
  106.             while (1) {
  107.                 ld c = abs(p(x - st));
  108.                 if (f > c) {
  109.                     f = c;
  110.                     x -= st;
  111.                 } else {
  112.                     break;
  113.                 }
  114.             }
  115.             while (1) {
  116.                 ld c = abs(p(x + st));
  117.                 if (f > c) {
  118.                     f = c;
  119.                     x += st;
  120.                 } else {
  121.                     break;
  122.                 }
  123.             }
  124.         }
  125.         assert(abs(p(x)) < EPS);
  126.         bool already = false;
  127.         for (ld e : roots) {
  128.             if (abs(e - x) < EPS) {
  129.                 already = true;
  130.             }
  131.         }
  132.         if (!already) {
  133.             roots.push_back(x);
  134.         }
  135.     }
  136.     sort(all(roots));
  137.     for (ld e : roots) {
  138.         cout << e << endl;
  139.     }
  140.     return 0;
  141. }
Add Comment
Please, Sign In to add comment