-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFFT by TFG.cpp
97 lines (87 loc) · 2.22 KB
/
FFT by TFG.cpp
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
typedef double ld;
const ld PI = acos(-1);
struct Complex {
ld real, imag;
Complex conj() { return Complex(real, -imag); }
Complex(ld a = 0, ld b = 0) : real(a), imag(b) {}
Complex operator + (const Complex &o) const { return Complex(real + o.real, imag + o.imag); }
Complex operator - (const Complex &o) const { return Complex(real - o.real, imag - o.imag); }
Complex operator * (const Complex &o) const { return Complex(real * o.real - imag * o.imag, real * o.imag + imag * o.real); }
Complex operator / (ld o) const { return Complex(real / o, imag / o); }
void operator *= (Complex o) { *this = *this * o; }
void operator /= (ld o) { real /= o, imag /= o; }
};
typedef std::vector<Complex> CVector;
const int ms = 1 << 22;
int bits[ms];
Complex root[ms];
void initFFT() {
root[1] = Complex(1);
for(int len = 2; len < ms; len += len) {
Complex z(cos(PI / len), sin(PI / len));
for(int i = len / 2; i < len; i++) {
root[2 * i] = root[i];
root[2 * i + 1] = root[i] * z;
}
}
}
void pre(int n) {
int LOG = 0;
while(1 << (LOG + 1) < n) {
LOG++;
}
for(int i = 1; i < n; i++) {
bits[i] = (bits[i >> 1] >> 1) | ((i & 1) << LOG);
}
}
CVector fft(CVector a, bool inv = false) {
int n = a.size();
pre(n);
if(inv) {
std::reverse(a.begin() + 1, a.end());
}
for(int i = 0; i < n; i++) {
int to = bits[i];
if(to > i) {
std::swap(a[to], a[i]);
}
}
for(int len = 1; len < n; len *= 2) {
for(int i = 0; i < n; i += 2 * len) {
for(int j = 0; j < len; j++) {
Complex u = a[i + j], v = a[i + j + len] * root[len + j];
a[i + j] = u + v;
a[i + j + len] = u - v;
}
}
}
if(inv) {
for(int i = 0; i < n; i++)
a[i] /= n;
}
return a;
}
std::vector<int> mul(const std::vector<int> &a, const std::vector<int> &b) {
int n = 1;
while (n - 1 < (int) a.size() + (int) b.size() - 2) n += n;
CVector poly(n);
for(int i = 0; i < n; i++) {
if(i < (int) a.size()) {
poly[i].real = a[i];
}
if(i < (int) b.size()) {
poly[i].imag = b[i];
}
}
poly = fft(poly);
for(int i = 0; i < n; i++) {
poly[i] *= poly[i];
}
poly = fft(poly, true);
std::vector<int> c(n, 0);
for(int i = 0; i < n; i++) {
c[i] = (int) (poly[i].imag / 2 + 0.5);
}
while (c.size() > 0 && c.back() == 0) c.pop_back();
return c;
}