Advertisement
Guest User

#1

a guest
Jun 19th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct number {
  5.     double x, y;
  6.     number() {}
  7.     number(double a, double b) :
  8.         x(a),
  9.         y(b) {}
  10. };
  11.  
  12. number operator+(const number a, const number b) {
  13.     return {a.x + b.x, a.y + b.y};
  14. }
  15.  
  16. number operator-(const number a, const number b) {
  17.     return {a.x - b.x, a.y - b.y};
  18. }
  19.  
  20. number operator*(const number a, const number b) {
  21.     return {a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x};
  22. }
  23.  
  24. ostream &operator<<(ostream &out, const number a) {
  25.     return out << a.x << " " << a.y;
  26. }
  27.  
  28. const int N = 4;
  29.  
  30. number root[N];
  31.  
  32. void init_fft() {
  33.     for (int i = 0; i < N; ++i) {
  34.         double w = (double)i / N;
  35.         root[i] = {cos(w), sin(w)};
  36.     }
  37. }
  38.  
  39. vector<number> fft(vector<number> a) {
  40.     if (a.size() == 1) {
  41.         return a;
  42.     }
  43.     assert(a.size());
  44.     vector<number> a0, a1;
  45.     a0.reserve(a.size() / 2);
  46.     a1.reserve(a.size() / 2);
  47.     for (int i = 0; i < (int)a.size(); ++i) {
  48.         if (i & 1) {
  49.             a1.push_back(a[i]);
  50.         } else {
  51.             a0.push_back(a[i]);
  52.         }
  53.     }
  54.     a0 = fft(a0);
  55.     a1 = fft(a1);
  56.     int scale = N / (int)a.size();
  57.     int half = (int)a.size() / 2;
  58.     for (int i = 0; i < half; ++i) {
  59.         a[i]        = a0[i] + root[i * scale] * a1[i];
  60.         a[i + half] = a0[i] - root[i * scale] * a1[i];
  61.     }
  62.     return a;
  63. }
  64.  
  65. number a[N];
  66.  
  67. int main() {
  68.     ios::sync_with_stdio(0);
  69.     cin.tie(0);
  70.     cout << fixed << setprecision(1);
  71.     cerr << fixed << setprecision(1);
  72.    
  73.     a[0] = {1, 0};
  74.    
  75.     clock_t st = clock();
  76.    
  77.     init_fft();
  78.     vector<number> f = fft({a, a + N});
  79.    
  80.     clock_t fn = clock();
  81.    
  82.     cout << "---------" << endl;
  83.     for (int i = 0; i < N; ++i) {
  84.         cout << f[i] << endl;
  85.     }
  86.     cout << endl;
  87.    
  88.     cout << 1000 * (fn - st) / CLOCKS_PER_SEC << " ms\n";
  89.    
  90.    
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement