Guest User

Untitled

a guest
Jul 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #define cpl complex<double>
  2. #define vcpl vector<cpl>
  3. const double pi = atan(1) * 4;
  4.  
  5. inline vcpl fftmul(vcpl u, vcpl v) {
  6. unsigned cnt = 0;
  7. while ((1u << cnt) < max(u.size(), v.size())) ++cnt;
  8. ++cnt;
  9.  
  10. auto fft = [&](vector<cpl>& a, bool inv) -> void {
  11. vcpl b = a;
  12. int n = 1 << cnt;
  13. for (int i = n; i--; ) {
  14. int f = 0, t = cnt;
  15. while (t--) f |= ((i >> t) & 1) << (cnt - t - 1);
  16. a[i] = b[f];
  17. }
  18.  
  19. for (int step = 2; step <= n; step <<= 1) {
  20. for (int i = 0; i < n; i += step) {
  21. double ang = 2 * pi / step * (inv ? -1 : 1);
  22. cpl w(1), pw(cos(ang), sin(ang));
  23. for (int f = 0; f < step / 2; ++f) {
  24. cpl x = a[i + f], y = a[i + f + step / 2] * w;
  25. a[i + f] = x + y;
  26. a[i + f + step / 2] = x - y;
  27. w *= pw;
  28. }
  29. }
  30. }
  31. if (inv) for (int i = n; i--; ) a[i] /= n;
  32. };
  33.  
  34. u.resize(1 << cnt);
  35. v.resize(1 << cnt);
  36.  
  37. fft(u, 0);
  38. fft(v, 0);
  39. for (int i = (1 << cnt); i--; ) u[i] *= v[i];
  40. fft(u, 1);
  41. return u;
  42. }
Add Comment
Please, Sign In to add comment