-
Notifications
You must be signed in to change notification settings - Fork 46
/
2_13.cpp
89 lines (74 loc) · 1.61 KB
/
2_13.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
#include<bits/stdc++.h>
using namespace std;
typedef complex<double> base;
typedef vector<base> vb;
void input(int &n, int &m, vb &x, vb &y){
cin >> n;
for(int i=0; i<=n; i++){
int tmp;
cin >> tmp;
base mycomplex(tmp,0);
x.push_back(mycomplex);
}
cin >> m;
for(int i=0; i<=m; i++){
int tmp;
cin >> tmp;
base mycomplex(tmp,0);
y.push_back(mycomplex);
}
}
void fft(vb & a, bool revert){
int n = (int)a.size();
for(int i=1, j=0; i<n; ++i){
int bit = n >> 1;
while(j>=bit){
j =j- bit;
bit=bit>>1;
}
j =j+ bit;
if (i < j)
swap (a[i], a[j]);
}
for(int len=2; len<=n; len<<=1){
double ang = 2*M_PI/len;
if(revert==1) ang=-ang;
complex<double> wlen (cos(ang), sin(ang));
for (int i=0; i<n; i+=len) {
complex<double> w (1);
for (int j=0; j<len/2; ++j) {
complex<double> u = a[i+j], v = a[i+j+len/2] * w;
a[i+j] = u + v;
a[i+j+len/2] = u - v;
w =w * wlen;
}
}
}
if(revert==1)
for(int i=0; i<n; ++i)
a[i] /=n;
}
int multiPly(int n, int m, vb x, vb y){
int p = 1;
while(p < max (n, m)) p=p<<1;
p=p<<1;
x.resize(p);
y.resize(p);
fft(x, false);
fft(y, false);
vector<base> h(p);
for (int i=0; i<p; i++)
h[i] =x[i]*y[i];
fft (h, true);
int res = (int)(real(h[0])+0.5);
for (int i=1; i<=p; i++){
res = res ^ (int)(real(h[i])+0.5);
}
return res;
}
int main(){
int n, m;
vector<base> x,y;
input(n,m,x,y);
cout << multiPly(n,m,x,y);
}