Advertisement
coloriot

HA54

Jun 7th, 2025
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. const double PI = acos(-1.0);
  7.  
  8. class FFT {
  9. private:
  10.     struct Complex {
  11.         double re, im;
  12.         Complex(double r = 0.0, double i = 0.0) : re(r), im(i) {}
  13.  
  14.         Complex operator+(const Complex& other) const {
  15.             return Complex(re + other.re, im + other.im);
  16.         }
  17.  
  18.         Complex operator-(const Complex& other) const {
  19.             return Complex(re - other.re, im - other.im);
  20.         }
  21.  
  22.         Complex operator*(const Complex& other) const {
  23.             return Complex(re * other.re - im * other.im,
  24.                            re * other.im + im * other.re);
  25.         }
  26.  
  27.         Complex conj() const { // Сопряжение
  28.             return Complex(re, -im);
  29.         }
  30.     };
  31.  
  32.     // Инверсия индексов
  33.     void bit_reverse(vector<Complex>& a) {
  34.         int n = a.size(), logn = 0;
  35.         while ((1 << logn) < n) ++logn;
  36.         for (int i = 0; i < n; ++i) {
  37.             int j = 0;
  38.             for (int k = 0; k < logn; ++k)
  39.                 if (i & (1 << k))
  40.                     j |= (1 << (logn - 1 - k));
  41.             if (i < j)
  42.                 swap(a[i], a[j]);
  43.         }
  44.     }
  45.  
  46.     // Быстрое преобразование Фурье
  47.     void fft(vector<Complex>& a, bool invert) {
  48.         int n = a.size();
  49.         bit_reverse(a);
  50.  
  51.         for (int len = 2; len <= n; len <<= 1) {
  52.             double ang = 2 * PI / len * (invert ? -1 : 1);
  53.             Complex wlen(cos(ang), sin(ang));
  54.             for (int i = 0; i < n; i += len) {
  55.                 Complex w(1, 0);
  56.                 for (int j = 0; j < len / 2; ++j) {
  57.                     Complex u = a[i + j];
  58.                     Complex v = a[i + j + len / 2] * w;
  59.                     a[i + j] = u + v;
  60.                     a[i + j + len / 2] = u - v;
  61.                     w = w * wlen;
  62.                 }
  63.             }
  64.         }
  65.  
  66.         if (invert) {
  67.             for (auto& x : a) {
  68.                 x.re /= n;
  69.                 x.im /= n;
  70.             }
  71.         }
  72.     }
  73.  
  74. public:
  75.     // Умножение двух полиномов через БПФ
  76.     vector<double> multiply(const vector<double>& a, const vector<double>& b) {
  77.         vector<Complex> fa(a.begin(), a.end()), fb(b.begin(), b.end());
  78.         int n = 1;
  79.         while (n < a.size() + b.size()) n <<= 1;
  80.         fa.resize(n);
  81.         fb.resize(n);
  82.  
  83.         fft(fa, false);
  84.         fft(fb, false);
  85.         for (int i = 0; i < n; ++i)
  86.             fa[i] = fa[i] * fb[i];
  87.         fft(fa, true);
  88.  
  89.         vector<double> result(n);
  90.         for (int i = 0; i < n; ++i)
  91.             result[i] = round(fa[i].re); // Округляем
  92.  
  93.         // Убираем хвостовые нули
  94.         while (!result.empty() && fabs(result.back()) < 1e-9)
  95.             result.pop_back();
  96.  
  97.         return result;
  98.     }
  99. };
  100.  
  101. int main() {
  102.     // Полиномы: (1 + 2x + 3x^2) * (4 + 5x)
  103.     vector<double> A = {1, 2, 3};  // 1 + 2x + 3x²
  104.     vector<double> B = {4, 5};     // 4 + 5x
  105.  
  106.     FFT fft;
  107.     vector<double> result = fft.multiply(A, B);
  108.  
  109.     cout << "Result:\n";
  110.     for (int i = 0; i < result.size(); ++i) {
  111.         cout << result[i] << (i ? "x^" + to_string(i) : "") << " ";
  112.         if (i + 1 < result.size()) cout << "+ ";
  113.     }
  114.     cout << '\n';
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement