-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFFT.h
176 lines (152 loc) · 3.28 KB
/
FFT.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <vector>
#include <iostream>
using namespace std;
// typedef complex<double> base;
struct base {
double re, im;
base(double x = 0, double y = 0):re(x),im(y){}
base operator +(const base& z) const {
return base(re + z.re, im + z.im);
}
base operator -(const base& z) const {
return base(re - z.re, im - z.im);
}
base operator *(const base& z) const {
return base(re * z.re - im * z.im, re * z.im + im * z.re);
}
void operator *=(const base& z){
double tmp = re * z.re - im * z.im;
im = re * z.im + im * z.re;
re = tmp;
}
void operator /=(const double& z){
re /= z;
im /= z;
}
inline double real() const {
return re;
}
inline double imag() const {
return im;
}
};
const long double PI = acosl(-1);
int rev(int n, int cnt){
int res = 0;
for (int i = 0; i < cnt; i++){
res = (res << 1) | (n & 1);
n >>= 1;
}
return res;
}
void fft(vector<base>& a, const vector<base>& angles, bool inv = false){
int n = a.size();
for (int i = 1, j = 0; i < n; i++){
int bit = n >> 1;
while (j >= bit){
j -= bit;
bit >>= 1;
}
j += bit;
if (i < j)
swap(a[i], a[j]);
}
for (int j = 0; j < n; j += 2){
base u = a[j], v = a[j + 1];
a[j] = u + v;
a[j + 1] = u - v;
}
if (n >= 4){
for (int j = 0; j < n; j += 4){
base u = a[j], v = a[j + 2];
a[j] = u + v;
a[j + 2] = u - v;
base w = inv ? angles[n / 4 * 3] : angles[n / 4];
u = a[j + 1], v = a[j + 3] * w;
a[j + 1] = u + v;
a[j + 3] = u - v;
}
}
for (int len = 8; len <= n; len <<= 1){
for (int j = 0; j < n; j += len){
auto uptr = a.begin() + j, vptr = a.begin() + j + len / 2;
for (int i = 0; i < len / 2; i++){
int index = n / len * i;
if (inv && index)
index = n - index;
base w = angles[index];
base u = *uptr, v = (*vptr) * w;
*uptr = u + v;
*vptr = u - v;
++uptr, ++vptr;
}
}
}
if (inv){
for (int i = 0; i < n; i++)
a[i] /= n;
}
}
vector<int> fft_mult(const vector<int>& a, const vector<int>& b){
int n = 1;
while (n < (int)a.size() || n < (int)b.size())
n *= 2;
n *= 2;
vector<base> ar(n), br(n);
for (int i = 0; i < (int)a.size(); i++)
ar[i] = a[i];
for (int i = 0; i < (int)b.size(); i++)
br[i] = b[i];
vector<base> angles(n);
for (int i = 0; i < n; i++){
angles[i] = base(cos(2 * PI * i / n), sin(2 * PI * i / n));
}
fft(ar, angles);
fft(br, angles);
for (int i = 0; i < n; i++)
ar[i] *= br[i];
fft(ar, angles, true);
vector<int> res(n);
for (int i = 0; i < n; i++){
res[i] = round(ar[i].real());
}
while (res.size() > 1 && res.back() == 0)
res.pop_back();
return res;
}
void test_fft(int n){
vector<int> a(n), b(n);
for (int i = 0; i < n; i++){
a[i] = rand() % 2000 - 1000;
b[i] = rand() % 2000 - 1000;
}
clock_t start = clock();
vector<int> c = fft_mult(a, b);
clock_t finish = clock();
cerr << "n = " << n << ": ";
if (n <= 1000){
vector<int> res;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (i + j >= (int)res.size())
res.push_back(0);
res[i + j] += a[i] * b[j];
}
}
if (res != c){
cerr << "incorrect fft\n";
return;
}
}
cerr.precision(6);
cerr << fixed;
cerr << 1.0 * (finish - start) / CLOCKS_PER_SEC << " secs.\n";
}
void run_fft(){
test_fft(1);
test_fft(2);
test_fft(3);
test_fft(4);
test_fft(100);
test_fft(200000);
}