fork download
  1. // Hello. This is pratiikgoogler here. This code is written in order to find the fast fourier transform of a polynomial.
  2. // Special thanks to Cormen and Emaxx for elaborating the code and making life easy for me. hope this helps everyone.
  3. #include<iostream>
  4. #include<complex>
  5. #include<vector>
  6. #include<cmath>
  7. #define PI acos(-1)
  8. using namespace std;
  9. void fft(vector<complex<double> >a,bool show ){ // we take the complex vector although the coefficient are passed as integers but we cannot be sure that the recursive calls would take just integers. Just to be on the saf side, we have taken complex data type.
  10. int n = a.size();
  11. if(n==1)
  12. return;
  13. vector<complex<double> >a1(n/2),a2(n/2);
  14. for(int i=0;i*2<n;i++){
  15. a1[i] = a[2*i]; // As said in the blog, we take coefficients of even powers in one array and those of odd powers in another.
  16. a2[i] = a[2*i+1];// Done as said earlier.
  17. }
  18. fft(a1,0);// We compute the FFT of a1 recursively. Divide and Conquer.
  19. fft(a2,0);// We compute the FFT of a2 recursively. DAC .
  20. double angle = 2*PI/n; // initialise varaible to 2pi/n, to help in computing nth roots of unity.
  21. complex<double> w(1),wn(cos(angle),sin(angle));
  22. for(int i=0;i*2<n;i++){
  23. a[i] = a1[i] + w*a2[i];// one value of a[i]
  24. a[i+n/2] = a1[i] - w*a2[i];// rotate a[i] by 180 degrees and we get a[i+n/2]
  25. }
  26. w = w*wn;// we multiply with powers of omega hece this step.
  27. if(show)
  28. for(int i=0;i<n;i++)
  29. cout<<a[i]<<" ";// Display the values of the corresponding values of the fourier transform i.re, the points which satisfies the polynomial.
  30. }
  31. int main(){
  32. int n;
  33. cin>>n;
  34. vector<complex<double> >a;
  35. while(n--){
  36. int k;
  37. cin>>k;
  38. a.push_back(k);
  39. }
  40. fft(a,1);
  41. }
Runtime error #stdin #stdout #stderr 0s 4552KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
prog: malloc.c:2392: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.