-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfft.cpp
More file actions
163 lines (140 loc) · 4.59 KB
/
fft.cpp
File metadata and controls
163 lines (140 loc) · 4.59 KB
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
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
typedef complex <double> Point;
const int BITSMAX = 16;
const int NMAX = (1 << BITSMAX);
/*struct Point {
double x, y;
Point(): x(0), y(0) {}
explicit Point(double _x, double _y): x(_x), y(_y) {}
explicit Point(double theta): x(cos(theta)), y(sin(theta)) {}
Point operator+(const Point &arg) const { return Point(x + arg.x, y + arg.y); }
Point operator-(const Point &arg) const { return Point(x - arg.x, y - arg.y); }
Point operator*(const Point &arg) const { return Point(x * arg.x - y * arg.y, x * arg.y + y * arg.x); }
Point operator/(const int &arg) const { return Point(x / arg, y / arg); }
friend inline Point conj(const Point &arg) { return Point(arg.x, -arg.y); }
};
ostream& operator<<(ostream &g, const Point &p) {
g << '(' << p.x << ',' << p.y << ')';
return g;
}
Point w[2][2 * NMAX];
void precomputeRoots() {
w[0][1] = w[1][1] = Point(1, 0);
for (int k = 1; k <= BITSMAX; ++ k) {
Point W0(2.0 * PI / (1 << k));
Point W1 = conj(W0);
const int bound = (1 << k);
for (int i = (1 << (k - 1)); i < bound; ++ i) {
w[0][2 * i] = w[0][i], w[0][2 * i + 1] = w[0][i] * W0;
w[1][2 * i] = w[1][i], w[1][2 * i + 1] = w[1][i] * W1;
}
}
}*/
int rev[NMAX];
void fft(Point v[], const int BITS, const bool inv = false) {
//static bool computed = false;
//if (!computed)
// precomputeRoots(), computed = true;
const int N = (1 << BITS);
for (int i = 1; i < N; ++ i) {
rev[i] = rev[i >> 1] >> 1;
if (i & 1)
rev[i] += N / 2;
}
for (int i = 0; i < N; ++ i)
if (i < rev[i])
swap(v[i], v[rev[i]]);
for (int len = 1; len < N; len <<= 1) {
double theta = PI / len;
if (inv) theta *= (-1);
const Point w = Point(cos(theta), sin(theta));
for (int start = 0; start < N; start += 2 * len) {
Point wk = Point(1, 0);
for (int i = 0; i < len; ++ i) {
//const Point b = v[start + i + len] * w[inv][2 * len + i];
const Point b = v[start + i + len] * wk;
v[start + i + len] = v[start + i] - b;
v[start + i] = v[start + i] + b;
wk = wk * w;
}
}
}
if (inv)
for (int i = 0; i < N; ++ i)
v[i] /= N;
}
Point points[NMAX], pointsA[NMAX], pointsB[NMAX], pointsRes[NMAX];
template <typename RandomIt>
void convolute(RandomIt a, int Na, RandomIt b, int Nb, RandomIt res) {
/*if (Na * Nb <= 16 * 16) {
for (int i = 0; i < Na + Nb; ++ i)
res[i] = 0;
for (int i = 0; i < Na; ++ i)
for (int j = 0; j < Nb; ++ j)
res[i + j] += a[i] * b[j];
return ;
}*/
int BITS = 0;
while ((1 << BITS) < Na + Nb - 1) ++ BITS;
const int N = (1 << BITS);
for (int i = 0; i < Na; ++ i)
points[i] = Point(a[i], 0);
for (int i = Na; i < N; ++ i)
points[i] = Point();
for (int i = 0; i < Nb; ++ i)
points[i] = Point(points[i].real(), b[i]);
fft(points, BITS);
for (int i = 0; i < N; ++ i) {
const Point &A = points[i];
const Point &B = conj(points[(N - i) & (N - 1)]);
pointsA[i] = (A + B) * Point(0.5, 0);
pointsB[i] = (A - B) * Point(0, -0.5);
}
for (int i = 0; i < N; ++ i)
pointsRes[i] = pointsA[i] * pointsB[i];
fft(pointsRes, BITS, true);
for (int i = 0; i < N; ++ i)
res[i] = pointsRes[i].real() + 0.5;
}
const int VALMAX = 30000 + 5;
int freqA[VALMAX];
int freqB[VALMAX];
int freqC[NMAX];
int main()
{
ifstream cin("bacterii2.in");
ofstream cout("bacterii2.out");
int T = 0;
cin >> T;
while (T --) {
memset(freqA, 0, sizeof freqA);
memset(freqB, 0, sizeof freqB);
int N;
cin >> N;
int maxA = 0;
while (N --) {
int val;
cin >> val;
++ freqA[val];
maxA = max(maxA, val);
}
int M;
cin >> M;
int maxB = 0;
while (M --) {
int val;
cin >> val;
++ freqB[val];
maxB = max(maxB, val);
}
convolute(freqA, maxA + 1, freqB, maxB + 1, freqC);
for (int i = 0; i <= maxA + maxB; ++ i)
if (freqC[i])
cout << i << ' ' << freqC[i] << '\n';
if (T)
cout << '\n';
}
return 0;
}