forked from Jonathan-Uy/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignal Processing.cpp
64 lines (54 loc) · 1.33 KB
/
Signal Processing.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
#include <bits/stdc++.h>
using namespace std;
typedef double ld;
typedef complex<ld> cd;
const int maxN = 2e5+5;
const int SIZE = 1<<19;
const ld PI = acos(-1);
int N, M;
vector<cd> A(SIZE), B(SIZE);
void fft(vector<cd> &a, bool inv){
int n = (int) 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]);
}
for(int len = 2; len <= n; len <<= 1){
ld theta = 2*PI / len * (inv ? -1 : 1);
cd wlen(cos(theta), sin(theta));
for(int i = 0; i < n; i += len){
cd w(1);
for(int j = 0; j < len / 2; j++){
cd u = a[i+j], v = a[i+j+len/2] * w;
a[i+j] = u + v;
a[i+j+len/2] = u - v;
w *= wlen;
}
}
}
if(inv)
for(cd &z : a)
z /= n;
}
int main(){
scanf("%d %d", &N, &M);
for(int i = 0, a; i < N; i++){
scanf("%d", &a);
A[i] = a;
}
for(int i = 0, b; i < M; i++){
scanf("%d", &b);
B[M-i-1] = b;
}
fft(A, false);
fft(B, false);
for(int i = 0; i < SIZE; i++)
A[i] *= B[i];
fft(A, true);
for(int i = 0; i < N+M-1; i++)
printf("%lld%c", llround(A[i].real()), (" \n")[i==N+M-2]);
}