快速傅里叶变换(FFT)板子
2017-11-28 19:45
246 查看
递归版
//UOJ 34 //Luogu 3803 //By Hany01 #include<bits/stdc++.h> #define For(i ,j , k) for (int i = (j) ; i <= (k) ; ++ i) #ifdef hany01 #define debug(...) fprintf(stderr , __VA_ARGS__) #else #define debug(...) #endif typedef long long LL; using namespace std; typedef complex<double>cpx; inline void file() { freopen("fft.in" , "r" , stdin); freopen("fft.out" , "w" , stdout); } template <typename T> inline bool chkmax(T &a , const T &b) { return a < b ? a = b , 1 : 0; } template <typename T> inline bool chkmin(T &a , const T &b) { return a > b ? a = b , 1 : 0; } char c_; int _ , __; inline int read() { for (_ = 0 , __ = 1 , c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-') __ = -1; for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48); return _ * __; } const int maxn = (1 << 22) + 10; const double Pi = acos(-1.0); int n , m; cpx a[maxn] , b[maxn]; inline void Init() { n = read(); m = read(); For(i , 0 , n) a[i] = read(); For(i , 0 , m) b[i] = read(); m += n; for (n = 1 ; n <= m ; n <<= 1) ; } inline void FFT(cpx *x , int n , int type) { if (n == 1) return ; cpx l[n >> 1] , r[n >> 1]; for (int i = 0 ; i < n ; i += 2) l[i >> 1] = x[i] , r[i >> 1] = x[i + 1]; FFT(l , n >> 1 , type); FFT(r , n >> 1 , type); cpx wn(cos(2 * Pi / n) , sin(type * 2 * Pi / n)) , w(1 , 0) , t; for (int i = 0 ; i < n >> 1 ; ++ i , w *= wn) { t = w * r[i]; x[i] = l[i] + t; x[i + (n >> 1)] = l[i] - t; } } int main() { #ifdef hany01 file(); #endif Init(); FFT(a , n , 1); FFT(b , n , 1); For(i , 0 , n) debug("%lf\n" , a[i].real()); For(i , 0 , n) a[i] *= b[i]; FFT(a , n , -1); For(i , 0 , m) printf("%d " , (int)round(a[i].real() / n)); return 0; } //千里黄云白日曛,北风吹雁雪纷纷。 // -- 高适《别董大二首》
迭代版(faster)
#include<bits/stdc++.h> using namespace std; #define rep(i, j) for (register int i = 0, i##_end_ = j; i < i##_end_; ++ i) #define getchar getchar_unlocked #define putchar putchar_unlocked inline int read() { register int _, __; register char c_; for (_ = 0 , __ = 1 , c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-') __ = -1; for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48); return _ * __; } const double pi = acos(-1.0); const int maxn = 1<<21; struct Complex { double real, imag; }a[maxn], b[maxn]; inline Complex operator + (const Complex &A, const Complex &B) { return (Complex){A.real + B.real, A.imag + B.imag}; } inline Complex operator - (const Complex &A, const Complex &B) { return (Complex){A.real - B.real, A.imag - B.imag}; } inline Complex operator * (const Complex &A, const Complex &B) { return (Complex){A.real * B.real - A.imag * B.imag, A.real * B.imag + A.imag * B.real}; } int n, r[maxn]; inline void fft(Complex *a, int type) { rep(i, n) if (i < r[i]) swap(a[i], a[r[i]]); for (register int i = 2; i <= n; i <<= 1) { register Complex wn = Complex{cos(2 * pi / i), sin(2 * pi / i * type)}; for (register int j = 0; j < n; j += i) { register Complex w = Complex{1, 0}; rep(k, i >> 1) { register Complex x = a[j + k], y = a[j + k + (i >> 1)] * w; a[j + k] = x + y; a[j + k + (i >> 1)] = x - y; w = w * wn; } } } } int main() { int n1, n2, m, cnt; n1 = read() + 1; n2 = read() + 1; m = n1 + n2 - 1; rep(i, n1) a[i].real = read(); rep(i, n2) b[i].real = read(); for (n = 1; n < m; n <<= 1) ++ cnt; rep(i, n) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (cnt - 1)); fft(a, 1); fft(b, 1); rep(i, n) a[i] = a[i] * b[i]; fft(a, -1); rep(i, m) printf("%d", (int)(a[i].real / n + 0.5)), putchar(' '); return 0; } //楚塞三湘接,荆门九派通。 //江流天地外,山色有无中。 //郡邑浮前浦,波澜动远空。 //襄阳好风日,留醉与山翁。 //--王维《汉江临泛》
相关文章推荐
- 快速傅里叶变换(FFT)
- 快速傅里叶变换(FFT)的原理及公式
- 几种快速傅里叶变换(FFT)的C++实现
- 几种快速傅里叶变换(FFT)的C++实现
- NTT FFT 数论变换 快速傅里叶变换 模板
- FFT快速傅里叶变换
- 快速傅里叶变换(FFT)相关内容汇总
- 几种快速傅里叶变换(FFT)的C++实现
- 快速傅里叶变换(FFT)(ZZ)
- FFT快速傅里叶变换;
- 【快速傅里叶变换】【FFT】【WikiOI】【P3132】【高精度练习之超大整数乘法】
- [FFT]快速傅里叶变换
- 快速傅里叶变换(FFT):COGS 2216. 你猜是不是KMP
- FFT(快速傅里叶变换)算法学习笔记
- 快速傅里叶变换FFT学习小记
- 快速傅里叶变换(FFT)(下)
- Python图像处理库PIL中快速傅里叶变换FFT的实现(一)
- 快速傅里叶变换(FFT)
- FFT—快速傅里叶变换学习小记
- 快速傅里叶变换(FFT)结果的物理意义是什么?(附Matlab程序)