快速傅里叶变换(Fast Fourier Transform),学习笔记
前言:这是涉及信号分析,域变换,卷积等非常高深内容的一个算法,一般在大学本科或研究生时学习,思维难度较高
背景:一个交响乐团的音乐画成声波图上可能看上去杂乱无章,但可以分解成不同频率不同强度的组合,同理在通讯中,可以去除一些“杂质”,或快速合成信号
问题引入:多项式函数与多项式的卷积是什么
如,,求
初中数学告诉我们,但如果写成代码模拟这个枚举过程时间复杂度是,还可以优化吗?
引入正题:我们观察式子,我们发现x是自变量,的值完全取决于序列的值,那么我们可以用一个序列来表示一个多项式函数,称作系数表示法(因为是的系数,即是的次项系数),这样多项式乘法变成了系数序列的乘法,但还是复杂度爆炸,因为不好直接乘,什么方便乘,当然是具体数值(其实是坐标,当一定时相乘),成为点值表示法,那么如何将系数表示法转为点值表示法,再怎么将点值表示法转为系数表示法(这种变换被称为域变换,常见的域变换还有笛卡尔坐标系转换为极坐标系)?
算法大体思路:带入特殊值,特殊点相乘,将特殊点变为函数
-
系数序列转为数值序列(FFT)
- 暴力做法,随机或枚举几个数字,将数字分别带入两个函数,如等,但时间复杂度还是会爆炸
- 快速做法,我们可以带入一些便于计算的值,在计算中我们观察函数发现和时都等于,和时都等于,观察函数发现和时分别等于和,和时分别等于和,这启发我们可以用一半算出另一半的值,具体来说将奇偶项分解为奇数项和偶数项(为什么呢?因为,可以一次性算出和)如,此时,为了不让在某次递归中变成空,需将系数补至系数总个数为的幂次,但你会发现或的中必然是非负数,所以你需要扩域到复数域,那此时为了不断递归,初始时的中的特殊值为次单位根(定义及求法在最底下),用 递归即可,这样我们可以将某个系数序列用上面的几个点表示,分别为,由于是分治,所以时间复杂度为但三角函数会产生很大的常数(比较加法可能有几十倍差距)。
代码
#include<vector>
#include<math.h>
#define _USE_MATH_DEFINES
struct Complex {
double real, imag;
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
Complex operator+(const Complex& other) const {
return Complex(real + other.real, imag + other.imag);
}
Complex operator-(const Complex& other) const {
return Complex(real - other.real, imag - other.imag);
}
Complex operator*(const Complex& other) const {
return Complex(real * other.real - imag * other.imag,
real * other.imag + imag * other.real);
}
Complex operator/(double scalar) const {
return Complex(real / scalar, imag / scalar);
}
Complex conj() const {
return Complex(real, -imag);
}
};
std::vector<Complex> FFT(const std::vector<Complex>& a) {
int n = a.size();
if (n == 1) {
return a;
}
double angle = 2.0 * M_PI / n;
Complex omega_n(cos(angle), sin(angle));
Complex omega(1, 0);
std::vector<Complex> a_even(n/2), a_odd(n/2);
for (int i = 0; i < n/2; i++) {
a_even[i] = a[2*i];
a_odd[i] = a[2*i + 1];
}
std::vector<Complex> y_even = FFT(a_even);
std::vector<Complex> y_odd = FFT(a_odd);
std::vector<Complex> y(n);
for (int k = 0; k < n/2; k++) {
Complex t = omega * y_odd[k];
y[k] = y_even[k] + t;
y[k + n/2] = y_even[k] - t;
omega = omega * omega_n;
}
return y;
}-
将两个函数的数值相乘
时间复杂度
代码
std::vector<Complex> mul(const std::vector<Complex>& a, const std::vector<Complex>& b) {
std::vector<Complex>c(a.size());
for (int i = 0; i < a.size(); i++) c[i] = a[i] * b[i];
return c;
}-
系数序列转为数值序列(IFFT)
- 暴力做法,我们可以使用待定系数法然后解方程,如已知的二次函数经过点(1,4),(0,-4),(-1,-2)可以解出它为
- 快速做法,其实傅里叶变换是模拟将单位根带入的过程,
那么系数序列有
求逆矩阵得 非常类似于FFT,时间复杂度与其一样,都为,伴随着极其巨大的常数
代码
std::vector<Complex> IFFT(const std::vector<Complex>& a) {
int n = a.size();
if (n == 1) {
return a;
}
double angle = -2.0 * M_PI / n;
Complex omega_n(cos(angle), sin(angle));
Complex omega(1, 0);
std::vector<Complex> a_even(n / 2), a_odd(n / 2);
for (int i = 0; i < n / 2; i++) {
a_even[i] = a[2 * i];
a_odd[i] = a[2 * i + 1];
}
std::vector<Complex> y_even = IFFT(a_even);
std::vector<Complex> y_odd = IFFT(a_odd);
std::vector<Complex> y(n);
for (int k = 0; k < n / 2; k++) {
Complex t = omega * y_odd[k];
y[k] = y_even[k] + t;
y[k + n / 2] = y_even[k] - t;
omega = omega * omega_n;
}
return y;
}
std::vector<Complex>inverse(const std::vector<Complex>&a) {
std::vector<Complex>res = IFFT(a);
for (int i = 0; i < res.size(); i++) res[i].real = res[i].real / res.size();
return res;
}总代码多项式乘法(FFT) 主体部分时间复杂度为以及大量的栈消耗的取三角函数时的消耗
代码
#include<vector>
#include<math.h>
#include<iostream>
#include <algorithm>
#define _USE_MATH_DEFINES
struct Complex {
double real, imag;
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
Complex operator+(const Complex& other) const {
return Complex(real + other.real, imag + other.imag);
}
Complex operator-(const Complex& other) const {
return Complex(real - other.real, imag - other.imag);
}
Complex operator*(const Complex& other) const {
return Complex(real * other.real - imag * other.imag,
real * other.imag + imag * other.real);
}
Complex operator/(double scalar) const {
return Complex(real / scalar, imag / scalar);
}
Complex conj() const {
return Complex(real, -imag);
}
};
std::vector<Complex> FFT(const std::vector<Complex>& a) {
int n = a.size();
if (n == 1) {
return a;
}
double angle = 2.0 * M_PI / n;
Complex omega_n(cos(angle), sin(angle));
Complex omega(1, 0);
std::vector<Complex> a_even(n / 2), a_odd(n / 2);
for (int i = 0; i < n / 2; i++) {
a_even[i] = a[2 * i];
a_odd[i] = a[2 * i + 1];
}
std::vector<Complex> y_even = FFT(a_even);
std::vector<Complex> y_odd = FFT(a_odd);
std::vector<Complex> y(n);
for (int k = 0; k < n / 2; k++) {
Complex t = omega * y_odd[k];
y[k] = y_even[k] + t;
y[k + n / 2] = y_even[k] - t;
omega = omega * omega_n;
}
return y;
}
std::vector<Complex> IFFT(const std::vector<Complex>& a) {
int n = a.size();
if (n == 1) {
return a;
}
double angle = -2.0 * M_PI / n;
Complex omega_n(cos(angle), sin(angle));
Complex omega(1, 0);
std::vector<Complex> a_even(n / 2), a_odd(n / 2);
for (int i = 0; i < n / 2; i++) {
a_even[i] = a[2 * i];
a_odd[i] = a[2 * i + 1];
}
std::vector<Complex> y_even = IFFT(a_even);
std::vector<Complex> y_odd = IFFT(a_odd);
std::vector<Complex> y(n);
for (int k = 0; k < n / 2; k++) {
Complex t = omega * y_odd[k];
y[k] = y_even[k] + t;
y[k + n / 2] = y_even[k] - t;
omega = omega * omega_n;
}
return y;
}
std::vector<Complex>inverse(const std::vector<Complex>&a) {
std::vector<Complex>res = IFFT(a);
for (int i = 0; i < res.size(); i++) res[i].real = res[i].real / res.size();
return res;
}
std::vector<Complex> mul(const std::vector<Complex>& a, const std::vector<Complex>& b) {
std::vector<Complex>c(a.size());
for (int i = 0; i < a.size(); i++) c[i] = a[i] * b[i];
return c;
}
std::vector<Complex>conv(std::vector<Complex>&a,std::vector<Complex>&b){
std::vector<Complex>c=FFT(a),d=FFT(b),e=mul(c,d);
return inverse(e);
}
std::vector<Complex> toComplex(const std::vector<int>& vec, int size) {
std::vector<Complex> result(size, Complex(0, 0));
for (int i = 0; i < vec.size(); i++) {
result[i] = Complex(vec[i], 0);
}
return result;
}
int getNextPow2(int n) {
int p = 1;
while (p < n) p <<= 1;
return p;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, m;
std::cin >> n >> m;
std::vector<int> poly1(n + 1), poly2(m + 1);
for (int i = 0; i <= n; i++) std::cin >> poly1[i];
for (int i = 0; i <= m; i++) std::cin >> poly2[i];
int result_size = n + m + 1;
int fft_size = 1;
while (fft_size < result_size) fft_size <<= 1;
fft_size <<= 1;
std::vector<Complex> a = toComplex(poly1, fft_size);
std::vector<Complex> b = toComplex(poly2, fft_size);
std::vector<Complex> result = conv(a, b);
for (int i = 0; i < result_size; i++) {
long long val = (long long)(result[i].real + 0.5);
std::cout << val << " ";
}
return 0;
}然后成功超时了,因为代码常数非常大,尤其是递归时和取三角函数(求单位根)耗费了大量时间
优化
- 三角函数方面可以直接预处理,虽然耗费部分内存,但可以加快非常多的时间效率
代码
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <cmath>
using namespace std;
struct Complex {
double real, imag;
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {} Complex operator+(const Complex& other) const {
return Complex(real + other.real, imag + other.imag);
} Complex operator-(const Complex& other) const {
return Complex(real - other.real, imag - other.imag);
} Complex operator*(const Complex& other) const {
return Complex(real * other.real - imag * other.imag, real * other.imag + imag * other.real);
} Complex operator/(double scalar) const {
return Complex(real / scalar, imag / scalar);
} Complex conj() const {
return Complex(real, -imag);
}
};
class PrecomputedFFT {
private:
vector<vector<Complex>> precomputed_omegas;
vector<vector<Complex>> precomputed_inv_omegas;
void precompute(int n) {
if (n <= 1) return;
int levels = 0;
int temp = n;
while (temp > 1) {
levels++;
temp >>= 1;
}
precomputed_omegas.resize(levels);
precomputed_inv_omegas.resize(levels);
temp = n;
for (int i = 0; i < levels; i++) {
int len = temp;
double angle = 2.0 * M_PI / len;
precomputed_omegas[i].resize(len / 2);
precomputed_inv_omegas[i].resize(len / 2);
for (int j = 0; j < len / 2; j++) {
double theta = angle * j;
precomputed_omegas[i][j] = Complex(cos(theta), sin(theta));
precomputed_inv_omegas[i][j] = Complex(cos(theta), -sin(theta));
}
temp >>= 1;
}
} vector<Complex> FFT_recursive(const vector<Complex>& a, int level, bool inverse) {
int n = a.size();
if (n == 1) return a;
vector<Complex> a_even(n / 2), a_odd(n / 2);
for (int i = 0; i < n / 2; i++) {
a_even[i] = a[2 * i];
a_odd[i] = a[2 * i + 1];
}
vector<Complex> y_even = FFT_recursive(a_even, level + 1, inverse);
vector<Complex> y_odd = FFT_recursive(a_odd, level + 1, inverse);
vector<Complex> y(n);
const vector<Complex>& omegas = inverse ? precomputed_inv_omegas[level] : precomputed_omegas[level];
for (int k = 0; k < n / 2; k++) {
Complex t = omegas[k] * y_odd[k];
y[k] = y_even[k] + t;
y[k + n / 2] = y_even[k] - t;
}
return y;
} public:
vector<Complex> FFT(const vector<Complex>& a, bool inverse = false) {
int n = a.size();
precompute(n);
return FFT_recursive(a, 0, inverse);
}
};
PrecomputedFFT fft_calculator;
std::vector<Complex> FFT(const std::vector<Complex>& a) {
return fft_calculator.FFT(a, false);
}
std::vector<Complex> IFFT(const std::vector<Complex>& a) {
return fft_calculator.FFT(a, true);
}
std::vector<Complex> inverse(const std::vector<Complex>& a) {
std::vector<Complex> res = IFFT(a);
int n = res.size();
for (int i = 0; i < n; i++) {
res[i] = res[i] / n;
}
return res;
}
std::vector<Complex> mul(const std::vector<Complex>& a, const std::vector<Complex>& b) {
std::vector<Complex> c(a.size());
for (int i = 0; i < (int)a.size(); i++) c[i] = a[i] * b[i];
return c;
}
std::vector<Complex> conv(std::vector<Complex>& a, std::vector<Complex>& b) {
std::vector<Complex> c = FFT(a), d = FFT(b), e = mul(c, d);
return inverse(e);
}
vector<Complex> iterative_FFT(vector<Complex> a, bool invert = false) {
int n = a.size();
for (int i = 1, j = 0; i < n; i++) {
int bit = n >> 1;
for (; j & bit; bit >>= 1) j ^= bit;
j ^= bit;
if (i < j) swap(a[i], a[j]);
}
vector<Complex> roots(n / 2);
for (int i = 0; i < n / 2; i++) {
double angle = 2 * M_PI * i / n;
if (invert) angle = -angle;
roots[i] = Complex(cos(angle), sin(angle));
}
for (int len = 2; len <= n; len <<= 1) {
int half = len / 2;
int step = n / len;
for (int i = 0; i < n; i += len) {
for (int j = 0; j < half; j++) {
Complex w = roots[j * step];
Complex u = a[i + j];
Complex v = a[i + j + half] * w;
a[i + j] = u + v;
a[i + j + half] = u - v;
}
}
}
if (invert) {
for (int i = 0; i < n; i++) a[i] = a[i] / n;
}
return a;
}
vector<long long> fast_conv(const vector<int>& a, const vector<int>& b) {
int n = 1;
int result_size = (int)a.size() + (int)b.size() - 1;
while (n < result_size) n <<= 1;
vector<Complex> fa(n), fb(n);
for (int i = 0; i < (int)a.size(); i++) fa[i] = Complex(a[i], 0);
for (int i = 0; i < (int)b.size(); i++) fb[i] = Complex(b[i], 0);
fa = iterative_FFT(fa, false);
fb = iterative_FFT(fb, false);
for (int i = 0; i < n; i++) fa[i] = fa[i] * fb[i];
fa = iterative_FFT(fa, true);
vector<long long> result(result_size);
for (int i = 0; i < result_size; i++) result[i] = (long long)(fa[i].real + 0.5);
return result;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, m;
cin >> n >> m;
vector<int> poly1(n + 1), poly2(m + 1);
for (int i = 0; i <= n; i++) cin >> poly1[i];
for (int i = 0; i <= m; i++) cin >> poly2[i];
vector<long long> result = fast_conv(poly1, poly2);
for (int i = 0; i <= n + m; i++) cout << result[i] << " ";
cout << endl;
return 0;
}- 递归方面可以通过调整顺序,来减少栈消耗
代码
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
struct Virt {
double r, i;
Virt(double r = 0, double i = 0) {
this->r = r, this->i = i;
}
Virt operator+(const Virt &x) {
return Virt(r + x.r, i + x.i);
}
Virt operator-(const Virt &x) {
return Virt(r - x.r, i - x.i);
}
Virt operator*(const Virt &x) {
return Virt(r * x.r - i * x.i, i * x.r + r * x.i);
}
} a[10000000], b[10000000];
void Rader(Virt F[], int len) {
int j = len >> 1;
for (int i = 1; i < len - 1; i++) {
if (i < j) swap(F[i], F[j]);
int k = len >> 1;
while (j >= k)
j -= k, k >>= 1;
if (j < k) j += k;
}
}
void FFT(Virt F[], int len, int on) {
Rader(F, len);
for (int h = 2; h <= len; h <<= 1) {
Virt wn(cos(-on * 2 * PI / h), sin(-on * 2 * PI / h));
for (int j = 0; j < len; j += h) {
Virt w(1, 0);
for (int k = j; k < j + h / 2; k++) {
Virt u = F[k];
Virt t = w * F[k + h / 2];
F[k] = u + t;
F[k + h / 2] = u - t;
w = w * wn;
}
}
}
if (on == -1)
for (int i = 0; i < len; i++)
F[i].r /= len;
}
void conv(Virt a[], Virt b[], int len) {
FFT(a, len, 1);
FFT(b, len, 1);
for (int i = 0; i < len; i++) a[i] = a[i] * b[i];
FFT(a, len, -1);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m, len;
cin >> n >> m;
n++, m++;
len = 1 << (int(log2(n + m) + 2));
for (int i = 0; i < n; i++) cin >> a[i].r;
for (int i = 0; i < m; i++) cin >> b[i].r;
conv(a, b, len);
for (int i = 0; i < n + m - 1; i++) cout << fixed << setprecision(0) << max(a[i].r, 0.0) << " ";
return 0;
}| 版本 | 时间 | 空间 |
|---|---|---|
| 原版 | TLE(2s),在第七个点跑了1.24s | 500MiB+ |
| 三角函数预处理 | 最慢864ms,第七个点289ms | 128.38MiB |
| 非递归版本 | 最慢1.97s,第七个点797ms | 306.34MiB |
例题,P1919 【模板】高精度乘法 / A*B Problem 升级版
题意:给定正整数和,求,其中
做法:就是中带入,只不过最后要进位,这里使用非递归版本
代码
#include <bits/stdc++.h>
using namespace std;
const double PI=acos(-1.0);
struct Virt{
double r,i;
Virt(double r=0,double i=0){
this->r=r,this->i=i;
}
Virt operator+(const Virt &x){
return Virt(r+x.r,i+x.i);
}
Virt operator-(const Virt &x){
return Virt(r-x.r,i-x.i);
}
Virt operator*(const Virt &x){
return Virt(r*x.r-i*x.i,i*x.r+r*x.i);
}
}a[10000000],b[10000000];
void Rader(Virt F[],int len){
int j=len>>1;
for(int i=1;i<len-1;i++){
if(i<j) swap(F[i],F[j]);
int k=len>>1;
while(j>=k)
j-=k,k>>=1;
if(j<k) j+=k;
}
}
void FFT(Virt F[],int len,int on){
Rader(F,len);
for(int h=2;h<=len;h<<=1){
Virt wn(cos(-on*2*PI/h),sin(-on*2*PI/h));
for(int j=0;j<len;j+=h){
Virt w(1,0);
for(int k=j;k<j+h/2;k++){
Virt u=F[k];
Virt t=w*F[k+h/2];
F[k]=u+t;
F[k+h/2]=u-t;
w=w*wn;
}
}
}
if(on==-1)
for(int i=0;i<len;i++)
F[i].r/=len;
}
void conv(Virt a[],Virt b[],int len){
FFT(a,len,1);
FFT(b,len,1);
for(int i=0;i<len;i++) a[i]=a[i]*b[i];
FFT(a,len,-1);
}
string x,y;
int ans[10000000];
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>x>>y;
int n,m,len;
n=x.length(),m=y.length();
len=1<<(int(log2(n+m)+2));
for(int i=0;i<n;i++) a[i].r=x[n-i-1]-'0';
for(int i=0;i<m;i++) b[i].r=y[m-i-1]-'0';
conv(a,b,len);
for(int i=0;i<10000000;i++) ans[i]=max(floor(a[i].r+0.1),0.0);
for(int i=0;i<10000000-1;i++) ans[i+1]+=ans[i]/10,ans[i]%=10;
int num=10000000-1;
while(num!=0&&ans[num]==0) num--;
while(num!=-1) cout<<ans[num],num--;
return 0;
}单位根(复数相关)
简单来说,复数(Complex)是一种扩展了实数的数系,它使得所有多项式方程都有解,特别是包含了“虚数单位” (满足 ,即),一般表示为,其中且为虚数单位,在FFT&ITTF中,我们不希望递归后失去正负性,就是在最后一层前的中,所以要引入复数
什么是单位根,就是方程的解(含虚数解),共个,计为,为什么要用它,因为,不断递归,每次平方,最后就是(一定是二的幂次,每次向下递归,递归层数为所以),放在复平面上就是均匀的一些点
其中轴表示实部,轴表示虚部,然后说白了就是转圈圈,均匀在上,怎么算呢,很明显,由三角函数,对于有所以由欧拉公式,得单位根,有公式
- 理由如下
- 理由如下
- 理由如下这就是折半引理
FFT&IFFT主要是由此优化的
应用
- 多项式乘法(最经典用途) 问题:计算两个多项式的乘积 暴力:时无法承受 FFT:时瞬间可解
- 大整数乘法(高精度运算) 技巧:将大整数视为多项式系数 用FFT计算多项式乘法,然后处理进位 复杂度:,比传统的竖式乘法快得多
- 字符串匹配与卷积技巧 问题变体:带通配符的字符串匹配、字符距离匹配等
- 生成函数与计数问题 问题:组合计数、背包问题变种 用FFT分治计算多项式乘积
- 卷积优化DP 问题:形如的卷积形式DP 优化:用FFT将优化到
DFT是FFT的基础,DFT的核心思想是:将离散信号拆解成一组正交的”频率基向量”的线性组合,从而在频域分析信号。形象来说:时域:听到的音乐是随时间变化的波形。频域(DFT后):看到的是每个音符(频率)的强度和时间分布。DFT:就是那个将音乐转换成乐谱的”翻译器” FFT(快速傅里叶变换)是计算 DFT 的一种聪明算法,速度极快。